diff options
author | Georg Brandl <georg@python.org> | 2010-12-04 11:12:43 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-12-04 11:12:43 (GMT) |
commit | 732324a3f86a352217b01ae2438b6db7691ae0b1 (patch) | |
tree | 8751b652a09b2fe8ac354edd571c431210dda3b2 /Lib | |
parent | c9fb3c64177f8efefd18a11f86bcf2f94d0f7c90 (diff) | |
download | cpython-732324a3f86a352217b01ae2438b6db7691ae0b1.zip cpython-732324a3f86a352217b01ae2438b6db7691ae0b1.tar.gz cpython-732324a3f86a352217b01ae2438b6db7691ae0b1.tar.bz2 |
#7905: Actually respect the keyencoding parameter to shelve.Shelf.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/shelve.py | 3 | ||||
-rw-r--r-- | Lib/test/test_shelve.py | 13 |
2 files changed, 15 insertions, 1 deletions
diff --git a/Lib/shelve.py b/Lib/shelve.py index 52e471a..cc1815e 100644 --- a/Lib/shelve.py +++ b/Lib/shelve.py @@ -73,6 +73,7 @@ class _ClosedDict(collections.MutableMapping): def __repr__(self): return '<Closed Dictionary>' + class Shelf(collections.MutableMapping): """Base class for shelf implementations. @@ -88,7 +89,7 @@ class Shelf(collections.MutableMapping): self._protocol = protocol self.writeback = writeback self.cache = {} - self.keyencoding = "utf-8" + self.keyencoding = keyencoding def __iter__(self): for k in self.dict.keys(): diff --git a/Lib/test/test_shelve.py b/Lib/test/test_shelve.py index 066208a..3e73f52 100644 --- a/Lib/test/test_shelve.py +++ b/Lib/test/test_shelve.py @@ -122,6 +122,19 @@ class TestCase(unittest.TestCase): self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1) + def test_keyencoding(self): + d = {} + key = 'Pöp' + # the default keyencoding is utf-8 + shelve.Shelf(d)[key] = [1] + self.assertIn(key.encode('utf-8'), d) + # but a different one can be given + shelve.Shelf(d, keyencoding='latin1')[key] = [1] + self.assertIn(key.encode('latin1'), d) + # with all consequences + s = shelve.Shelf(d, keyencoding='ascii') + self.assertRaises(UnicodeEncodeError, s.__setitem__, key, [1]) + def test_writeback_also_writes_immediately(self): # Issue 5754 d = {} |