]> granicus.if.org Git - python/commitdiff
bpo-23403: Bump pickle.DEFAULT_PROTOCOL to 4 (#6355)
authorŁukasz Langa <lukasz@langa.pl>
Wed, 4 Apr 2018 06:06:53 +0000 (23:06 -0700)
committerGitHub <noreply@github.com>
Wed, 4 Apr 2018 06:06:53 +0000 (23:06 -0700)
This makes performance better and produces shorter pickles. This change is backwards compatible up to the oldest currently supported version of Python (3.4).

Doc/library/pickle.rst
Doc/whatsnew/3.8.rst
Lib/pickle.py
Misc/NEWS.d/next/Library/2018-04-02-16-10-12.bpo-23403.KG7ADV.rst [new file with mode: 0644]
Modules/_pickle.c
Modules/clinic/_pickle.c.h

index d0c4cf937c8ac57c0d6a36c49c5d4d53d1b7eff9..ea854fae194455ff2f411d4a59a5ed8ae4a5cafc 100644 (file)
@@ -135,14 +135,14 @@ to read the pickle produced.
   information about improvements brought by protocol 2.
 
 * Protocol version 3 was added in Python 3.0.  It has explicit support for
-  :class:`bytes` objects and cannot be unpickled by Python 2.x.  This is
-  the default protocol, and the recommended protocol when compatibility with
-  other Python 3 versions is required.
+  :class:`bytes` objects and cannot be unpickled by Python 2.x.  This was
+  the default protocol in Python 3.0--3.7.
 
 * Protocol version 4 was added in Python 3.4.  It adds support for very large
   objects, pickling more kinds of objects, and some data format
-  optimizations.  Refer to :pep:`3154` for information about improvements
-  brought by protocol 4.
+  optimizations.  It is the default protocol starting with Python 3.8.
+  Refer to :pep:`3154` for information about improvements brought by
+  protocol 4.
 
 .. note::
    Serialization is a more primitive notion than persistence; although
@@ -179,8 +179,16 @@ The :mod:`pickle` module provides the following constants:
 
    An integer, the default :ref:`protocol version <pickle-protocols>` used
    for pickling.  May be less than :data:`HIGHEST_PROTOCOL`.  Currently the
-   default protocol is 3, a new protocol designed for Python 3.
+   default protocol is 4, first introduced in Python 3.4 and incompatible
+   with previous versions.
 
+   .. versionchanged:: 3.0
+
+      The default protocol is 3.
+
+   .. versionchanged:: 3.8
+
+      The default protocol is 4.
 
 The :mod:`pickle` module provides the following functions to make the pickling
 process more convenient:
index 4e6c85173751040080bd602eb5507072d51e038a..2f42a9f8ba77a1891579e7cec6953f81519fc332 100644 (file)
@@ -94,6 +94,9 @@ Improved Modules
 Optimizations
 =============
 
+* The default protocol in the :mod:`pickle` module is now Protocol 4,
+  first introduced in Python 3.4.  It offers better performance and smaller
+  size compared to Protocol 3 available since Python 3.0.
 
 Build and C API Changes
 =======================
index e6d003787bad89c99a25636d31a114598e235722..b852fbd2296b402badd1a7647771bb94738f810d 100644 (file)
@@ -57,9 +57,9 @@ compatible_formats = ["1.0",            # Original protocol 0
 HIGHEST_PROTOCOL = 4
 
 # The protocol we write by default.  May be less than HIGHEST_PROTOCOL.
-# We intentionally write a protocol that Python 2.x cannot read;
-# there are too many issues with that.
-DEFAULT_PROTOCOL = 3
+# Only bump this if the oldest still supported version of Python already
+# includes it.
+DEFAULT_PROTOCOL = 4
 
 class PickleError(Exception):
     """A common base class for the other pickling exceptions."""
@@ -376,8 +376,8 @@ class _Pickler:
 
         The optional *protocol* argument tells the pickler to use the
         given protocol; supported protocols are 0, 1, 2, 3 and 4.  The
-        default protocol is 3; a backward-incompatible protocol designed
-        for Python 3.
+        default protocol is 4. It was introduced in Python 3.4, it is
+        incompatible with previous versions.
 
         Specifying a negative protocol version selects the highest
         protocol version supported.  The higher the protocol used, the
diff --git a/Misc/NEWS.d/next/Library/2018-04-02-16-10-12.bpo-23403.KG7ADV.rst b/Misc/NEWS.d/next/Library/2018-04-02-16-10-12.bpo-23403.KG7ADV.rst
new file mode 100644 (file)
index 0000000..d1fbbeb
--- /dev/null
@@ -0,0 +1,4 @@
+``DEFAULT_PROTOCOL`` in :mod:`pickle` was bumped to 4. Protocol 4 is
+described in :pep:`3154` and available since Python 3.4. It offers
+better performance and smaller size compared to protocol 3 introduced
+in Python 3.0.
index 6a684f25fefd386e6e60dcb2faeeac2979c9d95e..aa672afacffd45c1f606940b784c2d15dd678d33 100644 (file)
@@ -20,10 +20,12 @@ class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoPro
 [clinic start generated code]*/
 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
 
-/* Bump this when new opcodes are added to the pickle protocol. */
+/* Bump HIGHEST_PROTOCOL when new opcodes are added to the pickle protocol.
+   Bump DEFAULT_PROTOCOL only when the oldest still supported version of Python
+   already includes it. */
 enum {
     HIGHEST_PROTOCOL = 4,
-    DEFAULT_PROTOCOL = 3
+    DEFAULT_PROTOCOL = 4
 };
 
 /* Pickle opcodes. These must be kept updated with pickle.py.
@@ -7176,8 +7178,9 @@ This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
 be more efficient.
 
 The optional *protocol* argument tells the pickler to use the given
-protocol supported protocols are 0, 1, 2, 3 and 4.  The default
-protocol is 3; a backward-incompatible protocol designed for Python 3.
+protocol; supported protocols are 0, 1, 2, 3 and 4.  The default
+protocol is 4. It was introduced in Python 3.4, it is incompatible
+with previous versions.
 
 Specifying a negative protocol version selects the highest protocol
 version supported.  The higher the protocol used, the more recent the
@@ -7196,7 +7199,7 @@ to map the new Python 3 names to the old module names used in Python
 static PyObject *
 _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
                   PyObject *protocol, int fix_imports)
-/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
+/*[clinic end generated code: output=a4774d5fde7d34de input=93f1408489a87472]*/
 {
     PicklerObject *pickler = _Pickler_New();
 
@@ -7236,7 +7239,8 @@ Return the pickled representation of the object as a bytes object.
 
 The optional *protocol* argument tells the pickler to use the given
 protocol; supported protocols are 0, 1, 2, 3 and 4.  The default
-protocol is 3; a backward-incompatible protocol designed for Python 3.
+protocol is 4. It was introduced in Python 3.4, it is incompatible
+with previous versions.
 
 Specifying a negative protocol version selects the highest protocol
 version supported.  The higher the protocol used, the more recent the
@@ -7250,7 +7254,7 @@ Python 2, so that the pickle data stream is readable with Python 2.
 static PyObject *
 _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
                    int fix_imports)
-/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
+/*[clinic end generated code: output=d75d5cda456fd261 input=b6efb45a7d19b5ab]*/
 {
     PyObject *result;
     PicklerObject *pickler = _Pickler_New();
index 7fc00a00479c7e9fa9cb8092f2dcdeafd86cdfb4..6d9072832ceaa8610d88ae35e90a44d95f2e8522 100644 (file)
@@ -367,8 +367,9 @@ PyDoc_STRVAR(_pickle_dump__doc__,
 "be more efficient.\n"
 "\n"
 "The optional *protocol* argument tells the pickler to use the given\n"
-"protocol supported protocols are 0, 1, 2, 3 and 4.  The default\n"
-"protocol is 3; a backward-incompatible protocol designed for Python 3.\n"
+"protocol; supported protocols are 0, 1, 2, 3 and 4.  The default\n"
+"protocol is 4. It was introduced in Python 3.4, it is incompatible\n"
+"with previous versions.\n"
 "\n"
 "Specifying a negative protocol version selects the highest protocol\n"
 "version supported.  The higher the protocol used, the more recent the\n"
@@ -419,7 +420,8 @@ PyDoc_STRVAR(_pickle_dumps__doc__,
 "\n"
 "The optional *protocol* argument tells the pickler to use the given\n"
 "protocol; supported protocols are 0, 1, 2, 3 and 4.  The default\n"
-"protocol is 3; a backward-incompatible protocol designed for Python 3.\n"
+"protocol is 4. It was introduced in Python 3.4, it is incompatible\n"
+"with previous versions.\n"
 "\n"
 "Specifying a negative protocol version selects the highest protocol\n"
 "version supported.  The higher the protocol used, the more recent the\n"
@@ -560,4 +562,4 @@ _pickle_loads(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=e995dd494045d876 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=6fc104b8299c82dd input=a9049054013a1b77]*/