summaryrefslogtreecommitdiff
path: root/tools/patman/tools.py
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2019-05-15 00:53:50 +0300
committerSimon Glass <sjg@chromium.org>2019-07-11 01:52:58 +0300
commit513eace47d63161cabfd3cce82f3e075525b164a (patch)
tree912b530a6efc46823f3e2f6e9894b0a5cb09e386 /tools/patman/tools.py
parentade1e3864f1508ad0ccbce72a39d815c2d7d7c87 (diff)
downloadu-boot-513eace47d63161cabfd3cce82f3e075525b164a.tar.xz
patman: Move unicode helpers to tools
Create helper functions in the tools module to deal with the differences between unicode in Python 2 (where we use the 'unicode' type) and Python 3 (where we use the 'str' type). Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'tools/patman/tools.py')
-rw-r--r--tools/patman/tools.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/tools/patman/tools.py b/tools/patman/tools.py
index 0ad0fb9705..7e6a45a3b0 100644
--- a/tools/patman/tools.py
+++ b/tools/patman/tools.py
@@ -258,3 +258,35 @@ def GetBytes(byte, size):
else:
data = chr(byte) * size
return data
+
+def ToUnicode(val):
+ """Make sure a value is a unicode string
+
+ This allows some amount of compatibility between Python 2 and Python3. For
+ the former, it returns a unicode object.
+
+ Args:
+ val: string or unicode object
+
+ Returns:
+ unicode version of val
+ """
+ if sys.version_info[0] >= 3:
+ return val
+ return val if isinstance(val, unicode) else val.decode('utf-8')
+
+def FromUnicode(val):
+ """Make sure a value is a non-unicode string
+
+ This allows some amount of compatibility between Python 2 and Python3. For
+ the former, it converts a unicode object to a string.
+
+ Args:
+ val: string or unicode object
+
+ Returns:
+ non-unicode version of val
+ """
+ if sys.version_info[0] >= 3:
+ return val
+ return val if isinstance(val, str) else val.encode('utf-8')