blob: 58becdb3366bb39a085c91a2b0b354087f1b41ec [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
Guido van Rossum93cf55e1995-09-13 17:35:28 +00003732 bits (such as the DEC Alpha), it
Guido van Rossum470be141995-03-17 16:07:09 +000038is 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
Guido van Rossumbbb1e261996-06-26 20:20:57 +000064 If the value has (or contains an object that has) an unsupported type,
65 a \code{ValueError} exception is raised -- but garbage data will also
66 be written to the file. The object will not be properly read back by
67 \code{load()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000068\end{funcdesc}
69
70\begin{funcdesc}{load}{file}
71 Read one value from the open file and return it. If no valid value
72 is read, raise \code{EOFError}, \code{ValueError} or
73 \code{TypeError}. The file must be an open file object.
Guido van Rossumbbb1e261996-06-26 20:20:57 +000074
75 Warning: If an object containing an unsupported type was marshalled
76 with \code{dump()}, \code{load()} will substitute \code{None} for the
77 unmarshallable type.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000078\end{funcdesc}
79
80\begin{funcdesc}{dumps}{value}
81 Return the string that would be written to a file by
82 \code{dump(value, file)}. The value must be a supported type.
Guido van Rossumbbb1e261996-06-26 20:20:57 +000083 Raise a \code{ValueError} exception if value has (or contains an
84 object that has) an unsupported type.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000085\end{funcdesc}
86
87\begin{funcdesc}{loads}{string}
88 Convert the string to a value. If no valid value is found, raise
89 \code{EOFError}, \code{ValueError} or \code{TypeError}. Extra
90 characters in the string are ignored.
91\end{funcdesc}