blob: a30dd1e909712f8cf781c02992f34c814da8a101 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{marshal} ---
2 Python object serialization (with different constraints).}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{marshal}
4
Fred Drake295da241998-08-10 19:42:37 +00005\modulesynopsis{Convert Python objects to streams of bytes and back
6(with different constraints).}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Fred Drake0c2af2b1998-03-08 06:28:00 +00008
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00009This module contains functions that can read and write Python
10values in a binary format. The format is specific to Python, but
11independent of machine architecture issues (e.g., you can write a
Guido van Rossum470be141995-03-17 16:07:09 +000012Python value to a file on a PC, transport the file to a Sun, and read
13it back there). Details of the format are undocumented on purpose;
14it may change between Python versions (although it rarely does).%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000015\footnote{The name of this module stems from a bit of terminology used
16by the designers of Modula-3 (amongst others), who use the term
17``marshalling'' for shipping of data around in a self-contained form.
18Strictly speaking, ``to marshal'' means to convert some data from
19internal to external form (in an RPC buffer for instance) and
20``unmarshalling'' for the reverse process.}
21
Guido van Rossum470be141995-03-17 16:07:09 +000022This is not a general ``persistency'' module. For general persistency
23and transfer of Python objects through RPC calls, see the modules
Fred Drake75062981998-02-16 20:40:37 +000024\module{pickle} and \module{shelve}. The \module{marshal} module exists
Guido van Rossum470be141995-03-17 16:07:09 +000025mainly to support reading and writing the ``pseudo-compiled'' code for
Fred Drake75062981998-02-16 20:40:37 +000026Python modules of \file{.pyc} files.
Fred Drake54820dc1997-12-15 21:56:05 +000027\refstmodindex{pickle}
28\refstmodindex{shelve}
Guido van Rossum470be141995-03-17 16:07:09 +000029\obindex{code}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000030
31Not all Python object types are supported; in general, only objects
32whose value is independent from a particular invocation of Python can
33be written and read by this module. The following types are supported:
34\code{None}, integers, long integers, floating point numbers,
35strings, tuples, lists, dictionaries, and code objects, where it
36should be understood that tuples, lists and dictionaries are only
37supported as long as the values contained therein are themselves
38supported; and recursive lists and dictionaries should not be written
Guido van Rossum470be141995-03-17 16:07:09 +000039(they will cause infinite loops).
40
Fred Drakeaf8a0151998-01-14 14:51:31 +000041\strong{Caveat:} On machines where C's \code{long int} type has more than
Guido van Rossum93cf55e1995-09-13 17:35:28 +00004232 bits (such as the DEC Alpha), it
Guido van Rossum470be141995-03-17 16:07:09 +000043is possible to create plain Python integers that are longer than 32
Fred Drake75062981998-02-16 20:40:37 +000044bits. Since the current \module{marshal} module uses 32 bits to
Guido van Rossum470be141995-03-17 16:07:09 +000045transfer plain Python integers, such values are silently truncated.
46This particularly affects the use of very long integer literals in
47Python modules --- these will be accepted by the parser on such
48machines, but will be silently be truncated when the module is read
Fred Drake75062981998-02-16 20:40:37 +000049from the \file{.pyc} instead.%
Guido van Rossum470be141995-03-17 16:07:09 +000050\footnote{A solution would be to refuse such literals in the parser,
51since they are inherently non-portable. Another solution would be to
Fred Drake75062981998-02-16 20:40:37 +000052let the \module{marshal} module raise an exception when an integer
53value would be truncated. At least one of these solutions will be
Guido van Rossum470be141995-03-17 16:07:09 +000054implemented in a future version.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055
56There are functions that read/write files as well as functions
57operating on strings.
58
59The module defines these functions:
60
Fred Drake0c2af2b1998-03-08 06:28:00 +000061\begin{funcdesc}{dump}{value, file}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000062 Write the value on the open file. The value must be a supported
63 type. The file must be an open file object such as
Fred Drake75062981998-02-16 20:40:37 +000064 \code{sys.stdout} or returned by \function{open()} or
65 \function{posix.popen()}.
66
Guido van Rossumbbb1e261996-06-26 20:20:57 +000067 If the value has (or contains an object that has) an unsupported type,
Fred Drake0c2af2b1998-03-08 06:28:00 +000068 a \exception{ValueError} exception is raised --- but garbage data
Fred Drake75062981998-02-16 20:40:37 +000069 will also be written to the file. The object will not be properly
70 read back by \function{load()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000071\end{funcdesc}
72
73\begin{funcdesc}{load}{file}
74 Read one value from the open file and return it. If no valid value
Fred Drake75062981998-02-16 20:40:37 +000075 is read, raise \exception{EOFError}, \exception{ValueError} or
76 \exception{TypeError}. The file must be an open file object.
Guido van Rossumbbb1e261996-06-26 20:20:57 +000077
Fred Drake0c2af2b1998-03-08 06:28:00 +000078 \strong{Warning:} If an object containing an unsupported type was
79 marshalled with \function{dump()}, \function{load()} will substitute
Fred Drake75062981998-02-16 20:40:37 +000080 \code{None} for the unmarshallable type.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000081\end{funcdesc}
82
83\begin{funcdesc}{dumps}{value}
84 Return the string that would be written to a file by
Fred Drake75062981998-02-16 20:40:37 +000085 \code{dump(\var{value}, \var{file})}. The value must be a supported
86 type. Raise a \exception{ValueError} exception if value has (or
87 contains an object that has) an unsupported type.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000088\end{funcdesc}
89
90\begin{funcdesc}{loads}{string}
91 Convert the string to a value. If no valid value is found, raise
Fred Drake75062981998-02-16 20:40:37 +000092 \exception{EOFError}, \exception{ValueError} or
93 \exception{TypeError}. Extra characters in the string are ignored.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000094\end{funcdesc}