blob: 168ef56d3b7184c48ff354a07443daf73d769797 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`pickle` --- Python object serialization
2=============================================
3
4.. index::
5 single: persistence
6 pair: persistent; objects
7 pair: serializing; objects
8 pair: marshalling; objects
9 pair: flattening; objects
10 pair: pickling; objects
11
12.. module:: pickle
13 :synopsis: Convert Python objects to streams of bytes and back.
Christian Heimes5b5e81c2007-12-31 16:14:33 +000014.. sectionauthor:: Jim Kerr <jbkerr@sr.hp.com>.
15.. sectionauthor:: Barry Warsaw <barry@zope.com>
Georg Brandl116aa622007-08-15 14:28:22 +000016
Alexandre Vassalotti9d7665d2009-04-03 06:13:29 +000017
Georg Brandl116aa622007-08-15 14:28:22 +000018The :mod:`pickle` module implements a fundamental, but powerful algorithm for
19serializing and de-serializing a Python object structure. "Pickling" is the
20process whereby a Python object hierarchy is converted into a byte stream, and
21"unpickling" is the inverse operation, whereby a byte stream is converted back
22into an object hierarchy. Pickling (and unpickling) is alternatively known as
23"serialization", "marshalling," [#]_ or "flattening", however, to avoid
Benjamin Petersonbe149d02008-06-20 21:03:22 +000024confusion, the terms used here are "pickling" and "unpickling"..
Georg Brandl116aa622007-08-15 14:28:22 +000025
Georg Brandl0036bcf2010-10-17 10:24:54 +000026.. warning::
27
28 The :mod:`pickle` module is not intended to be secure against erroneous or
29 maliciously constructed data. Never unpickle data received from an untrusted
30 or unauthenticated source.
31
Georg Brandl116aa622007-08-15 14:28:22 +000032
33Relationship to other Python modules
34------------------------------------
35
Benjamin Petersonbe149d02008-06-20 21:03:22 +000036The :mod:`pickle` module has an transparent optimizer (:mod:`_pickle`) written
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +000037in C. It is used whenever available. Otherwise the pure Python implementation is
Benjamin Petersonbe149d02008-06-20 21:03:22 +000038used.
Georg Brandl116aa622007-08-15 14:28:22 +000039
40Python has a more primitive serialization module called :mod:`marshal`, but in
41general :mod:`pickle` should always be the preferred way to serialize Python
42objects. :mod:`marshal` exists primarily to support Python's :file:`.pyc`
43files.
44
Georg Brandl5aa580f2010-11-30 14:57:54 +000045The :mod:`pickle` module differs from :mod:`marshal` in several significant ways:
Georg Brandl116aa622007-08-15 14:28:22 +000046
47* The :mod:`pickle` module keeps track of the objects it has already serialized,
48 so that later references to the same object won't be serialized again.
49 :mod:`marshal` doesn't do this.
50
51 This has implications both for recursive objects and object sharing. Recursive
52 objects are objects that contain references to themselves. These are not
53 handled by marshal, and in fact, attempting to marshal recursive objects will
54 crash your Python interpreter. Object sharing happens when there are multiple
55 references to the same object in different places in the object hierarchy being
56 serialized. :mod:`pickle` stores such objects only once, and ensures that all
57 other references point to the master copy. Shared objects remain shared, which
58 can be very important for mutable objects.
59
60* :mod:`marshal` cannot be used to serialize user-defined classes and their
61 instances. :mod:`pickle` can save and restore class instances transparently,
62 however the class definition must be importable and live in the same module as
63 when the object was stored.
64
65* The :mod:`marshal` serialization format is not guaranteed to be portable
66 across Python versions. Because its primary job in life is to support
67 :file:`.pyc` files, the Python implementers reserve the right to change the
68 serialization format in non-backwards compatible ways should the need arise.
69 The :mod:`pickle` serialization format is guaranteed to be backwards compatible
70 across Python releases.
71
Georg Brandl116aa622007-08-15 14:28:22 +000072Note that serialization is a more primitive notion than persistence; although
73:mod:`pickle` reads and writes file objects, it does not handle the issue of
74naming persistent objects, nor the (even more complicated) issue of concurrent
75access to persistent objects. The :mod:`pickle` module can transform a complex
76object into a byte stream and it can transform the byte stream into an object
77with the same internal structure. Perhaps the most obvious thing to do with
78these byte streams is to write them onto a file, but it is also conceivable to
79send them across a network or store them in a database. The module
80:mod:`shelve` provides a simple interface to pickle and unpickle objects on
81DBM-style database files.
82
83
84Data stream format
85------------------
86
87.. index::
Georg Brandl116aa622007-08-15 14:28:22 +000088 single: External Data Representation
89
90The data format used by :mod:`pickle` is Python-specific. This has the
91advantage that there are no restrictions imposed by external standards such as
Antoine Pitroua9494f62012-05-10 15:38:30 +020092JSON or XDR (which can't represent pointer sharing); however it means that
93non-Python programs may not be able to reconstruct pickled Python objects.
Georg Brandl116aa622007-08-15 14:28:22 +000094
Antoine Pitroua9494f62012-05-10 15:38:30 +020095By default, the :mod:`pickle` data format uses a relatively compact binary
96representation. If you need optimal size characteristics, you can efficiently
97:doc:`compress <archiving>` pickled data.
98
Alexandre Vassalotti758bca62008-10-18 19:25:07 +000099The module :mod:`pickletools` contains tools for analyzing data streams
Antoine Pitroua9494f62012-05-10 15:38:30 +0200100generated by :mod:`pickle`. :mod:`pickletools` source code has extensive
101comments about opcodes used by pickle protocols.
Georg Brandl116aa622007-08-15 14:28:22 +0000102
Georg Brandl42f2ae02008-04-06 08:39:37 +0000103There are currently 4 different protocols which can be used for pickling.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
Antoine Pitroua9494f62012-05-10 15:38:30 +0200105* Protocol version 0 is the original "human-readable" protocol and is
Alexandre Vassalottif7d08c72009-01-23 04:50:05 +0000106 backwards compatible with earlier versions of Python.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
Antoine Pitroua9494f62012-05-10 15:38:30 +0200108* Protocol version 1 is an old binary format which is also compatible with
Georg Brandl116aa622007-08-15 14:28:22 +0000109 earlier versions of Python.
110
111* Protocol version 2 was introduced in Python 2.3. It provides much more
Antoine Pitroua9494f62012-05-10 15:38:30 +0200112 efficient pickling of :term:`new-style class`\es. Refer to :pep:`307` for
113 information about improvements brought by protocol 2.
Georg Brandl116aa622007-08-15 14:28:22 +0000114
Antoine Pitroua9494f62012-05-10 15:38:30 +0200115* Protocol version 3 was added in Python 3. It has explicit support for
116 :class:`bytes` objects and cannot be unpickled by Python 2.x. This is
117 the default as well as the current recommended protocol; use it whenever
118 possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000119
Georg Brandl116aa622007-08-15 14:28:22 +0000120
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000121Module Interface
122----------------
Georg Brandl116aa622007-08-15 14:28:22 +0000123
Antoine Pitroua9494f62012-05-10 15:38:30 +0200124To serialize an object hierarchy, you simply call the :func:`dumps` function.
125Similarly, to de-serialize a data stream, you call the :func:`loads` function.
126However, if you want more control over serialization and de-serialization,
127you can create a :class:`Pickler` or an :class:`Unpickler` object, respectively.
128
129The :mod:`pickle` module provides the following constants:
Georg Brandl116aa622007-08-15 14:28:22 +0000130
131
132.. data:: HIGHEST_PROTOCOL
133
134 The highest protocol version available. This value can be passed as a
135 *protocol* value.
136
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000137.. data:: DEFAULT_PROTOCOL
138
139 The default protocol used for pickling. May be less than HIGHEST_PROTOCOL.
Antoine Pitroua9494f62012-05-10 15:38:30 +0200140 Currently the default protocol is 3, a new protocol designed for Python 3.0.
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000141
142
Georg Brandl116aa622007-08-15 14:28:22 +0000143The :mod:`pickle` module provides the following functions to make the pickling
144process more convenient:
145
Georg Brandl18244152009-09-02 20:34:52 +0000146.. function:: dump(obj, file, protocol=None, \*, fix_imports=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000147
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000148 Write a pickled representation of *obj* to the open :term:`file object` *file*.
149 This is equivalent to ``Pickler(file, protocol).dump(obj)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000151 The optional *protocol* argument tells the pickler to use the given protocol;
152 supported protocols are 0, 1, 2, 3. The default protocol is 3; a
153 backward-incompatible protocol designed for Python 3.0.
Georg Brandl116aa622007-08-15 14:28:22 +0000154
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000155 Specifying a negative protocol version selects the highest protocol version
156 supported. The higher the protocol used, the more recent the version of
157 Python needed to read the pickle produced.
Georg Brandl116aa622007-08-15 14:28:22 +0000158
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000159 The *file* argument must have a write() method that accepts a single bytes
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000160 argument. It can thus be an on-disk file opened for binary writing, a
161 :class:`io.BytesIO` instance, or any other custom object that meets this
162 interface.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000164 If *fix_imports* is True and *protocol* is less than 3, pickle will try to
165 map the new Python 3.x names to the old module names used in Python 2.x,
166 so that the pickle data stream is readable with Python 2.x.
167
Georg Brandl18244152009-09-02 20:34:52 +0000168.. function:: dumps(obj, protocol=None, \*, fix_imports=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000169
Mark Summerfieldb9e23042008-04-21 14:47:45 +0000170 Return the pickled representation of the object as a :class:`bytes`
171 object, instead of writing it to a file.
Georg Brandl116aa622007-08-15 14:28:22 +0000172
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000173 The optional *protocol* argument tells the pickler to use the given protocol;
174 supported protocols are 0, 1, 2, 3. The default protocol is 3; a
175 backward-incompatible protocol designed for Python 3.0.
176
177 Specifying a negative protocol version selects the highest protocol version
178 supported. The higher the protocol used, the more recent the version of
179 Python needed to read the pickle produced.
180
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000181 If *fix_imports* is True and *protocol* is less than 3, pickle will try to
182 map the new Python 3.x names to the old module names used in Python 2.x,
183 so that the pickle data stream is readable with Python 2.x.
184
Georg Brandl18244152009-09-02 20:34:52 +0000185.. function:: load(file, \*, fix_imports=True, encoding="ASCII", errors="strict")
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000186
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000187 Read a pickled object representation from the open :term:`file object` *file*
188 and return the reconstituted object hierarchy specified therein. This is
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000189 equivalent to ``Unpickler(file).load()``.
190
191 The protocol version of the pickle is detected automatically, so no protocol
192 argument is needed. Bytes past the pickled object's representation are
193 ignored.
194
195 The argument *file* must have two methods, a read() method that takes an
196 integer argument, and a readline() method that requires no arguments. Both
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000197 methods should return bytes. Thus *file* can be an on-disk file opened
198 for binary reading, a :class:`io.BytesIO` object, or any other custom object
199 that meets this interface.
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000200
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000201 Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Georg Brandl6faee4e2010-09-21 14:48:28 +0000202 which are used to control compatibility support for pickle stream generated
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000203 by Python 2.x. If *fix_imports* is True, pickle will try to map the old
204 Python 2.x names to the new names used in Python 3.x. The *encoding* and
205 *errors* tell pickle how to decode 8-bit string instances pickled by Python
206 2.x; these default to 'ASCII' and 'strict', respectively.
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000207
Georg Brandl18244152009-09-02 20:34:52 +0000208.. function:: loads(bytes_object, \*, fix_imports=True, encoding="ASCII", errors="strict")
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000209
210 Read a pickled object hierarchy from a :class:`bytes` object and return the
211 reconstituted object hierarchy specified therein
212
213 The protocol version of the pickle is detected automatically, so no protocol
214 argument is needed. Bytes past the pickled object's representation are
215 ignored.
216
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000217 Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Georg Brandl6faee4e2010-09-21 14:48:28 +0000218 which are used to control compatibility support for pickle stream generated
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000219 by Python 2.x. If *fix_imports* is True, pickle will try to map the old
220 Python 2.x names to the new names used in Python 3.x. The *encoding* and
221 *errors* tell pickle how to decode 8-bit string instances pickled by Python
222 2.x; these default to 'ASCII' and 'strict', respectively.
Georg Brandl116aa622007-08-15 14:28:22 +0000223
Georg Brandl116aa622007-08-15 14:28:22 +0000224
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000225The :mod:`pickle` module defines three exceptions:
Georg Brandl116aa622007-08-15 14:28:22 +0000226
227.. exception:: PickleError
228
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000229 Common base class for the other pickling exceptions. It inherits
Georg Brandl116aa622007-08-15 14:28:22 +0000230 :exc:`Exception`.
231
Georg Brandl116aa622007-08-15 14:28:22 +0000232.. exception:: PicklingError
233
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000234 Error raised when an unpicklable object is encountered by :class:`Pickler`.
235 It inherits :exc:`PickleError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000236
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000237 Refer to :ref:`pickle-picklable` to learn what kinds of objects can be
238 pickled.
239
Georg Brandl116aa622007-08-15 14:28:22 +0000240.. exception:: UnpicklingError
241
Ezio Melottie62aad32011-11-18 13:51:10 +0200242 Error raised when there is a problem unpickling an object, such as a data
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000243 corruption or a security violation. It inherits :exc:`PickleError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000244
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000245 Note that other exceptions may also be raised during unpickling, including
246 (but not necessarily limited to) AttributeError, EOFError, ImportError, and
247 IndexError.
248
249
250The :mod:`pickle` module exports two classes, :class:`Pickler` and
Georg Brandl116aa622007-08-15 14:28:22 +0000251:class:`Unpickler`:
252
Georg Brandl18244152009-09-02 20:34:52 +0000253.. class:: Pickler(file, protocol=None, \*, fix_imports=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000254
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000255 This takes a binary file for writing a pickle data stream.
Georg Brandl116aa622007-08-15 14:28:22 +0000256
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000257 The optional *protocol* argument tells the pickler to use the given protocol;
258 supported protocols are 0, 1, 2, 3. The default protocol is 3; a
259 backward-incompatible protocol designed for Python 3.0.
Georg Brandl116aa622007-08-15 14:28:22 +0000260
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000261 Specifying a negative protocol version selects the highest protocol version
262 supported. The higher the protocol used, the more recent the version of
263 Python needed to read the pickle produced.
Georg Brandl116aa622007-08-15 14:28:22 +0000264
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000265 The *file* argument must have a write() method that accepts a single bytes
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000266 argument. It can thus be an on-disk file opened for binary writing, a
267 :class:`io.BytesIO` instance, or any other custom object that meets this interface.
Georg Brandl116aa622007-08-15 14:28:22 +0000268
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000269 If *fix_imports* is True and *protocol* is less than 3, pickle will try to
270 map the new Python 3.x names to the old module names used in Python 2.x,
271 so that the pickle data stream is readable with Python 2.x.
272
Benjamin Petersone41251e2008-04-25 01:59:09 +0000273 .. method:: dump(obj)
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000275 Write a pickled representation of *obj* to the open file object given in
276 the constructor.
Georg Brandl116aa622007-08-15 14:28:22 +0000277
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000278 .. method:: persistent_id(obj)
279
280 Do nothing by default. This exists so a subclass can override it.
281
282 If :meth:`persistent_id` returns ``None``, *obj* is pickled as usual. Any
283 other value causes :class:`Pickler` to emit the returned value as a
284 persistent ID for *obj*. The meaning of this persistent ID should be
285 defined by :meth:`Unpickler.persistent_load`. Note that the value
286 returned by :meth:`persistent_id` cannot itself have a persistent ID.
287
288 See :ref:`pickle-persistent` for details and examples of uses.
Georg Brandl116aa622007-08-15 14:28:22 +0000289
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100290 .. attribute:: dispatch_table
291
292 A pickler object's dispatch table is a registry of *reduction
293 functions* of the kind which can be declared using
294 :func:`copyreg.pickle`. It is a mapping whose keys are classes
295 and whose values are reduction functions. A reduction function
296 takes a single argument of the associated class and should
297 conform to the same interface as a :meth:`~object.__reduce__`
298 method.
299
300 By default, a pickler object will not have a
301 :attr:`dispatch_table` attribute, and it will instead use the
302 global dispatch table managed by the :mod:`copyreg` module.
303 However, to customize the pickling for a specific pickler object
304 one can set the :attr:`dispatch_table` attribute to a dict-like
305 object. Alternatively, if a subclass of :class:`Pickler` has a
306 :attr:`dispatch_table` attribute then this will be used as the
307 default dispatch table for instances of that class.
308
309 See :ref:`pickle-dispatch` for usage examples.
310
311 .. versionadded:: 3.3
312
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000313 .. attribute:: fast
314
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000315 Deprecated. Enable fast mode if set to a true value. The fast mode
316 disables the usage of memo, therefore speeding the pickling process by not
317 generating superfluous PUT opcodes. It should not be used with
318 self-referential objects, doing otherwise will cause :class:`Pickler` to
319 recurse infinitely.
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000320
321 Use :func:`pickletools.optimize` if you need more compact pickles.
322
Georg Brandl116aa622007-08-15 14:28:22 +0000323
Georg Brandl18244152009-09-02 20:34:52 +0000324.. class:: Unpickler(file, \*, fix_imports=True, encoding="ASCII", errors="strict")
Georg Brandl116aa622007-08-15 14:28:22 +0000325
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000326 This takes a binary file for reading a pickle data stream.
Georg Brandl116aa622007-08-15 14:28:22 +0000327
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000328 The protocol version of the pickle is detected automatically, so no
329 protocol argument is needed.
330
331 The argument *file* must have two methods, a read() method that takes an
332 integer argument, and a readline() method that requires no arguments. Both
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000333 methods should return bytes. Thus *file* can be an on-disk file object opened
334 for binary reading, a :class:`io.BytesIO` object, or any other custom object
335 that meets this interface.
Georg Brandl116aa622007-08-15 14:28:22 +0000336
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000337 Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Georg Brandl6faee4e2010-09-21 14:48:28 +0000338 which are used to control compatibility support for pickle stream generated
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000339 by Python 2.x. If *fix_imports* is True, pickle will try to map the old
340 Python 2.x names to the new names used in Python 3.x. The *encoding* and
341 *errors* tell pickle how to decode 8-bit string instances pickled by Python
342 2.x; these default to 'ASCII' and 'strict', respectively.
Georg Brandl116aa622007-08-15 14:28:22 +0000343
Benjamin Petersone41251e2008-04-25 01:59:09 +0000344 .. method:: load()
Georg Brandl116aa622007-08-15 14:28:22 +0000345
Benjamin Petersone41251e2008-04-25 01:59:09 +0000346 Read a pickled object representation from the open file object given in
347 the constructor, and return the reconstituted object hierarchy specified
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000348 therein. Bytes past the pickled object's representation are ignored.
Georg Brandl116aa622007-08-15 14:28:22 +0000349
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000350 .. method:: persistent_load(pid)
Georg Brandl116aa622007-08-15 14:28:22 +0000351
Ezio Melottie62aad32011-11-18 13:51:10 +0200352 Raise an :exc:`UnpicklingError` by default.
Georg Brandl116aa622007-08-15 14:28:22 +0000353
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000354 If defined, :meth:`persistent_load` should return the object specified by
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000355 the persistent ID *pid*. If an invalid persistent ID is encountered, an
Ezio Melottie62aad32011-11-18 13:51:10 +0200356 :exc:`UnpicklingError` should be raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000357
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000358 See :ref:`pickle-persistent` for details and examples of uses.
359
360 .. method:: find_class(module, name)
361
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000362 Import *module* if necessary and return the object called *name* from it,
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000363 where the *module* and *name* arguments are :class:`str` objects. Note,
364 unlike its name suggests, :meth:`find_class` is also used for finding
365 functions.
Georg Brandl116aa622007-08-15 14:28:22 +0000366
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000367 Subclasses may override this to gain control over what type of objects and
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000368 how they can be loaded, potentially reducing security risks. Refer to
369 :ref:`pickle-restrict` for details.
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000370
371
372.. _pickle-picklable:
Georg Brandl116aa622007-08-15 14:28:22 +0000373
374What can be pickled and unpickled?
375----------------------------------
376
377The following types can be pickled:
378
379* ``None``, ``True``, and ``False``
380
Georg Brandlba956ae2007-11-29 17:24:34 +0000381* integers, floating point numbers, complex numbers
Georg Brandl116aa622007-08-15 14:28:22 +0000382
Georg Brandlf6945182008-02-01 11:56:49 +0000383* strings, bytes, bytearrays
Georg Brandl116aa622007-08-15 14:28:22 +0000384
385* tuples, lists, sets, and dictionaries containing only picklable objects
386
387* functions defined at the top level of a module
388
389* built-in functions defined at the top level of a module
390
391* classes that are defined at the top level of a module
392
Eli Bendersky78f3ce52013-01-02 05:53:59 -0800393* instances of such classes whose :attr:`__dict__` or the result of calling
394 :meth:`__getstate__` is picklable (see section :ref:`pickle-inst` for
395 details).
Georg Brandl116aa622007-08-15 14:28:22 +0000396
397Attempts to pickle unpicklable objects will raise the :exc:`PicklingError`
398exception; when this happens, an unspecified number of bytes may have already
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000399been written to the underlying file. Trying to pickle a highly recursive data
Georg Brandl116aa622007-08-15 14:28:22 +0000400structure may exceed the maximum recursion depth, a :exc:`RuntimeError` will be
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000401raised in this case. You can carefully raise this limit with
Georg Brandl116aa622007-08-15 14:28:22 +0000402:func:`sys.setrecursionlimit`.
403
404Note that functions (built-in and user-defined) are pickled by "fully qualified"
405name reference, not by value. This means that only the function name is
Eli Bendersky78f3ce52013-01-02 05:53:59 -0800406pickled, along with the name of the module the function is defined in. Neither
407the function's code, nor any of its function attributes are pickled. Thus the
Georg Brandl116aa622007-08-15 14:28:22 +0000408defining module must be importable in the unpickling environment, and the module
409must contain the named object, otherwise an exception will be raised. [#]_
410
411Similarly, classes are pickled by named reference, so the same restrictions in
412the unpickling environment apply. Note that none of the class's code or data is
413pickled, so in the following example the class attribute ``attr`` is not
414restored in the unpickling environment::
415
416 class Foo:
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000417 attr = 'A class attribute'
Georg Brandl116aa622007-08-15 14:28:22 +0000418
419 picklestring = pickle.dumps(Foo)
420
421These restrictions are why picklable functions and classes must be defined in
422the top level of a module.
423
424Similarly, when class instances are pickled, their class's code and data are not
425pickled along with them. Only the instance data are pickled. This is done on
426purpose, so you can fix bugs in a class or add methods to the class and still
427load objects that were created with an earlier version of the class. If you
428plan to have long-lived objects that will see many versions of a class, it may
429be worthwhile to put a version number in the objects so that suitable
430conversions can be made by the class's :meth:`__setstate__` method.
431
432
Georg Brandl116aa622007-08-15 14:28:22 +0000433.. _pickle-inst:
434
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000435Pickling Class Instances
436------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000437
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000438In this section, we describe the general mechanisms available to you to define,
439customize, and control how class instances are pickled and unpickled.
Georg Brandl116aa622007-08-15 14:28:22 +0000440
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000441In most cases, no additional code is needed to make instances picklable. By
442default, pickle will retrieve the class and the attributes of an instance via
443introspection. When a class instance is unpickled, its :meth:`__init__` method
444is usually *not* invoked. The default behaviour first creates an uninitialized
445instance and then restores the saved attributes. The following code shows an
446implementation of this behaviour::
Georg Brandl85eb8c12007-08-31 16:33:38 +0000447
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000448 def save(obj):
449 return (obj.__class__, obj.__dict__)
450
451 def load(cls, attributes):
452 obj = cls.__new__(cls)
453 obj.__dict__.update(attributes)
454 return obj
Georg Brandl116aa622007-08-15 14:28:22 +0000455
Georg Brandl6faee4e2010-09-21 14:48:28 +0000456Classes can alter the default behaviour by providing one or several special
Georg Brandlc8148262010-10-17 11:13:37 +0000457methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000458
Georg Brandlc8148262010-10-17 11:13:37 +0000459.. method:: object.__getnewargs__()
Georg Brandl116aa622007-08-15 14:28:22 +0000460
Georg Brandlc8148262010-10-17 11:13:37 +0000461 In protocol 2 and newer, classes that implements the :meth:`__getnewargs__`
462 method can dictate the values passed to the :meth:`__new__` method upon
463 unpickling. This is often needed for classes whose :meth:`__new__` method
464 requires arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000465
Georg Brandl116aa622007-08-15 14:28:22 +0000466
Georg Brandlc8148262010-10-17 11:13:37 +0000467.. method:: object.__getstate__()
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000468
Georg Brandlc8148262010-10-17 11:13:37 +0000469 Classes can further influence how their instances are pickled; if the class
470 defines the method :meth:`__getstate__`, it is called and the returned object
471 is pickled as the contents for the instance, instead of the contents of the
472 instance's dictionary. If the :meth:`__getstate__` method is absent, the
473 instance's :attr:`__dict__` is pickled as usual.
Georg Brandl116aa622007-08-15 14:28:22 +0000474
Georg Brandlc8148262010-10-17 11:13:37 +0000475
476.. method:: object.__setstate__(state)
477
478 Upon unpickling, if the class defines :meth:`__setstate__`, it is called with
479 the unpickled state. In that case, there is no requirement for the state
480 object to be a dictionary. Otherwise, the pickled state must be a dictionary
481 and its items are assigned to the new instance's dictionary.
482
483 .. note::
484
485 If :meth:`__getstate__` returns a false value, the :meth:`__setstate__`
486 method will not be called upon unpickling.
487
Georg Brandl116aa622007-08-15 14:28:22 +0000488
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000489Refer to the section :ref:`pickle-state` for more information about how to use
490the methods :meth:`__getstate__` and :meth:`__setstate__`.
Georg Brandl116aa622007-08-15 14:28:22 +0000491
Benjamin Petersond23f8222009-04-05 19:13:16 +0000492.. note::
Georg Brandle720c0a2009-04-27 16:20:50 +0000493
Benjamin Petersond23f8222009-04-05 19:13:16 +0000494 At unpickling time, some methods like :meth:`__getattr__`,
495 :meth:`__getattribute__`, or :meth:`__setattr__` may be called upon the
Georg Brandlc8148262010-10-17 11:13:37 +0000496 instance. In case those methods rely on some internal invariant being true,
497 the type should implement :meth:`__getnewargs__` to establish such an
498 invariant; otherwise, neither :meth:`__new__` nor :meth:`__init__` will be
499 called.
Benjamin Petersond23f8222009-04-05 19:13:16 +0000500
Georg Brandlc8148262010-10-17 11:13:37 +0000501.. index:: pair: copy; protocol
Christian Heimes05e8be12008-02-23 18:30:17 +0000502
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000503As we shall see, pickle does not use directly the methods described above. In
504fact, these methods are part of the copy protocol which implements the
505:meth:`__reduce__` special method. The copy protocol provides a unified
506interface for retrieving the data necessary for pickling and copying
Georg Brandl48310cd2009-01-03 21:18:54 +0000507objects. [#]_
Georg Brandl116aa622007-08-15 14:28:22 +0000508
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000509Although powerful, implementing :meth:`__reduce__` directly in your classes is
510error prone. For this reason, class designers should use the high-level
511interface (i.e., :meth:`__getnewargs__`, :meth:`__getstate__` and
Georg Brandlc8148262010-10-17 11:13:37 +0000512:meth:`__setstate__`) whenever possible. We will show, however, cases where
513using :meth:`__reduce__` is the only option or leads to more efficient pickling
514or both.
Georg Brandl116aa622007-08-15 14:28:22 +0000515
Georg Brandlc8148262010-10-17 11:13:37 +0000516.. method:: object.__reduce__()
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000517
Georg Brandlc8148262010-10-17 11:13:37 +0000518 The interface is currently defined as follows. The :meth:`__reduce__` method
519 takes no argument and shall return either a string or preferably a tuple (the
520 returned object is often referred to as the "reduce value").
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000521
Georg Brandlc8148262010-10-17 11:13:37 +0000522 If a string is returned, the string should be interpreted as the name of a
523 global variable. It should be the object's local name relative to its
524 module; the pickle module searches the module namespace to determine the
525 object's module. This behaviour is typically useful for singletons.
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000526
Georg Brandlc8148262010-10-17 11:13:37 +0000527 When a tuple is returned, it must be between two and five items long.
528 Optional items can either be omitted, or ``None`` can be provided as their
529 value. The semantics of each item are in order:
Georg Brandl116aa622007-08-15 14:28:22 +0000530
Georg Brandlc8148262010-10-17 11:13:37 +0000531 .. XXX Mention __newobj__ special-case?
Georg Brandl116aa622007-08-15 14:28:22 +0000532
Georg Brandlc8148262010-10-17 11:13:37 +0000533 * A callable object that will be called to create the initial version of the
534 object.
Georg Brandl116aa622007-08-15 14:28:22 +0000535
Georg Brandlc8148262010-10-17 11:13:37 +0000536 * A tuple of arguments for the callable object. An empty tuple must be given
537 if the callable does not accept any argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000538
Georg Brandlc8148262010-10-17 11:13:37 +0000539 * Optionally, the object's state, which will be passed to the object's
540 :meth:`__setstate__` method as previously described. If the object has no
541 such method then, the value must be a dictionary and it will be added to
542 the object's :attr:`__dict__` attribute.
Georg Brandl116aa622007-08-15 14:28:22 +0000543
Georg Brandlc8148262010-10-17 11:13:37 +0000544 * Optionally, an iterator (and not a sequence) yielding successive items.
545 These items will be appended to the object either using
546 ``obj.append(item)`` or, in batch, using ``obj.extend(list_of_items)``.
547 This is primarily used for list subclasses, but may be used by other
548 classes as long as they have :meth:`append` and :meth:`extend` methods with
549 the appropriate signature. (Whether :meth:`append` or :meth:`extend` is
550 used depends on which pickle protocol version is used as well as the number
551 of items to append, so both must be supported.)
Georg Brandl116aa622007-08-15 14:28:22 +0000552
Georg Brandlc8148262010-10-17 11:13:37 +0000553 * Optionally, an iterator (not a sequence) yielding successive key-value
554 pairs. These items will be stored to the object using ``obj[key] =
555 value``. This is primarily used for dictionary subclasses, but may be used
556 by other classes as long as they implement :meth:`__setitem__`.
Georg Brandl116aa622007-08-15 14:28:22 +0000557
Georg Brandlc8148262010-10-17 11:13:37 +0000558
559.. method:: object.__reduce_ex__(protocol)
560
561 Alternatively, a :meth:`__reduce_ex__` method may be defined. The only
562 difference is this method should take a single integer argument, the protocol
563 version. When defined, pickle will prefer it over the :meth:`__reduce__`
564 method. In addition, :meth:`__reduce__` automatically becomes a synonym for
565 the extended version. The main use for this method is to provide
566 backwards-compatible reduce values for older Python releases.
Georg Brandl116aa622007-08-15 14:28:22 +0000567
Alexandre Vassalotti758bca62008-10-18 19:25:07 +0000568.. _pickle-persistent:
569
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000570Persistence of External Objects
571^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000572
Christian Heimes05e8be12008-02-23 18:30:17 +0000573.. index::
574 single: persistent_id (pickle protocol)
575 single: persistent_load (pickle protocol)
576
Georg Brandl116aa622007-08-15 14:28:22 +0000577For the benefit of object persistence, the :mod:`pickle` module supports the
578notion of a reference to an object outside the pickled data stream. Such
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000579objects are referenced by a persistent ID, which should be either a string of
580alphanumeric characters (for protocol 0) [#]_ or just an arbitrary object (for
581any newer protocol).
Georg Brandl116aa622007-08-15 14:28:22 +0000582
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000583The resolution of such persistent IDs is not defined by the :mod:`pickle`
584module; it will delegate this resolution to the user defined methods on the
585pickler and unpickler, :meth:`persistent_id` and :meth:`persistent_load`
586respectively.
Georg Brandl116aa622007-08-15 14:28:22 +0000587
588To pickle objects that have an external persistent id, the pickler must have a
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000589custom :meth:`persistent_id` method that takes an object as an argument and
Georg Brandl116aa622007-08-15 14:28:22 +0000590returns either ``None`` or the persistent id for that object. When ``None`` is
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000591returned, the pickler simply pickles the object as normal. When a persistent ID
592string is returned, the pickler will pickle that object, along with a marker so
593that the unpickler will recognize it as a persistent ID.
Georg Brandl116aa622007-08-15 14:28:22 +0000594
595To unpickle external objects, the unpickler must have a custom
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000596:meth:`persistent_load` method that takes a persistent ID object and returns the
597referenced object.
Georg Brandl116aa622007-08-15 14:28:22 +0000598
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000599Here is a comprehensive example presenting how persistent ID can be used to
600pickle external objects by reference.
Georg Brandl116aa622007-08-15 14:28:22 +0000601
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000602.. literalinclude:: ../includes/dbpickle.py
Alexandre Vassalottibcd1e3a2009-01-23 05:28:16 +0000603
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100604.. _pickle-dispatch:
605
606Dispatch Tables
607^^^^^^^^^^^^^^^
608
609If one wants to customize pickling of some classes without disturbing
610any other code which depends on pickling, then one can create a
611pickler with a private dispatch table.
612
613The global dispatch table managed by the :mod:`copyreg` module is
614available as :data:`copyreg.dispatch_table`. Therefore, one may
615choose to use a modified copy of :data:`copyreg.dispatch_table` as a
616private dispatch table.
617
618For example ::
619
620 f = io.BytesIO()
621 p = pickle.Pickler(f)
622 p.dispatch_table = copyreg.dispatch_table.copy()
623 p.dispatch_table[SomeClass] = reduce_SomeClass
624
625creates an instance of :class:`pickle.Pickler` with a private dispatch
626table which handles the ``SomeClass`` class specially. Alternatively,
627the code ::
628
629 class MyPickler(pickle.Pickler):
630 dispatch_table = copyreg.dispatch_table.copy()
631 dispatch_table[SomeClass] = reduce_SomeClass
632 f = io.BytesIO()
633 p = MyPickler(f)
634
635does the same, but all instances of ``MyPickler`` will by default
636share the same dispatch table. The equivalent code using the
637:mod:`copyreg` module is ::
638
639 copyreg.pickle(SomeClass, reduce_SomeClass)
640 f = io.BytesIO()
641 p = pickle.Pickler(f)
Georg Brandl116aa622007-08-15 14:28:22 +0000642
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000643.. _pickle-state:
644
645Handling Stateful Objects
646^^^^^^^^^^^^^^^^^^^^^^^^^
647
648.. index::
649 single: __getstate__() (copy protocol)
650 single: __setstate__() (copy protocol)
651
652Here's an example that shows how to modify pickling behavior for a class.
653The :class:`TextReader` class opens a text file, and returns the line number and
654line contents each time its :meth:`readline` method is called. If a
655:class:`TextReader` instance is pickled, all attributes *except* the file object
656member are saved. When the instance is unpickled, the file is reopened, and
657reading resumes from the last location. The :meth:`__setstate__` and
658:meth:`__getstate__` methods are used to implement this behavior. ::
659
660 class TextReader:
661 """Print and number lines in a text file."""
662
663 def __init__(self, filename):
664 self.filename = filename
665 self.file = open(filename)
666 self.lineno = 0
667
668 def readline(self):
669 self.lineno += 1
670 line = self.file.readline()
671 if not line:
672 return None
Alexandre Vassalotti9d7665d2009-04-03 06:13:29 +0000673 if line.endswith('\n'):
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000674 line = line[:-1]
675 return "%i: %s" % (self.lineno, line)
676
677 def __getstate__(self):
678 # Copy the object's state from self.__dict__ which contains
679 # all our instance attributes. Always use the dict.copy()
680 # method to avoid modifying the original state.
681 state = self.__dict__.copy()
682 # Remove the unpicklable entries.
683 del state['file']
684 return state
685
686 def __setstate__(self, state):
687 # Restore instance attributes (i.e., filename and lineno).
688 self.__dict__.update(state)
689 # Restore the previously opened file's state. To do so, we need to
690 # reopen it and read from it until the line count is restored.
691 file = open(self.filename)
692 for _ in range(self.lineno):
693 file.readline()
694 # Finally, save the file.
695 self.file = file
696
697
698A sample usage might be something like this::
699
700 >>> reader = TextReader("hello.txt")
701 >>> reader.readline()
702 '1: Hello world!'
703 >>> reader.readline()
704 '2: I am line number two.'
705 >>> new_reader = pickle.loads(pickle.dumps(reader))
706 >>> new_reader.readline()
707 '3: Goodbye!'
708
709
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000710.. _pickle-restrict:
Georg Brandl116aa622007-08-15 14:28:22 +0000711
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000712Restricting Globals
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000713-------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000714
Christian Heimes05e8be12008-02-23 18:30:17 +0000715.. index::
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000716 single: find_class() (pickle protocol)
Christian Heimes05e8be12008-02-23 18:30:17 +0000717
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000718By default, unpickling will import any class or function that it finds in the
719pickle data. For many applications, this behaviour is unacceptable as it
720permits the unpickler to import and invoke arbitrary code. Just consider what
721this hand-crafted pickle data stream does when loaded::
Georg Brandl116aa622007-08-15 14:28:22 +0000722
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000723 >>> import pickle
724 >>> pickle.loads(b"cos\nsystem\n(S'echo hello world'\ntR.")
725 hello world
726 0
Georg Brandl116aa622007-08-15 14:28:22 +0000727
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000728In this example, the unpickler imports the :func:`os.system` function and then
729apply the string argument "echo hello world". Although this example is
730inoffensive, it is not difficult to imagine one that could damage your system.
Georg Brandl116aa622007-08-15 14:28:22 +0000731
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000732For this reason, you may want to control what gets unpickled by customizing
733:meth:`Unpickler.find_class`. Unlike its name suggests, :meth:`find_class` is
734called whenever a global (i.e., a class or a function) is requested. Thus it is
Ezio Melottie62aad32011-11-18 13:51:10 +0200735possible to either completely forbid globals or restrict them to a safe subset.
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000736
737Here is an example of an unpickler allowing only few safe classes from the
738:mod:`builtins` module to be loaded::
739
740 import builtins
741 import io
742 import pickle
743
744 safe_builtins = {
745 'range',
746 'complex',
747 'set',
748 'frozenset',
749 'slice',
750 }
751
752 class RestrictedUnpickler(pickle.Unpickler):
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000753
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000754 def find_class(self, module, name):
755 # Only allow safe classes from builtins.
756 if module == "builtins" and name in safe_builtins:
757 return getattr(builtins, name)
758 # Forbid everything else.
759 raise pickle.UnpicklingError("global '%s.%s' is forbidden" %
760 (module, name))
761
762 def restricted_loads(s):
763 """Helper function analogous to pickle.loads()."""
764 return RestrictedUnpickler(io.BytesIO(s)).load()
765
766A sample usage of our unpickler working has intended::
767
768 >>> restricted_loads(pickle.dumps([1, 2, range(15)]))
769 [1, 2, range(0, 15)]
770 >>> restricted_loads(b"cos\nsystem\n(S'echo hello world'\ntR.")
771 Traceback (most recent call last):
772 ...
773 pickle.UnpicklingError: global 'os.system' is forbidden
774 >>> restricted_loads(b'cbuiltins\neval\n'
775 ... b'(S\'getattr(__import__("os"), "system")'
776 ... b'("echo hello world")\'\ntR.')
777 Traceback (most recent call last):
778 ...
779 pickle.UnpicklingError: global 'builtins.eval' is forbidden
780
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000781
782.. XXX Add note about how extension codes could evade our protection
Georg Brandl48310cd2009-01-03 21:18:54 +0000783 mechanism (e.g. cached classes do not invokes find_class()).
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000784
785As our examples shows, you have to be careful with what you allow to be
786unpickled. Therefore if security is a concern, you may want to consider
Alexandre Vassalotti9d7665d2009-04-03 06:13:29 +0000787alternatives such as the marshalling API in :mod:`xmlrpc.client` or
788third-party solutions.
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000789
Georg Brandl116aa622007-08-15 14:28:22 +0000790
791.. _pickle-example:
792
Alexandre Vassalotti9d7665d2009-04-03 06:13:29 +0000793Examples
794--------
Georg Brandl116aa622007-08-15 14:28:22 +0000795
Alexandre Vassalotti9d7665d2009-04-03 06:13:29 +0000796For the simplest code, use the :func:`dump` and :func:`load` functions. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000797
798 import pickle
799
Alexandre Vassalottibcd1e3a2009-01-23 05:28:16 +0000800 # An arbitrary collection of objects supported by pickle.
801 data = {
Alexandre Vassalotti9d7665d2009-04-03 06:13:29 +0000802 'a': [1, 2.0, 3, 4+6j],
803 'b': ("character string", b"byte string"),
804 'c': set([None, True, False])
Alexandre Vassalottibcd1e3a2009-01-23 05:28:16 +0000805 }
Georg Brandl116aa622007-08-15 14:28:22 +0000806
Alexandre Vassalottibcd1e3a2009-01-23 05:28:16 +0000807 with open('data.pickle', 'wb') as f:
808 # Pickle the 'data' dictionary using the highest protocol available.
809 pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
Georg Brandl116aa622007-08-15 14:28:22 +0000810
Georg Brandl116aa622007-08-15 14:28:22 +0000811
Alexandre Vassalottibcd1e3a2009-01-23 05:28:16 +0000812The following example reads the resulting pickled data. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000813
Alexandre Vassalottibcd1e3a2009-01-23 05:28:16 +0000814 import pickle
Georg Brandl116aa622007-08-15 14:28:22 +0000815
Alexandre Vassalottibcd1e3a2009-01-23 05:28:16 +0000816 with open('data.pickle', 'rb') as f:
817 # The protocol version used is detected automatically, so we do not
818 # have to specify it.
819 data = pickle.load(f)
Georg Brandl116aa622007-08-15 14:28:22 +0000820
Georg Brandl116aa622007-08-15 14:28:22 +0000821
Alexandre Vassalotti9d7665d2009-04-03 06:13:29 +0000822.. XXX: Add examples showing how to optimize pickles for size (like using
823.. pickletools.optimize() or the gzip module).
824
825
Georg Brandl116aa622007-08-15 14:28:22 +0000826.. seealso::
827
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000828 Module :mod:`copyreg`
Georg Brandl116aa622007-08-15 14:28:22 +0000829 Pickle interface constructor registration for extension types.
830
Alexandre Vassalotti9d7665d2009-04-03 06:13:29 +0000831 Module :mod:`pickletools`
832 Tools for working with and analyzing pickled data.
833
Georg Brandl116aa622007-08-15 14:28:22 +0000834 Module :mod:`shelve`
835 Indexed databases of objects; uses :mod:`pickle`.
836
837 Module :mod:`copy`
838 Shallow and deep object copying.
839
840 Module :mod:`marshal`
841 High-performance serialization of built-in types.
842
843
Georg Brandl116aa622007-08-15 14:28:22 +0000844.. rubric:: Footnotes
845
846.. [#] Don't confuse this with the :mod:`marshal` module
847
Georg Brandl116aa622007-08-15 14:28:22 +0000848.. [#] The exception raised will likely be an :exc:`ImportError` or an
849 :exc:`AttributeError` but it could be something else.
850
Alexandre Vassalotti73b90a82008-10-29 23:32:33 +0000851.. [#] The :mod:`copy` module uses this protocol for shallow and deep copying
852 operations.
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000853
Alexandre Vassalottid0392862008-10-24 01:32:40 +0000854.. [#] The limitation on alphanumeric characters is due to the fact
855 the persistent IDs, in protocol 0, are delimited by the newline
856 character. Therefore if any kind of newline characters occurs in
Alexandre Vassalotti5f3b63a2008-10-18 20:47:58 +0000857 persistent IDs, the resulting pickle will become unreadable.