Debian Codegraph @

cpython27/Doc/library/marshal.rst

raw
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
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
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.
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 .. index:: object; code, code object
38
39 Not all Python object types are supported; in general, only objects whose value
40 is independent from a particular invocation of Python can be written and read by
41 this module. The following types are supported: booleans, integers, long
42 integers, floating point numbers, complex numbers, strings, Unicode objects,
43 tuples, lists, sets, frozensets, dictionaries, and code objects, where it should
44 be understood that tuples, lists, sets, frozensets and dictionaries are only
45 supported as long as the values contained therein are themselves supported; and
46 recursive lists, sets and dictionaries should not be written (they will cause
47 infinite loops). The singletons :const:`None`, :const:`Ellipsis` and
48 :exc:`StopIteration` can also be marshalled and unmarshalled.
49
50 .. warning::
51
52 On machines where C's ``long int`` type has more than 32 bits (such as the
53 DEC Alpha), it is possible to create plain Python integers that are longer
54 than 32 bits. If such an integer is marshaled and read back in on a machine
55 where C's ``long int`` type has only 32 bits, a Python long integer object
56 is returned instead. While of a different type, the numeric value is the
57 same. (This behavior is new in Python 2.2. In earlier versions, all but the
58 least-significant 32 bits of the value were lost, and a warning message was
59 printed.)
60
61 There are functions that read/write files as well as functions operating on
62 strings.
63
64 The module defines these functions:
65
66
67 .. function:: dump(value, file[, version])
68
69 Write the value on the open file. The value must be a supported type. The
70 file must be an open file object such as ``sys.stdout`` or returned by
71 :func:`open` or :func:`os.popen`. It may not be a wrapper such as
72 TemporaryFile on Windows. It must be opened in binary mode (``'wb'``
73 or ``'w+b'``).
74
75 If the value has (or contains an object that has) an unsupported type, a
76 :exc:`ValueError` exception is raised --- but garbage data will also be written
77 to the file. The object will not be properly read back by :func:`load`.
78
79 .. versionadded:: 2.4
80 The *version* argument indicates the data format that ``dump`` should use
81 (see below).
82
83
84 .. function:: load(file)
85
86 Read one value from the open file and return it. If no valid value is read
87 (e.g. because the data has a different Python version's incompatible marshal
88 format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. The
89 file must be an open file object opened in binary mode (``'rb'`` or
90 ``'r+b'``).
91
92 .. note::
93
94 If an object containing an unsupported type was marshalled with :func:`dump`,
95 :func:`load` will substitute ``None`` for the unmarshallable type.
96
97
98 .. function:: dumps(value[, version])
99
100 Return the string that would be written to a file by ``dump(value, file)``. The
101 value must be a supported type. Raise a :exc:`ValueError` exception if value
102 has (or contains an object that has) an unsupported type.
103
104 .. versionadded:: 2.4
105 The *version* argument indicates the data format that ``dumps`` should use
106 (see below).
107
108
109 .. function:: loads(string)
110
111 Convert the string to a value. If no valid value is found, raise
112 :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. Extra characters in the
113 string are ignored.
114
115
116 In addition, the following constants are defined:
117
118 .. data:: version
119
120 Indicates the format that the module uses. Version 0 is the historical format,
121 version 1 (added in Python 2.4) shares interned strings and version 2 (added in
122 Python 2.5) uses a binary format for floating point numbers. The current version
123 is 2.
124
125 .. versionadded:: 2.4
126
127
128 .. rubric:: Footnotes
129
130 .. [#] The name of this module stems from a bit of terminology used by the designers of
131 Modula-3 (amongst others), who use the term "marshalling" for shipping of data
132 around in a self-contained form. Strictly speaking, "to marshal" means to
133 convert some data from internal to external form (in an RPC buffer for instance)
134 and "unmarshalling" for the reverse process.
135