summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2019-05-17 22:00:35 -0600
committerSimon Glass <sjg@chromium.org>2019-07-10 16:52:58 -0600
commit2b6ed5e92e3b29fbfa3a966433d4bbc2ec1e9c58 (patch)
treec8d0f7232f82931ae27e1ee4e7888869270c3b5a
parent7e6952df36a28b570818ee1fd36b07c41ef14aea (diff)
downloadu-boot-2b6ed5e92e3b29fbfa3a966433d4bbc2ec1e9c58.tar.gz
u-boot-2b6ed5e92e3b29fbfa3a966433d4bbc2ec1e9c58.tar.xz
u-boot-2b6ed5e92e3b29fbfa3a966433d4bbc2ec1e9c58.zip
dtoc: Updates BytesToValue() for Python 3
The difference between the bytes and str types in Python 3 requires a number of minor changes to this function. Update it to handle the input data using the 'bytes' type. Create two useful helper functions which can be used by other modules too. Signed-off-by: Simon Glass <sjg@chromium.org>
-rw-r--r--tools/dtoc/fdt.py41
-rw-r--r--tools/patman/tools.py27
2 files changed, 52 insertions, 16 deletions
diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py
index 35453fbed9..2e74bc15be 100644
--- a/tools/dtoc/fdt.py
+++ b/tools/dtoc/fdt.py
@@ -30,50 +30,59 @@ def CheckErr(errnum, msg):
(errnum, libfdt.fdt_strerror(errnum), msg))
-def BytesToValue(bytes):
+def BytesToValue(data):
"""Converts a string of bytes into a type and value
Args:
- A string containing bytes
+ A bytes value (which on Python 2 is an alias for str)
Return:
A tuple:
Type of data
Data, either a single element or a list of elements. Each element
is one of:
- TYPE_STRING: string value from the property
- TYPE_INT: a byte-swapped integer stored as a 4-byte string
- TYPE_BYTE: a byte stored as a single-byte string
+ TYPE_STRING: str/bytes value from the property
+ TYPE_INT: a byte-swapped integer stored as a 4-byte str/bytes
+ TYPE_BYTE: a byte stored as a single-byte str/bytes
"""
- bytes = str(bytes)
- size = len(bytes)
- strings = bytes.split('\0')
+ data = bytes(data)
+ size = len(data)
+ strings = data.split(b'\0')
is_string = True
count = len(strings) - 1
- if count > 0 and not strings[-1]:
+ if count > 0 and not len(strings[-1]):
for string in strings[:-1]:
if not string:
is_string = False
break
for ch in string:
- if ch < ' ' or ch > '~':
+ # Handle Python 2 treating bytes as str
+ if type(ch) == str:
+ ch = ord(ch)
+ if ch < 32 or ch > 127:
is_string = False
break
else:
is_string = False
if is_string:
- if count == 1:
- return TYPE_STRING, strings[0]
+ if count == 1:
+ if sys.version_info[0] >= 3: # pragma: no cover
+ return TYPE_STRING, strings[0].decode()
+ else:
+ return TYPE_STRING, strings[0]
else:
- return TYPE_STRING, strings[:-1]
+ if sys.version_info[0] >= 3: # pragma: no cover
+ return TYPE_STRING, [s.decode() for s in strings[:-1]]
+ else:
+ return TYPE_STRING, strings[:-1]
if size % 4:
if size == 1:
- return TYPE_BYTE, bytes[0]
+ return TYPE_BYTE, tools.ToChar(data[0])
else:
- return TYPE_BYTE, list(bytes)
+ return TYPE_BYTE, [tools.ToChar(ch) for ch in list(data)]
val = []
for i in range(0, size, 4):
- val.append(bytes[i:i + 4])
+ val.append(data[i:i + 4])
if size == 4:
return TYPE_INT, val[0]
else:
diff --git a/tools/patman/tools.py b/tools/patman/tools.py
index 7e6a45a3b0..976670ef00 100644
--- a/tools/patman/tools.py
+++ b/tools/patman/tools.py
@@ -290,3 +290,30 @@ def FromUnicode(val):
if sys.version_info[0] >= 3:
return val
return val if isinstance(val, str) else val.encode('utf-8')
+
+def ToByte(ch):
+ """Convert a character to an ASCII value
+
+ This is useful because in Python 2 bytes is an alias for str, but in
+ Python 3 they are separate types. This function converts the argument to
+ an ASCII value in either case.
+
+ Args:
+ ch: A string (Python 2) or byte (Python 3) value
+
+ Returns:
+ integer ASCII value for ch
+ """
+ return ord(ch) if type(ch) == str else ch
+
+def ToChar(byte):
+ """Convert a byte to a character
+
+ This is useful because in Python 2 bytes is an alias for str, but in
+ Python 3 they are separate types. This function converts an ASCII value to
+ a value with the appropriate type in either case.
+
+ Args:
+ byte: A byte or str value
+ """
+ return chr(byte) if type(byte) != str else byte