Skip to content

Commit 5bbbc94

Browse files
Issue #19189: Improved cross-references in the pickle module documentation.
1 parent 47fd9d8 commit 5bbbc94

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

Doc/library/pickle.rst

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ The :mod:`pickle` module exports two classes, :class:`Pickler` and
294294
:func:`copyreg.pickle`. It is a mapping whose keys are classes
295295
and whose values are reduction functions. A reduction function
296296
takes a single argument of the associated class and should
297-
conform to the same interface as a :meth:`~object.__reduce__`
297+
conform to the same interface as a :meth:`__reduce__`
298298
method.
299299

300300
By default, a pickler object will not have a
@@ -390,8 +390,8 @@ The following types can be pickled:
390390

391391
* classes that are defined at the top level of a module
392392

393-
* instances of such classes whose :attr:`__dict__` or the result of calling
394-
:meth:`__getstate__` is picklable (see section :ref:`pickle-inst` for
393+
* instances of such classes whose :attr:`~object.__dict__` or the result of
394+
calling :meth:`__getstate__` is picklable (see section :ref:`pickle-inst` for
395395
details).
396396

397397
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.
435435
Pickling Class Instances
436436
------------------------
437437

438+
.. currentmodule:: None
439+
438440
In this section, we describe the general mechanisms available to you to define,
439441
customize, and control how class instances are pickled and unpickled.
440442

@@ -470,7 +472,7 @@ methods:
470472
defines the method :meth:`__getstate__`, it is called and the returned object
471473
is pickled as the contents for the instance, instead of the contents of the
472474
instance's dictionary. If the :meth:`__getstate__` method is absent, the
473-
instance's :attr:`__dict__` is pickled as usual.
475+
instance's :attr:`~object.__dict__` is pickled as usual.
474476

475477

476478
.. method:: object.__setstate__(state)
@@ -539,7 +541,7 @@ or both.
539541
* Optionally, the object's state, which will be passed to the object's
540542
:meth:`__setstate__` method as previously described. If the object has no
541543
such method then, the value must be a dictionary and it will be added to
542-
the object's :attr:`__dict__` attribute.
544+
the object's :attr:`~object.__dict__` attribute.
543545

544546
* Optionally, an iterator (and not a sequence) yielding successive items.
545547
These items will be appended to the object either using
@@ -565,6 +567,8 @@ or both.
565567
the extended version. The main use for this method is to provide
566568
backwards-compatible reduce values for older Python releases.
567569

570+
.. currentmodule:: pickle
571+
568572
.. _pickle-persistent:
569573

570574
Persistence of External Objects
@@ -582,19 +586,19 @@ any newer protocol).
582586

583587
The resolution of such persistent IDs is not defined by the :mod:`pickle`
584588
module; it will delegate this resolution to the user defined methods on the
585-
pickler and unpickler, :meth:`persistent_id` and :meth:`persistent_load`
586-
respectively.
589+
pickler and unpickler, :meth:`~Pickler.persistent_id` and
590+
:meth:`~Unpickler.persistent_load` respectively.
587591

588592
To pickle objects that have an external persistent id, the pickler must have a
589-
custom :meth:`persistent_id` method that takes an object as an argument and
590-
returns either ``None`` or the persistent id for that object. When ``None`` is
591-
returned, the pickler simply pickles the object as normal. When a persistent ID
592-
string is returned, the pickler will pickle that object, along with a marker so
593-
that the unpickler will recognize it as a persistent ID.
593+
custom :meth:`~Pickler.persistent_id` method that takes an object as an
594+
argument and returns either ``None`` or the persistent id for that object.
595+
When ``None`` is returned, the pickler simply pickles the object as normal.
596+
When a persistent ID string is returned, the pickler will pickle that object,
597+
along with a marker so that the unpickler will recognize it as a persistent ID.
594598

595599
To unpickle external objects, the unpickler must have a custom
596-
:meth:`persistent_load` method that takes a persistent ID object and returns the
597-
referenced object.
600+
:meth:`~Unpickler.persistent_load` method that takes a persistent ID object and
601+
returns the referenced object.
598602

599603
Here is a comprehensive example presenting how persistent ID can be used to
600604
pickle external objects by reference.
@@ -651,7 +655,7 @@ Handling Stateful Objects
651655

652656
Here's an example that shows how to modify pickling behavior for a class.
653657
The :class:`TextReader` class opens a text file, and returns the line number and
654-
line contents each time its :meth:`readline` method is called. If a
658+
line contents each time its :meth:`!readline` method is called. If a
655659
:class:`TextReader` instance is pickled, all attributes *except* the file object
656660
member are saved. When the instance is unpickled, the file is reopened, and
657661
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
730734
inoffensive, it is not difficult to imagine one that could damage your system.
731735

732736
For this reason, you may want to control what gets unpickled by customizing
733-
:meth:`Unpickler.find_class`. Unlike its name suggests, :meth:`find_class` is
734-
called whenever a global (i.e., a class or a function) is requested. Thus it is
735-
possible to either completely forbid globals or restrict them to a safe subset.
737+
:meth:`Unpickler.find_class`. Unlike its name suggests,
738+
:meth:`Unpickler.find_class` is called whenever a global (i.e., a class or
739+
a function) is requested. Thus it is possible to either completely forbid
740+
globals or restrict them to a safe subset.
736741

737742
Here is an example of an unpickler allowing only few safe classes from the
738743
:mod:`builtins` module to be loaded::

0 commit comments

Comments
 (0)