Skip to content

Commit d66b061

Browse files
authored
gh-118830: Bump pickle.DEFAULT_PROTOCOL to 5 (GH-119340)
1 parent a1df1b4 commit d66b061

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed

Doc/library/pickle.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,14 @@ to read the pickle produced.
156156

157157
* Protocol version 4 was added in Python 3.4. It adds support for very large
158158
objects, pickling more kinds of objects, and some data format
159-
optimizations. It is the default protocol starting with Python 3.8.
159+
optimizations. This was the default protocol in Python 3.8--3.13.
160160
Refer to :pep:`3154` for information about improvements brought by
161161
protocol 4.
162162

163163
* Protocol version 5 was added in Python 3.8. It adds support for out-of-band
164-
data and speedup for in-band data. Refer to :pep:`574` for information about
165-
improvements brought by protocol 5.
164+
data and speedup for in-band data. It is the default protocol starting with
165+
Python 3.14. Refer to :pep:`574` for information about improvements brought
166+
by protocol 5.
166167

167168
.. note::
168169
Serialization is a more primitive notion than persistence; although
@@ -199,8 +200,10 @@ The :mod:`pickle` module provides the following constants:
199200

200201
An integer, the default :ref:`protocol version <pickle-protocols>` used
201202
for pickling. May be less than :data:`HIGHEST_PROTOCOL`. Currently the
202-
default protocol is 4, first introduced in Python 3.4 and incompatible
203-
with previous versions.
203+
default protocol is 5, introduced in Python 3.8 and incompatible
204+
with previous versions. This version introduces support for out-of-band
205+
buffers, where :pep:`3118`-compatible data can be transmitted separately
206+
from the main pickle stream.
204207

205208
.. versionchanged:: 3.0
206209

@@ -210,6 +213,10 @@ The :mod:`pickle` module provides the following constants:
210213

211214
The default protocol is 4.
212215

216+
.. versionchanged:: 3.14
217+
218+
The default protocol is 5.
219+
213220
The :mod:`pickle` module provides the following functions to make the pickling
214221
process more convenient:
215222

Doc/whatsnew/3.14.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ symtable
146146

147147
(Contributed by Bénédikt Tran in :gh:`120029`.)
148148

149+
pickle
150+
------
151+
152+
* Set the default protocol version on the :mod:`pickle` module to 5.
153+
For more details, please see :ref:`pickle protocols <pickle-protocols>`.
154+
149155

150156
Optimizations
151157
=============
@@ -160,7 +166,6 @@ asyncio
160166

161167

162168

163-
164169
Deprecated
165170
==========
166171

Lib/pickle.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
bytes_types = (bytes, bytearray)
5252

5353
# These are purely informational; no code uses these.
54-
format_version = "4.0" # File format version we write
54+
format_version = "5.0" # File format version we write
5555
compatible_formats = ["1.0", # Original protocol 0
5656
"1.1", # Protocol 0 with INST added
5757
"1.2", # Original protocol 1
@@ -68,7 +68,7 @@
6868
# The protocol we write by default. May be less than HIGHEST_PROTOCOL.
6969
# Only bump this if the oldest still supported version of Python already
7070
# includes it.
71-
DEFAULT_PROTOCOL = 4
71+
DEFAULT_PROTOCOL = 5
7272

7373
class PickleError(Exception):
7474
"""A common base class for the other pickling exceptions."""
@@ -408,7 +408,7 @@ def __init__(self, file, protocol=None, *, fix_imports=True,
408408
409409
The optional *protocol* argument tells the pickler to use the
410410
given protocol; supported protocols are 0, 1, 2, 3, 4 and 5.
411-
The default protocol is 4. It was introduced in Python 3.4, and
411+
The default protocol is 5. It was introduced in Python 3.8, and
412412
is incompatible with previous versions.
413413
414414
Specifying a negative protocol version selects the highest
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Bump :mod:`pickle` default protocol to ``5``.

Modules/_pickle.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" ""
4040
already includes it. */
4141
enum {
4242
HIGHEST_PROTOCOL = 5,
43-
DEFAULT_PROTOCOL = 4
43+
DEFAULT_PROTOCOL = 5
4444
};
4545

4646
#ifdef MS_WINDOWS
@@ -4692,7 +4692,7 @@ This takes a binary file for writing a pickle data stream.
46924692
46934693
The optional *protocol* argument tells the pickler to use the given
46944694
protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
4695-
protocol is 4. It was introduced in Python 3.4, and is incompatible
4695+
protocol is 5. It was introduced in Python 3.8, and is incompatible
46964696
with previous versions.
46974697
46984698
Specifying a negative protocol version selects the highest protocol
@@ -4725,7 +4725,7 @@ static int
47254725
_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
47264726
PyObject *protocol, int fix_imports,
47274727
PyObject *buffer_callback)
4728-
/*[clinic end generated code: output=0abedc50590d259b input=a7c969699bf5dad3]*/
4728+
/*[clinic end generated code: output=0abedc50590d259b input=cddc50f66b770002]*/
47294729
{
47304730
/* In case of multiple __init__() calls, clear previous content. */
47314731
if (self->write != NULL)
@@ -7507,7 +7507,7 @@ be more efficient.
75077507
75087508
The optional *protocol* argument tells the pickler to use the given
75097509
protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
7510-
protocol is 4. It was introduced in Python 3.4, and is incompatible
7510+
protocol is 5. It was introduced in Python 3.8, and is incompatible
75117511
with previous versions.
75127512
75137513
Specifying a negative protocol version selects the highest protocol
@@ -7533,7 +7533,7 @@ static PyObject *
75337533
_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
75347534
PyObject *protocol, int fix_imports,
75357535
PyObject *buffer_callback)
7536-
/*[clinic end generated code: output=706186dba996490c input=5ed6653da99cd97c]*/
7536+
/*[clinic end generated code: output=706186dba996490c input=b89ce8d0e911fd46]*/
75377537
{
75387538
PickleState *state = _Pickle_GetState(module);
75397539
PicklerObject *pickler = _Pickler_New(state);
@@ -7578,7 +7578,7 @@ Return the pickled representation of the object as a bytes object.
75787578
75797579
The optional *protocol* argument tells the pickler to use the given
75807580
protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
7581-
protocol is 4. It was introduced in Python 3.4, and is incompatible
7581+
protocol is 5. It was introduced in Python 3.8, and is incompatible
75827582
with previous versions.
75837583
75847584
Specifying a negative protocol version selects the highest protocol
@@ -7598,7 +7598,7 @@ into *file* as part of the pickle stream. It is an error if
75987598
static PyObject *
75997599
_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
76007600
int fix_imports, PyObject *buffer_callback)
7601-
/*[clinic end generated code: output=fbab0093a5580fdf input=e543272436c6f987]*/
7601+
/*[clinic end generated code: output=fbab0093a5580fdf input=139fc546886c63ac]*/
76027602
{
76037603
PyObject *result;
76047604
PickleState *state = _Pickle_GetState(module);

Modules/clinic/_pickle.c.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)