diff options
author | Michael Wayne Goodman <goodman.m.w@gmail.com> | 2015-07-24 17:49:29 +0800 |
---|---|---|
committer | Michael Wayne Goodman <goodman.m.w@gmail.com> | 2015-07-24 17:49:29 +0800 |
commit | 2f6c7811694d33fb5d1cffe8a7cd1c564936120c (patch) | |
tree | 5934ad32a5d2d437cc471e5e1f578615674c60c4 | |
parent | 813be605c1429ed8be53f159fa7c7b3c05dda79e (diff) | |
download | bottle-2f6c7811694d33fb5d1cffe8a7cd1c564936120c.tar.gz |
Fix #720 : Allow unicode keys in ConfigDict.
-rw-r--r-- | bottle.py | 6 | ||||
-rw-r--r-- | test/test_configdict.py | 13 |
2 files changed, 16 insertions, 3 deletions
@@ -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() |