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