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

Skip to content

Commit 14d535c

Browse files
committed
[Bug #984952] Include some material from PEP 307
1 parent 2b3feec commit 14d535c

1 file changed

Lines changed: 35 additions & 4 deletions

File tree

Doc/lib/libpickle.tex

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,8 @@ \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 of length two
519-
or three, with the following semantics:
518+
pickled as normal. When a tuple is returned, it must be between two
519+
and five elements long, with the following semantics:
520520

521521
\begin{itemize}
522522

@@ -530,14 +530,30 @@ \subsubsection{Pickling and unpickling extension types}
530530

531531
\item A tuple of arguments for the callable object, or \code{None}.
532532
\deprecated{2.3}{Use the tuple of arguments instead}
533-
534533
\item Optionally, the object's state, which will be passed to
535534
the object's \method{__setstate__()} method as described in
536535
section~\ref{pickle-inst}. If the object has no
537536
\method{__setstate__()} method, then, as above, the value must
538537
be a dictionary and it will be added to the object's
539538
\member{__dict__}.
540539

540+
\item Optionally, an iterator (and not a sequence) yielding successive
541+
list items. These list items will be pickled, and appended to the
542+
object using either \code{obj.append(\var{item})} or
543+
\code{obj.extend(\var{list_of_items})}. This is primarily used for
544+
list subclasses, but may be used by other classes as long as they have
545+
\method{append()} and \method{extend()} methods with the appropriate
546+
signature. (Whether \method{append()} or \method{extend()} is used
547+
depends on which pickle protocol version is used as well as the number
548+
of items to append, so both must be supported.)
549+
550+
\item Optionally, an iterator (not a sequence)
551+
yielding successive dictionary items, which should be tuples of the
552+
form \code{(\var{key}, \var{value})}. These items will be pickled
553+
and stored to the object using \code{obj[\var{key}] = \var{value}}.
554+
This is primarily used for dictionary subclasses, but may be used by
555+
other classes as long as they implement \method{__setitem__}.
556+
541557
\end{itemize}
542558

543559
Upon unpickling, the callable will be called (provided that it meets
@@ -559,7 +575,22 @@ \subsubsection{Pickling and unpickling extension types}
559575
that they are called with a single argument, the object to be pickled.
560576

561577
The registered constructor is deemed a ``safe constructor'' for purposes
562-
of unpickling as described above.
578+
579+
It is sometimes useful to know the protocol version when implementing
580+
\method{__reduce__}. This can be done by implementing a method named
581+
\method{__reduce_ex__} instead of \method{__reduce__}.
582+
\method{__reduce_ex__}, when it exists, is called in preference over
583+
\method{__reduce__} (you may still provide \method{__reduce__} for
584+
backwards compatibility). The \method{__reduce_ex__} method will be
585+
called with a single integer argument, the protocol version.
586+
587+
The \class{object} class implements both \method{__reduce__} and
588+
\method{__reduce_ex__}; however, if a subclass overrides
589+
\method{__reduce__} but not \method{__reduce_ex__}, the
590+
\method{__reduce_ex__} implementation detects this and calls
591+
\method{__reduce__}.
592+
593+
563594

564595
\subsubsection{Pickling and unpickling external objects}
565596

0 commit comments

Comments
 (0)