rest25/library/marshal.rst => rest262/library/marshal.rst
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
n28-de-serializing Python objects, use the :mod:`pickle` module instead.
n28+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.
29
30.. warning::
31
32   The :mod:`marshal` module is not intended to be secure against erroneous or
n33-   maliciously constructed data.  Never unmarshal data received from an untrusted
n35+   maliciously constructed data.  Never unmarshal data received from an
34-   or unauthenticated source.
36+   untrusted or unauthenticated source.
35
36Not all Python object types are supported; in general, only objects whose value
37is independent from a particular invocation of Python can be written and read by
38this module.  The following types are supported: ``None``, integers, long
n39-integers, floating point numbers, strings, Unicode objects, tuples, lists,
n41+integers, floating point numbers, strings, Unicode objects, tuples, lists, sets,
40dictionaries, and code objects, where it should be understood that tuples, lists
41and dictionaries are only supported as long as the values contained therein are
42themselves supported; and recursive lists and dictionaries should not be written
43(they will cause infinite loops).
44
n47+.. warning::
48+ 
45-**Caveat:** On machines where C's ``long int`` type has more than 32 bits (such
49+   On machines where C's ``long int`` type has more than 32 bits (such as the
46-as the DEC Alpha), it is possible to create plain Python integers that are
50+   DEC Alpha), it is possible to create plain Python integers that are longer
47-longer than 32 bits. If such an integer is marshaled and read back in on a
51+   than 32 bits. If such an integer is marshaled and read back in on a machine
48-machine where C's ``long int`` type has only 32 bits, a Python long integer
52+   where C's ``long int`` type has only 32 bits, a Python long integer object
49-object is returned instead.  While of a different type, the numeric value is the
53+   is returned instead.  While of a different type, the numeric value is the
50-same.  (This behavior is new in Python 2.2.  In earlier versions, all but the
54+   same.  (This behavior is new in Python 2.2.  In earlier versions, all but the
51-least-significant 32 bits of the value were lost, and a warning message was
55+   least-significant 32 bits of the value were lost, and a warning message was
52-printed.)
56+   printed.)
53
54There are functions that read/write files as well as functions operating on
55strings.
56
57The module defines these functions:
58
59
60.. function:: dump(value, file[, version])
61
n62-   Write the value on the open file.  The value must be a supported type.  The file
n66+   Write the value on the open file.  The value must be a supported type.  The
63-   must be an open file object such as ``sys.stdout`` or returned by :func:`open`
67+   file must be an open file object such as ``sys.stdout`` or returned by
64-   or :func:`posix.popen`.  It must be opened in binary mode (``'wb'`` or
68+   :func:`open` or :func:`os.popen`.  It must be opened in binary mode (``'wb'``
65-   ``'w+b'``).
69+   or ``'w+b'``).
66
67   If the value has (or contains an object that has) an unsupported type, a
68   :exc:`ValueError` exception is raised --- but garbage data will also be written
69   to the file.  The object will not be properly read back by :func:`load`.
70
71   .. versionadded:: 2.4
n72-      The *version* argument indicates the data format that ``dump`` should use (see
n76+      The *version* argument indicates the data format that ``dump`` should use
73-      below).
77+      (see below).
74
75
76.. function:: load(file)
77
n78-   Read one value from the open file and return it.  If no valid value is read,
n82+   Read one value from the open file and return it.  If no valid value is read
83+   (e.g. because the data has a different Python version's incompatible marshal
79-   raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`.  The file must be
84+   format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`.  The
80-   an open file object opened in binary mode (``'rb'`` or ``'r+b'``).
85+   file must be an open file object opened in binary mode (``'rb'`` or
86+   ``'r+b'``).
81
82   .. warning::
83
84      If an object containing an unsupported type was marshalled with :func:`dump`,
85      :func:`load` will substitute ``None`` for the unmarshallable type.
86
87
88.. function:: dumps(value[, version])
89
90   Return the string that would be written to a file by ``dump(value, file)``.  The
91   value must be a supported type.  Raise a :exc:`ValueError` exception if value
92   has (or contains an object that has) an unsupported type.
93
94   .. versionadded:: 2.4
n95-      The *version* argument indicates the data format that ``dumps`` should use (see
n101+      The *version* argument indicates the data format that ``dumps`` should use
96-      below).
102+      (see below).
97
98
99.. function:: loads(string)
100
101   Convert the string to a value.  If no valid value is found, raise
102   :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`.  Extra characters in the
103   string are ignored.
104
n111+ 
105In addition, the following constants are defined:
n106- 
107
108.. data:: version
109
110   Indicates the format that the module uses. Version 0 is the historical format,
111   version 1 (added in Python 2.4) shares interned strings and version 2 (added in
112   Python 2.5) uses a binary format for floating point numbers. The current version
113   is 2.
114
115   .. versionadded:: 2.4
116
t123+ 
117.. rubric:: Footnotes
118
119.. [#] The name of this module stems from a bit of terminology used by the designers of
120   Modula-3 (amongst others), who use the term "marshalling" for shipping of data
121   around in a self-contained form. Strictly speaking, "to marshal" means to
122   convert some data from internal to external form (in an RPC buffer for instance)
123   and "unmarshalling" for the reverse process.
124
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op