bpo-23403: Bump pickle.DEFAULT_PROTOCOL to 4 (#6355) · pythoncapi/cpython@c51d8c9 · GitHub
Skip to content

Commit c51d8c9

Browse files
authored
bpo-23403: Bump pickle.DEFAULT_PROTOCOL to 4 (python#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

6 files changed

Lines changed: 43 additions & 22 deletions

File tree

Doc/library/pickle.rst

Lines changed: 14 additions & 6 deletions

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

0 commit comments

Comments
 (0)