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