Skip to content

Commit c51d8c9

Browse files
authored
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).
1 parent 42ec190 commit c51d8c9

File tree

6 files changed

+43
-22
lines changed

6 files changed

+43
-22
lines changed

Doc/library/pickle.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ to read the pickle produced.
135135
information about improvements brought by protocol 2.
136136

137137
* Protocol version 3 was added in Python 3.0. It has explicit support for
138-
:class:`bytes` objects and cannot be unpickled by Python 2.x. This is
139-
the default protocol, and the recommended protocol when compatibility with
140-
other Python 3 versions is required.
138+
:class:`bytes` objects and cannot be unpickled by Python 2.x. This was
139+
the default protocol in Python 3.0--3.7.
141140

142141
* Protocol version 4 was added in Python 3.4. It adds support for very large
143142
objects, pickling more kinds of objects, and some data format
144-
optimizations. Refer to :pep:`3154` for information about improvements
145-
brought by protocol 4.
143+
optimizations. It is the default protocol starting with Python 3.8.
144+
Refer to :pep:`3154` for information about improvements brought by
145+
protocol 4.
146146

147147
.. note::
148148
Serialization is a more primitive notion than persistence; although
@@ -179,8 +179,16 @@ The :mod:`pickle` module provides the following constants:
179179

180180
An integer, the default :ref:`protocol version <pickle-protocols>` used
181181
for pickling. May be less than :data:`HIGHEST_PROTOCOL`. Currently the
182-
default protocol is 3, a new protocol designed for Python 3.
182+
default protocol is 4, first introduced in Python 3.4 and incompatible
183+
with previous versions.
183184

185+
.. versionchanged:: 3.0
186+
187+
The default protocol is 3.
188+
189+
.. versionchanged:: 3.8
190+
191+
The default protocol is 4.
184192

185193
The :mod:`pickle` module provides the following functions to make the pickling
186194
process more convenient:

Doc/whatsnew/3.8.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ Improved Modules
9494
Optimizations
9595
=============
9696

97+
* The default protocol in the :mod:`pickle` module is now Protocol 4,
98+
first introduced in Python 3.4. It offers better performance and smaller
99+
size compared to Protocol 3 available since Python 3.0.
97100

98101
Build and C API Changes
99102
=======================

Lib/pickle.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@
5757
HIGHEST_PROTOCOL = 4
5858

5959
# The protocol we write by default. May be less than HIGHEST_PROTOCOL.
60-
# We intentionally write a protocol that Python 2.x cannot read;
61-
# there are too many issues with that.
62-
DEFAULT_PROTOCOL = 3
60+
# Only bump this if the oldest still supported version of Python already
61+
# includes it.
62+
DEFAULT_PROTOCOL = 4
6363

6464
class PickleError(Exception):
6565
"""A common base class for the other pickling exceptions."""
@@ -376,8 +376,8 @@ def __init__(self, file, protocol=None, *, fix_imports=True):
376376
377377
The optional *protocol* argument tells the pickler to use the
378378
given protocol; supported protocols are 0, 1, 2, 3 and 4. The
379-
default protocol is 3; a backward-incompatible protocol designed
380-
for Python 3.
379+
default protocol is 4. It was introduced in Python 3.4, it is
380+
incompatible with previous versions.
381381
382382
Specifying a negative protocol version selects the highest
383383
protocol version supported. The higher the protocol used, the
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``DEFAULT_PROTOCOL`` in :mod:`pickle` was bumped to 4. Protocol 4 is
2+
described in :pep:`3154` and available since Python 3.4. It offers
3+
better performance and smaller size compared to protocol 3 introduced
4+
in Python 3.0.

Modules/_pickle.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoPro
2020
[clinic start generated code]*/
2121
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
2222

23-
/* Bump this when new opcodes are added to the pickle protocol. */
23+
/* Bump HIGHEST_PROTOCOL when new opcodes are added to the pickle protocol.
24+
Bump DEFAULT_PROTOCOL only when the oldest still supported version of Python
25+
already includes it. */
2426
enum {
2527
HIGHEST_PROTOCOL = 4,
26-
DEFAULT_PROTOCOL = 3
28+
DEFAULT_PROTOCOL = 4
2729
};
2830

2931
/* 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
71767178
be more efficient.
71777179
71787180
The optional *protocol* argument tells the pickler to use the given
7179-
protocol supported protocols are 0, 1, 2, 3 and 4. The default
7180-
protocol is 3; a backward-incompatible protocol designed for Python 3.
7181+
protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7182+
protocol is 4. It was introduced in Python 3.4, it is incompatible
7183+
with previous versions.
71817184
71827185
Specifying a negative protocol version selects the highest protocol
71837186
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
71967199
static PyObject *
71977200
_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
71987201
PyObject *protocol, int fix_imports)
7199-
/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
7202+
/*[clinic end generated code: output=a4774d5fde7d34de input=93f1408489a87472]*/
72007203
{
72017204
PicklerObject *pickler = _Pickler_New();
72027205

@@ -7236,7 +7239,8 @@ Return the pickled representation of the object as a bytes object.
72367239
72377240
The optional *protocol* argument tells the pickler to use the given
72387241
protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7239-
protocol is 3; a backward-incompatible protocol designed for Python 3.
7242+
protocol is 4. It was introduced in Python 3.4, it is incompatible
7243+
with previous versions.
72407244
72417245
Specifying a negative protocol version selects the highest protocol
72427246
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.
72507254
static PyObject *
72517255
_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
72527256
int fix_imports)
7253-
/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
7257+
/*[clinic end generated code: output=d75d5cda456fd261 input=b6efb45a7d19b5ab]*/
72547258
{
72557259
PyObject *result;
72567260
PicklerObject *pickler = _Pickler_New();

Modules/clinic/_pickle.c.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,9 @@ PyDoc_STRVAR(_pickle_dump__doc__,
367367
"be more efficient.\n"
368368
"\n"
369369
"The optional *protocol* argument tells the pickler to use the given\n"
370-
"protocol supported protocols are 0, 1, 2, 3 and 4. The default\n"
371-
"protocol is 3; a backward-incompatible protocol designed for Python 3.\n"
370+
"protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n"
371+
"protocol is 4. It was introduced in Python 3.4, it is incompatible\n"
372+
"with previous versions.\n"
372373
"\n"
373374
"Specifying a negative protocol version selects the highest protocol\n"
374375
"version supported. The higher the protocol used, the more recent the\n"
@@ -419,7 +420,8 @@ PyDoc_STRVAR(_pickle_dumps__doc__,
419420
"\n"
420421
"The optional *protocol* argument tells the pickler to use the given\n"
421422
"protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n"
422-
"protocol is 3; a backward-incompatible protocol designed for Python 3.\n"
423+
"protocol is 4. It was introduced in Python 3.4, it is incompatible\n"
424+
"with previous versions.\n"
423425
"\n"
424426
"Specifying a negative protocol version selects the highest protocol\n"
425427
"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
560562
exit:
561563
return return_value;
562564
}
563-
/*[clinic end generated code: output=e995dd494045d876 input=a9049054013a1b77]*/
565+
/*[clinic end generated code: output=6fc104b8299c82dd input=a9049054013a1b77]*/

0 commit comments

Comments
 (0)