summaryrefslogtreecommitdiff
path: root/tools/patman/tools.py
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2019-05-18 07:00:35 +0300
committerSimon Glass <sjg@chromium.org>2019-07-11 01:52:58 +0300
commit2b6ed5e92e3b29fbfa3a966433d4bbc2ec1e9c58 (patch)
treec8d0f7232f82931ae27e1ee4e7888869270c3b5a /tools/patman/tools.py
parent7e6952df36a28b570818ee1fd36b07c41ef14aea (diff)
downloadu-boot-2b6ed5e92e3b29fbfa3a966433d4bbc2ec1e9c58.tar.xz
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>
Diffstat (limited to 'tools/patman/tools.py')
-rw-r--r--tools/patman/tools.py27
1 files changed, 27 insertions, 0 deletions
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