Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 14 | .. sectionauthor:: Jim Kerr <jbkerr@sr.hp.com>. |
Andrew Kuchling | 587e970 | 2013-11-12 10:02:35 -0500 | [diff] [blame] | 15 | .. sectionauthor:: Barry Warsaw <barry@python.org> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 17 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | The :mod:`pickle` module implements a fundamental, but powerful algorithm for |
| 19 | serializing and de-serializing a Python object structure. "Pickling" is the |
| 20 | process 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 |
| 22 | into an object hierarchy. Pickling (and unpickling) is alternatively known as |
| 23 | "serialization", "marshalling," [#]_ or "flattening", however, to avoid |
Benjamin Peterson | be149d0 | 2008-06-20 21:03:22 +0000 | [diff] [blame] | 24 | confusion, the terms used here are "pickling" and "unpickling".. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
Georg Brandl | 0036bcf | 2010-10-17 10:24:54 +0000 | [diff] [blame] | 26 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
| 33 | Relationship to other Python modules |
| 34 | ------------------------------------ |
| 35 | |
Benjamin Peterson | be149d0 | 2008-06-20 21:03:22 +0000 | [diff] [blame] | 36 | The :mod:`pickle` module has an transparent optimizer (:mod:`_pickle`) written |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 37 | in C. It is used whenever available. Otherwise the pure Python implementation is |
Benjamin Peterson | be149d0 | 2008-06-20 21:03:22 +0000 | [diff] [blame] | 38 | used. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | |
| 40 | Python has a more primitive serialization module called :mod:`marshal`, but in |
| 41 | general :mod:`pickle` should always be the preferred way to serialize Python |
| 42 | objects. :mod:`marshal` exists primarily to support Python's :file:`.pyc` |
| 43 | files. |
| 44 | |
Georg Brandl | 5aa580f | 2010-11-30 14:57:54 +0000 | [diff] [blame] | 45 | The :mod:`pickle` module differs from :mod:`marshal` in several significant ways: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | Note 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 |
| 74 | naming persistent objects, nor the (even more complicated) issue of concurrent |
| 75 | access to persistent objects. The :mod:`pickle` module can transform a complex |
| 76 | object into a byte stream and it can transform the byte stream into an object |
| 77 | with the same internal structure. Perhaps the most obvious thing to do with |
| 78 | these byte streams is to write them onto a file, but it is also conceivable to |
| 79 | send 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 |
| 81 | DBM-style database files. |
| 82 | |
| 83 | |
| 84 | Data stream format |
| 85 | ------------------ |
| 86 | |
| 87 | .. index:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | single: External Data Representation |
| 89 | |
| 90 | The data format used by :mod:`pickle` is Python-specific. This has the |
| 91 | advantage that there are no restrictions imposed by external standards such as |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 92 | JSON or XDR (which can't represent pointer sharing); however it means that |
| 93 | non-Python programs may not be able to reconstruct pickled Python objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 95 | By default, the :mod:`pickle` data format uses a relatively compact binary |
| 96 | representation. If you need optimal size characteristics, you can efficiently |
| 97 | :doc:`compress <archiving>` pickled data. |
| 98 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 99 | The module :mod:`pickletools` contains tools for analyzing data streams |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 100 | generated by :mod:`pickle`. :mod:`pickletools` source code has extensive |
| 101 | comments about opcodes used by pickle protocols. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
Georg Brandl | 42f2ae0 | 2008-04-06 08:39:37 +0000 | [diff] [blame] | 103 | There are currently 4 different protocols which can be used for pickling. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 105 | * Protocol version 0 is the original "human-readable" protocol and is |
Alexandre Vassalotti | f7d08c7 | 2009-01-23 04:50:05 +0000 | [diff] [blame] | 106 | backwards compatible with earlier versions of Python. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 108 | * Protocol version 1 is an old binary format which is also compatible with |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | earlier versions of Python. |
| 110 | |
| 111 | * Protocol version 2 was introduced in Python 2.3. It provides much more |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 112 | efficient pickling of :term:`new-style class`\es. Refer to :pep:`307` for |
| 113 | information about improvements brought by protocol 2. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 115 | * 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 120 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 121 | Module Interface |
| 122 | ---------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 123 | |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 124 | To serialize an object hierarchy, you simply call the :func:`dumps` function. |
| 125 | Similarly, to de-serialize a data stream, you call the :func:`loads` function. |
| 126 | However, if you want more control over serialization and de-serialization, |
| 127 | you can create a :class:`Pickler` or an :class:`Unpickler` object, respectively. |
| 128 | |
| 129 | The :mod:`pickle` module provides the following constants: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 130 | |
| 131 | |
| 132 | .. data:: HIGHEST_PROTOCOL |
| 133 | |
| 134 | The highest protocol version available. This value can be passed as a |
| 135 | *protocol* value. |
| 136 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 137 | .. data:: DEFAULT_PROTOCOL |
| 138 | |
| 139 | The default protocol used for pickling. May be less than HIGHEST_PROTOCOL. |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 140 | Currently the default protocol is 3, a new protocol designed for Python 3.0. |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 141 | |
| 142 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | The :mod:`pickle` module provides the following functions to make the pickling |
| 144 | process more convenient: |
| 145 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 146 | .. function:: dump(obj, file, protocol=None, \*, fix_imports=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 147 | |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 148 | Write a pickled representation of *obj* to the open :term:`file object` *file*. |
| 149 | This is equivalent to ``Pickler(file, protocol).dump(obj)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 151 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 154 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 155 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 158 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 159 | The *file* argument must have a write() method that accepts a single bytes |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 160 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 164 | If *fix_imports* is true and *protocol* is less than 3, pickle will try to |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 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 Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 168 | .. function:: dumps(obj, protocol=None, \*, fix_imports=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 169 | |
Mark Summerfield | b9e2304 | 2008-04-21 14:47:45 +0000 | [diff] [blame] | 170 | Return the pickled representation of the object as a :class:`bytes` |
| 171 | object, instead of writing it to a file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 173 | 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 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 181 | If *fix_imports* is true and *protocol* is less than 3, pickle will try to |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 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 Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 185 | .. function:: load(file, \*, fix_imports=True, encoding="ASCII", errors="strict") |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 186 | |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 187 | Read a pickled object representation from the open :term:`file object` *file* |
| 188 | and return the reconstituted object hierarchy specified therein. This is |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 189 | 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 Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 197 | 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 Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 201 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Georg Brandl | 6faee4e | 2010-09-21 14:48:28 +0000 | [diff] [blame] | 202 | which are used to control compatibility support for pickle stream generated |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 203 | by Python 2.x. If *fix_imports* is true, pickle will try to map the old |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 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 Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 207 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 208 | .. function:: loads(bytes_object, \*, fix_imports=True, encoding="ASCII", errors="strict") |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 209 | |
| 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 Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 217 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Georg Brandl | 6faee4e | 2010-09-21 14:48:28 +0000 | [diff] [blame] | 218 | which are used to control compatibility support for pickle stream generated |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 219 | by Python 2.x. If *fix_imports* is true, pickle will try to map the old |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 225 | The :mod:`pickle` module defines three exceptions: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 226 | |
| 227 | .. exception:: PickleError |
| 228 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 229 | Common base class for the other pickling exceptions. It inherits |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 230 | :exc:`Exception`. |
| 231 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 232 | .. exception:: PicklingError |
| 233 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 234 | Error raised when an unpicklable object is encountered by :class:`Pickler`. |
| 235 | It inherits :exc:`PickleError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 237 | Refer to :ref:`pickle-picklable` to learn what kinds of objects can be |
| 238 | pickled. |
| 239 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 240 | .. exception:: UnpicklingError |
| 241 | |
Ezio Melotti | e62aad3 | 2011-11-18 13:51:10 +0200 | [diff] [blame] | 242 | Error raised when there is a problem unpickling an object, such as a data |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 243 | corruption or a security violation. It inherits :exc:`PickleError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 245 | 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 | |
| 250 | The :mod:`pickle` module exports two classes, :class:`Pickler` and |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | :class:`Unpickler`: |
| 252 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 253 | .. class:: Pickler(file, protocol=None, \*, fix_imports=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 255 | This takes a binary file for writing a pickle data stream. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 256 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 257 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 260 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 261 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 264 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 265 | The *file* argument must have a write() method that accepts a single bytes |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 266 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 268 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 269 | If *fix_imports* is true and *protocol* is less than 3, pickle will try to |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 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 Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 273 | .. method:: dump(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 274 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 275 | Write a pickled representation of *obj* to the open file object given in |
| 276 | the constructor. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 277 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 278 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 290 | .. 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 |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 297 | conform to the same interface as a :meth:`__reduce__` |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 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 Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 313 | .. attribute:: fast |
| 314 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 315 | 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 Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 320 | |
| 321 | Use :func:`pickletools.optimize` if you need more compact pickles. |
| 322 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 323 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 324 | .. class:: Unpickler(file, \*, fix_imports=True, encoding="ASCII", errors="strict") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 325 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 326 | This takes a binary file for reading a pickle data stream. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 327 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 328 | 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 Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 333 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 336 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 337 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Georg Brandl | 6faee4e | 2010-09-21 14:48:28 +0000 | [diff] [blame] | 338 | which are used to control compatibility support for pickle stream generated |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 339 | by Python 2.x. If *fix_imports* is true, pickle will try to map the old |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 343 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 344 | .. method:: load() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 345 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 346 | Read a pickled object representation from the open file object given in |
| 347 | the constructor, and return the reconstituted object hierarchy specified |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 348 | therein. Bytes past the pickled object's representation are ignored. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 349 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 350 | .. method:: persistent_load(pid) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 351 | |
Ezio Melotti | e62aad3 | 2011-11-18 13:51:10 +0200 | [diff] [blame] | 352 | Raise an :exc:`UnpicklingError` by default. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 353 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 354 | If defined, :meth:`persistent_load` should return the object specified by |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 355 | the persistent ID *pid*. If an invalid persistent ID is encountered, an |
Ezio Melotti | e62aad3 | 2011-11-18 13:51:10 +0200 | [diff] [blame] | 356 | :exc:`UnpicklingError` should be raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 357 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 358 | See :ref:`pickle-persistent` for details and examples of uses. |
| 359 | |
| 360 | .. method:: find_class(module, name) |
| 361 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 362 | Import *module* if necessary and return the object called *name* from it, |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 363 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 366 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 367 | Subclasses may override this to gain control over what type of objects and |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 368 | how they can be loaded, potentially reducing security risks. Refer to |
| 369 | :ref:`pickle-restrict` for details. |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 370 | |
| 371 | |
| 372 | .. _pickle-picklable: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 373 | |
| 374 | What can be pickled and unpickled? |
| 375 | ---------------------------------- |
| 376 | |
| 377 | The following types can be pickled: |
| 378 | |
| 379 | * ``None``, ``True``, and ``False`` |
| 380 | |
Georg Brandl | ba956ae | 2007-11-29 17:24:34 +0000 | [diff] [blame] | 381 | * integers, floating point numbers, complex numbers |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 382 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 383 | * strings, bytes, bytearrays |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 384 | |
| 385 | * tuples, lists, sets, and dictionaries containing only picklable objects |
| 386 | |
Ethan Furman | 2498d9e | 2013-10-18 00:45:40 -0700 | [diff] [blame] | 387 | * functions defined at the top level of a module (using :keyword:`def`, not |
| 388 | :keyword:`lambda`) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 389 | |
| 390 | * built-in functions defined at the top level of a module |
| 391 | |
| 392 | * classes that are defined at the top level of a module |
| 393 | |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 394 | * instances of such classes whose :attr:`~object.__dict__` or the result of |
| 395 | calling :meth:`__getstate__` is picklable (see section :ref:`pickle-inst` for |
Eli Bendersky | 78f3ce5 | 2013-01-02 05:53:59 -0800 | [diff] [blame] | 396 | details). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 397 | |
| 398 | Attempts to pickle unpicklable objects will raise the :exc:`PicklingError` |
| 399 | exception; when this happens, an unspecified number of bytes may have already |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 400 | been written to the underlying file. Trying to pickle a highly recursive data |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 401 | structure may exceed the maximum recursion depth, a :exc:`RuntimeError` will be |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 402 | raised in this case. You can carefully raise this limit with |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 403 | :func:`sys.setrecursionlimit`. |
| 404 | |
| 405 | Note that functions (built-in and user-defined) are pickled by "fully qualified" |
Ethan Furman | 2498d9e | 2013-10-18 00:45:40 -0700 | [diff] [blame] | 406 | name reference, not by value. [#]_ This means that only the function name is |
Eli Bendersky | 78f3ce5 | 2013-01-02 05:53:59 -0800 | [diff] [blame] | 407 | pickled, along with the name of the module the function is defined in. Neither |
| 408 | the function's code, nor any of its function attributes are pickled. Thus the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 409 | defining module must be importable in the unpickling environment, and the module |
| 410 | must contain the named object, otherwise an exception will be raised. [#]_ |
| 411 | |
| 412 | Similarly, classes are pickled by named reference, so the same restrictions in |
| 413 | the unpickling environment apply. Note that none of the class's code or data is |
| 414 | pickled, so in the following example the class attribute ``attr`` is not |
| 415 | restored in the unpickling environment:: |
| 416 | |
| 417 | class Foo: |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 418 | attr = 'A class attribute' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 419 | |
| 420 | picklestring = pickle.dumps(Foo) |
| 421 | |
| 422 | These restrictions are why picklable functions and classes must be defined in |
| 423 | the top level of a module. |
| 424 | |
| 425 | Similarly, when class instances are pickled, their class's code and data are not |
| 426 | pickled along with them. Only the instance data are pickled. This is done on |
| 427 | purpose, so you can fix bugs in a class or add methods to the class and still |
| 428 | load objects that were created with an earlier version of the class. If you |
| 429 | plan to have long-lived objects that will see many versions of a class, it may |
| 430 | be worthwhile to put a version number in the objects so that suitable |
| 431 | conversions can be made by the class's :meth:`__setstate__` method. |
| 432 | |
| 433 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 434 | .. _pickle-inst: |
| 435 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 436 | Pickling Class Instances |
| 437 | ------------------------ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 438 | |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 439 | .. currentmodule:: None |
| 440 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 441 | In this section, we describe the general mechanisms available to you to define, |
| 442 | customize, and control how class instances are pickled and unpickled. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 443 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 444 | In most cases, no additional code is needed to make instances picklable. By |
| 445 | default, pickle will retrieve the class and the attributes of an instance via |
| 446 | introspection. When a class instance is unpickled, its :meth:`__init__` method |
| 447 | is usually *not* invoked. The default behaviour first creates an uninitialized |
| 448 | instance and then restores the saved attributes. The following code shows an |
| 449 | implementation of this behaviour:: |
Georg Brandl | 85eb8c1 | 2007-08-31 16:33:38 +0000 | [diff] [blame] | 450 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 451 | def save(obj): |
| 452 | return (obj.__class__, obj.__dict__) |
| 453 | |
| 454 | def load(cls, attributes): |
| 455 | obj = cls.__new__(cls) |
| 456 | obj.__dict__.update(attributes) |
| 457 | return obj |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 458 | |
Georg Brandl | 6faee4e | 2010-09-21 14:48:28 +0000 | [diff] [blame] | 459 | Classes can alter the default behaviour by providing one or several special |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 460 | methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 461 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 462 | .. method:: object.__getnewargs_ex__() |
| 463 | |
| 464 | In protocols 4 and newer, classes that implements the |
| 465 | :meth:`__getnewargs_ex__` method can dictate the values passed to the |
| 466 | :meth:`__new__` method upon unpickling. The method must return a pair |
| 467 | ``(args, kwargs)`` where *args* is a tuple of positional arguments |
| 468 | and *kwargs* a dictionary of named arguments for constructing the |
| 469 | object. Those will be passed to the :meth:`__new__` method upon |
| 470 | unpickling. |
| 471 | |
| 472 | You should implement this method if the :meth:`__new__` method of your |
| 473 | class requires keyword-only arguments. Otherwise, it is recommended for |
| 474 | compatibility to implement :meth:`__getnewargs__`. |
| 475 | |
| 476 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 477 | .. method:: object.__getnewargs__() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 478 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 479 | This method serve a similar purpose as :meth:`__getnewargs_ex__` but |
| 480 | for protocols 2 and newer. It must return a tuple of arguments `args` |
| 481 | which will be passed to the :meth:`__new__` method upon unpickling. |
| 482 | |
| 483 | In protocols 4 and newer, :meth:`__getnewargs__` will not be called if |
| 484 | :meth:`__getnewargs_ex__` is defined. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 485 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 486 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 487 | .. method:: object.__getstate__() |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 488 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 489 | Classes can further influence how their instances are pickled; if the class |
| 490 | defines the method :meth:`__getstate__`, it is called and the returned object |
| 491 | is pickled as the contents for the instance, instead of the contents of the |
| 492 | instance's dictionary. If the :meth:`__getstate__` method is absent, the |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 493 | instance's :attr:`~object.__dict__` is pickled as usual. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 494 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 495 | |
| 496 | .. method:: object.__setstate__(state) |
| 497 | |
| 498 | Upon unpickling, if the class defines :meth:`__setstate__`, it is called with |
| 499 | the unpickled state. In that case, there is no requirement for the state |
| 500 | object to be a dictionary. Otherwise, the pickled state must be a dictionary |
| 501 | and its items are assigned to the new instance's dictionary. |
| 502 | |
| 503 | .. note:: |
| 504 | |
| 505 | If :meth:`__getstate__` returns a false value, the :meth:`__setstate__` |
| 506 | method will not be called upon unpickling. |
| 507 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 508 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 509 | Refer to the section :ref:`pickle-state` for more information about how to use |
| 510 | the methods :meth:`__getstate__` and :meth:`__setstate__`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 511 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 512 | .. note:: |
Georg Brandl | e720c0a | 2009-04-27 16:20:50 +0000 | [diff] [blame] | 513 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 514 | At unpickling time, some methods like :meth:`__getattr__`, |
| 515 | :meth:`__getattribute__`, or :meth:`__setattr__` may be called upon the |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 516 | instance. In case those methods rely on some internal invariant being |
| 517 | true, the type should implement :meth:`__getnewargs__` or |
| 518 | :meth:`__getnewargs_ex__` to establish such an invariant; otherwise, |
| 519 | neither :meth:`__new__` nor :meth:`__init__` will be called. |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 520 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 521 | .. index:: pair: copy; protocol |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 522 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 523 | As we shall see, pickle does not use directly the methods described above. In |
| 524 | fact, these methods are part of the copy protocol which implements the |
| 525 | :meth:`__reduce__` special method. The copy protocol provides a unified |
| 526 | interface for retrieving the data necessary for pickling and copying |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 527 | objects. [#]_ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 528 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 529 | Although powerful, implementing :meth:`__reduce__` directly in your classes is |
| 530 | error prone. For this reason, class designers should use the high-level |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 531 | interface (i.e., :meth:`__getnewargs_ex__`, :meth:`__getstate__` and |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 532 | :meth:`__setstate__`) whenever possible. We will show, however, cases where |
| 533 | using :meth:`__reduce__` is the only option or leads to more efficient pickling |
| 534 | or both. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 535 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 536 | .. method:: object.__reduce__() |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 537 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 538 | The interface is currently defined as follows. The :meth:`__reduce__` method |
| 539 | takes no argument and shall return either a string or preferably a tuple (the |
| 540 | returned object is often referred to as the "reduce value"). |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 541 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 542 | If a string is returned, the string should be interpreted as the name of a |
| 543 | global variable. It should be the object's local name relative to its |
| 544 | module; the pickle module searches the module namespace to determine the |
| 545 | object's module. This behaviour is typically useful for singletons. |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 546 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 547 | When a tuple is returned, it must be between two and five items long. |
| 548 | Optional items can either be omitted, or ``None`` can be provided as their |
| 549 | value. The semantics of each item are in order: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 550 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 551 | .. XXX Mention __newobj__ special-case? |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 552 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 553 | * A callable object that will be called to create the initial version of the |
| 554 | object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 555 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 556 | * A tuple of arguments for the callable object. An empty tuple must be given |
| 557 | if the callable does not accept any argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 558 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 559 | * Optionally, the object's state, which will be passed to the object's |
| 560 | :meth:`__setstate__` method as previously described. If the object has no |
| 561 | such method then, the value must be a dictionary and it will be added to |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 562 | the object's :attr:`~object.__dict__` attribute. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 563 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 564 | * Optionally, an iterator (and not a sequence) yielding successive items. |
| 565 | These items will be appended to the object either using |
| 566 | ``obj.append(item)`` or, in batch, using ``obj.extend(list_of_items)``. |
| 567 | This is primarily used for list subclasses, but may be used by other |
| 568 | classes as long as they have :meth:`append` and :meth:`extend` methods with |
| 569 | the appropriate signature. (Whether :meth:`append` or :meth:`extend` is |
| 570 | used depends on which pickle protocol version is used as well as the number |
| 571 | of items to append, so both must be supported.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 572 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 573 | * Optionally, an iterator (not a sequence) yielding successive key-value |
| 574 | pairs. These items will be stored to the object using ``obj[key] = |
| 575 | value``. This is primarily used for dictionary subclasses, but may be used |
| 576 | by other classes as long as they implement :meth:`__setitem__`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 577 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 578 | |
| 579 | .. method:: object.__reduce_ex__(protocol) |
| 580 | |
| 581 | Alternatively, a :meth:`__reduce_ex__` method may be defined. The only |
| 582 | difference is this method should take a single integer argument, the protocol |
| 583 | version. When defined, pickle will prefer it over the :meth:`__reduce__` |
| 584 | method. In addition, :meth:`__reduce__` automatically becomes a synonym for |
| 585 | the extended version. The main use for this method is to provide |
| 586 | backwards-compatible reduce values for older Python releases. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 587 | |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 588 | .. currentmodule:: pickle |
| 589 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 590 | .. _pickle-persistent: |
| 591 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 592 | Persistence of External Objects |
| 593 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 594 | |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 595 | .. index:: |
| 596 | single: persistent_id (pickle protocol) |
| 597 | single: persistent_load (pickle protocol) |
| 598 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 599 | For the benefit of object persistence, the :mod:`pickle` module supports the |
| 600 | notion of a reference to an object outside the pickled data stream. Such |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 601 | objects are referenced by a persistent ID, which should be either a string of |
| 602 | alphanumeric characters (for protocol 0) [#]_ or just an arbitrary object (for |
| 603 | any newer protocol). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 604 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 605 | The resolution of such persistent IDs is not defined by the :mod:`pickle` |
| 606 | module; it will delegate this resolution to the user defined methods on the |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 607 | pickler and unpickler, :meth:`~Pickler.persistent_id` and |
| 608 | :meth:`~Unpickler.persistent_load` respectively. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 609 | |
| 610 | To pickle objects that have an external persistent id, the pickler must have a |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 611 | custom :meth:`~Pickler.persistent_id` method that takes an object as an |
| 612 | argument and returns either ``None`` or the persistent id for that object. |
| 613 | When ``None`` is returned, the pickler simply pickles the object as normal. |
| 614 | When a persistent ID string is returned, the pickler will pickle that object, |
| 615 | along with a marker so that the unpickler will recognize it as a persistent ID. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 616 | |
| 617 | To unpickle external objects, the unpickler must have a custom |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 618 | :meth:`~Unpickler.persistent_load` method that takes a persistent ID object and |
| 619 | returns the referenced object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 620 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 621 | Here is a comprehensive example presenting how persistent ID can be used to |
| 622 | pickle external objects by reference. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 623 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 624 | .. literalinclude:: ../includes/dbpickle.py |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 625 | |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 626 | .. _pickle-dispatch: |
| 627 | |
| 628 | Dispatch Tables |
| 629 | ^^^^^^^^^^^^^^^ |
| 630 | |
| 631 | If one wants to customize pickling of some classes without disturbing |
| 632 | any other code which depends on pickling, then one can create a |
| 633 | pickler with a private dispatch table. |
| 634 | |
| 635 | The global dispatch table managed by the :mod:`copyreg` module is |
| 636 | available as :data:`copyreg.dispatch_table`. Therefore, one may |
| 637 | choose to use a modified copy of :data:`copyreg.dispatch_table` as a |
| 638 | private dispatch table. |
| 639 | |
| 640 | For example :: |
| 641 | |
| 642 | f = io.BytesIO() |
| 643 | p = pickle.Pickler(f) |
| 644 | p.dispatch_table = copyreg.dispatch_table.copy() |
| 645 | p.dispatch_table[SomeClass] = reduce_SomeClass |
| 646 | |
| 647 | creates an instance of :class:`pickle.Pickler` with a private dispatch |
| 648 | table which handles the ``SomeClass`` class specially. Alternatively, |
| 649 | the code :: |
| 650 | |
| 651 | class MyPickler(pickle.Pickler): |
| 652 | dispatch_table = copyreg.dispatch_table.copy() |
| 653 | dispatch_table[SomeClass] = reduce_SomeClass |
| 654 | f = io.BytesIO() |
| 655 | p = MyPickler(f) |
| 656 | |
| 657 | does the same, but all instances of ``MyPickler`` will by default |
| 658 | share the same dispatch table. The equivalent code using the |
| 659 | :mod:`copyreg` module is :: |
| 660 | |
| 661 | copyreg.pickle(SomeClass, reduce_SomeClass) |
| 662 | f = io.BytesIO() |
| 663 | p = pickle.Pickler(f) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 664 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 665 | .. _pickle-state: |
| 666 | |
| 667 | Handling Stateful Objects |
| 668 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 669 | |
| 670 | .. index:: |
| 671 | single: __getstate__() (copy protocol) |
| 672 | single: __setstate__() (copy protocol) |
| 673 | |
| 674 | Here's an example that shows how to modify pickling behavior for a class. |
| 675 | The :class:`TextReader` class opens a text file, and returns the line number and |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 676 | line contents each time its :meth:`!readline` method is called. If a |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 677 | :class:`TextReader` instance is pickled, all attributes *except* the file object |
| 678 | member are saved. When the instance is unpickled, the file is reopened, and |
| 679 | reading resumes from the last location. The :meth:`__setstate__` and |
| 680 | :meth:`__getstate__` methods are used to implement this behavior. :: |
| 681 | |
| 682 | class TextReader: |
| 683 | """Print and number lines in a text file.""" |
| 684 | |
| 685 | def __init__(self, filename): |
| 686 | self.filename = filename |
| 687 | self.file = open(filename) |
| 688 | self.lineno = 0 |
| 689 | |
| 690 | def readline(self): |
| 691 | self.lineno += 1 |
| 692 | line = self.file.readline() |
| 693 | if not line: |
| 694 | return None |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 695 | if line.endswith('\n'): |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 696 | line = line[:-1] |
| 697 | return "%i: %s" % (self.lineno, line) |
| 698 | |
| 699 | def __getstate__(self): |
| 700 | # Copy the object's state from self.__dict__ which contains |
| 701 | # all our instance attributes. Always use the dict.copy() |
| 702 | # method to avoid modifying the original state. |
| 703 | state = self.__dict__.copy() |
| 704 | # Remove the unpicklable entries. |
| 705 | del state['file'] |
| 706 | return state |
| 707 | |
| 708 | def __setstate__(self, state): |
| 709 | # Restore instance attributes (i.e., filename and lineno). |
| 710 | self.__dict__.update(state) |
| 711 | # Restore the previously opened file's state. To do so, we need to |
| 712 | # reopen it and read from it until the line count is restored. |
| 713 | file = open(self.filename) |
| 714 | for _ in range(self.lineno): |
| 715 | file.readline() |
| 716 | # Finally, save the file. |
| 717 | self.file = file |
| 718 | |
| 719 | |
| 720 | A sample usage might be something like this:: |
| 721 | |
| 722 | >>> reader = TextReader("hello.txt") |
| 723 | >>> reader.readline() |
| 724 | '1: Hello world!' |
| 725 | >>> reader.readline() |
| 726 | '2: I am line number two.' |
| 727 | >>> new_reader = pickle.loads(pickle.dumps(reader)) |
| 728 | >>> new_reader.readline() |
| 729 | '3: Goodbye!' |
| 730 | |
| 731 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 732 | .. _pickle-restrict: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 733 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 734 | Restricting Globals |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 735 | ------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 736 | |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 737 | .. index:: |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 738 | single: find_class() (pickle protocol) |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 739 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 740 | By default, unpickling will import any class or function that it finds in the |
| 741 | pickle data. For many applications, this behaviour is unacceptable as it |
| 742 | permits the unpickler to import and invoke arbitrary code. Just consider what |
| 743 | this hand-crafted pickle data stream does when loaded:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 744 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 745 | >>> import pickle |
| 746 | >>> pickle.loads(b"cos\nsystem\n(S'echo hello world'\ntR.") |
| 747 | hello world |
| 748 | 0 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 749 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 750 | In this example, the unpickler imports the :func:`os.system` function and then |
| 751 | apply the string argument "echo hello world". Although this example is |
| 752 | inoffensive, it is not difficult to imagine one that could damage your system. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 753 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 754 | For this reason, you may want to control what gets unpickled by customizing |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 755 | :meth:`Unpickler.find_class`. Unlike its name suggests, |
| 756 | :meth:`Unpickler.find_class` is called whenever a global (i.e., a class or |
| 757 | a function) is requested. Thus it is possible to either completely forbid |
| 758 | globals or restrict them to a safe subset. |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 759 | |
| 760 | Here is an example of an unpickler allowing only few safe classes from the |
| 761 | :mod:`builtins` module to be loaded:: |
| 762 | |
| 763 | import builtins |
| 764 | import io |
| 765 | import pickle |
| 766 | |
| 767 | safe_builtins = { |
| 768 | 'range', |
| 769 | 'complex', |
| 770 | 'set', |
| 771 | 'frozenset', |
| 772 | 'slice', |
| 773 | } |
| 774 | |
| 775 | class RestrictedUnpickler(pickle.Unpickler): |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 776 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 777 | def find_class(self, module, name): |
| 778 | # Only allow safe classes from builtins. |
| 779 | if module == "builtins" and name in safe_builtins: |
| 780 | return getattr(builtins, name) |
| 781 | # Forbid everything else. |
| 782 | raise pickle.UnpicklingError("global '%s.%s' is forbidden" % |
| 783 | (module, name)) |
| 784 | |
| 785 | def restricted_loads(s): |
| 786 | """Helper function analogous to pickle.loads().""" |
| 787 | return RestrictedUnpickler(io.BytesIO(s)).load() |
| 788 | |
| 789 | A sample usage of our unpickler working has intended:: |
| 790 | |
| 791 | >>> restricted_loads(pickle.dumps([1, 2, range(15)])) |
| 792 | [1, 2, range(0, 15)] |
| 793 | >>> restricted_loads(b"cos\nsystem\n(S'echo hello world'\ntR.") |
| 794 | Traceback (most recent call last): |
| 795 | ... |
| 796 | pickle.UnpicklingError: global 'os.system' is forbidden |
| 797 | >>> restricted_loads(b'cbuiltins\neval\n' |
| 798 | ... b'(S\'getattr(__import__("os"), "system")' |
| 799 | ... b'("echo hello world")\'\ntR.') |
| 800 | Traceback (most recent call last): |
| 801 | ... |
| 802 | pickle.UnpicklingError: global 'builtins.eval' is forbidden |
| 803 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 804 | |
| 805 | .. XXX Add note about how extension codes could evade our protection |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 806 | mechanism (e.g. cached classes do not invokes find_class()). |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 807 | |
| 808 | As our examples shows, you have to be careful with what you allow to be |
| 809 | unpickled. Therefore if security is a concern, you may want to consider |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 810 | alternatives such as the marshalling API in :mod:`xmlrpc.client` or |
| 811 | third-party solutions. |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 812 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 813 | |
| 814 | .. _pickle-example: |
| 815 | |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 816 | Examples |
| 817 | -------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 818 | |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 819 | For the simplest code, use the :func:`dump` and :func:`load` functions. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 820 | |
| 821 | import pickle |
| 822 | |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 823 | # An arbitrary collection of objects supported by pickle. |
| 824 | data = { |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 825 | 'a': [1, 2.0, 3, 4+6j], |
| 826 | 'b': ("character string", b"byte string"), |
| 827 | 'c': set([None, True, False]) |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 828 | } |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 829 | |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 830 | with open('data.pickle', 'wb') as f: |
| 831 | # Pickle the 'data' dictionary using the highest protocol available. |
| 832 | pickle.dump(data, f, pickle.HIGHEST_PROTOCOL) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 833 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 834 | |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 835 | The following example reads the resulting pickled data. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 836 | |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 837 | import pickle |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 838 | |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 839 | with open('data.pickle', 'rb') as f: |
| 840 | # The protocol version used is detected automatically, so we do not |
| 841 | # have to specify it. |
| 842 | data = pickle.load(f) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 843 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 844 | |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 845 | .. XXX: Add examples showing how to optimize pickles for size (like using |
| 846 | .. pickletools.optimize() or the gzip module). |
| 847 | |
| 848 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 849 | .. seealso:: |
| 850 | |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 851 | Module :mod:`copyreg` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 852 | Pickle interface constructor registration for extension types. |
| 853 | |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 854 | Module :mod:`pickletools` |
| 855 | Tools for working with and analyzing pickled data. |
| 856 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 857 | Module :mod:`shelve` |
| 858 | Indexed databases of objects; uses :mod:`pickle`. |
| 859 | |
| 860 | Module :mod:`copy` |
| 861 | Shallow and deep object copying. |
| 862 | |
| 863 | Module :mod:`marshal` |
| 864 | High-performance serialization of built-in types. |
| 865 | |
| 866 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 867 | .. rubric:: Footnotes |
| 868 | |
| 869 | .. [#] Don't confuse this with the :mod:`marshal` module |
| 870 | |
Ethan Furman | 2498d9e | 2013-10-18 00:45:40 -0700 | [diff] [blame] | 871 | .. [#] This is why :keyword:`lambda` functions cannot be pickled: all |
| 872 | :keyword:`lambda` functions share the same name: ``<lambda>``. |
| 873 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 874 | .. [#] The exception raised will likely be an :exc:`ImportError` or an |
| 875 | :exc:`AttributeError` but it could be something else. |
| 876 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 877 | .. [#] The :mod:`copy` module uses this protocol for shallow and deep copying |
| 878 | operations. |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 879 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 880 | .. [#] The limitation on alphanumeric characters is due to the fact |
| 881 | the persistent IDs, in protocol 0, are delimited by the newline |
| 882 | character. Therefore if any kind of newline characters occurs in |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 883 | persistent IDs, the resulting pickle will become unreadable. |