bpo-23403: Bump pickle.DEFAULT_PROTOCOL to 4 (#6355)

This makes performance better and produces shorter pickles. This change is backwards compatible up to the oldest currently supported version of Python (3.4).
This commit is contained in:
Łukasz Langa 2018-04-03 23:06:53 -07:00 committed by GitHub
parent 42ec190761
commit c51d8c9ba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 22 deletions

View File

@ -135,14 +135,14 @@ to read the pickle produced.
information about improvements brought by protocol 2. information about improvements brought by protocol 2.
* Protocol version 3 was added in Python 3.0. It has explicit support for * 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 :class:`bytes` objects and cannot be unpickled by Python 2.x. This was
the default protocol, and the recommended protocol when compatibility with the default protocol in Python 3.0--3.7.
other Python 3 versions is required.
* Protocol version 4 was added in Python 3.4. It adds support for very large * 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 objects, pickling more kinds of objects, and some data format
optimizations. Refer to :pep:`3154` for information about improvements optimizations. It is the default protocol starting with Python 3.8.
brought by protocol 4. Refer to :pep:`3154` for information about improvements brought by
protocol 4.
.. note:: .. note::
Serialization is a more primitive notion than persistence; although 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 An integer, the default :ref:`protocol version <pickle-protocols>` used
for pickling. May be less than :data:`HIGHEST_PROTOCOL`. Currently the 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 The :mod:`pickle` module provides the following functions to make the pickling
process more convenient: process more convenient:

View File

@ -94,6 +94,9 @@ Improved Modules
Optimizations 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 Build and C API Changes
======================= =======================

View File

@ -57,9 +57,9 @@ compatible_formats = ["1.0", # Original protocol 0
HIGHEST_PROTOCOL = 4 HIGHEST_PROTOCOL = 4
# The protocol we write by default. May be less than HIGHEST_PROTOCOL. # The protocol we write by default. May be less than HIGHEST_PROTOCOL.
# We intentionally write a protocol that Python 2.x cannot read; # Only bump this if the oldest still supported version of Python already
# there are too many issues with that. # includes it.
DEFAULT_PROTOCOL = 3 DEFAULT_PROTOCOL = 4
class PickleError(Exception): class PickleError(Exception):
"""A common base class for the other pickling exceptions.""" """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 The optional *protocol* argument tells the pickler to use the
given protocol; supported protocols are 0, 1, 2, 3 and 4. The given protocol; supported protocols are 0, 1, 2, 3 and 4. The
default protocol is 3; a backward-incompatible protocol designed default protocol is 4. It was introduced in Python 3.4, it is
for Python 3. incompatible with previous versions.
Specifying a negative protocol version selects the highest Specifying a negative protocol version selects the highest
protocol version supported. The higher the protocol used, the protocol version supported. The higher the protocol used, the

View File

@ -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.

View File

@ -20,10 +20,12 @@ class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoPro
[clinic start generated code]*/ [clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/ /*[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 { enum {
HIGHEST_PROTOCOL = 4, HIGHEST_PROTOCOL = 4,
DEFAULT_PROTOCOL = 3 DEFAULT_PROTOCOL = 4
}; };
/* Pickle opcodes. These must be kept updated with pickle.py. /* 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. be more efficient.
The optional *protocol* argument tells the pickler to use the given The optional *protocol* argument tells the pickler to use the given
protocol supported protocols are 0, 1, 2, 3 and 4. The default 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 Specifying a negative protocol version selects the highest protocol
version supported. The higher the protocol used, the more recent the 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 * static PyObject *
_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file, _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
PyObject *protocol, int fix_imports) 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(); 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 The optional *protocol* argument tells the pickler to use the given
protocol; supported protocols are 0, 1, 2, 3 and 4. The default 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 Specifying a negative protocol version selects the highest protocol
version supported. The higher the protocol used, the more recent the 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 * static PyObject *
_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol, _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
int fix_imports) int fix_imports)
/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/ /*[clinic end generated code: output=d75d5cda456fd261 input=b6efb45a7d19b5ab]*/
{ {
PyObject *result; PyObject *result;
PicklerObject *pickler = _Pickler_New(); PicklerObject *pickler = _Pickler_New();

View File

@ -367,8 +367,9 @@ PyDoc_STRVAR(_pickle_dump__doc__,
"be more efficient.\n" "be more efficient.\n"
"\n" "\n"
"The optional *protocol* argument tells the pickler to use the given\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; 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" "\n"
"Specifying a negative protocol version selects the highest protocol\n" "Specifying a negative protocol version selects the highest protocol\n"
"version supported. The higher the protocol used, the more recent the\n" "version supported. The higher the protocol used, the more recent the\n"
@ -419,7 +420,8 @@ PyDoc_STRVAR(_pickle_dumps__doc__,
"\n" "\n"
"The optional *protocol* argument tells the pickler to use the given\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; 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" "\n"
"Specifying a negative protocol version selects the highest protocol\n" "Specifying a negative protocol version selects the highest protocol\n"
"version supported. The higher the protocol used, the more recent the\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: exit:
return return_value; return return_value;
} }
/*[clinic end generated code: output=e995dd494045d876 input=a9049054013a1b77]*/ /*[clinic end generated code: output=6fc104b8299c82dd input=a9049054013a1b77]*/