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