blob: 319c195139c638c965605557a0c926ea75a4cfd6 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Module \sectcode{marshal}}
2
3\bimodindex{marshal}
4This module contains functions that can read and write Python
5values in a binary format. The format is specific to Python, but
6independent of machine architecture issues (e.g., you can write a
Guido van Rossum470be141995-03-17 16:07:09 +00007Python value to a file on a PC, transport the file to a Sun, and read
8it back there). Details of the format are undocumented on purpose;
9it may change between Python versions (although it rarely does).%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000010\footnote{The name of this module stems from a bit of terminology used
11by the designers of Modula-3 (amongst others), who use the term
12``marshalling'' for shipping of data around in a self-contained form.
13Strictly speaking, ``to marshal'' means to convert some data from
14internal to external form (in an RPC buffer for instance) and
15``unmarshalling'' for the reverse process.}
16
Guido van Rossum470be141995-03-17 16:07:09 +000017This is not a general ``persistency'' module. For general persistency
18and transfer of Python objects through RPC calls, see the modules
19\code{pickle} and \code{shelve}. The \code{marshal} module exists
20mainly to support reading and writing the ``pseudo-compiled'' code for
21Python modules of \samp{.pyc} files.
22\stmodindex{pickle}
23\stmodindex{shelve}
24\obindex{code}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000025
26Not all Python object types are supported; in general, only objects
27whose value is independent from a particular invocation of Python can
28be written and read by this module. The following types are supported:
29\code{None}, integers, long integers, floating point numbers,
30strings, tuples, lists, dictionaries, and code objects, where it
31should be understood that tuples, lists and dictionaries are only
32supported as long as the values contained therein are themselves
33supported; and recursive lists and dictionaries should not be written
Guido van Rossum470be141995-03-17 16:07:09 +000034(they will cause infinite loops).
35
36{\bf Caveat:} On machines where C's \code{long int} type has more than
3732 bits (such as the DEC Alpha or the HP Precision Architecture), it
38is possible to create plain Python integers that are longer than 32
39bits. Since the current \code{marshal} module uses 32 bits to
40transfer plain Python integers, such values are silently truncated.
41This particularly affects the use of very long integer literals in
42Python modules --- these will be accepted by the parser on such
43machines, but will be silently be truncated when the module is read
44from the \code{.pyc} instead.%
45\footnote{A solution would be to refuse such literals in the parser,
46since they are inherently non-portable. Another solution would be to
47let the \code{marshal} module raise an exception when an integer value
48would be truncated. At least one of these solutions will be
49implemented in a future version.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000050
51There are functions that read/write files as well as functions
52operating on strings.
53
54The module defines these functions:
55
56\renewcommand{\indexsubitem}{(in module marshal)}
Guido van Rossum470be141995-03-17 16:07:09 +000057
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000058\begin{funcdesc}{dump}{value\, file}
59 Write the value on the open file. The value must be a supported
60 type. The file must be an open file object such as
61 \code{sys.stdout} or returned by \code{open()} or
62 \code{posix.popen()}.
63
64 If the value has an unsupported type, garbage is written which cannot
65 be read back by \code{load()}.
66\end{funcdesc}
67
68\begin{funcdesc}{load}{file}
69 Read one value from the open file and return it. If no valid value
70 is read, raise \code{EOFError}, \code{ValueError} or
71 \code{TypeError}. The file must be an open file object.
72\end{funcdesc}
73
74\begin{funcdesc}{dumps}{value}
75 Return the string that would be written to a file by
76 \code{dump(value, file)}. The value must be a supported type.
77\end{funcdesc}
78
79\begin{funcdesc}{loads}{string}
80 Convert the string to a value. If no valid value is found, raise
81 \code{EOFError}, \code{ValueError} or \code{TypeError}. Extra
82 characters in the string are ignored.
83\end{funcdesc}