blob: 7e1daf3a0c20fc7b8142e253038c8486942d2bdc [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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
9This module contains functions that can read and write Python values in a binary
10format. The format is specific to Python, but independent of machine
11architecture issues (e.g., you can write a Python value to a file on a PC,
12transport the file to a Sun, and read it back there). Details of the format are
13undocumented on purpose; it may change between Python versions (although it
14rarely does). [#]_
15
16.. index::
17 module: pickle
18 module: shelve
19 object: code
20
21This is not a general "persistence" module. For general persistence and
22transfer 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
24writing the "pseudo-compiled" code for Python modules of :file:`.pyc` files.
25Therefore, the Python maintainers reserve the right to modify the marshal format
26in backward incompatible ways should the need arise. If you're serializing and
Georg Brandl9afde1c2007-11-01 20:32:30 +000027de-serializing Python objects, use the :mod:`pickle` module instead -- the
28performance is comparable, version independence is guaranteed, and pickle
29supports a substantially wider range of objects than marshal.
Georg Brandl116aa622007-08-15 14:28:22 +000030
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
37Not all Python object types are supported; in general, only objects whose value
38is independent from a particular invocation of Python can be written and read by
Georg Brandlba956ae2007-11-29 17:24:34 +000039this module. The following types are supported: ``None``, integers,
Georg Brandlf6945182008-02-01 11:56:49 +000040floating point numbers, strings, bytes, bytearrays, tuples, lists, sets,
Georg Brandl116aa622007-08-15 14:28:22 +000041dictionaries, and code objects, where it should be understood that tuples, lists
42and dictionaries are only supported as long as the values contained therein are
43themselves supported; and recursive lists and dictionaries should not be written
44(they will cause infinite loops).
45
Georg Brandl116aa622007-08-15 14:28:22 +000046There are functions that read/write files as well as functions operating on
47strings.
48
49The module defines these functions:
50
51
52.. function:: dump(value, file[, version])
53
54 Write the value on the open file. The value must be a supported type. The
55 file must be an open file object such as ``sys.stdout`` or returned by
56 :func:`open` or :func:`os.popen`. It must be opened in binary mode (``'wb'``
57 or ``'w+b'``).
58
59 If the value has (or contains an object that has) an unsupported type, a
60 :exc:`ValueError` exception is raised --- but garbage data will also be written
61 to the file. The object will not be properly read back by :func:`load`.
62
Georg Brandl55ac8f02007-09-01 13:51:09 +000063 The *version* argument indicates the data format that ``dump`` should use
64 (see below).
Georg Brandl116aa622007-08-15 14:28:22 +000065
66
67.. function:: load(file)
68
69 Read one value from the open file and return it. If no valid value is read
70 (e.g. because the data has a different Python version's incompatible marshal
71 format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. The
72 file must be an open file object opened in binary mode (``'rb'`` or
73 ``'r+b'``).
74
Georg Brandle720c0a2009-04-27 16:20:50 +000075 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +000076
77 If an object containing an unsupported type was marshalled with :func:`dump`,
78 :func:`load` will substitute ``None`` for the unmarshallable type.
79
80
81.. function:: dumps(value[, version])
82
83 Return the string that would be written to a file by ``dump(value, file)``. The
84 value must be a supported type. Raise a :exc:`ValueError` exception if value
85 has (or contains an object that has) an unsupported type.
86
Georg Brandl55ac8f02007-09-01 13:51:09 +000087 The *version* argument indicates the data format that ``dumps`` should use
88 (see below).
Georg Brandl116aa622007-08-15 14:28:22 +000089
90
91.. function:: loads(string)
92
93 Convert the string to a value. If no valid value is found, raise
94 :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. Extra characters in the
95 string are ignored.
96
97
98In addition, the following constants are defined:
99
100.. data:: version
101
Georg Brandle6bcc912008-05-12 18:05:20 +0000102 Indicates the format that the module uses. Version 0 is the historical
103 format, version 1 shares interned strings and version 2 uses a binary format
104 for floating point numbers. The current version is 2.
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107.. rubric:: Footnotes
108
109.. [#] The name of this module stems from a bit of terminology used by the designers of
110 Modula-3 (amongst others), who use the term "marshalling" for shipping of data
111 around in a self-contained form. Strictly speaking, "to marshal" means to
112 convert some data from internal to external form (in an RPC buffer for instance)
113 and "unmarshalling" for the reverse process.
114