blob: 0cf69b4ff9f3bcce5981384025cb712fe82ca347 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
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
10This module contains functions that can read and write Python values in a binary
11format. The format is specific to Python, but independent of machine
12architecture issues (e.g., you can write a Python value to a file on a PC,
13transport the file to a Sun, and read it back there). Details of the format are
14undocumented on purpose; it may change between Python versions (although it
15rarely does). [#]_
16
17.. index::
18 module: pickle
19 module: shelve
20 object: code
21
22This is not a general "persistence" module. For general persistence and
23transfer 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
25writing the "pseudo-compiled" code for Python modules of :file:`.pyc` files.
26Therefore, the Python maintainers reserve the right to modify the marshal format
27in backward incompatible ways should the need arise. If you're serializing and
Georg Brandl9afde1c2007-11-01 20:32:30 +000028de-serializing Python objects, use the :mod:`pickle` module instead -- the
29performance is comparable, version independence is guaranteed, and pickle
30supports a substantially wider range of objects than marshal.
Georg Brandl116aa622007-08-15 14:28:22 +000031
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
38Not all Python object types are supported; in general, only objects whose value
39is independent from a particular invocation of Python can be written and read by
40this module. The following types are supported: ``None``, integers, long
Georg Brandl9afde1c2007-11-01 20:32:30 +000041integers, floating point numbers, strings, Unicode objects, tuples, lists, sets,
Georg Brandl116aa622007-08-15 14:28:22 +000042dictionaries, and code objects, where it should be understood that tuples, lists
43and dictionaries are only supported as long as the values contained therein are
44themselves supported; and recursive lists and dictionaries should not be written
45(they will cause infinite loops).
46
Guido van Rossumda27fd22007-08-17 00:24:54 +000047.. warning::
Georg Brandl9afde1c2007-11-01 20:32:30 +000048
49 Some unsupported types such as subclasses of builtins will appear to marshal
50 and unmarshal correctly, but in fact, their type will change and the
51 additional subclass functionality and instance attributes will be lost.
52
53.. warning::
Guido van Rossumda27fd22007-08-17 00:24:54 +000054
55 On machines where C's ``long int`` type has more than 32 bits (such as the
56 DEC Alpha), it is possible to create plain Python integers that are longer
57 than 32 bits. If such an integer is marshaled and read back in on a machine
58 where C's ``long int`` type has only 32 bits, a Python long integer object
59 is returned instead. While of a different type, the numeric value is the
60 same. (This behavior is new in Python 2.2. In earlier versions, all but the
61 least-significant 32 bits of the value were lost, and a warning message was
62 printed.)
Georg Brandl116aa622007-08-15 14:28:22 +000063
64There are functions that read/write files as well as functions operating on
65strings.
66
67The module defines these functions:
68
69
70.. function:: dump(value, file[, version])
71
72 Write the value on the open file. The value must be a supported type. The
73 file must be an open file object such as ``sys.stdout`` or returned by
74 :func:`open` or :func:`os.popen`. It must be opened in binary mode (``'wb'``
75 or ``'w+b'``).
76
77 If the value has (or contains an object that has) an unsupported type, a
78 :exc:`ValueError` exception is raised --- but garbage data will also be written
79 to the file. The object will not be properly read back by :func:`load`.
80
Georg Brandl55ac8f02007-09-01 13:51:09 +000081 The *version* argument indicates the data format that ``dump`` should use
82 (see below).
Georg Brandl116aa622007-08-15 14:28:22 +000083
84
85.. function:: load(file)
86
87 Read one value from the open file and return it. If no valid value is read
88 (e.g. because the data has a different Python version's incompatible marshal
89 format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. The
90 file must be an open file object opened in binary mode (``'rb'`` or
91 ``'r+b'``).
92
93 .. warning::
94
95 If an object containing an unsupported type was marshalled with :func:`dump`,
96 :func:`load` will substitute ``None`` for the unmarshallable type.
97
98
99.. function:: dumps(value[, version])
100
101 Return the string that would be written to a file by ``dump(value, file)``. The
102 value must be a supported type. Raise a :exc:`ValueError` exception if value
103 has (or contains an object that has) an unsupported type.
104
Georg Brandl55ac8f02007-09-01 13:51:09 +0000105 The *version* argument indicates the data format that ``dumps`` should use
106 (see below).
Georg Brandl116aa622007-08-15 14:28:22 +0000107
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
116In 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
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126.. rubric:: Footnotes
127
128.. [#] The name of this module stems from a bit of terminology used by the designers of
129 Modula-3 (amongst others), who use the term "marshalling" for shipping of data
130 around in a self-contained form. Strictly speaking, "to marshal" means to
131 convert some data from internal to external form (in an RPC buffer for instance)
132 and "unmarshalling" for the reverse process.
133