Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 732324a

Browse files
committed
#7905: Actually respect the keyencoding parameter to shelve.Shelf.
1 parent c9fb3c6 commit 732324a

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

Doc/library/shelve.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Restrictions
101101
implementation used.
102102

103103

104-
.. class:: Shelf(dict, protocol=None, writeback=False)
104+
.. class:: Shelf(dict, protocol=None, writeback=False, keyencoding='utf-8')
105105

106106
A subclass of :class:`collections.MutableMapping` which stores pickled values
107107
in the *dict* object.
@@ -115,8 +115,15 @@ Restrictions
115115
This allows natural operations on mutable entries, but can consume much more
116116
memory and make sync and close take a long time.
117117

118+
The *keyencoding* parameter is the encoding used to encode keys before they
119+
are used with the underlying dict.
118120

119-
.. class:: BsdDbShelf(dict, protocol=None, writeback=False)
121+
.. versionadded:: 3.2
122+
The *keyencoding* parameter; previously, keys were always encoded in
123+
UTF-8.
124+
125+
126+
.. class:: BsdDbShelf(dict, protocol=None, writeback=False, keyencoding='utf-8')
120127

121128
A subclass of :class:`Shelf` which exposes :meth:`first`, :meth:`!next`,
122129
:meth:`previous`, :meth:`last` and :meth:`set_location` which are available
@@ -125,8 +132,8 @@ Restrictions
125132
modules. The *dict* object passed to the constructor must support those
126133
methods. This is generally accomplished by calling one of
127134
:func:`bsddb.hashopen`, :func:`bsddb.btopen` or :func:`bsddb.rnopen`. The
128-
optional *protocol* and *writeback* parameters have the same interpretation
129-
as for the :class:`Shelf` class.
135+
optional *protocol*, *writeback*, and *keyencoding* parameters have the same
136+
interpretation as for the :class:`Shelf` class.
130137

131138

132139
.. class:: DbfilenameShelf(filename, flag='c', protocol=None, writeback=False)

Lib/shelve.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def closed(self, *args):
7373
def __repr__(self):
7474
return '<Closed Dictionary>'
7575

76+
7677
class Shelf(collections.MutableMapping):
7778
"""Base class for shelf implementations.
7879
@@ -88,7 +89,7 @@ def __init__(self, dict, protocol=None, writeback=False,
8889
self._protocol = protocol
8990
self.writeback = writeback
9091
self.cache = {}
91-
self.keyencoding = "utf-8"
92+
self.keyencoding = keyencoding
9293

9394
def __iter__(self):
9495
for k in self.dict.keys():

Lib/test/test_shelve.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ def test_mutable_entry(self):
122122
self.assertEqual(len(d1), 1)
123123
self.assertEqual(len(d2), 1)
124124

125+
def test_keyencoding(self):
126+
d = {}
127+
key = 'Pöp'
128+
# the default keyencoding is utf-8
129+
shelve.Shelf(d)[key] = [1]
130+
self.assertIn(key.encode('utf-8'), d)
131+
# but a different one can be given
132+
shelve.Shelf(d, keyencoding='latin1')[key] = [1]
133+
self.assertIn(key.encode('latin1'), d)
134+
# with all consequences
135+
s = shelve.Shelf(d, keyencoding='ascii')
136+
self.assertRaises(UnicodeEncodeError, s.__setitem__, key, [1])
137+
125138
def test_writeback_also_writes_immediately(self):
126139
# Issue 5754
127140
d = {}

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Core and Builtins
4545
Library
4646
-------
4747

48+
- Issue #7905: Actually respect the keyencoding parameter to shelve.Shelf.
49+
4850
- Issue #1569291: Speed up array.repeat().
4951

5052
- Provide an interface to set the optimization level of compilation in

0 commit comments

Comments
 (0)