Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
732324a3
Commit
732324a3
authored
Dec 04, 2010
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#7905: Actually respect the keyencoding parameter to shelve.Shelf.
parent
c9fb3c64
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
28 additions
and
5 deletions
+28
-5
Doc/library/shelve.rst
Doc/library/shelve.rst
+11
-4
Lib/shelve.py
Lib/shelve.py
+2
-1
Lib/test/test_shelve.py
Lib/test/test_shelve.py
+13
-0
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/shelve.rst
View file @
732324a3
...
...
@@ -101,7 +101,7 @@ Restrictions
implementation used.
.. class:: Shelf(dict, protocol=None, writeback=False)
.. class:: Shelf(dict, protocol=None, writeback=False
, keyencoding='utf-8'
)
A subclass of :class:`collections.MutableMapping` which stores pickled values
in the *dict* object.
...
...
@@ -115,8 +115,15 @@ Restrictions
This allows natural operations on mutable entries, but can consume much more
memory and make sync and close take a long time.
The *keyencoding* parameter is the encoding used to encode keys before they
are used with the underlying dict.
.. class:: BsdDbShelf(dict, protocol=None, writeback=False)
.. versionadded:: 3.2
The *keyencoding* parameter; previously, keys were always encoded in
UTF-8.
.. class:: BsdDbShelf(dict, protocol=None, writeback=False, keyencoding='utf-8')
A subclass of :class:`Shelf` which exposes :meth:`first`, :meth:`!next`,
:meth:`previous`, :meth:`last` and :meth:`set_location` which are available
...
...
@@ -125,8 +132,8 @@ Restrictions
modules. The *dict* object passed to the constructor must support those
methods. This is generally accomplished by calling one of
:func:`bsddb.hashopen`, :func:`bsddb.btopen` or :func:`bsddb.rnopen`. The
optional *protocol*
and *writeback* parameters have the same interpretation
as for the :class:`Shelf` class.
optional *protocol*
, *writeback*, and *keyencoding* parameters have the same
interpretation
as for the :class:`Shelf` class.
.. class:: DbfilenameShelf(filename, flag='c', protocol=None, writeback=False)
...
...
Lib/shelve.py
View file @
732324a3
...
...
@@ -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
():
...
...
Lib/test/test_shelve.py
View file @
732324a3
...
...
@@ -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
=
{}
...
...
Misc/NEWS
View file @
732324a3
...
...
@@ -45,6 +45,8 @@ Core and Builtins
Library
-------
- Issue #7905: Actually respect the keyencoding parameter to shelve.Shelf.
- Issue #1569291: Speed up array.repeat().
- Provide an interface to set the optimization level of compilation in
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment