From 2f6c7811694d33fb5d1cffe8a7cd1c564936120c Mon Sep 17 00:00:00 2001 From: Michael Wayne Goodman Date: Fri, 24 Jul 2015 17:49:29 +0800 Subject: Fix #720 : Allow unicode keys in ConfigDict. --- bottle.py | 6 +++--- test/test_configdict.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/bottle.py b/bottle.py index 535ddd3..f99f46a 100644 --- a/bottle.py +++ b/bottle.py @@ -2107,7 +2107,7 @@ class ConfigDict(dict): if not isinstance(source, dict): raise TypeError('Source is not a dict (r)' % type(key)) for key, value in source.items(): - if not isinstance(key, str): + if not isinstance(key, basestring): raise TypeError('Key is not a string (%r)' % type(key)) full_key = prefix + '.' + key if prefix else key if isinstance(value, dict): @@ -2123,7 +2123,7 @@ class ConfigDict(dict): namespace. Apart from that it works just as the usual dict.update(). Example: ``update('some.namespace', key='value')`` ''' prefix = '' - if a and isinstance(a[0], str): + if a and isinstance(a[0], basestring): prefix = a[0].strip('.') + '.' a = a[1:] for key, value in dict(*a, **ka).items(): @@ -2135,7 +2135,7 @@ class ConfigDict(dict): return self[key] def __setitem__(self, key, value): - if not isinstance(key, str): + if not isinstance(key, basestring): raise TypeError('Key has type %r (not a string)' % type(key)) value = self.meta_get(key, 'filter', lambda x: x)(value) diff --git a/test/test_configdict.py b/test/test_configdict.py index 654ad7a..16719ab 100644 --- a/test/test_configdict.py +++ b/test/test_configdict.py @@ -75,6 +75,19 @@ class TestConfigDict(unittest.TestCase): self.assertRaises(TypeError, lambda: setitem(c, 5, 6)) self.assertRaises(TypeError, lambda: c.load_dict({5:6})) + def test_issue720(self): + """Accept unicode keys.""" + try: + key = unichr(12354) + except NameError: + key = chr(12354) + c = ConfigDict() + c.load_dict({key: 'value'}) + self.assertEqual('value', c[key]) + c = ConfigDict() + c.load_dict({key: {'subkey': 'value'}}) + self.assertEqual('value', c[key + '.subkey']) + if __name__ == '__main__': #pragma: no cover unittest.main() -- cgit v1.2.1