[U-Boot] [PATCH 16/21] patman: Move unicode helpers to tools
Simon Glass
sjg at chromium.org
Tue May 14 21:53:50 UTC 2019
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 at chromium.org>
---
tools/patman/gitutil.py | 4 ++--
tools/patman/series.py | 3 ++-
tools/patman/settings.py | 16 +++-------------
tools/patman/tools.py | 32 ++++++++++++++++++++++++++++++++
4 files changed, 39 insertions(+), 16 deletions(-)
diff --git a/tools/patman/gitutil.py b/tools/patman/gitutil.py
index 11aeb73b74..fb9e67c0f0 100644
--- a/tools/patman/gitutil.py
+++ b/tools/patman/gitutil.py
@@ -12,6 +12,7 @@ import terminal
import checkpatch
import settings
+import tools
# True to use --no-decorate - we check this in Setup()
use_no_decorate = True
@@ -325,9 +326,8 @@ def BuildEmailList(in_list, tag=None, alias=None, raise_on_error=True):
raw += LookupEmail(item, alias, raise_on_error=raise_on_error)
result = []
for item in raw:
+ item = tools.FromUnicode(item)
if not item in result:
- if type(item) == unicode:
- item = item.encode('utf-8')
result.append(item)
if tag:
return ['%s %s%s%s' % (tag, quote, email, quote) for email in result]
diff --git a/tools/patman/series.py b/tools/patman/series.py
index 2735afaf88..0b71a896cb 100644
--- a/tools/patman/series.py
+++ b/tools/patman/series.py
@@ -11,6 +11,7 @@ import get_maintainer
import gitutil
import settings
import terminal
+import tools
# Series-xxx tags that we understand
valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
@@ -249,7 +250,7 @@ class Series(dict):
cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
cover_cc = [m.encode('utf-8') if type(m) != str else m
for m in cover_cc]
- cc_list = ', '.join([x.decode('utf-8')
+ cc_list = ', '.join([tools.ToUnicode(x)
for x in set(cover_cc + all_ccs)])
print(cover_fname, cc_list.encode('utf-8'), file=fd)
diff --git a/tools/patman/settings.py b/tools/patman/settings.py
index 07bf6a6ea4..b080136d88 100644
--- a/tools/patman/settings.py
+++ b/tools/patman/settings.py
@@ -14,6 +14,7 @@ import re
import command
import gitutil
+import tools
"""Default settings per-project.
@@ -99,17 +100,6 @@ class _ProjectConfigParser(ConfigParser.SafeConfigParser):
for setting_name, setting_value in project_defaults.items():
self.set(project_settings, setting_name, setting_value)
- def _to_unicode(self, val):
- """Make sure a value is of type 'unicode'
-
- Args:
- val: string or unicode object
-
- Returns:
- unicode version of val
- """
- return val if isinstance(val, unicode) else val.decode('utf-8')
-
def get(self, section, option, *args, **kwargs):
"""Extend SafeConfigParser to try project_section before section.
@@ -127,7 +117,7 @@ class _ProjectConfigParser(ConfigParser.SafeConfigParser):
val = ConfigParser.SafeConfigParser.get(
self, section, option, *args, **kwargs
)
- return self._to_unicode(val)
+ return tools.ToUnicode(val)
def items(self, section, *args, **kwargs):
"""Extend SafeConfigParser to add project_section to section.
@@ -162,7 +152,7 @@ class _ProjectConfigParser(ConfigParser.SafeConfigParser):
item_dict = dict(top_items)
item_dict.update(project_items)
- return {(self._to_unicode(item), self._to_unicode(val))
+ return {(tools.ToUnicode(item), tools.ToUnicode(val))
for item, val in item_dict.items()}
def ReadGitAliases(fname):
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')
--
2.21.0.1020.gf2820cf01a-goog
More information about the U-Boot
mailing list