Skip to content

Commit e68c386

Browse files
committed
bpo-34204: Make the shelve library use Pickle's default protocol version instead of defaulting to version 3
1 parent 4173320 commit e68c386

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

Doc/library/shelve.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ lots of shared sub-objects. The keys are ordinary strings.
2525
database file is opened for reading and writing. The optional *flag* parameter
2626
has the same interpretation as the *flag* parameter of :func:`dbm.open`.
2727

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

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

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

115115
If the *writeback* parameter is ``True``, the object will hold a cache of all
116116
entries accessed and write them back to the *dict* at sync and close times.

Doc/whatsnew/3.10.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ py_compile
210210
Added ``--quiet`` option to command-line interface of :mod:`py_compile`.
211211
(Contributed by Gregory Schevchenko in :issue:`38731`.)
212212

213+
shelve
214+
------
215+
216+
The :mod:`shelve` module now uses :mod:`pickle` default protocol for the
217+
Python version in use rather than protocol version 3.
218+
(Contributed by Marco Castelluccio in :issue:`34204`.)
219+
213220
sys
214221
---
215222

Lib/shelve.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
the persistent dictionary on disk, if feasible).
5757
"""
5858

59-
from pickle import Pickler, Unpickler
59+
from pickle import Pickler, Unpickler, DEFAULT_PROTOCOL
6060
from io import BytesIO
6161

6262
import collections.abc
@@ -85,7 +85,7 @@ def __init__(self, dict, protocol=None, writeback=False,
8585
keyencoding="utf-8"):
8686
self.dict = dict
8787
if protocol is None:
88-
protocol = 3
88+
protocol = DEFAULT_PROTOCOL
8989
self._protocol = protocol
9090
self.writeback = writeback
9191
self.cache = {}

Lib/test/test_shelve.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import pickle
23
import shelve
34
import glob
45
from test import support
@@ -160,7 +161,7 @@ def test_with(self):
160161

161162
def test_default_protocol(self):
162163
with shelve.Shelf({}) as s:
163-
self.assertEqual(s._protocol, 3)
164+
self.assertEqual(s._protocol, pickle.DEFAULT_PROTOCOL)
164165

165166
from test import mapping_tests
166167

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The :mod:`shelve` module now uses :mod:`pickle` default protocol for the
2+
Python version in use rather than protocol version 3.

0 commit comments

Comments
 (0)