blob: 53ca6688562846abf8f64b6b098b75755c1716e9 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{marshal} ---
Barry Warsaw69b2d752001-11-15 23:55:12 +00002 Internal Python object serialization}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakeffbe6871999-04-22 21:23:22 +00004\declaremodule{builtin}{marshal}
Fred Drake295da241998-08-10 19:42:37 +00005\modulesynopsis{Convert Python objects to streams of bytes and back
Fred Drakeffbe6871999-04-22 21:23:22 +00006 (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;
Fred Drakeea003fc1999-04-05 21:59:15 +000014it may change between Python versions (although it rarely
15does).\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.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000021
Thomas Woutersf8316632000-07-16 19:01:10 +000022This is not a general ``persistence'' module. For general persistence
Guido van Rossum470be141995-03-17 16:07:09 +000023and transfer of Python objects through RPC calls, see the modules
Fred Drakeffbe6871999-04-22 21:23:22 +000024\refmodule{pickle} and \refmodule{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
Barry Warsaw69b2d752001-11-15 23:55:12 +000026Python modules of \file{.pyc} files. Therefore, the Python
27maintainers reserve the right to modify the marshal format in backward
28incompatible ways should the need arise. If you're serializing and
Andrew M. Kuchling76963442003-05-14 16:51:46 +000029de-serializing Python objects, use the \module{pickle} module instead.
Fred Drake54820dc1997-12-15 21:56:05 +000030\refstmodindex{pickle}
31\refstmodindex{shelve}
Guido van Rossum470be141995-03-17 16:07:09 +000032\obindex{code}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033
Andrew M. Kuchling76963442003-05-14 16:51:46 +000034\begin{notice}[warning]
35The \module{marshal} module is not intended to be secure against
36erroneous or maliciously constructed data. Never unmarshal data
37received from an untrusted or unauthenticated source.
38\end{notice}
39
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000040Not all Python object types are supported; in general, only objects
41whose value is independent from a particular invocation of Python can
42be written and read by this module. The following types are supported:
43\code{None}, integers, long integers, floating point numbers,
Fred Drake61098f22000-04-06 14:47:20 +000044strings, Unicode objects, tuples, lists, dictionaries, and code
45objects, where it should be understood that tuples, lists and
46dictionaries are only supported as long as the values contained
47therein are themselves supported; and recursive lists and dictionaries
48should not be written (they will cause infinite loops).
Guido van Rossum470be141995-03-17 16:07:09 +000049
Fred Drakeaf8a0151998-01-14 14:51:31 +000050\strong{Caveat:} On machines where C's \code{long int} type has more than
Tim Petersad2dc3f2001-09-14 20:40:13 +00005132 bits (such as the DEC Alpha), it is possible to create plain Python
52integers that are longer than 32 bits.
53If such an integer is marshaled and read back in on a machine where
54C's \code{long int} type has only 32 bits, a Python long integer object
55is returned instead. While of a different type, the numeric value is
56the same. (This behavior is new in Python 2.2. In earlier versions,
57all but the least-significant 32 bits of the value were lost, and a
58warning message was printed.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000059
60There are functions that read/write files as well as functions
61operating on strings.
62
63The module defines these functions:
64
Fred Drake0c2af2b1998-03-08 06:28:00 +000065\begin{funcdesc}{dump}{value, file}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000066 Write the value on the open file. The value must be a supported
67 type. The file must be an open file object such as
Fred Drake75062981998-02-16 20:40:37 +000068 \code{sys.stdout} or returned by \function{open()} or
Fred Drake38e5d272000-04-03 20:13:55 +000069 \function{posix.popen()}. It must be opened in binary mode
70 (\code{'wb'} or \code{'w+b'}).
Fred Drake75062981998-02-16 20:40:37 +000071
Guido van Rossumbbb1e261996-06-26 20:20:57 +000072 If the value has (or contains an object that has) an unsupported type,
Fred Drake0c2af2b1998-03-08 06:28:00 +000073 a \exception{ValueError} exception is raised --- but garbage data
Fred Drake75062981998-02-16 20:40:37 +000074 will also be written to the file. The object will not be properly
75 read back by \function{load()}.
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000076
77 \versionadded[The \var{version} argument indicates the data
78 format that \code{dumps} should use.]{2.4}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079\end{funcdesc}
80
81\begin{funcdesc}{load}{file}
82 Read one value from the open file and return it. If no valid value
Fred Drake75062981998-02-16 20:40:37 +000083 is read, raise \exception{EOFError}, \exception{ValueError} or
Fred Drake38e5d272000-04-03 20:13:55 +000084 \exception{TypeError}. The file must be an open file object opened
85 in binary mode (\code{'rb'} or \code{'r+b'}).
Guido van Rossumbbb1e261996-06-26 20:20:57 +000086
Fred Drake0aa811c2001-10-20 04:24:09 +000087 \warning{If an object containing an unsupported type was
Fred Drake0c2af2b1998-03-08 06:28:00 +000088 marshalled with \function{dump()}, \function{load()} will substitute
Fred Drake0aa811c2001-10-20 04:24:09 +000089 \code{None} for the unmarshallable type.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000090\end{funcdesc}
91
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000092\begin{funcdesc}{dumps}{value\optional{, version}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000093 Return the string that would be written to a file by
Fred Drake75062981998-02-16 20:40:37 +000094 \code{dump(\var{value}, \var{file})}. The value must be a supported
95 type. Raise a \exception{ValueError} exception if value has (or
96 contains an object that has) an unsupported type.
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000097
98 \versionadded[The \var{version} argument indicates the data
99 format that \code{dumps} should use.]{2.4}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000100\end{funcdesc}
101
102\begin{funcdesc}{loads}{string}
103 Convert the string to a value. If no valid value is found, raise
Fred Drake75062981998-02-16 20:40:37 +0000104 \exception{EOFError}, \exception{ValueError} or
105 \exception{TypeError}. Extra characters in the string are ignored.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000106\end{funcdesc}
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000107
108In addition, the following constants are defined:
109
110\begin{datadesc}{version}
111 Indicates the format that the module uses. Version 0 is the
112 historical format, version 1 (added in Python 2.4) shares
113 interned strings. The current version is 1.
114
115 \versionadded{2.4}
116\end{datadesc}