1 \section{\module{marshal
} ---
2 Internal 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 ``persistence'' module. For general persistence
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. Therefore, the Python
27 maintainers reserve the right to modify the marshal format in backward
28 incompatible ways should the need arise. If you're serializing and
29 de-serializing Python objects, use the
\module{pickle
} module. There
30 may also be unknown security problems with
31 \module{marshal
}\footnote{As opposed to the known security issues in
32 the
\module{pickle
} module!
}.
33 \refstmodindex{pickle
}
34 \refstmodindex{shelve
}
37 Not all Python object types are supported; in general, only objects
38 whose value is independent from a particular invocation of Python can
39 be written and read by this module. The following types are supported:
40 \code{None
}, integers, long integers, floating point numbers,
41 strings, Unicode objects, tuples, lists, dictionaries, and code
42 objects, where it should be understood that tuples, lists and
43 dictionaries are only supported as long as the values contained
44 therein are themselves supported; and recursive lists and dictionaries
45 should not be written (they will cause infinite loops).
47 \strong{Caveat:
} On machines where C's
\code{long int
} type has more than
48 32 bits (such as the DEC Alpha), it is possible to create plain Python
49 integers that are longer than
32 bits.
50 If such an integer is marshaled and read back in on a machine where
51 C's
\code{long int
} type has only
32 bits, a Python long integer object
52 is returned instead. While of a different type, the numeric value is
53 the same. (This behavior is new in Python
2.2. In earlier versions,
54 all but the least-significant
32 bits of the value were lost, and a
55 warning message was printed.)
57 There are functions that read/write files as well as functions
60 The module defines these functions:
62 \begin{funcdesc
}{dump
}{value, file
}
63 Write the value on the open file. The value must be a supported
64 type. The file must be an open file object such as
65 \code{sys.stdout
} or returned by
\function{open()
} or
66 \function{posix.popen()
}. It must be opened in binary mode
67 (
\code{'wb'
} or
\code{'w+b'
}).
69 If the value has (or contains an object that has) an unsupported type,
70 a
\exception{ValueError
} exception is raised --- but garbage data
71 will also be written to the file. The object will not be properly
72 read back by
\function{load()
}.
75 \begin{funcdesc
}{load
}{file
}
76 Read one value from the open file and return it. If no valid value
77 is read, raise
\exception{EOFError
},
\exception{ValueError
} or
78 \exception{TypeError
}. The file must be an open file object opened
79 in binary mode (
\code{'rb'
} or
\code{'r+b'
}).
81 \warning{If an object containing an unsupported type was
82 marshalled with
\function{dump()
},
\function{load()
} will substitute
83 \code{None
} for the unmarshallable type.
}
86 \begin{funcdesc
}{dumps
}{value
}
87 Return the string that would be written to a file by
88 \code{dump(
\var{value
},
\var{file
})
}. The value must be a supported
89 type. Raise a
\exception{ValueError
} exception if value has (or
90 contains an object that has) an unsupported type.
93 \begin{funcdesc
}{loads
}{string
}
94 Convert the string to a value. If no valid value is found, raise
95 \exception{EOFError
},
\exception{ValueError
} or
96 \exception{TypeError
}. Extra characters in the string are ignored.