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