1995-03-17 12:07:09 -04:00
|
|
|
\section{Standard Module \sectcode{pickle}}
|
1997-07-17 13:34:52 -03:00
|
|
|
\label{module-pickle}
|
1995-02-15 11:53:08 -04:00
|
|
|
\stmodindex{pickle}
|
|
|
|
\index{persistency}
|
|
|
|
\indexii{persistent}{objects}
|
|
|
|
\indexii{serializing}{objects}
|
|
|
|
\indexii{marshalling}{objects}
|
|
|
|
\indexii{flattening}{objects}
|
|
|
|
\indexii{pickling}{objects}
|
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\setindexsubitem{(in module pickle)}
|
1995-03-17 12:07:09 -04:00
|
|
|
|
1995-02-15 11:53:08 -04:00
|
|
|
The \code{pickle} module implements a basic but powerful algorithm for
|
1995-03-13 06:03:32 -04:00
|
|
|
``pickling'' (a.k.a.\ serializing, marshalling or flattening) nearly
|
1995-03-28 09:35:14 -04:00
|
|
|
arbitrary Python objects. This is the act of converting objects to a
|
|
|
|
stream of bytes (and back: ``unpickling'').
|
|
|
|
This is a more primitive notion than
|
1995-02-15 11:53:08 -04:00
|
|
|
persistency --- although \code{pickle} reads and writes file objects,
|
|
|
|
it does not handle the issue of naming persistent objects, nor the
|
|
|
|
(even more complicated) area of concurrent access to persistent
|
|
|
|
objects. The \code{pickle} module can transform a complex object into
|
|
|
|
a byte stream and it can transform the byte stream into an object with
|
|
|
|
the same internal structure. The most obvious thing to do with these
|
|
|
|
byte streams is to write them onto a file, but it is also conceivable
|
|
|
|
to send them across a network or store them in a database. The module
|
|
|
|
\code{shelve} provides a simple interface to pickle and unpickle
|
|
|
|
objects on ``dbm''-style database files.
|
1997-12-15 17:56:05 -04:00
|
|
|
\refstmodindex{shelve}
|
1995-02-15 11:53:08 -04:00
|
|
|
|
1997-12-09 16:45:08 -04:00
|
|
|
\strong{Note:} The \code{pickle} module is rather slow. A
|
|
|
|
reimplementation of the same algorithm in C, which is up to 1000 times
|
1998-01-09 18:36:51 -04:00
|
|
|
faster, is available as the \code{cPickle}\refbimodindex{cPickle}
|
|
|
|
module. This has the same interface except that \code{Pickler} and
|
|
|
|
\code{Unpickler} are factory functions, not classes (so they cannot be
|
|
|
|
used as a base class for inheritance).
|
1997-12-09 16:45:08 -04:00
|
|
|
|
1995-02-15 11:53:08 -04:00
|
|
|
Unlike the built-in module \code{marshal}, \code{pickle} handles the
|
|
|
|
following correctly:
|
1997-12-15 17:56:05 -04:00
|
|
|
\refbimodindex{marshal}
|
1995-02-15 11:53:08 -04:00
|
|
|
|
|
|
|
\begin{itemize}
|
|
|
|
|
1995-03-17 12:07:09 -04:00
|
|
|
\item recursive objects (objects containing references to themselves)
|
1995-02-15 11:53:08 -04:00
|
|
|
|
1995-03-17 12:07:09 -04:00
|
|
|
\item object sharing (references to the same object in different places)
|
1995-02-15 11:53:08 -04:00
|
|
|
|
1995-03-17 12:07:09 -04:00
|
|
|
\item user-defined classes and their instances
|
1995-02-15 11:53:08 -04:00
|
|
|
|
|
|
|
\end{itemize}
|
|
|
|
|
|
|
|
The data format used by \code{pickle} is Python-specific. This has
|
|
|
|
the advantage that there are no restrictions imposed by external
|
1998-01-09 18:36:51 -04:00
|
|
|
standards such as XDR%
|
|
|
|
\index{XDR}
|
|
|
|
\index{External Data Representation}
|
|
|
|
(which can't represent pointer sharing); however
|
|
|
|
it means that non-Python programs may not be able to reconstruct
|
|
|
|
pickled Python objects.
|
1995-02-15 11:53:08 -04:00
|
|
|
|
1997-12-09 16:45:08 -04:00
|
|
|
By default, the \code{pickle} data format uses a printable \ASCII{}
|
|
|
|
representation. This is slightly more voluminous than a binary
|
|
|
|
representation. The big advantage of using printable \ASCII{} (and of
|
|
|
|
some other characteristics of \code{pickle}'s representation) is that
|
|
|
|
for debugging or recovery purposes it is possible for a human to read
|
|
|
|
the pickled file with a standard text editor.
|
|
|
|
|
|
|
|
A binary format, which is slightly more efficient, can be chosen by
|
|
|
|
specifying a nonzero (true) value for the \var{bin} argument to the
|
|
|
|
\code{Pickler} constructor or the \code{dump()} and \code{dumps()}
|
|
|
|
functions. The binary format is not the default because of backwards
|
|
|
|
compatibility with the Python 1.4 pickle module. In a future version,
|
|
|
|
the default may change to binary.
|
1995-02-15 11:53:08 -04:00
|
|
|
|
|
|
|
The \code{pickle} module doesn't handle code objects, which the
|
|
|
|
\code{marshal} module does. I suppose \code{pickle} could, and maybe
|
|
|
|
it should, but there's probably no great need for it right now (as
|
|
|
|
long as \code{marshal} continues to be used for reading and writing
|
|
|
|
code objects), and at least this avoids the possibility of smuggling
|
|
|
|
Trojan horses into a program.
|
1997-12-15 17:56:05 -04:00
|
|
|
\refbimodindex{marshal}
|
1995-02-15 11:53:08 -04:00
|
|
|
|
|
|
|
For the benefit of persistency modules written using \code{pickle}, it
|
|
|
|
supports the notion of a reference to an object outside the pickled
|
|
|
|
data stream. Such objects are referenced by a name, which is an
|
1995-03-17 12:07:09 -04:00
|
|
|
arbitrary string of printable \ASCII{} characters. The resolution of
|
1995-02-15 11:53:08 -04:00
|
|
|
such names is not defined by the \code{pickle} module --- the
|
|
|
|
persistent object module will have to implement a method
|
1998-01-09 18:36:51 -04:00
|
|
|
\code{persistent_load()}. To write references to persistent objects,
|
|
|
|
the persistent module must define a method \code{persistent_id()} which
|
1995-02-15 11:53:08 -04:00
|
|
|
returns either \code{None} or the persistent ID of the object.
|
|
|
|
|
|
|
|
There are some restrictions on the pickling of class instances.
|
|
|
|
|
|
|
|
First of all, the class must be defined at the top level in a module.
|
1997-12-09 16:45:08 -04:00
|
|
|
Furthermore, all its instance variables must be picklable.
|
1995-02-15 11:53:08 -04:00
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\setindexsubitem{(pickle protocol)}
|
1995-03-17 12:07:09 -04:00
|
|
|
|
1998-01-09 18:36:51 -04:00
|
|
|
When a pickled class instance is unpickled, its \code{__init__()} method
|
1997-12-09 16:45:08 -04:00
|
|
|
is normally \emph{not} invoked. \strong{Note:} This is a deviation
|
|
|
|
from previous versions of this module; the change was introduced in
|
|
|
|
Python 1.5b2. The reason for the change is that in many cases it is
|
|
|
|
desirable to have a constructor that requires arguments; it is a
|
1998-01-09 18:36:51 -04:00
|
|
|
(minor) nuisance to have to provide a \code{__getinitargs__()} method.
|
1997-12-09 16:45:08 -04:00
|
|
|
|
1998-01-09 18:36:51 -04:00
|
|
|
If it is desirable that the \code{__init__()} method be called on
|
1997-12-09 16:45:08 -04:00
|
|
|
unpickling, a class can define a method \code{__getinitargs__()},
|
1998-01-09 18:36:51 -04:00
|
|
|
which should return a \emph{tuple} containing the arguments to be
|
1997-12-30 13:44:48 -04:00
|
|
|
passed to the class constructor (\code{__init__()}). This method is
|
|
|
|
called at pickle time; the tuple it returns is incorporated in the
|
|
|
|
pickle for the instance.
|
1995-02-15 11:53:08 -04:00
|
|
|
\ttindex{__getinitargs__}
|
|
|
|
\ttindex{__init__}
|
|
|
|
|
1995-03-17 12:07:09 -04:00
|
|
|
Classes can further influence how their instances are pickled --- if the class
|
1995-02-15 11:53:08 -04:00
|
|
|
defines the method \code{__getstate__()}, it is called and the return
|
|
|
|
state is pickled as the contents for the instance, and if the class
|
|
|
|
defines the method \code{__setstate__()}, it is called with the
|
|
|
|
unpickled state. (Note that these methods can also be used to
|
|
|
|
implement copying class instances.) If there is no
|
|
|
|
\code{__getstate__()} method, the instance's \code{__dict__} is
|
|
|
|
pickled. If there is no \code{__setstate__()} method, the pickled
|
|
|
|
object must be a dictionary and its items are assigned to the new
|
|
|
|
instance's dictionary. (If a class defines both \code{__getstate__()}
|
|
|
|
and \code{__setstate__()}, the state object needn't be a dictionary
|
|
|
|
--- these methods can do what they want.) This protocol is also used
|
|
|
|
by the shallow and deep copying operations defined in the \code{copy}
|
|
|
|
module.
|
|
|
|
\ttindex{__getstate__}
|
|
|
|
\ttindex{__setstate__}
|
|
|
|
\ttindex{__dict__}
|
|
|
|
|
|
|
|
Note that when class instances are pickled, their class's code and
|
1995-03-13 06:03:32 -04:00
|
|
|
data are not pickled along with them. Only the instance data are
|
1995-02-15 11:53:08 -04:00
|
|
|
pickled. This is done on purpose, so you can fix bugs in a class or
|
|
|
|
add methods and still load objects that were created with an earlier
|
|
|
|
version of the class. If you plan to have long-lived objects that
|
1995-03-13 06:03:32 -04:00
|
|
|
will see many versions of a class, it may be worthwhile to put a version
|
1995-02-15 11:53:08 -04:00
|
|
|
number in the objects so that suitable conversions can be made by the
|
|
|
|
class's \code{__setstate__()} method.
|
|
|
|
|
1995-03-17 12:07:09 -04:00
|
|
|
When a class itself is pickled, only its name is pickled --- the class
|
|
|
|
definition is not pickled, but re-imported by the unpickling process.
|
|
|
|
Therefore, the restriction that the class must be defined at the top
|
|
|
|
level in a module applies to pickled classes as well.
|
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\setindexsubitem{(in module pickle)}
|
1995-03-17 12:07:09 -04:00
|
|
|
|
1995-02-15 11:53:08 -04:00
|
|
|
The interface can be summarized as follows.
|
|
|
|
|
|
|
|
To pickle an object \code{x} onto a file \code{f}, open for writing:
|
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\begin{verbatim}
|
1995-02-15 11:53:08 -04:00
|
|
|
p = pickle.Pickler(f)
|
|
|
|
p.dump(x)
|
1998-02-13 02:58:54 -04:00
|
|
|
\end{verbatim}
|
1997-07-17 13:34:52 -03:00
|
|
|
%
|
1995-03-17 12:07:09 -04:00
|
|
|
A shorthand for this is:
|
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\begin{verbatim}
|
1995-03-17 12:07:09 -04:00
|
|
|
pickle.dump(x, f)
|
1998-02-13 02:58:54 -04:00
|
|
|
\end{verbatim}
|
1997-07-17 13:34:52 -03:00
|
|
|
%
|
1995-02-15 11:53:08 -04:00
|
|
|
To unpickle an object \code{x} from a file \code{f}, open for reading:
|
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\begin{verbatim}
|
1995-02-15 11:53:08 -04:00
|
|
|
u = pickle.Unpickler(f)
|
1995-04-10 08:34:00 -03:00
|
|
|
x = u.load()
|
1998-02-13 02:58:54 -04:00
|
|
|
\end{verbatim}
|
1997-07-17 13:34:52 -03:00
|
|
|
%
|
1995-03-17 12:07:09 -04:00
|
|
|
A shorthand is:
|
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\begin{verbatim}
|
1995-03-17 12:07:09 -04:00
|
|
|
x = pickle.load(f)
|
1998-02-13 02:58:54 -04:00
|
|
|
\end{verbatim}
|
1997-07-17 13:34:52 -03:00
|
|
|
%
|
1998-01-09 18:36:51 -04:00
|
|
|
The \code{Pickler} class only calls the method \code{f.write()} with a
|
|
|
|
string argument. The \code{Unpickler} calls the methods \code{f.read()}
|
|
|
|
(with an integer argument) and \code{f.readline()} (without argument),
|
1995-02-15 11:53:08 -04:00
|
|
|
both returning a string. It is explicitly allowed to pass non-file
|
|
|
|
objects here, as long as they have the right methods.
|
1995-03-17 12:07:09 -04:00
|
|
|
\ttindex{Unpickler}
|
|
|
|
\ttindex{Pickler}
|
1995-02-15 11:53:08 -04:00
|
|
|
|
1997-12-09 16:45:08 -04:00
|
|
|
The constructor for the \code{Pickler} class has an optional second
|
|
|
|
argument, \var{bin}. If this is present and nonzero, the binary
|
|
|
|
pickle format is used; if it is zero or absent, the (less efficient,
|
|
|
|
but backwards compatible) text pickle format is used. The
|
|
|
|
\code{Unpickler} class does not have an argument to distinguish
|
|
|
|
between binary and text pickle formats; it accepts either format.
|
|
|
|
|
1995-02-15 11:53:08 -04:00
|
|
|
The following types can be pickled:
|
|
|
|
\begin{itemize}
|
|
|
|
|
|
|
|
\item \code{None}
|
|
|
|
|
|
|
|
\item integers, long integers, floating point numbers
|
|
|
|
|
|
|
|
\item strings
|
|
|
|
|
|
|
|
\item tuples, lists and dictionaries containing only picklable objects
|
|
|
|
|
1995-03-17 12:07:09 -04:00
|
|
|
\item classes that are defined at the top level in a module
|
|
|
|
|
|
|
|
\item instances of such classes whose \code{__dict__} or
|
|
|
|
\code{__setstate__()} is picklable
|
1995-02-15 11:53:08 -04:00
|
|
|
|
|
|
|
\end{itemize}
|
|
|
|
|
1995-03-17 12:07:09 -04:00
|
|
|
Attempts to pickle unpicklable objects will raise the
|
|
|
|
\code{PicklingError} exception; when this happens, an unspecified
|
|
|
|
number of bytes may have been written to the file.
|
|
|
|
|
|
|
|
It is possible to make multiple calls to the \code{dump()} method of
|
|
|
|
the same \code{Pickler} instance. These must then be matched to the
|
|
|
|
same number of calls to the \code{load()} instance of the
|
|
|
|
corresponding \code{Unpickler} instance. If the same object is
|
|
|
|
pickled by multiple \code{dump()} calls, the \code{load()} will all
|
1998-01-09 18:36:51 -04:00
|
|
|
yield references to the same object. \emph{Warning}: this is intended
|
1995-03-17 12:07:09 -04:00
|
|
|
for pickling multiple objects without intervening modifications to the
|
|
|
|
objects or their parts. If you modify an object and then pickle it
|
|
|
|
again using the same \code{Pickler} instance, the object is not
|
|
|
|
pickled again --- a reference to it is pickled and the
|
|
|
|
\code{Unpickler} will return the old value, not the modified one.
|
|
|
|
(There are two problems here: (a) detecting changes, and (b)
|
|
|
|
marshalling a minimal set of changes. I have no answers. Garbage
|
|
|
|
Collection may also become a problem here.)
|
|
|
|
|
|
|
|
Apart from the \code{Pickler} and \code{Unpickler} classes, the
|
|
|
|
module defines the following functions, and an exception:
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{dump}{object, file\optional{, bin}}
|
1995-03-17 12:07:09 -04:00
|
|
|
Write a pickled representation of \var{obect} to the open file object
|
1997-12-09 16:45:08 -04:00
|
|
|
\var{file}. This is equivalent to
|
|
|
|
\code{Pickler(\var{file}, \var{bin}).dump(\var{object})}.
|
|
|
|
If the optional \var{bin} argument is present and nonzero, the binary
|
|
|
|
pickle format is used; if it is zero or absent, the (less efficient)
|
|
|
|
text pickle format is used.
|
1995-03-17 12:07:09 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{load}{file}
|
|
|
|
Read a pickled object from the open file object \var{file}. This is
|
1998-01-09 18:36:51 -04:00
|
|
|
equivalent to \code{Unpickler(\var{file}).load()}.
|
1995-03-17 12:07:09 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-09 16:45:08 -04:00
|
|
|
\begin{funcdesc}{dumps}{object\optional{, bin}}
|
1995-03-17 12:07:09 -04:00
|
|
|
Return the pickled representation of the object as a string, instead
|
1997-12-09 16:45:08 -04:00
|
|
|
of writing it to a file. If the optional \var{bin} argument is
|
|
|
|
present and nonzero, the binary pickle format is used; if it is zero
|
|
|
|
or absent, the (less efficient) text pickle format is used.
|
1995-03-17 12:07:09 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{loads}{string}
|
|
|
|
Read a pickled object from a string instead of a file. Characters in
|
|
|
|
the string past the pickled object's representation are ignored.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{excdesc}{PicklingError}
|
|
|
|
This exception is raised when an unpicklable object is passed to
|
|
|
|
\code{Pickler.dump()}.
|
|
|
|
\end{excdesc}
|
1998-03-06 17:27:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
\begin{seealso}
|
|
|
|
\seemodule[copyreg]{copy_reg}{pickle interface constructor
|
|
|
|
registration}
|
|
|
|
\end{seealso}
|