mirror of https://github.com/python/cpython
Issue #19189: Improved cross-references in the pickle module documentation.
This commit is contained in:
parent
47fd9d8e09
commit
5bbbc94073
|
@ -294,7 +294,7 @@ The :mod:`pickle` module exports two classes, :class:`Pickler` and
|
||||||
:func:`copyreg.pickle`. It is a mapping whose keys are classes
|
:func:`copyreg.pickle`. It is a mapping whose keys are classes
|
||||||
and whose values are reduction functions. A reduction function
|
and whose values are reduction functions. A reduction function
|
||||||
takes a single argument of the associated class and should
|
takes a single argument of the associated class and should
|
||||||
conform to the same interface as a :meth:`~object.__reduce__`
|
conform to the same interface as a :meth:`__reduce__`
|
||||||
method.
|
method.
|
||||||
|
|
||||||
By default, a pickler object will not have a
|
By default, a pickler object will not have a
|
||||||
|
@ -390,8 +390,8 @@ The following types can be pickled:
|
||||||
|
|
||||||
* classes that are defined at the top level of a module
|
* classes that are defined at the top level of a module
|
||||||
|
|
||||||
* instances of such classes whose :attr:`__dict__` or the result of calling
|
* instances of such classes whose :attr:`~object.__dict__` or the result of
|
||||||
:meth:`__getstate__` is picklable (see section :ref:`pickle-inst` for
|
calling :meth:`__getstate__` is picklable (see section :ref:`pickle-inst` for
|
||||||
details).
|
details).
|
||||||
|
|
||||||
Attempts to pickle unpicklable objects will raise the :exc:`PicklingError`
|
Attempts to pickle unpicklable objects will raise the :exc:`PicklingError`
|
||||||
|
@ -435,6 +435,8 @@ conversions can be made by the class's :meth:`__setstate__` method.
|
||||||
Pickling Class Instances
|
Pickling Class Instances
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
|
.. currentmodule:: None
|
||||||
|
|
||||||
In this section, we describe the general mechanisms available to you to define,
|
In this section, we describe the general mechanisms available to you to define,
|
||||||
customize, and control how class instances are pickled and unpickled.
|
customize, and control how class instances are pickled and unpickled.
|
||||||
|
|
||||||
|
@ -470,7 +472,7 @@ methods:
|
||||||
defines the method :meth:`__getstate__`, it is called and the returned object
|
defines the method :meth:`__getstate__`, it is called and the returned object
|
||||||
is pickled as the contents for the instance, instead of the contents of the
|
is pickled as the contents for the instance, instead of the contents of the
|
||||||
instance's dictionary. If the :meth:`__getstate__` method is absent, the
|
instance's dictionary. If the :meth:`__getstate__` method is absent, the
|
||||||
instance's :attr:`__dict__` is pickled as usual.
|
instance's :attr:`~object.__dict__` is pickled as usual.
|
||||||
|
|
||||||
|
|
||||||
.. method:: object.__setstate__(state)
|
.. method:: object.__setstate__(state)
|
||||||
|
@ -539,7 +541,7 @@ or both.
|
||||||
* Optionally, the object's state, which will be passed to the object's
|
* Optionally, the object's state, which will be passed to the object's
|
||||||
:meth:`__setstate__` method as previously described. If the object has no
|
:meth:`__setstate__` method as previously described. If the object has no
|
||||||
such method then, the value must be a dictionary and it will be added to
|
such method then, the value must be a dictionary and it will be added to
|
||||||
the object's :attr:`__dict__` attribute.
|
the object's :attr:`~object.__dict__` attribute.
|
||||||
|
|
||||||
* Optionally, an iterator (and not a sequence) yielding successive items.
|
* Optionally, an iterator (and not a sequence) yielding successive items.
|
||||||
These items will be appended to the object either using
|
These items will be appended to the object either using
|
||||||
|
@ -565,6 +567,8 @@ or both.
|
||||||
the extended version. The main use for this method is to provide
|
the extended version. The main use for this method is to provide
|
||||||
backwards-compatible reduce values for older Python releases.
|
backwards-compatible reduce values for older Python releases.
|
||||||
|
|
||||||
|
.. currentmodule:: pickle
|
||||||
|
|
||||||
.. _pickle-persistent:
|
.. _pickle-persistent:
|
||||||
|
|
||||||
Persistence of External Objects
|
Persistence of External Objects
|
||||||
|
@ -582,19 +586,19 @@ any newer protocol).
|
||||||
|
|
||||||
The resolution of such persistent IDs is not defined by the :mod:`pickle`
|
The resolution of such persistent IDs is not defined by the :mod:`pickle`
|
||||||
module; it will delegate this resolution to the user defined methods on the
|
module; it will delegate this resolution to the user defined methods on the
|
||||||
pickler and unpickler, :meth:`persistent_id` and :meth:`persistent_load`
|
pickler and unpickler, :meth:`~Pickler.persistent_id` and
|
||||||
respectively.
|
:meth:`~Unpickler.persistent_load` respectively.
|
||||||
|
|
||||||
To pickle objects that have an external persistent id, the pickler must have a
|
To pickle objects that have an external persistent id, the pickler must have a
|
||||||
custom :meth:`persistent_id` method that takes an object as an argument and
|
custom :meth:`~Pickler.persistent_id` method that takes an object as an
|
||||||
returns either ``None`` or the persistent id for that object. When ``None`` is
|
argument and returns either ``None`` or the persistent id for that object.
|
||||||
returned, the pickler simply pickles the object as normal. When a persistent ID
|
When ``None`` is returned, the pickler simply pickles the object as normal.
|
||||||
string is returned, the pickler will pickle that object, along with a marker so
|
When a persistent ID string is returned, the pickler will pickle that object,
|
||||||
that the unpickler will recognize it as a persistent ID.
|
along with a marker so that the unpickler will recognize it as a persistent ID.
|
||||||
|
|
||||||
To unpickle external objects, the unpickler must have a custom
|
To unpickle external objects, the unpickler must have a custom
|
||||||
:meth:`persistent_load` method that takes a persistent ID object and returns the
|
:meth:`~Unpickler.persistent_load` method that takes a persistent ID object and
|
||||||
referenced object.
|
returns the referenced object.
|
||||||
|
|
||||||
Here is a comprehensive example presenting how persistent ID can be used to
|
Here is a comprehensive example presenting how persistent ID can be used to
|
||||||
pickle external objects by reference.
|
pickle external objects by reference.
|
||||||
|
@ -651,7 +655,7 @@ Handling Stateful Objects
|
||||||
|
|
||||||
Here's an example that shows how to modify pickling behavior for a class.
|
Here's an example that shows how to modify pickling behavior for a class.
|
||||||
The :class:`TextReader` class opens a text file, and returns the line number and
|
The :class:`TextReader` class opens a text file, and returns the line number and
|
||||||
line contents each time its :meth:`readline` method is called. If a
|
line contents each time its :meth:`!readline` method is called. If a
|
||||||
:class:`TextReader` instance is pickled, all attributes *except* the file object
|
:class:`TextReader` instance is pickled, all attributes *except* the file object
|
||||||
member are saved. When the instance is unpickled, the file is reopened, and
|
member are saved. When the instance is unpickled, the file is reopened, and
|
||||||
reading resumes from the last location. The :meth:`__setstate__` and
|
reading resumes from the last location. The :meth:`__setstate__` and
|
||||||
|
@ -730,9 +734,10 @@ apply the string argument "echo hello world". Although this example is
|
||||||
inoffensive, it is not difficult to imagine one that could damage your system.
|
inoffensive, it is not difficult to imagine one that could damage your system.
|
||||||
|
|
||||||
For this reason, you may want to control what gets unpickled by customizing
|
For this reason, you may want to control what gets unpickled by customizing
|
||||||
:meth:`Unpickler.find_class`. Unlike its name suggests, :meth:`find_class` is
|
:meth:`Unpickler.find_class`. Unlike its name suggests,
|
||||||
called whenever a global (i.e., a class or a function) is requested. Thus it is
|
:meth:`Unpickler.find_class` is called whenever a global (i.e., a class or
|
||||||
possible to either completely forbid globals or restrict them to a safe subset.
|
a function) is requested. Thus it is possible to either completely forbid
|
||||||
|
globals or restrict them to a safe subset.
|
||||||
|
|
||||||
Here is an example of an unpickler allowing only few safe classes from the
|
Here is an example of an unpickler allowing only few safe classes from the
|
||||||
:mod:`builtins` module to be loaded::
|
:mod:`builtins` module to be loaded::
|
||||||
|
|
Loading…
Reference in New Issue