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

Skip to content

Commit df59273

Browse files
authored
bpo-34204: Use pickle.DEFAULT_PROTOCOL in shelve (GH-19639)
Use pickle.DEFAULT_PROTOCOL (currently 5) in shelve instead of a hardcoded 3.
1 parent 4173320 commit df59273

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

Doc/library/shelve.rst

+15-5
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ 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, pickles created with :data:`pickle.DEFAULT_PROTOCOL` are used
29+
to serialize values. The version of the pickle protocol can be specified
30+
with the *protocol* parameter.
3031

3132
Because of Python semantics, a shelf cannot know when a mutable
3233
persistent-dictionary entry is modified. By default modified objects are
@@ -40,6 +41,10 @@ lots of shared sub-objects. The keys are ordinary strings.
4041
determine which accessed entries are mutable, nor which ones were actually
4142
mutated).
4243

44+
.. versionchanged:: 3.10
45+
:data:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle
46+
protocol.
47+
4348
.. note::
4449

4550
Do not rely on the shelf being closed automatically; always call
@@ -108,9 +113,10 @@ Restrictions
108113
A subclass of :class:`collections.abc.MutableMapping` which stores pickled
109114
values in the *dict* object.
110115

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.
116+
By default, pickles created with :data:`pickle.DEFAULT_PROTOCOL` are used
117+
to serialize values. The version of the pickle protocol can be specified
118+
with the *protocol* parameter. See the :mod:`pickle` documentation for a
119+
discussion of the pickle protocols.
114120

115121
If the *writeback* parameter is ``True``, the object will hold a cache of all
116122
entries accessed and write them back to the *dict* at sync and close times.
@@ -130,6 +136,10 @@ Restrictions
130136
.. versionchanged:: 3.4
131137
Added context manager support.
132138

139+
.. versionchanged:: 3.10
140+
:data:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle
141+
protocol.
142+
133143

134144
.. class:: BsdDbShelf(dict, protocol=None, writeback=False, keyencoding='utf-8')
135145

Doc/whatsnew/3.10.rst

+7
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 :data:`pickle.DEFAULT_PROTOCOL` by default
217+
instead of :mod:`pickle` protocol ``3`` when creating shelves.
218+
(Contributed by Zackery Spytz in :issue:`34204`.)
219+
213220
sys
214221
---
215222

Lib/shelve.py

+2-2
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 DEFAULT_PROTOCOL, Pickler, Unpickler
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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import unittest
22
import shelve
33
import glob
4+
import pickle
5+
46
from test import support
57
from test.support import os_helper
68
from collections.abc import MutableMapping
@@ -160,7 +162,7 @@ def test_with(self):
160162

161163
def test_default_protocol(self):
162164
with shelve.Shelf({}) as s:
163-
self.assertEqual(s._protocol, 3)
165+
self.assertEqual(s._protocol, pickle.DEFAULT_PROTOCOL)
164166

165167
from test import mapping_tests
166168

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The :mod:`shelve` module now uses :data:`pickle.DEFAULT_PROTOCOL` by default
2+
instead of :mod:`pickle` protocol ``3``.

0 commit comments

Comments
 (0)