Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`marshal` --- Internal Python object serialization |
| 2 | ======================================================= |
| 3 | |
| 4 | .. module:: marshal |
| 5 | :synopsis: Convert Python objects to streams of bytes and back (with different |
| 6 | constraints). |
| 7 | |
| 8 | |
| 9 | This module contains functions that can read and write Python values in a binary |
| 10 | format. The format is specific to Python, but independent of machine |
| 11 | architecture issues (e.g., you can write a Python value to a file on a PC, |
| 12 | transport the file to a Sun, and read it back there). Details of the format are |
| 13 | undocumented on purpose; it may change between Python versions (although it |
| 14 | rarely does). [#]_ |
| 15 | |
| 16 | .. index:: |
| 17 | module: pickle |
| 18 | module: shelve |
| 19 | object: code |
| 20 | |
| 21 | This is not a general "persistence" module. For general persistence and |
| 22 | transfer of Python objects through RPC calls, see the modules :mod:`pickle` and |
| 23 | :mod:`shelve`. The :mod:`marshal` module exists mainly to support reading and |
| 24 | writing the "pseudo-compiled" code for Python modules of :file:`.pyc` files. |
| 25 | Therefore, the Python maintainers reserve the right to modify the marshal format |
| 26 | in backward incompatible ways should the need arise. If you're serializing and |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 27 | de-serializing Python objects, use the :mod:`pickle` module instead -- the |
| 28 | performance is comparable, version independence is guaranteed, and pickle |
| 29 | supports a substantially wider range of objects than marshal. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | |
| 31 | .. warning:: |
| 32 | |
| 33 | The :mod:`marshal` module is not intended to be secure against erroneous or |
| 34 | maliciously constructed data. Never unmarshal data received from an |
| 35 | untrusted or unauthenticated source. |
| 36 | |
| 37 | Not all Python object types are supported; in general, only objects whose value |
| 38 | is independent from a particular invocation of Python can be written and read by |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 39 | this module. The following types are supported: booleans, integers, floating |
| 40 | point numbers, complex numbers, strings, bytes, bytearrays, tuples, lists, sets, |
| 41 | frozensets, dictionaries, and code objects, where it should be understood that |
| 42 | tuples, lists, sets, frozensets and dictionaries are only supported as long as |
| 43 | the values contained therein are themselves supported; and recursive lists, sets |
| 44 | and dictionaries should not be written (they will cause infinite loops). The |
| 45 | singletons :const:`None`, :const:`Ellipsis` and :exc:`StopIteration` can also be |
| 46 | marshalled and unmarshalled. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | There are functions that read/write files as well as functions operating on |
| 49 | strings. |
| 50 | |
| 51 | The module defines these functions: |
| 52 | |
| 53 | |
| 54 | .. function:: dump(value, file[, version]) |
| 55 | |
| 56 | Write the value on the open file. The value must be a supported type. The |
| 57 | file must be an open file object such as ``sys.stdout`` or returned by |
| 58 | :func:`open` or :func:`os.popen`. It must be opened in binary mode (``'wb'`` |
| 59 | or ``'w+b'``). |
| 60 | |
| 61 | If the value has (or contains an object that has) an unsupported type, a |
| 62 | :exc:`ValueError` exception is raised --- but garbage data will also be written |
| 63 | to the file. The object will not be properly read back by :func:`load`. |
| 64 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 65 | The *version* argument indicates the data format that ``dump`` should use |
| 66 | (see below). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
| 68 | |
| 69 | .. function:: load(file) |
| 70 | |
| 71 | Read one value from the open file and return it. If no valid value is read |
| 72 | (e.g. because the data has a different Python version's incompatible marshal |
| 73 | format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. The |
| 74 | file must be an open file object opened in binary mode (``'rb'`` or |
| 75 | ``'r+b'``). |
| 76 | |
Georg Brandl | e720c0a | 2009-04-27 16:20:50 +0000 | [diff] [blame] | 77 | .. note:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
| 79 | If an object containing an unsupported type was marshalled with :func:`dump`, |
| 80 | :func:`load` will substitute ``None`` for the unmarshallable type. |
| 81 | |
| 82 | |
| 83 | .. function:: dumps(value[, version]) |
| 84 | |
| 85 | Return the string that would be written to a file by ``dump(value, file)``. The |
| 86 | value must be a supported type. Raise a :exc:`ValueError` exception if value |
| 87 | has (or contains an object that has) an unsupported type. |
| 88 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 89 | The *version* argument indicates the data format that ``dumps`` should use |
| 90 | (see below). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | .. function:: loads(string) |
| 94 | |
| 95 | Convert the string to a value. If no valid value is found, raise |
| 96 | :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. Extra characters in the |
| 97 | string are ignored. |
| 98 | |
| 99 | |
| 100 | In addition, the following constants are defined: |
| 101 | |
| 102 | .. data:: version |
| 103 | |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 104 | Indicates the format that the module uses. Version 0 is the historical |
| 105 | format, version 1 shares interned strings and version 2 uses a binary format |
| 106 | for floating point numbers. The current version is 2. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 | |
| 109 | .. rubric:: Footnotes |
| 110 | |
| 111 | .. [#] The name of this module stems from a bit of terminology used by the designers of |
| 112 | Modula-3 (amongst others), who use the term "marshalling" for shipping of data |
| 113 | around in a self-contained form. Strictly speaking, "to marshal" means to |
| 114 | convert some data from internal to external form (in an RPC buffer for instance) |
| 115 | and "unmarshalling" for the reverse process. |
| 116 | |