blob: ab7cd0521d1f8571a15aa0d55791c111a947a916 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{marshal}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{builtin}{marshal}
3
4\modulesynopsis{Convert Python objects to streams of bytes and back (with
5different constraints).}
6
Fred Drake0c2af2b1998-03-08 06:28:00 +00007
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008This module contains functions that can read and write Python
9values in a binary format. The format is specific to Python, but
10independent of machine architecture issues (e.g., you can write a
Guido van Rossum470be141995-03-17 16:07:09 +000011Python value to a file on a PC, transport the file to a Sun, and read
12it back there). Details of the format are undocumented on purpose;
13it may change between Python versions (although it rarely does).%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000014\footnote{The name of this module stems from a bit of terminology used
15by the designers of Modula-3 (amongst others), who use the term
16``marshalling'' for shipping of data around in a self-contained form.
17Strictly speaking, ``to marshal'' means to convert some data from
18internal to external form (in an RPC buffer for instance) and
19``unmarshalling'' for the reverse process.}
20
Guido van Rossum470be141995-03-17 16:07:09 +000021This is not a general ``persistency'' module. For general persistency
22and transfer of Python objects through RPC calls, see the modules
Fred Drake75062981998-02-16 20:40:37 +000023\module{pickle} and \module{shelve}. The \module{marshal} module exists
Guido van Rossum470be141995-03-17 16:07:09 +000024mainly to support reading and writing the ``pseudo-compiled'' code for
Fred Drake75062981998-02-16 20:40:37 +000025Python modules of \file{.pyc} files.
Fred Drake54820dc1997-12-15 21:56:05 +000026\refstmodindex{pickle}
27\refstmodindex{shelve}
Guido van Rossum470be141995-03-17 16:07:09 +000028\obindex{code}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000029
30Not all Python object types are supported; in general, only objects
31whose value is independent from a particular invocation of Python can
32be written and read by this module. The following types are supported:
33\code{None}, integers, long integers, floating point numbers,
34strings, tuples, lists, dictionaries, and code objects, where it
35should be understood that tuples, lists and dictionaries are only
36supported as long as the values contained therein are themselves
37supported; and recursive lists and dictionaries should not be written
Guido van Rossum470be141995-03-17 16:07:09 +000038(they will cause infinite loops).
39
Fred Drakeaf8a0151998-01-14 14:51:31 +000040\strong{Caveat:} On machines where C's \code{long int} type has more than
Guido van Rossum93cf55e1995-09-13 17:35:28 +00004132 bits (such as the DEC Alpha), it
Guido van Rossum470be141995-03-17 16:07:09 +000042is possible to create plain Python integers that are longer than 32
Fred Drake75062981998-02-16 20:40:37 +000043bits. Since the current \module{marshal} module uses 32 bits to
Guido van Rossum470be141995-03-17 16:07:09 +000044transfer plain Python integers, such values are silently truncated.
45This particularly affects the use of very long integer literals in
46Python modules --- these will be accepted by the parser on such
47machines, but will be silently be truncated when the module is read
Fred Drake75062981998-02-16 20:40:37 +000048from the \file{.pyc} instead.%
Guido van Rossum470be141995-03-17 16:07:09 +000049\footnote{A solution would be to refuse such literals in the parser,
50since they are inherently non-portable. Another solution would be to
Fred Drake75062981998-02-16 20:40:37 +000051let the \module{marshal} module raise an exception when an integer
52value would be truncated. At least one of these solutions will be
Guido van Rossum470be141995-03-17 16:07:09 +000053implemented in a future version.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054
55There are functions that read/write files as well as functions
56operating on strings.
57
58The module defines these functions:
59
Fred Drake0c2af2b1998-03-08 06:28:00 +000060\begin{funcdesc}{dump}{value, file}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000061 Write the value on the open file. The value must be a supported
62 type. The file must be an open file object such as
Fred Drake75062981998-02-16 20:40:37 +000063 \code{sys.stdout} or returned by \function{open()} or
64 \function{posix.popen()}.
65
Guido van Rossumbbb1e261996-06-26 20:20:57 +000066 If the value has (or contains an object that has) an unsupported type,
Fred Drake0c2af2b1998-03-08 06:28:00 +000067 a \exception{ValueError} exception is raised --- but garbage data
Fred Drake75062981998-02-16 20:40:37 +000068 will also be written to the file. The object will not be properly
69 read back by \function{load()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000070\end{funcdesc}
71
72\begin{funcdesc}{load}{file}
73 Read one value from the open file and return it. If no valid value
Fred Drake75062981998-02-16 20:40:37 +000074 is read, raise \exception{EOFError}, \exception{ValueError} or
75 \exception{TypeError}. The file must be an open file object.
Guido van Rossumbbb1e261996-06-26 20:20:57 +000076
Fred Drake0c2af2b1998-03-08 06:28:00 +000077 \strong{Warning:} If an object containing an unsupported type was
78 marshalled with \function{dump()}, \function{load()} will substitute
Fred Drake75062981998-02-16 20:40:37 +000079 \code{None} for the unmarshallable type.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080\end{funcdesc}
81
82\begin{funcdesc}{dumps}{value}
83 Return the string that would be written to a file by
Fred Drake75062981998-02-16 20:40:37 +000084 \code{dump(\var{value}, \var{file})}. The value must be a supported
85 type. Raise a \exception{ValueError} exception if value has (or
86 contains an object that has) an unsupported type.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000087\end{funcdesc}
88
89\begin{funcdesc}{loads}{string}
90 Convert the string to a value. If no valid value is found, raise
Fred Drake75062981998-02-16 20:40:37 +000091 \exception{EOFError}, \exception{ValueError} or
92 \exception{TypeError}. Extra characters in the string are ignored.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000093\end{funcdesc}