Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cbbee6f

Browse files
committed
[Bug #984952] Include more material from PEP 307.
I haven't tried to include all the material on old-style classes using protocols 0,1. The details are lengthy; someone who knows more about the pickle module should decide if they're important enough to be in the docs or not.
1 parent 8896bf5 commit cbbee6f

1 file changed

Lines changed: 35 additions & 30 deletions

File tree

Doc/lib/libpickle.tex

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -515,21 +515,36 @@ \subsubsection{Pickling and unpickling extension types}
515515
must return either a string or a tuple.
516516

517517
If a string is returned, it names a global variable whose contents are
518-
pickled as normal. When a tuple is returned, it must be between two
519-
and five elements long, with the following semantics:
518+
pickled as normal. The string returned by \method{__reduce__} should
519+
be the object's local name relative to its module; the pickle module
520+
searches the module namespace to determine the object's module.
521+
522+
When a tuple is returned, it must be between two and five elements
523+
long. Optional elements can either be omitted, or \code{None} can be provided
524+
as their value. The semantics of each element are:
520525

521526
\begin{itemize}
522527

523-
\item A callable object, which in the unpickling environment must be
524-
either a class, a callable registered as a ``safe constructor''
525-
(see below), or it must have an attribute
526-
\member{__safe_for_unpickling__} with a true value. Otherwise,
527-
an \exception{UnpicklingError} will be raised in the unpickling
528-
environment. Note that as usual, the callable itself is pickled
529-
by name.
528+
\item A callable object that will be called to create the initial
529+
version of the object. The next element of the tuple will provide
530+
arguments for this callable, and later elements provide additional
531+
state information that will subsequently be used to fully reconstruct
532+
the pickled date.
533+
534+
In the unpickling environment this object must be either a class, a
535+
callable registered as a ``safe constructor'' (see below), or it must
536+
have an attribute \member{__safe_for_unpickling__} with a true value.
537+
Otherwise, an \exception{UnpicklingError} will be raised in the
538+
unpickling environment. Note that as usual, the callable itself is
539+
pickled by name.
530540

531541
\item A tuple of arguments for the callable object, or \code{None}.
532-
\deprecated{2.3}{Use the tuple of arguments instead}
542+
\deprecated{2.3}{If this item is \code{None}, then instead of calling
543+
the callable directly, its \method{__basicnew__()} method is called
544+
without arguments; this method should also return the unpickled
545+
object. Providing \code{None} is deprecated, however; return a
546+
tuple of arguments instead.}
547+
533548
\item Optionally, the object's state, which will be passed to
534549
the object's \method{__setstate__()} method as described in
535550
section~\ref{pickle-inst}. If the object has no
@@ -556,26 +571,6 @@ \subsubsection{Pickling and unpickling extension types}
556571

557572
\end{itemize}
558573

559-
Upon unpickling, the callable will be called (provided that it meets
560-
the above criteria), passing in the tuple of arguments; it should
561-
return the unpickled object.
562-
563-
If the second item was \code{None}, then instead of calling the
564-
callable directly, its \method{__basicnew__()} method is called
565-
without arguments. It should also return the unpickled object.
566-
567-
\deprecated{2.3}{Use the tuple of arguments instead}
568-
569-
An alternative to implementing a \method{__reduce__()} method on the
570-
object to be pickled, is to register the callable with the
571-
\refmodule[copyreg]{copy_reg} module. This module provides a way
572-
for programs to register ``reduction functions'' and constructors for
573-
user-defined types. Reduction functions have the same semantics and
574-
interface as the \method{__reduce__()} method described above, except
575-
that they are called with a single argument, the object to be pickled.
576-
577-
The registered constructor is deemed a ``safe constructor'' for purposes
578-
579574
It is sometimes useful to know the protocol version when implementing
580575
\method{__reduce__}. This can be done by implementing a method named
581576
\method{__reduce_ex__} instead of \method{__reduce__}.
@@ -590,6 +585,16 @@ \subsubsection{Pickling and unpickling extension types}
590585
\method{__reduce_ex__} implementation detects this and calls
591586
\method{__reduce__}.
592587

588+
An alternative to implementing a \method{__reduce__()} method on the
589+
object to be pickled, is to register the callable with the
590+
\refmodule[copyreg]{copy_reg} module. This module provides a way
591+
for programs to register ``reduction functions'' and constructors for
592+
user-defined types. Reduction functions have the same semantics and
593+
interface as the \method{__reduce__()} method described above, except
594+
that they are called with a single argument, the object to be pickled.
595+
596+
The registered constructor is deemed a ``safe constructor'' for purposes
597+
of unpickling as described above.
593598

594599

595600
\subsubsection{Pickling and unpickling external objects}

0 commit comments

Comments
 (0)