aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorSimon Glass2018-10-01 22:12:33 -0500
committerSimon Glass2018-10-08 08:34:34 -0500
commitec9e0f471291233491d1bd213d32bb259821da95 (patch)
tree5c44359d4e2978b124656a627436af1a679f6456 /tools
parent9f8037ea9ca81fc158bc190f7427f329d96ad76c (diff)
downloadu-boot-ec9e0f471291233491d1bd213d32bb259821da95.tar.gz
u-boot-ec9e0f471291233491d1bd213d32bb259821da95.tar.xz
u-boot-ec9e0f471291233491d1bd213d32bb259821da95.zip
patman: Handle unicode in _ProjectConfigParser tests
With Python 2.7.15rc1, ConfigParser.SafeConfigParser has unfortunately started returning unicode, for unknown reasons. Adjust the code to handle this by converting everything to unicode. We cannot convert things to ASCII since email addresses may be encoded with UTF-8. Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'tools')
-rw-r--r--tools/patman/settings.py27
1 files changed, 20 insertions, 7 deletions
diff --git a/tools/patman/settings.py b/tools/patman/settings.py
index ca4334426b..ea2bc74f75 100644
--- a/tools/patman/settings.py
+++ b/tools/patman/settings.py
@@ -58,25 +58,25 @@ class _ProjectConfigParser(ConfigParser.SafeConfigParser):
58 >>> config = _ProjectConfigParser("zzz") 58 >>> config = _ProjectConfigParser("zzz")
59 >>> config.readfp(StringIO(sample_config)) 59 >>> config.readfp(StringIO(sample_config))
60 >>> config.get("alias", "enemies") 60 >>> config.get("alias", "enemies")
61 'Evil <evil@example.com>' 61 u'Evil <evil@example.com>'
62 62
63 # Check to make sure that alias gets overridden by project. 63 # Check to make sure that alias gets overridden by project.
64 >>> config = _ProjectConfigParser("sm") 64 >>> config = _ProjectConfigParser("sm")
65 >>> config.readfp(StringIO(sample_config)) 65 >>> config.readfp(StringIO(sample_config))
66 >>> config.get("alias", "enemies") 66 >>> config.get("alias", "enemies")
67 'Green G. <ugly@example.com>' 67 u'Green G. <ugly@example.com>'
68 68
69 # Check to make sure that settings get merged with project. 69 # Check to make sure that settings get merged with project.
70 >>> config = _ProjectConfigParser("linux") 70 >>> config = _ProjectConfigParser("linux")
71 >>> config.readfp(StringIO(sample_config)) 71 >>> config.readfp(StringIO(sample_config))
72 >>> sorted(config.items("settings")) 72 >>> sorted(config.items("settings"))
73 [('am_hero', 'True'), ('process_tags', 'False')] 73 [(u'am_hero', u'True'), (u'process_tags', u'False')]
74 74
75 # Check to make sure that settings works with unknown project. 75 # Check to make sure that settings works with unknown project.
76 >>> config = _ProjectConfigParser("unknown") 76 >>> config = _ProjectConfigParser("unknown")
77 >>> config.readfp(StringIO(sample_config)) 77 >>> config.readfp(StringIO(sample_config))
78 >>> sorted(config.items("settings")) 78 >>> sorted(config.items("settings"))
79 [('am_hero', 'True')] 79 [(u'am_hero', u'True')]
80 """ 80 """
81 def __init__(self, project_name): 81 def __init__(self, project_name):
82 """Construct _ProjectConfigParser. 82 """Construct _ProjectConfigParser.
@@ -99,6 +99,17 @@ class _ProjectConfigParser(ConfigParser.SafeConfigParser):
99 for setting_name, setting_value in project_defaults.items(): 99 for setting_name, setting_value in project_defaults.items():
100 self.set(project_settings, setting_name, setting_value) 100 self.set(project_settings, setting_name, setting_value)
101 101
102 def _to_unicode(self, val):
103 """Make sure a value is of type 'unicode'
104
105 Args:
106 val: string or unicode object
107
108 Returns:
109 unicode version of val
110 """
111 return val if isinstance(val, unicode) else val.decode('utf-8')
112
102 def get(self, section, option, *args, **kwargs): 113 def get(self, section, option, *args, **kwargs):
103 """Extend SafeConfigParser to try project_section before section. 114 """Extend SafeConfigParser to try project_section before section.
104 115
@@ -108,14 +119,15 @@ class _ProjectConfigParser(ConfigParser.SafeConfigParser):
108 See SafeConfigParser. 119 See SafeConfigParser.
109 """ 120 """
110 try: 121 try:
111 return ConfigParser.SafeConfigParser.get( 122 val = ConfigParser.SafeConfigParser.get(
112 self, "%s_%s" % (self._project_name, section), option, 123 self, "%s_%s" % (self._project_name, section), option,
113 *args, **kwargs 124 *args, **kwargs
114 ) 125 )
115 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): 126 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
116 return ConfigParser.SafeConfigParser.get( 127 val = ConfigParser.SafeConfigParser.get(
117 self, section, option, *args, **kwargs 128 self, section, option, *args, **kwargs
118 ) 129 )
130 return self._to_unicode(val)
119 131
120 def items(self, section, *args, **kwargs): 132 def items(self, section, *args, **kwargs):
121 """Extend SafeConfigParser to add project_section to section. 133 """Extend SafeConfigParser to add project_section to section.
@@ -150,7 +162,8 @@ class _ProjectConfigParser(ConfigParser.SafeConfigParser):
150 162
151 item_dict = dict(top_items) 163 item_dict = dict(top_items)
152 item_dict.update(project_items) 164 item_dict.update(project_items)
153 return item_dict.items() 165 return {(self._to_unicode(item), self._to_unicode(val))
166 for item, val in item_dict.iteritems()}
154 167
155def ReadGitAliases(fname): 168def ReadGitAliases(fname):
156 """Read a git alias file. This is in the form used by git: 169 """Read a git alias file. This is in the form used by git: