Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions Doc/library/pickle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,19 @@ The :mod:`!pickle` module exports three classes, :class:`Pickler`,
If not specified, the default is :data:`DEFAULT_PROTOCOL`. If a negative
number is specified, :data:`HIGHEST_PROTOCOL` is selected.

The *file* argument must have a write() method that accepts a single bytes
argument. It can thus be an on-disk file opened for binary writing, an
:class:`io.BytesIO` instance, or any other custom object that meets this
interface.
The *file* argument must have a write() method that accepts a single
:term:`bytes-like object` argument. It can thus be an on-disk file opened
for binary writing, an :class:`io.BytesIO` instance, or any other custom
object that meets this interface.

.. note::

Depending on the protocol and on the objects being pickled, *write()*
may be passed :class:`bytes`, :class:`bytearray`, :class:`memoryview`
or :class:`PickleBuffer` instances. Not all of these support
:func:`len`, and for a non-byte-format buffer :func:`len` does not give
the size in bytes; use ``memoryview(data).nbytes`` if the number of
bytes is needed.

If *fix_imports* is true and *protocol* is less than 3, pickle will try to
map the new Python 3 names to the old module names used in Python 2, so
Expand Down
6 changes: 3 additions & 3 deletions Lib/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,9 @@ def __init__(self, file, protocol=None, *, fix_imports=True,
produced.

The *file* argument must have a write() method that accepts a
single bytes argument. It can thus be a file object opened for
binary writing, an io.BytesIO instance, or any other custom
object that meets this interface.
single bytes-like object argument. It can thus be a file object
opened for binary writing, an io.BytesIO instance, or any other
custom object that meets this interface.

If *fix_imports* is True and *protocol* is less than 3, pickle
will try to map the new Python 3 names to the old module names
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Clarify that the *file* argument of :class:`pickle.Pickler` and
:func:`pickle.dump` must have a ``write()`` method accepting a
:term:`bytes-like object`, not only :class:`bytes`.
16 changes: 8 additions & 8 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -5298,9 +5298,9 @@ version supported. The higher the protocol used, the more recent the
version of Python needed to read the pickle produced.

The *file* argument must have a write() method that accepts a single
bytes argument. It can thus be a file object opened for binary
writing, an io.BytesIO instance, or any other custom object that meets
this interface.
bytes-like object argument. It can thus be a file object opened for
binary writing, an io.BytesIO instance, or any other custom object that
meets this interface.

If *fix_imports* is True and protocol is less than 3, pickle will try
to map the new Python 3 names to the old module names used in Python
Expand All @@ -5323,7 +5323,7 @@ static int
_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
PyObject *protocol, int fix_imports,
PyObject *buffer_callback)
/*[clinic end generated code: output=0abedc50590d259b input=cddc50f66b770002]*/
/*[clinic end generated code: output=0abedc50590d259b input=fe16061a9f3ae4c3]*/
{
BEGIN_USING_PICKLER(self, -1);
/* In case of multiple __init__() calls, clear previous content. */
Expand Down Expand Up @@ -8230,9 +8230,9 @@ version supported. The higher the protocol used, the more recent the
version of Python needed to read the pickle produced.

The *file* argument must have a write() method that accepts a single
bytes argument. It can thus be a file object opened for binary
writing, an io.BytesIO instance, or any other custom object that meets
this interface.
bytes-like object argument. It can thus be a file object opened for
binary writing, an io.BytesIO instance, or any other custom object that
meets this interface.

If *fix_imports* is True and protocol is less than 3, pickle will try
to map the new Python 3 names to the old module names used in Python
Expand All @@ -8248,7 +8248,7 @@ static PyObject *
_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
PyObject *protocol, int fix_imports,
PyObject *buffer_callback)
/*[clinic end generated code: output=706186dba996490c input=b89ce8d0e911fd46]*/
/*[clinic end generated code: output=706186dba996490c input=3172ad2215ef9472]*/
{
PickleState *state = _Pickle_GetState(module);
PicklerObject *pickler = _Pickler_New(state);
Expand Down
14 changes: 7 additions & 7 deletions Modules/clinic/_pickle.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading