-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-99631: Add custom loads and dumps support for the shelve module #118065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 58 commits
e269c09
3465149
44b9fa1
f2eed32
b3e5723
53d5557
d496eab
1e295ba
2011baa
3cbabe9
67b7340
c6b43e2
52a90f7
4f79cf6
798fdb2
bb1150d
98d841b
1159bb6
4b4f1b6
bc399fa
41448d3
fbbe5ea
fdd3e8e
6823ef2
2affece
da8bc91
6bfebee
82d58a7
7dca8b4
5f97676
048daee
00837d0
1292963
3431920
97a6d7c
3becbc8
3a5d6ed
fb74832
d670c95
f2e22eb
e00a52f
3af3f97
9d232e5
26fc959
ab005aa
6052309
87b66d5
0c2f255
3db0c8e
5c39d94
1ca1801
5a42de1
b0a5ee3
4202ede
2827eb4
9918531
54188bd
786a248
20c2450
b3770ae
588623a
4d9599b
9b204b7
8b06918
b0f0bbc
d1bb227
791743b
6b4be8b
34a32b9
bf6f3aa
4b000cd
2dcda2a
00bfb01
23ea842
4df9b58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,8 @@ This includes most class instances, recursive data types, and objects containing | |
lots of shared sub-objects. The keys are ordinary strings. | ||
|
||
|
||
.. function:: open(filename, flag='c', protocol=None, writeback=False) | ||
.. function:: open(filename, flag='c', protocol=None, writeback=False, *, \ | ||
serializer=None, deserializer=None) | ||
|
||
Open a persistent dictionary. The filename specified is the base filename for | ||
the underlying database. As a side-effect, an extension may be added to the | ||
|
@@ -41,13 +42,27 @@ lots of shared sub-objects. The keys are ordinary strings. | |
determine which accessed entries are mutable, nor which ones were actually | ||
mutated). | ||
|
||
By default, :mod:`shelve` uses :func:`pickle.dumps` and :func:`pickle.loads` | ||
for serializing and deserializing. This can be changed by supplying | ||
*serializer* and *deserializer*, respectively. The *serializer* argument | ||
should be a function that takes an object and the *protocol* argument passed | ||
to the open function and returns its representation as a | ||
:term:`bytes-like object`; *protocol* argument that may be ignored by the | ||
function. *deserializer* should be a function that takes :class:`bytes` and | ||
returns the corresponding object. If one of these is given, the other must | ||
be given as well. Otherwise :mod:`shelve` will raise a :exc:`ShelveError`. | ||
|
||
.. versionchanged:: 3.10 | ||
:const:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle | ||
protocol. | ||
|
||
.. versionchanged:: 3.11 | ||
Accepts :term:`path-like object` for filename. | ||
|
||
.. versionchanged:: 3.14 | ||
Accepts custom *serializer* and *deserializer* functions in place of | ||
:func:`pickle.dumps` and :func:`pickle.loads`. | ||
|
||
.. note:: | ||
|
||
Do not rely on the shelf being closed automatically; always call | ||
|
@@ -117,7 +132,8 @@ Restrictions | |
which can cause hard crashes when trying to read from the database. | ||
|
||
|
||
.. class:: Shelf(dict, protocol=None, writeback=False, keyencoding='utf-8') | ||
.. class:: Shelf(dict, protocol=None, writeback=False, \ | ||
keyencoding='utf-8', *, serializer=None, deserializer=None) | ||
|
||
A subclass of :class:`collections.abc.MutableMapping` which stores pickled | ||
values in the *dict* object. | ||
|
@@ -135,6 +151,9 @@ Restrictions | |
The *keyencoding* parameter is the encoding used to encode keys before they | ||
are used with the underlying dict. | ||
|
||
The *serializer* and *deserializer* parameters have the same interpretation | ||
as in :func:`~shelve.open`. | ||
|
||
A :class:`Shelf` object can also be used as a context manager, in which | ||
case it will be automatically closed when the :keyword:`with` block ends. | ||
|
||
|
@@ -149,8 +168,13 @@ Restrictions | |
:const:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle | ||
protocol. | ||
|
||
.. versionchanged:: 3.14 | ||
Added the *serializer* and *deserializer* parameters. | ||
furkanonder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. class:: BsdDbShelf(dict, protocol=None, writeback=False, keyencoding='utf-8') | ||
|
||
.. class:: BsdDbShelf(dict, protocol=None, writeback=False, \ | ||
keyencoding='utf-8', *, \ | ||
serializer=None, deserializer=None) | ||
|
||
A subclass of :class:`Shelf` which exposes :meth:`!first`, :meth:`!next`, | ||
:meth:`!previous`, :meth:`!last` and :meth:`!set_location` methods. | ||
|
@@ -160,18 +184,27 @@ 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the way, we also need to update this sentence (from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the change would be bigger: it’s a new module (although the docs are not very clear) with maybe a new API. Updating or deprecating this should be discussed in its own ticket 🙂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree on it. It would be better to open a new ticket to discuss this issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... or consider instead opening topic on Discourse; this may need a larger audience than what you'd get on the bug tracker. |
||
optional *protocol*, *writeback*, and *keyencoding* parameters have the same | ||
interpretation as for the :class:`Shelf` class. | ||
optional *protocol*, *writeback*, *keyencoding*, *serializer* and *deserializer* | ||
parameters have the same interpretation as in :func:`~shelve.open`. | ||
|
||
.. versionchanged:: 3.14 | ||
Added the *serializer* and *deserializer* parameters. | ||
furkanonder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
.. class:: DbfilenameShelf(filename, flag='c', protocol=None, writeback=False) | ||
.. class:: DbfilenameShelf(filename, flag='c', protocol=None, \ | ||
writeback=False, *, serializer=None, \ | ||
deserializer=None) | ||
|
||
A subclass of :class:`Shelf` which accepts a *filename* instead of a dict-like | ||
object. The underlying file will be opened using :func:`dbm.open`. By | ||
default, the file will be created and opened for both read and write. The | ||
optional *flag* parameter has the same interpretation as for the :func:`.open` | ||
function. The optional *protocol* and *writeback* parameters have the same | ||
interpretation as for the :class:`Shelf` class. | ||
optional *flag* parameter has the same interpretation as for the | ||
:func:`.open` function. The optional *protocol*, *writeback*, *serializer* | ||
and *deserializer* parameters have the same interpretation as in the | ||
furkanonder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
:func:`~shelve.open`. | ||
|
||
.. versionchanged:: 3.14 | ||
Added the *serializer* and *deserializer* parameters. | ||
furkanonder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
.. _shelve-example: | ||
|
@@ -213,6 +246,20 @@ object):: | |
d.close() # close it | ||
|
||
|
||
Exceptions | ||
---------- | ||
|
||
.. exception:: ShelveError | ||
|
||
Exception raised when one of the arguments *deserializer* and *serializer* | ||
is missing in the :func:`~shelve.open`, :class:`Shelf`, :class:`BsdDbShelf` | ||
and :class:`DbfilenameShelf` | ||
furkanonder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The *deserializer* and *serializer* arguments must be given together. | ||
|
||
.. versionadded:: 3.14 | ||
|
||
furkanonder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. seealso:: | ||
|
||
Module :mod:`dbm` | ||
|
Uh oh!
There was an error while loading. Please reload this page.