Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`pickle` --- Python object serialization |
| 2 | ============================================= |
| 3 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 4 | .. module:: pickle |
| 5 | :synopsis: Convert Python objects to streams of bytes and back. |
| 6 | |
| 7 | .. sectionauthor:: Jim Kerr <jbkerr@sr.hp.com>. |
| 8 | .. sectionauthor:: Barry Warsaw <barry@python.org> |
| 9 | |
| 10 | **Source code:** :source:`Lib/pickle.py` |
| 11 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | .. index:: |
| 13 | single: persistence |
| 14 | pair: persistent; objects |
| 15 | pair: serializing; objects |
| 16 | pair: marshalling; objects |
| 17 | pair: flattening; objects |
| 18 | pair: pickling; objects |
| 19 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 20 | -------------- |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 21 | |
Antoine Pitrou | d4d6055 | 2013-12-07 00:56:59 +0100 | [diff] [blame] | 22 | The :mod:`pickle` module implements binary protocols for serializing and |
| 23 | de-serializing a Python object structure. *"Pickling"* is the process |
| 24 | whereby a Python object hierarchy is converted into a byte stream, and |
| 25 | *"unpickling"* is the inverse operation, whereby a byte stream |
| 26 | (from a :term:`binary file` or :term:`bytes-like object`) is converted |
| 27 | back into an object hierarchy. Pickling (and unpickling) is alternatively |
| 28 | known as "serialization", "marshalling," [#]_ or "flattening"; however, to |
| 29 | avoid confusion, the terms used here are "pickling" and "unpickling". |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | |
Georg Brandl | 0036bcf | 2010-10-17 10:24:54 +0000 | [diff] [blame] | 31 | .. warning:: |
| 32 | |
Daniel Pope | daa82d0 | 2019-08-31 06:51:33 +0100 | [diff] [blame] | 33 | The ``pickle`` module **is not secure**. Only unpickle data you trust. |
| 34 | |
| 35 | It is possible to construct malicious pickle data which will **execute |
| 36 | arbitrary code during unpickling**. Never unpickle data that could have come |
| 37 | from an untrusted source, or that could have been tampered with. |
| 38 | |
| 39 | Consider signing data with :mod:`hmac` if you need to ensure that it has not |
| 40 | been tampered with. |
| 41 | |
| 42 | Safer serialization formats such as :mod:`json` may be more appropriate if |
| 43 | you are processing untrusted data. See :ref:`comparison-with-json`. |
Georg Brandl | 0036bcf | 2010-10-17 10:24:54 +0000 | [diff] [blame] | 44 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | |
| 46 | Relationship to other Python modules |
| 47 | ------------------------------------ |
| 48 | |
Antoine Pitrou | d4d6055 | 2013-12-07 00:56:59 +0100 | [diff] [blame] | 49 | Comparison with ``marshal`` |
| 50 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
| 52 | Python has a more primitive serialization module called :mod:`marshal`, but in |
| 53 | general :mod:`pickle` should always be the preferred way to serialize Python |
| 54 | objects. :mod:`marshal` exists primarily to support Python's :file:`.pyc` |
| 55 | files. |
| 56 | |
Georg Brandl | 5aa580f | 2010-11-30 14:57:54 +0000 | [diff] [blame] | 57 | The :mod:`pickle` module differs from :mod:`marshal` in several significant ways: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
| 59 | * The :mod:`pickle` module keeps track of the objects it has already serialized, |
| 60 | so that later references to the same object won't be serialized again. |
| 61 | :mod:`marshal` doesn't do this. |
| 62 | |
| 63 | This has implications both for recursive objects and object sharing. Recursive |
| 64 | objects are objects that contain references to themselves. These are not |
| 65 | handled by marshal, and in fact, attempting to marshal recursive objects will |
| 66 | crash your Python interpreter. Object sharing happens when there are multiple |
| 67 | references to the same object in different places in the object hierarchy being |
| 68 | serialized. :mod:`pickle` stores such objects only once, and ensures that all |
| 69 | other references point to the master copy. Shared objects remain shared, which |
| 70 | can be very important for mutable objects. |
| 71 | |
| 72 | * :mod:`marshal` cannot be used to serialize user-defined classes and their |
| 73 | instances. :mod:`pickle` can save and restore class instances transparently, |
| 74 | however the class definition must be importable and live in the same module as |
| 75 | when the object was stored. |
| 76 | |
| 77 | * The :mod:`marshal` serialization format is not guaranteed to be portable |
| 78 | across Python versions. Because its primary job in life is to support |
| 79 | :file:`.pyc` files, the Python implementers reserve the right to change the |
| 80 | serialization format in non-backwards compatible ways should the need arise. |
| 81 | The :mod:`pickle` serialization format is guaranteed to be backwards compatible |
Gregory P. Smith | e328753 | 2018-12-09 11:42:58 -0800 | [diff] [blame] | 82 | across Python releases provided a compatible pickle protocol is chosen and |
| 83 | pickling and unpickling code deals with Python 2 to Python 3 type differences |
| 84 | if your data is crossing that unique breaking change language boundary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | |
Daniel Pope | daa82d0 | 2019-08-31 06:51:33 +0100 | [diff] [blame] | 86 | |
| 87 | .. _comparison-with-json: |
| 88 | |
Antoine Pitrou | d4d6055 | 2013-12-07 00:56:59 +0100 | [diff] [blame] | 89 | Comparison with ``json`` |
| 90 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
Antoine Pitrou | d4d6055 | 2013-12-07 00:56:59 +0100 | [diff] [blame] | 92 | There are fundamental differences between the pickle protocols and |
| 93 | `JSON (JavaScript Object Notation) <http://json.org>`_: |
| 94 | |
| 95 | * JSON is a text serialization format (it outputs unicode text, although |
| 96 | most of the time it is then encoded to ``utf-8``), while pickle is |
| 97 | a binary serialization format; |
| 98 | |
| 99 | * JSON is human-readable, while pickle is not; |
| 100 | |
| 101 | * JSON is interoperable and widely used outside of the Python ecosystem, |
| 102 | while pickle is Python-specific; |
| 103 | |
| 104 | * JSON, by default, can only represent a subset of the Python built-in |
| 105 | types, and no custom classes; pickle can represent an extremely large |
| 106 | number of Python types (many of them automatically, by clever usage |
| 107 | of Python's introspection facilities; complex cases can be tackled by |
Daniel Pope | daa82d0 | 2019-08-31 06:51:33 +0100 | [diff] [blame] | 108 | implementing :ref:`specific object APIs <pickle-inst>`); |
| 109 | |
| 110 | * Unlike pickle, deserializing untrusted JSON does not in itself create an |
| 111 | arbitrary code execution vulnerability. |
Antoine Pitrou | d4d6055 | 2013-12-07 00:56:59 +0100 | [diff] [blame] | 112 | |
| 113 | .. seealso:: |
| 114 | The :mod:`json` module: a standard library module allowing JSON |
| 115 | serialization and deserialization. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | 9bcb112 | 2013-12-07 01:05:57 +0100 | [diff] [blame] | 117 | |
| 118 | .. _pickle-protocols: |
| 119 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 120 | Data stream format |
| 121 | ------------------ |
| 122 | |
| 123 | .. index:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | single: External Data Representation |
| 125 | |
| 126 | The data format used by :mod:`pickle` is Python-specific. This has the |
| 127 | advantage that there are no restrictions imposed by external standards such as |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 128 | JSON or XDR (which can't represent pointer sharing); however it means that |
| 129 | non-Python programs may not be able to reconstruct pickled Python objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 130 | |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 131 | By default, the :mod:`pickle` data format uses a relatively compact binary |
| 132 | representation. If you need optimal size characteristics, you can efficiently |
| 133 | :doc:`compress <archiving>` pickled data. |
| 134 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 135 | The module :mod:`pickletools` contains tools for analyzing data streams |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 136 | generated by :mod:`pickle`. :mod:`pickletools` source code has extensive |
| 137 | comments about opcodes used by pickle protocols. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | |
Dima Tisnek | d0e0f5b | 2019-11-03 20:55:33 +0900 | [diff] [blame] | 139 | There are currently 6 different protocols which can be used for pickling. |
Antoine Pitrou | b645724 | 2014-01-21 02:39:54 +0100 | [diff] [blame] | 140 | The higher the protocol used, the more recent the version of Python needed |
| 141 | to read the pickle produced. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 142 | |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 143 | * Protocol version 0 is the original "human-readable" protocol and is |
Alexandre Vassalotti | f7d08c7 | 2009-01-23 04:50:05 +0000 | [diff] [blame] | 144 | backwards compatible with earlier versions of Python. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 145 | |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 146 | * 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] | 147 | earlier versions of Python. |
| 148 | |
| 149 | * 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] | 150 | efficient pickling of :term:`new-style class`\es. Refer to :pep:`307` for |
| 151 | information about improvements brought by protocol 2. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 152 | |
Antoine Pitrou | 9bcb112 | 2013-12-07 01:05:57 +0100 | [diff] [blame] | 153 | * Protocol version 3 was added in Python 3.0. It has explicit support for |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 154 | :class:`bytes` objects and cannot be unpickled by Python 2.x. This was |
| 155 | the default protocol in Python 3.0--3.7. |
Antoine Pitrou | 9bcb112 | 2013-12-07 01:05:57 +0100 | [diff] [blame] | 156 | |
| 157 | * Protocol version 4 was added in Python 3.4. It adds support for very large |
| 158 | objects, pickling more kinds of objects, and some data format |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 159 | optimizations. It is the default protocol starting with Python 3.8. |
| 160 | Refer to :pep:`3154` for information about improvements brought by |
| 161 | protocol 4. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
Dima Tisnek | d0e0f5b | 2019-11-03 20:55:33 +0900 | [diff] [blame] | 163 | * Protocol version 5 was added in Python 3.8. It adds support for out-of-band |
| 164 | data and speedup for in-band data. Refer to :pep:`574` for information about |
| 165 | improvements brought by protocol 5. |
| 166 | |
Antoine Pitrou | d4d6055 | 2013-12-07 00:56:59 +0100 | [diff] [blame] | 167 | .. note:: |
| 168 | Serialization is a more primitive notion than persistence; although |
| 169 | :mod:`pickle` reads and writes file objects, it does not handle the issue of |
| 170 | naming persistent objects, nor the (even more complicated) issue of concurrent |
| 171 | access to persistent objects. The :mod:`pickle` module can transform a complex |
| 172 | object into a byte stream and it can transform the byte stream into an object |
| 173 | with the same internal structure. Perhaps the most obvious thing to do with |
| 174 | these byte streams is to write them onto a file, but it is also conceivable to |
| 175 | send them across a network or store them in a database. The :mod:`shelve` |
| 176 | module provides a simple interface to pickle and unpickle objects on |
| 177 | DBM-style database files. |
| 178 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 179 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 180 | Module Interface |
| 181 | ---------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
Antoine Pitrou | a9494f6 | 2012-05-10 15:38:30 +0200 | [diff] [blame] | 183 | To serialize an object hierarchy, you simply call the :func:`dumps` function. |
| 184 | Similarly, to de-serialize a data stream, you call the :func:`loads` function. |
| 185 | However, if you want more control over serialization and de-serialization, |
| 186 | you can create a :class:`Pickler` or an :class:`Unpickler` object, respectively. |
| 187 | |
| 188 | The :mod:`pickle` module provides the following constants: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 189 | |
| 190 | |
| 191 | .. data:: HIGHEST_PROTOCOL |
| 192 | |
Antoine Pitrou | 9bcb112 | 2013-12-07 01:05:57 +0100 | [diff] [blame] | 193 | An integer, the highest :ref:`protocol version <pickle-protocols>` |
| 194 | available. This value can be passed as a *protocol* value to functions |
| 195 | :func:`dump` and :func:`dumps` as well as the :class:`Pickler` |
| 196 | constructor. |
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 | .. data:: DEFAULT_PROTOCOL |
| 199 | |
Antoine Pitrou | 9bcb112 | 2013-12-07 01:05:57 +0100 | [diff] [blame] | 200 | An integer, the default :ref:`protocol version <pickle-protocols>` used |
| 201 | for pickling. May be less than :data:`HIGHEST_PROTOCOL`. Currently the |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 202 | default protocol is 4, first introduced in Python 3.4 and incompatible |
| 203 | with previous versions. |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 204 | |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 205 | .. versionchanged:: 3.0 |
| 206 | |
| 207 | The default protocol is 3. |
| 208 | |
| 209 | .. versionchanged:: 3.8 |
| 210 | |
| 211 | The default protocol is 4. |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 212 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 213 | The :mod:`pickle` module provides the following functions to make the pickling |
| 214 | process more convenient: |
| 215 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 216 | .. function:: dump(obj, file, protocol=None, \*, fix_imports=True, buffer_callback=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 | |
Géry Ogam | 362f535 | 2019-08-07 07:02:23 +0200 | [diff] [blame] | 218 | Write the pickled representation of the object *obj* to the open |
| 219 | :term:`file object` *file*. This is equivalent to |
| 220 | ``Pickler(file, protocol).dump(obj)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 221 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 222 | Arguments *file*, *protocol*, *fix_imports* and *buffer_callback* have |
| 223 | the same meaning as in the :class:`Pickler` constructor. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 225 | .. versionchanged:: 3.8 |
| 226 | The *buffer_callback* argument was added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 227 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 228 | .. function:: dumps(obj, protocol=None, \*, fix_imports=True, buffer_callback=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 229 | |
Géry Ogam | 362f535 | 2019-08-07 07:02:23 +0200 | [diff] [blame] | 230 | Return the pickled representation of the object *obj* as a :class:`bytes` object, |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 231 | instead of writing it to a file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 233 | Arguments *protocol*, *fix_imports* and *buffer_callback* have the same |
| 234 | meaning as in the :class:`Pickler` constructor. |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 235 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 236 | .. versionchanged:: 3.8 |
| 237 | The *buffer_callback* argument was added. |
| 238 | |
| 239 | .. function:: load(file, \*, fix_imports=True, encoding="ASCII", errors="strict", buffers=None) |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 240 | |
Géry Ogam | 362f535 | 2019-08-07 07:02:23 +0200 | [diff] [blame] | 241 | Read the pickled representation of an object from the open :term:`file object` |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 242 | *file* and return the reconstituted object hierarchy specified therein. |
| 243 | This is equivalent to ``Unpickler(file).load()``. |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 244 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 245 | The protocol version of the pickle is detected automatically, so no |
Géry Ogam | 362f535 | 2019-08-07 07:02:23 +0200 | [diff] [blame] | 246 | protocol argument is needed. Bytes past the pickled representation |
| 247 | of the object are ignored. |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 249 | Arguments *file*, *fix_imports*, *encoding*, *errors*, *strict* and *buffers* |
| 250 | have the same meaning as in the :class:`Unpickler` constructor. |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 252 | .. versionchanged:: 3.8 |
| 253 | The *buffers* argument was added. |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 254 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 255 | .. function:: loads(bytes_object, \*, fix_imports=True, encoding="ASCII", errors="strict", buffers=None) |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 256 | |
Géry Ogam | 362f535 | 2019-08-07 07:02:23 +0200 | [diff] [blame] | 257 | Return the reconstituted object hierarchy of the pickled representation |
| 258 | *bytes_object* of an object. |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 259 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 260 | The protocol version of the pickle is detected automatically, so no |
Géry Ogam | 362f535 | 2019-08-07 07:02:23 +0200 | [diff] [blame] | 261 | protocol argument is needed. Bytes past the pickled representation |
| 262 | of the object are ignored. |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 264 | Arguments *file*, *fix_imports*, *encoding*, *errors*, *strict* and *buffers* |
| 265 | have the same meaning as in the :class:`Unpickler` constructor. |
| 266 | |
| 267 | .. versionchanged:: 3.8 |
| 268 | The *buffers* argument was added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 269 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 270 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 271 | The :mod:`pickle` module defines three exceptions: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 272 | |
| 273 | .. exception:: PickleError |
| 274 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 275 | Common base class for the other pickling exceptions. It inherits |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 276 | :exc:`Exception`. |
| 277 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 278 | .. exception:: PicklingError |
| 279 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 280 | Error raised when an unpicklable object is encountered by :class:`Pickler`. |
| 281 | It inherits :exc:`PickleError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 282 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 283 | Refer to :ref:`pickle-picklable` to learn what kinds of objects can be |
| 284 | pickled. |
| 285 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 286 | .. exception:: UnpicklingError |
| 287 | |
Ezio Melotti | e62aad3 | 2011-11-18 13:51:10 +0200 | [diff] [blame] | 288 | 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] | 289 | corruption or a security violation. It inherits :exc:`PickleError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 290 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 291 | Note that other exceptions may also be raised during unpickling, including |
| 292 | (but not necessarily limited to) AttributeError, EOFError, ImportError, and |
| 293 | IndexError. |
| 294 | |
| 295 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 296 | The :mod:`pickle` module exports three classes, :class:`Pickler`, |
| 297 | :class:`Unpickler` and :class:`PickleBuffer`: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 298 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 299 | .. class:: Pickler(file, protocol=None, \*, fix_imports=True, buffer_callback=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 300 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 301 | This takes a binary file for writing a pickle data stream. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | b645724 | 2014-01-21 02:39:54 +0100 | [diff] [blame] | 303 | The optional *protocol* argument, an integer, tells the pickler to use |
| 304 | the given protocol; supported protocols are 0 to :data:`HIGHEST_PROTOCOL`. |
| 305 | If not specified, the default is :data:`DEFAULT_PROTOCOL`. If a negative |
| 306 | number is specified, :data:`HIGHEST_PROTOCOL` is selected. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 307 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 308 | The *file* argument must have a write() method that accepts a single bytes |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 309 | argument. It can thus be an on-disk file opened for binary writing, an |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 310 | :class:`io.BytesIO` instance, or any other custom object that meets this |
| 311 | interface. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 312 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 313 | If *fix_imports* is true and *protocol* is less than 3, pickle will try to |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 314 | map the new Python 3 names to the old module names used in Python 2, so |
| 315 | that the pickle data stream is readable with Python 2. |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 316 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 317 | If *buffer_callback* is None (the default), buffer views are |
| 318 | serialized into *file* as part of the pickle stream. |
| 319 | |
| 320 | If *buffer_callback* is not None, then it can be called any number |
| 321 | of times with a buffer view. If the callback returns a false value |
| 322 | (such as None), the given buffer is :ref:`out-of-band <pickle-oob>`; |
| 323 | otherwise the buffer is serialized in-band, i.e. inside the pickle stream. |
| 324 | |
| 325 | It is an error if *buffer_callback* is not None and *protocol* is |
| 326 | None or smaller than 5. |
| 327 | |
| 328 | .. versionchanged:: 3.8 |
| 329 | The *buffer_callback* argument was added. |
| 330 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 331 | .. method:: dump(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 332 | |
Géry Ogam | 362f535 | 2019-08-07 07:02:23 +0200 | [diff] [blame] | 333 | Write the pickled representation of *obj* to the open file object given in |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 334 | the constructor. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 335 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 336 | .. method:: persistent_id(obj) |
| 337 | |
| 338 | Do nothing by default. This exists so a subclass can override it. |
| 339 | |
| 340 | If :meth:`persistent_id` returns ``None``, *obj* is pickled as usual. Any |
| 341 | other value causes :class:`Pickler` to emit the returned value as a |
| 342 | persistent ID for *obj*. The meaning of this persistent ID should be |
| 343 | defined by :meth:`Unpickler.persistent_load`. Note that the value |
| 344 | returned by :meth:`persistent_id` cannot itself have a persistent ID. |
| 345 | |
| 346 | See :ref:`pickle-persistent` for details and examples of uses. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 347 | |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 348 | .. attribute:: dispatch_table |
| 349 | |
| 350 | A pickler object's dispatch table is a registry of *reduction |
| 351 | functions* of the kind which can be declared using |
| 352 | :func:`copyreg.pickle`. It is a mapping whose keys are classes |
| 353 | and whose values are reduction functions. A reduction function |
| 354 | takes a single argument of the associated class and should |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 355 | conform to the same interface as a :meth:`__reduce__` |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 356 | method. |
| 357 | |
| 358 | By default, a pickler object will not have a |
| 359 | :attr:`dispatch_table` attribute, and it will instead use the |
| 360 | global dispatch table managed by the :mod:`copyreg` module. |
| 361 | However, to customize the pickling for a specific pickler object |
| 362 | one can set the :attr:`dispatch_table` attribute to a dict-like |
| 363 | object. Alternatively, if a subclass of :class:`Pickler` has a |
| 364 | :attr:`dispatch_table` attribute then this will be used as the |
| 365 | default dispatch table for instances of that class. |
| 366 | |
| 367 | See :ref:`pickle-dispatch` for usage examples. |
| 368 | |
| 369 | .. versionadded:: 3.3 |
| 370 | |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 371 | .. method:: reducer_override(self, obj) |
| 372 | |
| 373 | Special reducer that can be defined in :class:`Pickler` subclasses. This |
| 374 | method has priority over any reducer in the :attr:`dispatch_table`. It |
| 375 | should conform to the same interface as a :meth:`__reduce__` method, and |
| 376 | can optionally return ``NotImplemented`` to fallback on |
| 377 | :attr:`dispatch_table`-registered reducers to pickle ``obj``. |
| 378 | |
| 379 | For a detailed example, see :ref:`reducer_override`. |
| 380 | |
| 381 | .. versionadded:: 3.8 |
| 382 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 383 | .. attribute:: fast |
| 384 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 385 | Deprecated. Enable fast mode if set to a true value. The fast mode |
| 386 | disables the usage of memo, therefore speeding the pickling process by not |
| 387 | generating superfluous PUT opcodes. It should not be used with |
| 388 | self-referential objects, doing otherwise will cause :class:`Pickler` to |
| 389 | recurse infinitely. |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 390 | |
| 391 | Use :func:`pickletools.optimize` if you need more compact pickles. |
| 392 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 393 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 394 | .. class:: Unpickler(file, \*, fix_imports=True, encoding="ASCII", errors="strict", buffers=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 395 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 396 | This takes a binary file for reading a pickle data stream. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 397 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 398 | The protocol version of the pickle is detected automatically, so no |
| 399 | protocol argument is needed. |
| 400 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 401 | The argument *file* must have three methods, a read() method that takes an |
| 402 | integer argument, a readinto() method that takes a buffer argument |
| 403 | and a readline() method that requires no arguments, as in the |
| 404 | :class:`io.BufferedIOBase` interface. Thus *file* can be an on-disk file |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 405 | opened for binary reading, an :class:`io.BytesIO` object, or any other |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 406 | custom object that meets this interface. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 407 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 408 | The optional arguments *fix_imports*, *encoding* and *errors* are used |
| 409 | to control compatibility support for pickle stream generated by Python 2. |
| 410 | If *fix_imports* is true, pickle will try to map the old Python 2 names |
| 411 | to the new names used in Python 3. The *encoding* and *errors* tell |
| 412 | pickle how to decode 8-bit string instances pickled by Python 2; |
| 413 | these default to 'ASCII' and 'strict', respectively. The *encoding* can |
Sebastian Pucilowski | a8d25a1 | 2017-12-21 20:00:49 +1100 | [diff] [blame] | 414 | be 'bytes' to read these 8-bit string instances as bytes objects. |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 415 | Using ``encoding='latin1'`` is required for unpickling NumPy arrays and |
| 416 | instances of :class:`~datetime.datetime`, :class:`~datetime.date` and |
| 417 | :class:`~datetime.time` pickled by Python 2. |
| 418 | |
| 419 | If *buffers* is None (the default), then all data necessary for |
| 420 | deserialization must be contained in the pickle stream. This means |
| 421 | that the *buffer_callback* argument was None when a :class:`Pickler` |
| 422 | was instantiated (or when :func:`dump` or :func:`dumps` was called). |
| 423 | |
| 424 | If *buffers* is not None, it should be an iterable of buffer-enabled |
| 425 | objects that is consumed each time the pickle stream references |
| 426 | an :ref:`out-of-band <pickle-oob>` buffer view. Such buffers have been |
| 427 | given in order to the *buffer_callback* of a Pickler object. |
| 428 | |
| 429 | .. versionchanged:: 3.8 |
| 430 | The *buffers* argument was added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 431 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 432 | .. method:: load() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 433 | |
Géry Ogam | 362f535 | 2019-08-07 07:02:23 +0200 | [diff] [blame] | 434 | Read the pickled representation of an object from the open file object |
| 435 | given in the constructor, and return the reconstituted object hierarchy |
| 436 | specified therein. Bytes past the pickled representation of the object |
| 437 | are ignored. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 438 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 439 | .. method:: persistent_load(pid) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 440 | |
Ezio Melotti | e62aad3 | 2011-11-18 13:51:10 +0200 | [diff] [blame] | 441 | Raise an :exc:`UnpicklingError` by default. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 442 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 443 | If defined, :meth:`persistent_load` should return the object specified by |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 444 | the persistent ID *pid*. If an invalid persistent ID is encountered, an |
Ezio Melotti | e62aad3 | 2011-11-18 13:51:10 +0200 | [diff] [blame] | 445 | :exc:`UnpicklingError` should be raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 446 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 447 | See :ref:`pickle-persistent` for details and examples of uses. |
| 448 | |
| 449 | .. method:: find_class(module, name) |
| 450 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 451 | Import *module* if necessary and return the object called *name* from it, |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 452 | where the *module* and *name* arguments are :class:`str` objects. Note, |
| 453 | unlike its name suggests, :meth:`find_class` is also used for finding |
| 454 | functions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 455 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 456 | 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] | 457 | how they can be loaded, potentially reducing security risks. Refer to |
| 458 | :ref:`pickle-restrict` for details. |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 459 | |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 460 | .. audit-event:: pickle.find_class module,name pickle.Unpickler.find_class |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 461 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 462 | .. class:: PickleBuffer(buffer) |
| 463 | |
| 464 | A wrapper for a buffer representing picklable data. *buffer* must be a |
| 465 | :ref:`buffer-providing <bufferobjects>` object, such as a |
| 466 | :term:`bytes-like object` or a N-dimensional array. |
| 467 | |
| 468 | :class:`PickleBuffer` is itself a buffer provider, therefore it is |
| 469 | possible to pass it to other APIs expecting a buffer-providing object, |
| 470 | such as :class:`memoryview`. |
| 471 | |
| 472 | :class:`PickleBuffer` objects can only be serialized using pickle |
| 473 | protocol 5 or higher. They are eligible for |
| 474 | :ref:`out-of-band serialization <pickle-oob>`. |
| 475 | |
| 476 | .. versionadded:: 3.8 |
| 477 | |
| 478 | .. method:: raw() |
| 479 | |
| 480 | Return a :class:`memoryview` of the memory area underlying this buffer. |
| 481 | The returned object is a one-dimensional, C-contiguous memoryview |
| 482 | with format ``B`` (unsigned bytes). :exc:`BufferError` is raised if |
| 483 | the buffer is neither C- nor Fortran-contiguous. |
| 484 | |
| 485 | .. method:: release() |
| 486 | |
| 487 | Release the underlying buffer exposed by the PickleBuffer object. |
| 488 | |
| 489 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 490 | .. _pickle-picklable: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 491 | |
| 492 | What can be pickled and unpickled? |
| 493 | ---------------------------------- |
| 494 | |
| 495 | The following types can be pickled: |
| 496 | |
| 497 | * ``None``, ``True``, and ``False`` |
| 498 | |
Georg Brandl | ba956ae | 2007-11-29 17:24:34 +0000 | [diff] [blame] | 499 | * integers, floating point numbers, complex numbers |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 500 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 501 | * strings, bytes, bytearrays |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 502 | |
| 503 | * tuples, lists, sets, and dictionaries containing only picklable objects |
| 504 | |
Ethan Furman | 2498d9e | 2013-10-18 00:45:40 -0700 | [diff] [blame] | 505 | * functions defined at the top level of a module (using :keyword:`def`, not |
| 506 | :keyword:`lambda`) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 507 | |
| 508 | * built-in functions defined at the top level of a module |
| 509 | |
| 510 | * classes that are defined at the top level of a module |
| 511 | |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 512 | * instances of such classes whose :attr:`~object.__dict__` or the result of |
| 513 | calling :meth:`__getstate__` is picklable (see section :ref:`pickle-inst` for |
Eli Bendersky | 78f3ce5 | 2013-01-02 05:53:59 -0800 | [diff] [blame] | 514 | details). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 515 | |
| 516 | Attempts to pickle unpicklable objects will raise the :exc:`PicklingError` |
| 517 | exception; when this happens, an unspecified number of bytes may have already |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 518 | been written to the underlying file. Trying to pickle a highly recursive data |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 519 | structure may exceed the maximum recursion depth, a :exc:`RecursionError` will be |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 520 | raised in this case. You can carefully raise this limit with |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 521 | :func:`sys.setrecursionlimit`. |
| 522 | |
| 523 | 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] | 524 | 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] | 525 | pickled, along with the name of the module the function is defined in. Neither |
| 526 | 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] | 527 | defining module must be importable in the unpickling environment, and the module |
| 528 | must contain the named object, otherwise an exception will be raised. [#]_ |
| 529 | |
| 530 | Similarly, classes are pickled by named reference, so the same restrictions in |
| 531 | the unpickling environment apply. Note that none of the class's code or data is |
| 532 | pickled, so in the following example the class attribute ``attr`` is not |
| 533 | restored in the unpickling environment:: |
| 534 | |
| 535 | class Foo: |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 536 | attr = 'A class attribute' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 537 | |
| 538 | picklestring = pickle.dumps(Foo) |
| 539 | |
| 540 | These restrictions are why picklable functions and classes must be defined in |
| 541 | the top level of a module. |
| 542 | |
| 543 | Similarly, when class instances are pickled, their class's code and data are not |
| 544 | pickled along with them. Only the instance data are pickled. This is done on |
| 545 | purpose, so you can fix bugs in a class or add methods to the class and still |
| 546 | load objects that were created with an earlier version of the class. If you |
| 547 | plan to have long-lived objects that will see many versions of a class, it may |
| 548 | be worthwhile to put a version number in the objects so that suitable |
| 549 | conversions can be made by the class's :meth:`__setstate__` method. |
| 550 | |
| 551 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 552 | .. _pickle-inst: |
| 553 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 554 | Pickling Class Instances |
| 555 | ------------------------ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 556 | |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 557 | .. currentmodule:: None |
| 558 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 559 | In this section, we describe the general mechanisms available to you to define, |
| 560 | customize, and control how class instances are pickled and unpickled. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 561 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 562 | In most cases, no additional code is needed to make instances picklable. By |
| 563 | default, pickle will retrieve the class and the attributes of an instance via |
| 564 | introspection. When a class instance is unpickled, its :meth:`__init__` method |
| 565 | is usually *not* invoked. The default behaviour first creates an uninitialized |
| 566 | instance and then restores the saved attributes. The following code shows an |
| 567 | implementation of this behaviour:: |
Georg Brandl | 85eb8c1 | 2007-08-31 16:33:38 +0000 | [diff] [blame] | 568 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 569 | def save(obj): |
| 570 | return (obj.__class__, obj.__dict__) |
| 571 | |
| 572 | def load(cls, attributes): |
| 573 | obj = cls.__new__(cls) |
| 574 | obj.__dict__.update(attributes) |
| 575 | return obj |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 576 | |
Georg Brandl | 6faee4e | 2010-09-21 14:48:28 +0000 | [diff] [blame] | 577 | Classes can alter the default behaviour by providing one or several special |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 578 | methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 579 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 580 | .. method:: object.__getnewargs_ex__() |
| 581 | |
Serhiy Storchaka | b6d8483 | 2015-10-13 21:26:35 +0300 | [diff] [blame] | 582 | In protocols 2 and newer, classes that implements the |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 583 | :meth:`__getnewargs_ex__` method can dictate the values passed to the |
| 584 | :meth:`__new__` method upon unpickling. The method must return a pair |
| 585 | ``(args, kwargs)`` where *args* is a tuple of positional arguments |
| 586 | and *kwargs* a dictionary of named arguments for constructing the |
| 587 | object. Those will be passed to the :meth:`__new__` method upon |
| 588 | unpickling. |
| 589 | |
| 590 | You should implement this method if the :meth:`__new__` method of your |
| 591 | class requires keyword-only arguments. Otherwise, it is recommended for |
| 592 | compatibility to implement :meth:`__getnewargs__`. |
| 593 | |
Serhiy Storchaka | b6d8483 | 2015-10-13 21:26:35 +0300 | [diff] [blame] | 594 | .. versionchanged:: 3.6 |
| 595 | :meth:`__getnewargs_ex__` is now used in protocols 2 and 3. |
| 596 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 597 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 598 | .. method:: object.__getnewargs__() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 599 | |
Andrés Delfino | 0e0534c | 2018-06-09 21:41:09 -0300 | [diff] [blame] | 600 | This method serves a similar purpose as :meth:`__getnewargs_ex__`, but |
Serhiy Storchaka | b6d8483 | 2015-10-13 21:26:35 +0300 | [diff] [blame] | 601 | supports only positional arguments. It must return a tuple of arguments |
| 602 | ``args`` which will be passed to the :meth:`__new__` method upon unpickling. |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 603 | |
Serhiy Storchaka | b6d8483 | 2015-10-13 21:26:35 +0300 | [diff] [blame] | 604 | :meth:`__getnewargs__` will not be called if :meth:`__getnewargs_ex__` is |
| 605 | defined. |
| 606 | |
| 607 | .. versionchanged:: 3.6 |
| 608 | Before Python 3.6, :meth:`__getnewargs__` was called instead of |
| 609 | :meth:`__getnewargs_ex__` in protocols 2 and 3. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 610 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 611 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 612 | .. method:: object.__getstate__() |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 613 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 614 | Classes can further influence how their instances are pickled; if the class |
| 615 | defines the method :meth:`__getstate__`, it is called and the returned object |
| 616 | is pickled as the contents for the instance, instead of the contents of the |
| 617 | instance's dictionary. If the :meth:`__getstate__` method is absent, the |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 618 | instance's :attr:`~object.__dict__` is pickled as usual. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 619 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 620 | |
| 621 | .. method:: object.__setstate__(state) |
| 622 | |
| 623 | Upon unpickling, if the class defines :meth:`__setstate__`, it is called with |
| 624 | the unpickled state. In that case, there is no requirement for the state |
| 625 | object to be a dictionary. Otherwise, the pickled state must be a dictionary |
| 626 | and its items are assigned to the new instance's dictionary. |
| 627 | |
| 628 | .. note:: |
| 629 | |
| 630 | If :meth:`__getstate__` returns a false value, the :meth:`__setstate__` |
| 631 | method will not be called upon unpickling. |
| 632 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 633 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 634 | Refer to the section :ref:`pickle-state` for more information about how to use |
| 635 | the methods :meth:`__getstate__` and :meth:`__setstate__`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 636 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 637 | .. note:: |
Georg Brandl | e720c0a | 2009-04-27 16:20:50 +0000 | [diff] [blame] | 638 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 639 | At unpickling time, some methods like :meth:`__getattr__`, |
| 640 | :meth:`__getattribute__`, or :meth:`__setattr__` may be called upon the |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 641 | instance. In case those methods rely on some internal invariant being |
| 642 | true, the type should implement :meth:`__getnewargs__` or |
| 643 | :meth:`__getnewargs_ex__` to establish such an invariant; otherwise, |
| 644 | neither :meth:`__new__` nor :meth:`__init__` will be called. |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 645 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 646 | .. index:: pair: copy; protocol |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 647 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 648 | As we shall see, pickle does not use directly the methods described above. In |
| 649 | fact, these methods are part of the copy protocol which implements the |
| 650 | :meth:`__reduce__` special method. The copy protocol provides a unified |
| 651 | interface for retrieving the data necessary for pickling and copying |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 652 | objects. [#]_ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 653 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 654 | Although powerful, implementing :meth:`__reduce__` directly in your classes is |
| 655 | error prone. For this reason, class designers should use the high-level |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 656 | interface (i.e., :meth:`__getnewargs_ex__`, :meth:`__getstate__` and |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 657 | :meth:`__setstate__`) whenever possible. We will show, however, cases where |
| 658 | using :meth:`__reduce__` is the only option or leads to more efficient pickling |
| 659 | or both. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 660 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 661 | .. method:: object.__reduce__() |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 662 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 663 | The interface is currently defined as follows. The :meth:`__reduce__` method |
| 664 | takes no argument and shall return either a string or preferably a tuple (the |
| 665 | returned object is often referred to as the "reduce value"). |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 666 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 667 | If a string is returned, the string should be interpreted as the name of a |
| 668 | global variable. It should be the object's local name relative to its |
| 669 | module; the pickle module searches the module namespace to determine the |
| 670 | object's module. This behaviour is typically useful for singletons. |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 671 | |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 672 | When a tuple is returned, it must be between two and six items long. |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 673 | Optional items can either be omitted, or ``None`` can be provided as their |
| 674 | value. The semantics of each item are in order: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 675 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 676 | .. XXX Mention __newobj__ special-case? |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 677 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 678 | * A callable object that will be called to create the initial version of the |
| 679 | object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 680 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 681 | * A tuple of arguments for the callable object. An empty tuple must be given |
| 682 | if the callable does not accept any argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 683 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 684 | * Optionally, the object's state, which will be passed to the object's |
| 685 | :meth:`__setstate__` method as previously described. If the object has no |
| 686 | 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] | 687 | the object's :attr:`~object.__dict__` attribute. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 688 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 689 | * Optionally, an iterator (and not a sequence) yielding successive items. |
| 690 | These items will be appended to the object either using |
| 691 | ``obj.append(item)`` or, in batch, using ``obj.extend(list_of_items)``. |
| 692 | This is primarily used for list subclasses, but may be used by other |
| 693 | classes as long as they have :meth:`append` and :meth:`extend` methods with |
| 694 | the appropriate signature. (Whether :meth:`append` or :meth:`extend` is |
| 695 | used depends on which pickle protocol version is used as well as the number |
| 696 | of items to append, so both must be supported.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 697 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 698 | * Optionally, an iterator (not a sequence) yielding successive key-value |
| 699 | pairs. These items will be stored to the object using ``obj[key] = |
| 700 | value``. This is primarily used for dictionary subclasses, but may be used |
| 701 | by other classes as long as they implement :meth:`__setitem__`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 702 | |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 703 | * Optionally, a callable with a ``(obj, state)`` signature. This |
Xtreak | 9b5a0ef | 2019-05-16 10:04:24 +0530 | [diff] [blame] | 704 | callable allows the user to programmatically control the state-updating |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 705 | behavior of a specific object, instead of using ``obj``'s static |
| 706 | :meth:`__setstate__` method. If not ``None``, this callable will have |
| 707 | priority over ``obj``'s :meth:`__setstate__`. |
| 708 | |
| 709 | .. versionadded:: 3.8 |
| 710 | The optional sixth tuple item, ``(obj, state)``, was added. |
| 711 | |
Georg Brandl | c814826 | 2010-10-17 11:13:37 +0000 | [diff] [blame] | 712 | |
| 713 | .. method:: object.__reduce_ex__(protocol) |
| 714 | |
| 715 | Alternatively, a :meth:`__reduce_ex__` method may be defined. The only |
| 716 | difference is this method should take a single integer argument, the protocol |
| 717 | version. When defined, pickle will prefer it over the :meth:`__reduce__` |
| 718 | method. In addition, :meth:`__reduce__` automatically becomes a synonym for |
| 719 | the extended version. The main use for this method is to provide |
| 720 | backwards-compatible reduce values for older Python releases. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 721 | |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 722 | .. currentmodule:: pickle |
| 723 | |
Alexandre Vassalotti | 758bca6 | 2008-10-18 19:25:07 +0000 | [diff] [blame] | 724 | .. _pickle-persistent: |
| 725 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 726 | Persistence of External Objects |
| 727 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 728 | |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 729 | .. index:: |
| 730 | single: persistent_id (pickle protocol) |
| 731 | single: persistent_load (pickle protocol) |
| 732 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 733 | For the benefit of object persistence, the :mod:`pickle` module supports the |
| 734 | 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] | 735 | objects are referenced by a persistent ID, which should be either a string of |
| 736 | alphanumeric characters (for protocol 0) [#]_ or just an arbitrary object (for |
| 737 | any newer protocol). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 738 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 739 | The resolution of such persistent IDs is not defined by the :mod:`pickle` |
Géry Ogam | 362f535 | 2019-08-07 07:02:23 +0200 | [diff] [blame] | 740 | 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] | 741 | pickler and unpickler, :meth:`~Pickler.persistent_id` and |
| 742 | :meth:`~Unpickler.persistent_load` respectively. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 743 | |
Géry Ogam | 362f535 | 2019-08-07 07:02:23 +0200 | [diff] [blame] | 744 | 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] | 745 | custom :meth:`~Pickler.persistent_id` method that takes an object as an |
Géry Ogam | 362f535 | 2019-08-07 07:02:23 +0200 | [diff] [blame] | 746 | argument and returns either ``None`` or the persistent ID for that object. |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 747 | When ``None`` is returned, the pickler simply pickles the object as normal. |
| 748 | When a persistent ID string is returned, the pickler will pickle that object, |
| 749 | 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] | 750 | |
| 751 | To unpickle external objects, the unpickler must have a custom |
Serhiy Storchaka | 5bbbc94 | 2013-10-14 10:43:46 +0300 | [diff] [blame] | 752 | :meth:`~Unpickler.persistent_load` method that takes a persistent ID object and |
| 753 | returns the referenced object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 754 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 755 | Here is a comprehensive example presenting how persistent ID can be used to |
| 756 | pickle external objects by reference. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 757 | |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 758 | .. literalinclude:: ../includes/dbpickle.py |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 759 | |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 760 | .. _pickle-dispatch: |
| 761 | |
| 762 | Dispatch Tables |
| 763 | ^^^^^^^^^^^^^^^ |
| 764 | |
| 765 | If one wants to customize pickling of some classes without disturbing |
| 766 | any other code which depends on pickling, then one can create a |
| 767 | pickler with a private dispatch table. |
| 768 | |
| 769 | The global dispatch table managed by the :mod:`copyreg` module is |
| 770 | available as :data:`copyreg.dispatch_table`. Therefore, one may |
| 771 | choose to use a modified copy of :data:`copyreg.dispatch_table` as a |
| 772 | private dispatch table. |
| 773 | |
| 774 | For example :: |
| 775 | |
| 776 | f = io.BytesIO() |
| 777 | p = pickle.Pickler(f) |
| 778 | p.dispatch_table = copyreg.dispatch_table.copy() |
| 779 | p.dispatch_table[SomeClass] = reduce_SomeClass |
| 780 | |
| 781 | creates an instance of :class:`pickle.Pickler` with a private dispatch |
| 782 | table which handles the ``SomeClass`` class specially. Alternatively, |
| 783 | the code :: |
| 784 | |
| 785 | class MyPickler(pickle.Pickler): |
| 786 | dispatch_table = copyreg.dispatch_table.copy() |
| 787 | dispatch_table[SomeClass] = reduce_SomeClass |
| 788 | f = io.BytesIO() |
| 789 | p = MyPickler(f) |
| 790 | |
| 791 | does the same, but all instances of ``MyPickler`` will by default |
| 792 | share the same dispatch table. The equivalent code using the |
| 793 | :mod:`copyreg` module is :: |
| 794 | |
| 795 | copyreg.pickle(SomeClass, reduce_SomeClass) |
| 796 | f = io.BytesIO() |
| 797 | p = pickle.Pickler(f) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 798 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 799 | .. _pickle-state: |
| 800 | |
| 801 | Handling Stateful Objects |
| 802 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 803 | |
| 804 | .. index:: |
| 805 | single: __getstate__() (copy protocol) |
| 806 | single: __setstate__() (copy protocol) |
| 807 | |
| 808 | Here's an example that shows how to modify pickling behavior for a class. |
| 809 | 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] | 810 | line contents each time its :meth:`!readline` method is called. If a |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 811 | :class:`TextReader` instance is pickled, all attributes *except* the file object |
| 812 | member are saved. When the instance is unpickled, the file is reopened, and |
| 813 | reading resumes from the last location. The :meth:`__setstate__` and |
| 814 | :meth:`__getstate__` methods are used to implement this behavior. :: |
| 815 | |
| 816 | class TextReader: |
| 817 | """Print and number lines in a text file.""" |
| 818 | |
| 819 | def __init__(self, filename): |
| 820 | self.filename = filename |
| 821 | self.file = open(filename) |
| 822 | self.lineno = 0 |
| 823 | |
| 824 | def readline(self): |
| 825 | self.lineno += 1 |
| 826 | line = self.file.readline() |
| 827 | if not line: |
| 828 | return None |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 829 | if line.endswith('\n'): |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 830 | line = line[:-1] |
| 831 | return "%i: %s" % (self.lineno, line) |
| 832 | |
| 833 | def __getstate__(self): |
| 834 | # Copy the object's state from self.__dict__ which contains |
| 835 | # all our instance attributes. Always use the dict.copy() |
| 836 | # method to avoid modifying the original state. |
| 837 | state = self.__dict__.copy() |
| 838 | # Remove the unpicklable entries. |
| 839 | del state['file'] |
| 840 | return state |
| 841 | |
| 842 | def __setstate__(self, state): |
| 843 | # Restore instance attributes (i.e., filename and lineno). |
| 844 | self.__dict__.update(state) |
| 845 | # Restore the previously opened file's state. To do so, we need to |
| 846 | # reopen it and read from it until the line count is restored. |
| 847 | file = open(self.filename) |
| 848 | for _ in range(self.lineno): |
| 849 | file.readline() |
| 850 | # Finally, save the file. |
| 851 | self.file = file |
| 852 | |
| 853 | |
| 854 | A sample usage might be something like this:: |
| 855 | |
| 856 | >>> reader = TextReader("hello.txt") |
| 857 | >>> reader.readline() |
| 858 | '1: Hello world!' |
| 859 | >>> reader.readline() |
| 860 | '2: I am line number two.' |
| 861 | >>> new_reader = pickle.loads(pickle.dumps(reader)) |
| 862 | >>> new_reader.readline() |
| 863 | '3: Goodbye!' |
| 864 | |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 865 | .. _reducer_override: |
| 866 | |
| 867 | Custom Reduction for Types, Functions, and Other Objects |
| 868 | -------------------------------------------------------- |
| 869 | |
| 870 | .. versionadded:: 3.8 |
| 871 | |
| 872 | Sometimes, :attr:`~Pickler.dispatch_table` may not be flexible enough. |
| 873 | In particular we may want to customize pickling based on another criterion |
| 874 | than the object's type, or we may want to customize the pickling of |
| 875 | functions and classes. |
| 876 | |
| 877 | For those cases, it is possible to subclass from the :class:`Pickler` class and |
| 878 | implement a :meth:`~Pickler.reducer_override` method. This method can return an |
| 879 | arbitrary reduction tuple (see :meth:`__reduce__`). It can alternatively return |
| 880 | ``NotImplemented`` to fallback to the traditional behavior. |
| 881 | |
| 882 | If both the :attr:`~Pickler.dispatch_table` and |
| 883 | :meth:`~Pickler.reducer_override` are defined, then |
| 884 | :meth:`~Pickler.reducer_override` method takes priority. |
| 885 | |
| 886 | .. Note:: |
| 887 | For performance reasons, :meth:`~Pickler.reducer_override` may not be |
| 888 | called for the following objects: ``None``, ``True``, ``False``, and |
| 889 | exact instances of :class:`int`, :class:`float`, :class:`bytes`, |
| 890 | :class:`str`, :class:`dict`, :class:`set`, :class:`frozenset`, :class:`list` |
| 891 | and :class:`tuple`. |
| 892 | |
| 893 | Here is a simple example where we allow pickling and reconstructing |
| 894 | a given class:: |
| 895 | |
| 896 | import io |
| 897 | import pickle |
| 898 | |
| 899 | class MyClass: |
| 900 | my_attribute = 1 |
| 901 | |
| 902 | class MyPickler(pickle.Pickler): |
| 903 | def reducer_override(self, obj): |
| 904 | """Custom reducer for MyClass.""" |
| 905 | if getattr(obj, "__name__", None) == "MyClass": |
| 906 | return type, (obj.__name__, obj.__bases__, |
| 907 | {'my_attribute': obj.my_attribute}) |
| 908 | else: |
| 909 | # For any other object, fallback to usual reduction |
| 910 | return NotImplemented |
| 911 | |
| 912 | f = io.BytesIO() |
| 913 | p = MyPickler(f) |
| 914 | p.dump(MyClass) |
| 915 | |
| 916 | del MyClass |
| 917 | |
| 918 | unpickled_class = pickle.loads(f.getvalue()) |
| 919 | |
| 920 | assert isinstance(unpickled_class, type) |
| 921 | assert unpickled_class.__name__ == "MyClass" |
| 922 | assert unpickled_class.my_attribute == 1 |
| 923 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 925 | .. _pickle-oob: |
| 926 | |
| 927 | Out-of-band Buffers |
| 928 | ------------------- |
| 929 | |
| 930 | .. versionadded:: 3.8 |
| 931 | |
| 932 | In some contexts, the :mod:`pickle` module is used to transfer massive amounts |
| 933 | of data. Therefore, it can be important to minimize the number of memory |
| 934 | copies, to preserve performance and resource consumption. However, normal |
| 935 | operation of the :mod:`pickle` module, as it transforms a graph-like structure |
| 936 | of objects into a sequential stream of bytes, intrinsically involves copying |
| 937 | data to and from the pickle stream. |
| 938 | |
| 939 | This constraint can be eschewed if both the *provider* (the implementation |
| 940 | of the object types to be transferred) and the *consumer* (the implementation |
| 941 | of the communications system) support the out-of-band transfer facilities |
| 942 | provided by pickle protocol 5 and higher. |
| 943 | |
| 944 | Provider API |
| 945 | ^^^^^^^^^^^^ |
| 946 | |
| 947 | The large data objects to be pickled must implement a :meth:`__reduce_ex__` |
| 948 | method specialized for protocol 5 and higher, which returns a |
| 949 | :class:`PickleBuffer` instance (instead of e.g. a :class:`bytes` object) |
| 950 | for any large data. |
| 951 | |
| 952 | A :class:`PickleBuffer` object *signals* that the underlying buffer is |
| 953 | eligible for out-of-band data transfer. Those objects remain compatible |
| 954 | with normal usage of the :mod:`pickle` module. However, consumers can also |
| 955 | opt-in to tell :mod:`pickle` that they will handle those buffers by |
| 956 | themselves. |
| 957 | |
| 958 | Consumer API |
| 959 | ^^^^^^^^^^^^ |
| 960 | |
| 961 | A communications system can enable custom handling of the :class:`PickleBuffer` |
| 962 | objects generated when serializing an object graph. |
| 963 | |
| 964 | On the sending side, it needs to pass a *buffer_callback* argument to |
| 965 | :class:`Pickler` (or to the :func:`dump` or :func:`dumps` function), which |
| 966 | will be called with each :class:`PickleBuffer` generated while pickling |
| 967 | the object graph. Buffers accumulated by the *buffer_callback* will not |
| 968 | see their data copied into the pickle stream, only a cheap marker will be |
| 969 | inserted. |
| 970 | |
| 971 | On the receiving side, it needs to pass a *buffers* argument to |
| 972 | :class:`Unpickler` (or to the :func:`load` or :func:`loads` function), |
| 973 | which is an iterable of the buffers which were passed to *buffer_callback*. |
| 974 | That iterable should produce buffers in the same order as they were passed |
| 975 | to *buffer_callback*. Those buffers will provide the data expected by the |
| 976 | reconstructors of the objects whose pickling produced the original |
| 977 | :class:`PickleBuffer` objects. |
| 978 | |
| 979 | Between the sending side and the receiving side, the communications system |
| 980 | is free to implement its own transfer mechanism for out-of-band buffers. |
| 981 | Potential optimizations include the use of shared memory or datatype-dependent |
| 982 | compression. |
| 983 | |
| 984 | Example |
| 985 | ^^^^^^^ |
| 986 | |
| 987 | Here is a trivial example where we implement a :class:`bytearray` subclass |
| 988 | able to participate in out-of-band buffer pickling:: |
| 989 | |
| 990 | class ZeroCopyByteArray(bytearray): |
| 991 | |
| 992 | def __reduce_ex__(self, protocol): |
| 993 | if protocol >= 5: |
| 994 | return type(self)._reconstruct, (PickleBuffer(self),), None |
| 995 | else: |
| 996 | # PickleBuffer is forbidden with pickle protocols <= 4. |
| 997 | return type(self)._reconstruct, (bytearray(self),) |
| 998 | |
| 999 | @classmethod |
| 1000 | def _reconstruct(cls, obj): |
| 1001 | with memoryview(obj) as m: |
| 1002 | # Get a handle over the original buffer object |
| 1003 | obj = m.obj |
| 1004 | if type(obj) is cls: |
| 1005 | # Original buffer object is a ZeroCopyByteArray, return it |
| 1006 | # as-is. |
| 1007 | return obj |
| 1008 | else: |
| 1009 | return cls(obj) |
| 1010 | |
| 1011 | The reconstructor (the ``_reconstruct`` class method) returns the buffer's |
| 1012 | providing object if it has the right type. This is an easy way to simulate |
| 1013 | zero-copy behaviour on this toy example. |
| 1014 | |
| 1015 | On the consumer side, we can pickle those objects the usual way, which |
| 1016 | when unserialized will give us a copy of the original object:: |
| 1017 | |
| 1018 | b = ZeroCopyByteArray(b"abc") |
| 1019 | data = pickle.dumps(b, protocol=5) |
| 1020 | new_b = pickle.loads(data) |
| 1021 | print(b == new_b) # True |
| 1022 | print(b is new_b) # False: a copy was made |
| 1023 | |
| 1024 | But if we pass a *buffer_callback* and then give back the accumulated |
| 1025 | buffers when unserializing, we are able to get back the original object:: |
| 1026 | |
| 1027 | b = ZeroCopyByteArray(b"abc") |
| 1028 | buffers = [] |
| 1029 | data = pickle.dumps(b, protocol=5, buffer_callback=buffers.append) |
| 1030 | new_b = pickle.loads(data, buffers=buffers) |
| 1031 | print(b == new_b) # True |
| 1032 | print(b is new_b) # True: no copy was made |
| 1033 | |
| 1034 | This example is limited by the fact that :class:`bytearray` allocates its |
| 1035 | own memory: you cannot create a :class:`bytearray` instance that is backed |
| 1036 | by another object's memory. However, third-party datatypes such as NumPy |
| 1037 | arrays do not have this limitation, and allow use of zero-copy pickling |
| 1038 | (or making as few copies as possible) when transferring between distinct |
| 1039 | processes or systems. |
| 1040 | |
| 1041 | .. seealso:: :pep:`574` -- Pickle protocol 5 with out-of-band data |
| 1042 | |
| 1043 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 1044 | .. _pickle-restrict: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1045 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 1046 | Restricting Globals |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 1047 | ------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1048 | |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 1049 | .. index:: |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 1050 | single: find_class() (pickle protocol) |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 1051 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 1052 | By default, unpickling will import any class or function that it finds in the |
| 1053 | pickle data. For many applications, this behaviour is unacceptable as it |
| 1054 | permits the unpickler to import and invoke arbitrary code. Just consider what |
| 1055 | this hand-crafted pickle data stream does when loaded:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1056 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 1057 | >>> import pickle |
| 1058 | >>> pickle.loads(b"cos\nsystem\n(S'echo hello world'\ntR.") |
| 1059 | hello world |
| 1060 | 0 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1061 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 1062 | In this example, the unpickler imports the :func:`os.system` function and then |
| 1063 | apply the string argument "echo hello world". Although this example is |
| 1064 | 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] | 1065 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 1066 | 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] | 1067 | :meth:`Unpickler.find_class`. Unlike its name suggests, |
| 1068 | :meth:`Unpickler.find_class` is called whenever a global (i.e., a class or |
| 1069 | a function) is requested. Thus it is possible to either completely forbid |
| 1070 | globals or restrict them to a safe subset. |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 1071 | |
| 1072 | Here is an example of an unpickler allowing only few safe classes from the |
| 1073 | :mod:`builtins` module to be loaded:: |
| 1074 | |
| 1075 | import builtins |
| 1076 | import io |
| 1077 | import pickle |
| 1078 | |
| 1079 | safe_builtins = { |
| 1080 | 'range', |
| 1081 | 'complex', |
| 1082 | 'set', |
| 1083 | 'frozenset', |
| 1084 | 'slice', |
| 1085 | } |
| 1086 | |
| 1087 | class RestrictedUnpickler(pickle.Unpickler): |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 1088 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 1089 | def find_class(self, module, name): |
| 1090 | # Only allow safe classes from builtins. |
| 1091 | if module == "builtins" and name in safe_builtins: |
| 1092 | return getattr(builtins, name) |
| 1093 | # Forbid everything else. |
| 1094 | raise pickle.UnpicklingError("global '%s.%s' is forbidden" % |
| 1095 | (module, name)) |
| 1096 | |
| 1097 | def restricted_loads(s): |
| 1098 | """Helper function analogous to pickle.loads().""" |
| 1099 | return RestrictedUnpickler(io.BytesIO(s)).load() |
| 1100 | |
| 1101 | A sample usage of our unpickler working has intended:: |
| 1102 | |
| 1103 | >>> restricted_loads(pickle.dumps([1, 2, range(15)])) |
| 1104 | [1, 2, range(0, 15)] |
| 1105 | >>> restricted_loads(b"cos\nsystem\n(S'echo hello world'\ntR.") |
| 1106 | Traceback (most recent call last): |
| 1107 | ... |
| 1108 | pickle.UnpicklingError: global 'os.system' is forbidden |
| 1109 | >>> restricted_loads(b'cbuiltins\neval\n' |
| 1110 | ... b'(S\'getattr(__import__("os"), "system")' |
| 1111 | ... b'("echo hello world")\'\ntR.') |
| 1112 | Traceback (most recent call last): |
| 1113 | ... |
| 1114 | pickle.UnpicklingError: global 'builtins.eval' is forbidden |
| 1115 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 1116 | |
| 1117 | .. XXX Add note about how extension codes could evade our protection |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1118 | mechanism (e.g. cached classes do not invokes find_class()). |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 1119 | |
| 1120 | As our examples shows, you have to be careful with what you allow to be |
| 1121 | unpickled. Therefore if security is a concern, you may want to consider |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 1122 | alternatives such as the marshalling API in :mod:`xmlrpc.client` or |
| 1123 | third-party solutions. |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 1124 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1125 | |
Antoine Pitrou | d4d6055 | 2013-12-07 00:56:59 +0100 | [diff] [blame] | 1126 | Performance |
| 1127 | ----------- |
| 1128 | |
| 1129 | Recent versions of the pickle protocol (from protocol 2 and upwards) feature |
| 1130 | efficient binary encodings for several common features and built-in types. |
| 1131 | Also, the :mod:`pickle` module has a transparent optimizer written in C. |
| 1132 | |
| 1133 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1134 | .. _pickle-example: |
| 1135 | |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 1136 | Examples |
| 1137 | -------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1138 | |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 1139 | For the simplest code, use the :func:`dump` and :func:`load` functions. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1140 | |
| 1141 | import pickle |
| 1142 | |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 1143 | # An arbitrary collection of objects supported by pickle. |
| 1144 | data = { |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 1145 | 'a': [1, 2.0, 3, 4+6j], |
| 1146 | 'b': ("character string", b"byte string"), |
Raymond Hettinger | df1b699 | 2014-11-09 15:56:33 -0800 | [diff] [blame] | 1147 | 'c': {None, True, False} |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 1148 | } |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1149 | |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 1150 | with open('data.pickle', 'wb') as f: |
| 1151 | # Pickle the 'data' dictionary using the highest protocol available. |
| 1152 | pickle.dump(data, f, pickle.HIGHEST_PROTOCOL) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1153 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1154 | |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 1155 | The following example reads the resulting pickled data. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1156 | |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 1157 | import pickle |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1158 | |
Alexandre Vassalotti | bcd1e3a | 2009-01-23 05:28:16 +0000 | [diff] [blame] | 1159 | with open('data.pickle', 'rb') as f: |
| 1160 | # The protocol version used is detected automatically, so we do not |
| 1161 | # have to specify it. |
| 1162 | data = pickle.load(f) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1163 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1164 | |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 1165 | .. XXX: Add examples showing how to optimize pickles for size (like using |
| 1166 | .. pickletools.optimize() or the gzip module). |
| 1167 | |
| 1168 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1169 | .. seealso:: |
| 1170 | |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 1171 | Module :mod:`copyreg` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1172 | Pickle interface constructor registration for extension types. |
| 1173 | |
Alexandre Vassalotti | 9d7665d | 2009-04-03 06:13:29 +0000 | [diff] [blame] | 1174 | Module :mod:`pickletools` |
| 1175 | Tools for working with and analyzing pickled data. |
| 1176 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1177 | Module :mod:`shelve` |
| 1178 | Indexed databases of objects; uses :mod:`pickle`. |
| 1179 | |
| 1180 | Module :mod:`copy` |
| 1181 | Shallow and deep object copying. |
| 1182 | |
| 1183 | Module :mod:`marshal` |
| 1184 | High-performance serialization of built-in types. |
| 1185 | |
| 1186 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1187 | .. rubric:: Footnotes |
| 1188 | |
| 1189 | .. [#] Don't confuse this with the :mod:`marshal` module |
| 1190 | |
Ethan Furman | 2498d9e | 2013-10-18 00:45:40 -0700 | [diff] [blame] | 1191 | .. [#] This is why :keyword:`lambda` functions cannot be pickled: all |
Serhiy Storchaka | 2b57c43 | 2018-12-19 08:09:46 +0200 | [diff] [blame] | 1192 | :keyword:`!lambda` functions share the same name: ``<lambda>``. |
Ethan Furman | 2498d9e | 2013-10-18 00:45:40 -0700 | [diff] [blame] | 1193 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1194 | .. [#] The exception raised will likely be an :exc:`ImportError` or an |
| 1195 | :exc:`AttributeError` but it could be something else. |
| 1196 | |
Alexandre Vassalotti | 73b90a8 | 2008-10-29 23:32:33 +0000 | [diff] [blame] | 1197 | .. [#] The :mod:`copy` module uses this protocol for shallow and deep copying |
| 1198 | operations. |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 1199 | |
Alexandre Vassalotti | d039286 | 2008-10-24 01:32:40 +0000 | [diff] [blame] | 1200 | .. [#] The limitation on alphanumeric characters is due to the fact |
| 1201 | the persistent IDs, in protocol 0, are delimited by the newline |
| 1202 | character. Therefore if any kind of newline characters occurs in |
Alexandre Vassalotti | 5f3b63a | 2008-10-18 20:47:58 +0000 | [diff] [blame] | 1203 | persistent IDs, the resulting pickle will become unreadable. |