Skip to content

bpo-34204: Make the shelve library use Pickle's default protocol version instead of defaulting to version 3 #22751

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Doc/library/shelve.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ lots of shared sub-objects. The keys are ordinary strings.
database file is opened for reading and writing. The optional *flag* parameter
has the same interpretation as the *flag* parameter of :func:`dbm.open`.

By default, version 3 pickles are used to serialize values. The version of the
pickle protocol can be specified with the *protocol* parameter.
By default, pickle's default protocol version is used to serialize values. The
version of the pickle protocol can be specified with the *protocol* parameter.

Because of Python semantics, a shelf cannot know when a mutable
persistent-dictionary entry is modified. By default modified objects are
Expand Down Expand Up @@ -108,9 +108,9 @@ Restrictions
A subclass of :class:`collections.abc.MutableMapping` which stores pickled
values in the *dict* object.

By default, version 3 pickles are used to serialize values. The version of the
pickle protocol can be specified with the *protocol* parameter. See the
:mod:`pickle` documentation for a discussion of the pickle protocols.
By default, pickle's default protocol version is used to serialize values. The
version of the pickle protocol can be specified with the *protocol* parameter.
See the :mod:`pickle` documentation for a discussion of the pickle protocols.

If the *writeback* parameter is ``True``, the object will hold a cache of all
entries accessed and write them back to the *dict* at sync and close times.
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ py_compile
Added ``--quiet`` option to command-line interface of :mod:`py_compile`.
(Contributed by Gregory Schevchenko in :issue:`38731`.)

shelve
------

The :mod:`shelve` module now uses :mod:`pickle` default protocol for the
Python version in use rather than protocol version 3.
(Contributed by Marco Castelluccio in :issue:`34204`.)

sys
---

Expand Down
4 changes: 2 additions & 2 deletions Lib/shelve.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
the persistent dictionary on disk, if feasible).
"""

from pickle import Pickler, Unpickler
from pickle import Pickler, Unpickler, DEFAULT_PROTOCOL
from io import BytesIO

import collections.abc
Expand Down Expand Up @@ -85,7 +85,7 @@ def __init__(self, dict, protocol=None, writeback=False,
keyencoding="utf-8"):
self.dict = dict
if protocol is None:
protocol = 3
protocol = DEFAULT_PROTOCOL
self._protocol = protocol
self.writeback = writeback
self.cache = {}
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_shelve.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import pickle
import shelve
import glob
from test import support
Expand Down Expand Up @@ -160,7 +161,7 @@ def test_with(self):

def test_default_protocol(self):
with shelve.Shelf({}) as s:
self.assertEqual(s._protocol, 3)
self.assertEqual(s._protocol, pickle.DEFAULT_PROTOCOL)

from test import mapping_tests

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The :mod:`shelve` module now uses :mod:`pickle` default protocol for the
Python version in use rather than protocol version 3.