Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Doc / lib / libmarshal.tex
blobbe2a7176b79f7284fdc6b0da7e2de935e12b7f3d
1 \section{\module{marshal} ---
2 Alternate Python object serialization}
4 \declaremodule{builtin}{marshal}
5 \modulesynopsis{Convert Python objects to streams of bytes and back
6 (with different constraints).}
9 This module contains functions that can read and write Python
10 values in a binary format. The format is specific to Python, but
11 independent of machine architecture issues (e.g., you can write a
12 Python value to a file on a PC, transport the file to a Sun, and read
13 it back there). Details of the format are undocumented on purpose;
14 it may change between Python versions (although it rarely
15 does).\footnote{The name of this module stems from a bit of
16 terminology used by the designers of Modula-3 (amongst others), who
17 use the term ``marshalling'' for shipping of data around in a
18 self-contained form. Strictly speaking, ``to marshal'' means to
19 convert some data from internal to external form (in an RPC buffer for
20 instance) and ``unmarshalling'' for the reverse process.}
22 This is not a general ``persistency'' module. For general persistency
23 and transfer of Python objects through RPC calls, see the modules
24 \refmodule{pickle} and \refmodule{shelve}. The \module{marshal} module exists
25 mainly to support reading and writing the ``pseudo-compiled'' code for
26 Python modules of \file{.pyc} files.
27 \refstmodindex{pickle}
28 \refstmodindex{shelve}
29 \obindex{code}
31 Not all Python object types are supported; in general, only objects
32 whose value is independent from a particular invocation of Python can
33 be written and read by this module. The following types are supported:
34 \code{None}, integers, long integers, floating point numbers,
35 strings, tuples, lists, dictionaries, and code objects, where it
36 should be understood that tuples, lists and dictionaries are only
37 supported as long as the values contained therein are themselves
38 supported; and recursive lists and dictionaries should not be written
39 (they will cause infinite loops).
41 \strong{Caveat:} On machines where C's \code{long int} type has more than
42 32 bits (such as the DEC Alpha), it
43 is possible to create plain Python integers that are longer than 32
44 bits. Since the current \module{marshal} module uses 32 bits to
45 transfer plain Python integers, such values are silently truncated.
46 This particularly affects the use of very long integer literals in
47 Python modules --- these will be accepted by the parser on such
48 machines, but will be silently be truncated when the module is read
49 from the \file{.pyc} instead.\footnote{
50 A solution would be to refuse such literals in the parser,
51 since they are inherently non-portable. Another solution would be to
52 let the \module{marshal} module raise an exception when an integer
53 value would be truncated. At least one of these solutions will be
54 implemented in a future version.}
56 There are functions that read/write files as well as functions
57 operating on strings.
59 The module defines these functions:
61 \begin{funcdesc}{dump}{value, file}
62 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
64 \code{sys.stdout} or returned by \function{open()} or
65 \function{posix.popen()}.
67 If the value has (or contains an object that has) an unsupported type,
68 a \exception{ValueError} exception is raised --- but garbage data
69 will also be written to the file. The object will not be properly
70 read back by \function{load()}.
71 \end{funcdesc}
73 \begin{funcdesc}{load}{file}
74 Read one value from the open file and return it. If no valid value
75 is read, raise \exception{EOFError}, \exception{ValueError} or
76 \exception{TypeError}. The file must be an open file object.
78 \strong{Warning:} If an object containing an unsupported type was
79 marshalled with \function{dump()}, \function{load()} will substitute
80 \code{None} for the unmarshallable type.
81 \end{funcdesc}
83 \begin{funcdesc}{dumps}{value}
84 Return the string that would be written to a file by
85 \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.
88 \end{funcdesc}
90 \begin{funcdesc}{loads}{string}
91 Convert the string to a value. If no valid value is found, raise
92 \exception{EOFError}, \exception{ValueError} or
93 \exception{TypeError}. Extra characters in the string are ignored.
94 \end{funcdesc}