[Python-checkins] bpo-34204: Use pickle.DEFAULT_PROTOCOL in shelve (GH-19639)

miss-islington webhook-mailer at python.org
Thu Oct 29 05:45:11 EDT 2020


https://github.com/python/cpython/commit/df59273c7a384ea8c890fa8e9b80c92825df841c
commit: df59273c7a384ea8c890fa8e9b80c92825df841c
branch: master
author: Zackery Spytz <zspytz at gmail.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2020-10-29T02:44:35-07:00
summary:

bpo-34204: Use pickle.DEFAULT_PROTOCOL in shelve (GH-19639)



Use pickle.DEFAULT_PROTOCOL (currently 5) in shelve instead of a
hardcoded 3.

files:
A Misc/NEWS.d/next/Library/2020-04-21-17-18-33.bpo-34204.9wXTtY.rst
M Doc/library/shelve.rst
M Doc/whatsnew/3.10.rst
M Lib/shelve.py
M Lib/test/test_shelve.py

diff --git a/Doc/library/shelve.rst b/Doc/library/shelve.rst
index f08c58179a2f9..07caf91d5b7d9 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,10 @@ lots of shared  sub-objects.  The keys are ordinary strings.
    determine which accessed entries are mutable, nor which ones were actually
    mutated).
 
+   .. versionchanged:: 3.10
+      :data:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle
+      protocol.
+
    .. note::
 
       Do not rely on the shelf being closed automatically; always call
@@ -108,9 +113,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.
@@ -130,6 +136,10 @@ Restrictions
    .. versionchanged:: 3.4
       Added context manager support.
 
+   .. versionchanged:: 3.10
+      :data:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle
+      protocol.
+
 
 .. class:: BsdDbShelf(dict, protocol=None, writeback=False, keyencoding='utf-8')
 
diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst
index b2c6d10ba8deb..45258db492571 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/Lib/shelve.py b/Lib/shelve.py
index 5d443a0fa8d4f..e053c397345a0 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 ac25eee2e52fd..cfdd67c26c5f5 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 test.support import os_helper
 from collections.abc import MutableMapping
@@ -160,7 +162,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 0000000000000..bce6d39148a37
--- /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``.



More information about the Python-checkins mailing list