Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{marshal}} |
| 2 | |
| 3 | \bimodindex{marshal} |
| 4 | This module contains functions that can read and write Python |
| 5 | values in a binary format. The format is specific to Python, but |
| 6 | independent of machine architecture issues (e.g., you can write a |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 7 | Python value to a file on a PC, transport the file to a Sun, and read |
| 8 | it back there). Details of the format are undocumented on purpose; |
| 9 | it may change between Python versions (although it rarely does).% |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 10 | \footnote{The name of this module stems from a bit of terminology used |
| 11 | by the designers of Modula-3 (amongst others), who use the term |
| 12 | ``marshalling'' for shipping of data around in a self-contained form. |
| 13 | Strictly speaking, ``to marshal'' means to convert some data from |
| 14 | internal to external form (in an RPC buffer for instance) and |
| 15 | ``unmarshalling'' for the reverse process.} |
| 16 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 17 | This is not a general ``persistency'' module. For general persistency |
| 18 | and transfer of Python objects through RPC calls, see the modules |
| 19 | \code{pickle} and \code{shelve}. The \code{marshal} module exists |
| 20 | mainly to support reading and writing the ``pseudo-compiled'' code for |
| 21 | Python modules of \samp{.pyc} files. |
| 22 | \stmodindex{pickle} |
| 23 | \stmodindex{shelve} |
| 24 | \obindex{code} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 25 | |
| 26 | Not all Python object types are supported; in general, only objects |
| 27 | whose value is independent from a particular invocation of Python can |
| 28 | be written and read by this module. The following types are supported: |
| 29 | \code{None}, integers, long integers, floating point numbers, |
| 30 | strings, tuples, lists, dictionaries, and code objects, where it |
| 31 | should be understood that tuples, lists and dictionaries are only |
| 32 | supported as long as the values contained therein are themselves |
| 33 | supported; and recursive lists and dictionaries should not be written |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 34 | (they will cause infinite loops). |
| 35 | |
| 36 | {\bf Caveat:} On machines where C's \code{long int} type has more than |
| 37 | 32 bits (such as the DEC Alpha or the HP Precision Architecture), it |
| 38 | is possible to create plain Python integers that are longer than 32 |
| 39 | bits. Since the current \code{marshal} module uses 32 bits to |
| 40 | transfer plain Python integers, such values are silently truncated. |
| 41 | This particularly affects the use of very long integer literals in |
| 42 | Python modules --- these will be accepted by the parser on such |
| 43 | machines, but will be silently be truncated when the module is read |
| 44 | from the \code{.pyc} instead.% |
| 45 | \footnote{A solution would be to refuse such literals in the parser, |
| 46 | since they are inherently non-portable. Another solution would be to |
| 47 | let the \code{marshal} module raise an exception when an integer value |
| 48 | would be truncated. At least one of these solutions will be |
| 49 | implemented in a future version.} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 50 | |
| 51 | There are functions that read/write files as well as functions |
| 52 | operating on strings. |
| 53 | |
| 54 | The module defines these functions: |
| 55 | |
| 56 | \renewcommand{\indexsubitem}{(in module marshal)} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 58 | \begin{funcdesc}{dump}{value\, file} |
| 59 | Write the value on the open file. The value must be a supported |
| 60 | type. The file must be an open file object such as |
| 61 | \code{sys.stdout} or returned by \code{open()} or |
| 62 | \code{posix.popen()}. |
| 63 | |
| 64 | If the value has an unsupported type, garbage is written which cannot |
| 65 | be read back by \code{load()}. |
| 66 | \end{funcdesc} |
| 67 | |
| 68 | \begin{funcdesc}{load}{file} |
| 69 | Read one value from the open file and return it. If no valid value |
| 70 | is read, raise \code{EOFError}, \code{ValueError} or |
| 71 | \code{TypeError}. The file must be an open file object. |
| 72 | \end{funcdesc} |
| 73 | |
| 74 | \begin{funcdesc}{dumps}{value} |
| 75 | Return the string that would be written to a file by |
| 76 | \code{dump(value, file)}. The value must be a supported type. |
| 77 | \end{funcdesc} |
| 78 | |
| 79 | \begin{funcdesc}{loads}{string} |
| 80 | Convert the string to a value. If no valid value is found, raise |
| 81 | \code{EOFError}, \code{ValueError} or \code{TypeError}. Extra |
| 82 | characters in the string are ignored. |
| 83 | \end{funcdesc} |