This makes performance better and produces shorter pickles. This change is backwards compatible up to the oldest currently supported version of Python (3.4).
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
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:
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
=======================
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."""
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
--- /dev/null
+``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.
[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.
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
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();
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
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();
"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"
"\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"
exit:
return return_value;
}
-/*[clinic end generated code: output=e995dd494045d876 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=6fc104b8299c82dd input=a9049054013a1b77]*/