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