Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{pickle}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-pickle} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 3 | \stmodindex{pickle} |
| 4 | \index{persistency} |
| 5 | \indexii{persistent}{objects} |
| 6 | \indexii{serializing}{objects} |
| 7 | \indexii{marshalling}{objects} |
| 8 | \indexii{flattening}{objects} |
| 9 | \indexii{pickling}{objects} |
| 10 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 11 | \renewcommand{\indexsubitem}{(in module pickle)} |
| 12 | |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 13 | The \code{pickle} module implements a basic but powerful algorithm for |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 14 | ``pickling'' (a.k.a.\ serializing, marshalling or flattening) nearly |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 15 | arbitrary Python objects. This is the act of converting objects to a |
| 16 | stream of bytes (and back: ``unpickling''). |
| 17 | This is a more primitive notion than |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 18 | persistency --- although \code{pickle} reads and writes file objects, |
| 19 | it does not handle the issue of naming persistent objects, nor the |
| 20 | (even more complicated) area of concurrent access to persistent |
| 21 | objects. The \code{pickle} module can transform a complex object into |
| 22 | a byte stream and it can transform the byte stream into an object with |
| 23 | the same internal structure. The most obvious thing to do with these |
| 24 | byte streams is to write them onto a file, but it is also conceivable |
| 25 | to send them across a network or store them in a database. The module |
| 26 | \code{shelve} provides a simple interface to pickle and unpickle |
| 27 | objects on ``dbm''-style database files. |
Fred Drake | 54820dc | 1997-12-15 21:56:05 +0000 | [diff] [blame] | 28 | \refstmodindex{shelve} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 30 | \strong{Note:} The \code{pickle} module is rather slow. A |
| 31 | reimplementation of the same algorithm in C, which is up to 1000 times |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame^] | 32 | faster, is available as the \code{cPickle}\refbimodindex{cPickle} |
| 33 | module. This has the same interface except that \code{Pickler} and |
| 34 | \code{Unpickler} are factory functions, not classes (so they cannot be |
| 35 | used as a base class for inheritance). |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 37 | Unlike the built-in module \code{marshal}, \code{pickle} handles the |
| 38 | following correctly: |
Fred Drake | 54820dc | 1997-12-15 21:56:05 +0000 | [diff] [blame] | 39 | \refbimodindex{marshal} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 40 | |
| 41 | \begin{itemize} |
| 42 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 43 | \item recursive objects (objects containing references to themselves) |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 45 | \item object sharing (references to the same object in different places) |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 47 | \item user-defined classes and their instances |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 48 | |
| 49 | \end{itemize} |
| 50 | |
| 51 | The data format used by \code{pickle} is Python-specific. This has |
| 52 | the advantage that there are no restrictions imposed by external |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame^] | 53 | standards such as XDR% |
| 54 | \index{XDR} |
| 55 | \index{External Data Representation} |
| 56 | (which can't represent pointer sharing); however |
| 57 | it means that non-Python programs may not be able to reconstruct |
| 58 | pickled Python objects. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 60 | By default, the \code{pickle} data format uses a printable \ASCII{} |
| 61 | representation. This is slightly more voluminous than a binary |
| 62 | representation. The big advantage of using printable \ASCII{} (and of |
| 63 | some other characteristics of \code{pickle}'s representation) is that |
| 64 | for debugging or recovery purposes it is possible for a human to read |
| 65 | the pickled file with a standard text editor. |
| 66 | |
| 67 | A binary format, which is slightly more efficient, can be chosen by |
| 68 | specifying a nonzero (true) value for the \var{bin} argument to the |
| 69 | \code{Pickler} constructor or the \code{dump()} and \code{dumps()} |
| 70 | functions. The binary format is not the default because of backwards |
| 71 | compatibility with the Python 1.4 pickle module. In a future version, |
| 72 | the default may change to binary. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 73 | |
| 74 | The \code{pickle} module doesn't handle code objects, which the |
| 75 | \code{marshal} module does. I suppose \code{pickle} could, and maybe |
| 76 | it should, but there's probably no great need for it right now (as |
| 77 | long as \code{marshal} continues to be used for reading and writing |
| 78 | code objects), and at least this avoids the possibility of smuggling |
| 79 | Trojan horses into a program. |
Fred Drake | 54820dc | 1997-12-15 21:56:05 +0000 | [diff] [blame] | 80 | \refbimodindex{marshal} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 81 | |
| 82 | For the benefit of persistency modules written using \code{pickle}, it |
| 83 | supports the notion of a reference to an object outside the pickled |
| 84 | data stream. Such objects are referenced by a name, which is an |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 85 | arbitrary string of printable \ASCII{} characters. The resolution of |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 86 | such names is not defined by the \code{pickle} module --- the |
| 87 | persistent object module will have to implement a method |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame^] | 88 | \code{persistent_load()}. To write references to persistent objects, |
| 89 | the persistent module must define a method \code{persistent_id()} which |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 90 | returns either \code{None} or the persistent ID of the object. |
| 91 | |
| 92 | There are some restrictions on the pickling of class instances. |
| 93 | |
| 94 | First of all, the class must be defined at the top level in a module. |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 95 | Furthermore, all its instance variables must be picklable. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 97 | \renewcommand{\indexsubitem}{(pickle protocol)} |
| 98 | |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame^] | 99 | When a pickled class instance is unpickled, its \code{__init__()} method |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 100 | is normally \emph{not} invoked. \strong{Note:} This is a deviation |
| 101 | from previous versions of this module; the change was introduced in |
| 102 | Python 1.5b2. The reason for the change is that in many cases it is |
| 103 | desirable to have a constructor that requires arguments; it is a |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame^] | 104 | (minor) nuisance to have to provide a \code{__getinitargs__()} method. |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 105 | |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame^] | 106 | If it is desirable that the \code{__init__()} method be called on |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 107 | unpickling, a class can define a method \code{__getinitargs__()}, |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame^] | 108 | which should return a \emph{tuple} containing the arguments to be |
Guido van Rossum | 5793039 | 1997-12-30 17:44:48 +0000 | [diff] [blame] | 109 | passed to the class constructor (\code{__init__()}). This method is |
| 110 | called at pickle time; the tuple it returns is incorporated in the |
| 111 | pickle for the instance. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 112 | \ttindex{__getinitargs__} |
| 113 | \ttindex{__init__} |
| 114 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 115 | Classes can further influence how their instances are pickled --- if the class |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 116 | defines the method \code{__getstate__()}, it is called and the return |
| 117 | state is pickled as the contents for the instance, and if the class |
| 118 | defines the method \code{__setstate__()}, it is called with the |
| 119 | unpickled state. (Note that these methods can also be used to |
| 120 | implement copying class instances.) If there is no |
| 121 | \code{__getstate__()} method, the instance's \code{__dict__} is |
| 122 | pickled. If there is no \code{__setstate__()} method, the pickled |
| 123 | object must be a dictionary and its items are assigned to the new |
| 124 | instance's dictionary. (If a class defines both \code{__getstate__()} |
| 125 | and \code{__setstate__()}, the state object needn't be a dictionary |
| 126 | --- these methods can do what they want.) This protocol is also used |
| 127 | by the shallow and deep copying operations defined in the \code{copy} |
| 128 | module. |
| 129 | \ttindex{__getstate__} |
| 130 | \ttindex{__setstate__} |
| 131 | \ttindex{__dict__} |
| 132 | |
| 133 | Note that when class instances are pickled, their class's code and |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 134 | data are not pickled along with them. Only the instance data are |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 135 | pickled. This is done on purpose, so you can fix bugs in a class or |
| 136 | add methods and still load objects that were created with an earlier |
| 137 | version of the class. If you plan to have long-lived objects that |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 138 | will see many versions of a class, it may be worthwhile to put a version |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 139 | number in the objects so that suitable conversions can be made by the |
| 140 | class's \code{__setstate__()} method. |
| 141 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 142 | When a class itself is pickled, only its name is pickled --- the class |
| 143 | definition is not pickled, but re-imported by the unpickling process. |
| 144 | Therefore, the restriction that the class must be defined at the top |
| 145 | level in a module applies to pickled classes as well. |
| 146 | |
| 147 | \renewcommand{\indexsubitem}{(in module pickle)} |
| 148 | |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 149 | The interface can be summarized as follows. |
| 150 | |
| 151 | To pickle an object \code{x} onto a file \code{f}, open for writing: |
| 152 | |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 153 | \bcode\begin{verbatim} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 154 | p = pickle.Pickler(f) |
| 155 | p.dump(x) |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 156 | \end{verbatim}\ecode |
| 157 | % |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 158 | A shorthand for this is: |
| 159 | |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 160 | \bcode\begin{verbatim} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 161 | pickle.dump(x, f) |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 162 | \end{verbatim}\ecode |
| 163 | % |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 164 | To unpickle an object \code{x} from a file \code{f}, open for reading: |
| 165 | |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 166 | \bcode\begin{verbatim} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 167 | u = pickle.Unpickler(f) |
Guido van Rossum | 96628a9 | 1995-04-10 11:34:00 +0000 | [diff] [blame] | 168 | x = u.load() |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 169 | \end{verbatim}\ecode |
| 170 | % |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 171 | A shorthand is: |
| 172 | |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 173 | \bcode\begin{verbatim} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 174 | x = pickle.load(f) |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 175 | \end{verbatim}\ecode |
| 176 | % |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame^] | 177 | The \code{Pickler} class only calls the method \code{f.write()} with a |
| 178 | string argument. The \code{Unpickler} calls the methods \code{f.read()} |
| 179 | (with an integer argument) and \code{f.readline()} (without argument), |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 180 | both returning a string. It is explicitly allowed to pass non-file |
| 181 | objects here, as long as they have the right methods. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 182 | \ttindex{Unpickler} |
| 183 | \ttindex{Pickler} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 185 | The constructor for the \code{Pickler} class has an optional second |
| 186 | argument, \var{bin}. If this is present and nonzero, the binary |
| 187 | pickle format is used; if it is zero or absent, the (less efficient, |
| 188 | but backwards compatible) text pickle format is used. The |
| 189 | \code{Unpickler} class does not have an argument to distinguish |
| 190 | between binary and text pickle formats; it accepts either format. |
| 191 | |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 192 | The following types can be pickled: |
| 193 | \begin{itemize} |
| 194 | |
| 195 | \item \code{None} |
| 196 | |
| 197 | \item integers, long integers, floating point numbers |
| 198 | |
| 199 | \item strings |
| 200 | |
| 201 | \item tuples, lists and dictionaries containing only picklable objects |
| 202 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 203 | \item classes that are defined at the top level in a module |
| 204 | |
| 205 | \item instances of such classes whose \code{__dict__} or |
| 206 | \code{__setstate__()} is picklable |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 207 | |
| 208 | \end{itemize} |
| 209 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 210 | Attempts to pickle unpicklable objects will raise the |
| 211 | \code{PicklingError} exception; when this happens, an unspecified |
| 212 | number of bytes may have been written to the file. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 214 | It is possible to make multiple calls to the \code{dump()} method of |
| 215 | the same \code{Pickler} instance. These must then be matched to the |
| 216 | same number of calls to the \code{load()} instance of the |
| 217 | corresponding \code{Unpickler} instance. If the same object is |
| 218 | pickled by multiple \code{dump()} calls, the \code{load()} will all |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame^] | 219 | yield references to the same object. \emph{Warning}: this is intended |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 220 | for pickling multiple objects without intervening modifications to the |
| 221 | objects or their parts. If you modify an object and then pickle it |
| 222 | again using the same \code{Pickler} instance, the object is not |
| 223 | pickled again --- a reference to it is pickled and the |
| 224 | \code{Unpickler} will return the old value, not the modified one. |
| 225 | (There are two problems here: (a) detecting changes, and (b) |
| 226 | marshalling a minimal set of changes. I have no answers. Garbage |
| 227 | Collection may also become a problem here.) |
| 228 | |
| 229 | Apart from the \code{Pickler} and \code{Unpickler} classes, the |
| 230 | module defines the following functions, and an exception: |
| 231 | |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 232 | \begin{funcdesc}{dump}{object\, file\optional{, bin}} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 233 | Write a pickled representation of \var{obect} to the open file object |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 234 | \var{file}. This is equivalent to |
| 235 | \code{Pickler(\var{file}, \var{bin}).dump(\var{object})}. |
| 236 | If the optional \var{bin} argument is present and nonzero, the binary |
| 237 | pickle format is used; if it is zero or absent, the (less efficient) |
| 238 | text pickle format is used. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 239 | \end{funcdesc} |
| 240 | |
| 241 | \begin{funcdesc}{load}{file} |
| 242 | Read a pickled object from the open file object \var{file}. This is |
Fred Drake | cf7e830 | 1998-01-09 22:36:51 +0000 | [diff] [blame^] | 243 | equivalent to \code{Unpickler(\var{file}).load()}. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 244 | \end{funcdesc} |
| 245 | |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 246 | \begin{funcdesc}{dumps}{object\optional{, bin}} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 247 | Return the pickled representation of the object as a string, instead |
Guido van Rossum | 736fe5e | 1997-12-09 20:45:08 +0000 | [diff] [blame] | 248 | of writing it to a file. If the optional \var{bin} argument is |
| 249 | present and nonzero, the binary pickle format is used; if it is zero |
| 250 | or absent, the (less efficient) text pickle format is used. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 251 | \end{funcdesc} |
| 252 | |
| 253 | \begin{funcdesc}{loads}{string} |
| 254 | Read a pickled object from a string instead of a file. Characters in |
| 255 | the string past the pickled object's representation are ignored. |
| 256 | \end{funcdesc} |
| 257 | |
| 258 | \begin{excdesc}{PicklingError} |
| 259 | This exception is raised when an unpicklable object is passed to |
| 260 | \code{Pickler.dump()}. |
| 261 | \end{excdesc} |