From 1c9214e3d37ed2bf6a7e39d36231bd7f17748d61 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Tue, 21 Apr 2020 17:19:14 -0600 Subject: [PATCH 1/4] bpo-34204: Use pickle.DEFAULT_PROTOCOL in shelve Use pickle.DEFAULT_PROTOCOL (currently 4) in shelve instead of a hardcoded 3. --- Doc/library/shelve.rst | 10 ++++++++-- Lib/shelve.py | 4 ++-- Lib/test/test_shelve.py | 4 +++- .../Library/2020-04-21-17-18-33.bpo-34204.9wXTtY.rst | 2 ++ 4 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-04-21-17-18-33.bpo-34204.9wXTtY.rst diff --git a/Doc/library/shelve.rst b/Doc/library/shelve.rst index f08c58179a2f9f..b586bd70ef9488 100644 --- a/Doc/library/shelve.rst +++ b/Doc/library/shelve.rst @@ -25,8 +25,9 @@ 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, pickles created with :data:`pickle.DEFAULT_PROTOCOL` are 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 @@ -40,6 +41,11 @@ lots of shared sub-objects. The keys are ordinary strings. determine which accessed entries are mutable, nor which ones were actually mutated). + .. versionchanged:: 3.9 + + :data:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle + protocol. + .. note:: Do not rely on the shelf being closed automatically; always call diff --git a/Lib/shelve.py b/Lib/shelve.py index 5d443a0fa8d4f1..e053c397345a07 100644 --- a/Lib/shelve.py +++ b/Lib/shelve.py @@ -56,7 +56,7 @@ the persistent dictionary on disk, if feasible). """ -from pickle import Pickler, Unpickler +from pickle import DEFAULT_PROTOCOL, Pickler, Unpickler from io import BytesIO import collections.abc @@ -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 = {} diff --git a/Lib/test/test_shelve.py b/Lib/test/test_shelve.py index 9ffe2cbeae4d86..6624c3caa20e7e 100644 --- a/Lib/test/test_shelve.py +++ b/Lib/test/test_shelve.py @@ -1,6 +1,8 @@ import unittest import shelve import glob +import pickle + from test import support from collections.abc import MutableMapping from test.test_dbm import dbm_iterator @@ -159,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 diff --git a/Misc/NEWS.d/next/Library/2020-04-21-17-18-33.bpo-34204.9wXTtY.rst b/Misc/NEWS.d/next/Library/2020-04-21-17-18-33.bpo-34204.9wXTtY.rst new file mode 100644 index 00000000000000..bce6d39148a376 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-04-21-17-18-33.bpo-34204.9wXTtY.rst @@ -0,0 +1,2 @@ +The :mod:`shelve` module now uses :data:`pickle.DEFAULT_PROTOCOL` by default +instead of :mod:`pickle` protocol ``3``. From 4925fcd80846a7bc3a6aa73ee36f572d4aaa908b Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Wed, 22 Apr 2020 13:13:06 -0600 Subject: [PATCH 2/4] Update Doc/whatsnew/3.9.rst --- Doc/library/shelve.rst | 7 ++++--- Doc/whatsnew/3.9.rst | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Doc/library/shelve.rst b/Doc/library/shelve.rst index b586bd70ef9488..5351877161d825 100644 --- a/Doc/library/shelve.rst +++ b/Doc/library/shelve.rst @@ -114,9 +114,10 @@ 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, pickles created with :data:`pickle.DEFAULT_PROTOCOL` 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. 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. diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index c4b49feed9fc1d..db331abc8d2582 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -370,6 +370,13 @@ random Add a new :attr:`random.Random.randbytes` method: generate random bytes. (Contributed by Victor Stinner in :issue:`40286`.) +shelve +------ + +The :mod:`shelve` module now uses :data:`pickle.DEFAULT_PROTOCOL` by default +instead of :mod:`pickle` protocol ``3`` when creating shelves. +(Contributed by Zackery Spytz in :issue:`34204`.) + signal ------ From 04ad2c6bede84aa9218210d7a9bfa03c3087d4d6 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Wed, 22 Apr 2020 14:54:41 -0600 Subject: [PATCH 3/4] Doc tweaks. --- Doc/library/shelve.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Doc/library/shelve.rst b/Doc/library/shelve.rst index 5351877161d825..afa48e395bf8b9 100644 --- a/Doc/library/shelve.rst +++ b/Doc/library/shelve.rst @@ -42,7 +42,6 @@ lots of shared sub-objects. The keys are ordinary strings. mutated). .. versionchanged:: 3.9 - :data:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle protocol. @@ -137,6 +136,10 @@ Restrictions .. versionchanged:: 3.4 Added context manager support. + .. versionchanged:: 3.9 + :data:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle + protocol. + .. class:: BsdDbShelf(dict, protocol=None, writeback=False, keyencoding='utf-8') From 2b9cae4025a74795dc6b7828293ce847d08e5e22 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Tue, 27 Oct 2020 18:40:14 -0600 Subject: [PATCH 4/4] 3.9 -> 3.10 --- Doc/library/shelve.rst | 4 ++-- Doc/whatsnew/3.10.rst | 7 +++++++ Doc/whatsnew/3.9.rst | 7 ------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Doc/library/shelve.rst b/Doc/library/shelve.rst index afa48e395bf8b9..07caf91d5b7d99 100644 --- a/Doc/library/shelve.rst +++ b/Doc/library/shelve.rst @@ -41,7 +41,7 @@ lots of shared sub-objects. The keys are ordinary strings. determine which accessed entries are mutable, nor which ones were actually mutated). - .. versionchanged:: 3.9 + .. versionchanged:: 3.10 :data:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle protocol. @@ -136,7 +136,7 @@ Restrictions .. versionchanged:: 3.4 Added context manager support. - .. versionchanged:: 3.9 + .. versionchanged:: 3.10 :data:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle protocol. diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index b2c6d10ba8deb7..45258db4925718 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -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 :data:`pickle.DEFAULT_PROTOCOL` by default +instead of :mod:`pickle` protocol ``3`` when creating shelves. +(Contributed by Zackery Spytz in :issue:`34204`.) + sys --- diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index b80187b8aea63b..1a37f16ea2b09a 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -627,13 +627,6 @@ random Added a new :attr:`random.Random.randbytes` method: generate random bytes. (Contributed by Victor Stinner in :issue:`40286`.) -shelve ------- - -The :mod:`shelve` module now uses :data:`pickle.DEFAULT_PROTOCOL` by default -instead of :mod:`pickle` protocol ``3`` when creating shelves. -(Contributed by Zackery Spytz in :issue:`34204`.) - signal ------