blob: d65afc200411334700eb18d3a2a0cc13027422ce [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`marshal` --- Internal Python object serialization
2=======================================================
3
4.. module:: marshal
5 :synopsis: Convert Python objects to streams of bytes and back (with different
6 constraints).
7
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04008--------------
Georg Brandl116aa622007-08-15 14:28:22 +00009
10This module contains functions that can read and write Python values in a binary
11format. The format is specific to Python, but independent of machine
12architecture issues (e.g., you can write a Python value to a file on a PC,
13transport the file to a Sun, and read it back there). Details of the format are
14undocumented on purpose; it may change between Python versions (although it
15rarely does). [#]_
16
17.. index::
18 module: pickle
19 module: shelve
Georg Brandl116aa622007-08-15 14:28:22 +000020
21This is not a general "persistence" module. For general persistence and
22transfer of Python objects through RPC calls, see the modules :mod:`pickle` and
23:mod:`shelve`. The :mod:`marshal` module exists mainly to support reading and
24writing the "pseudo-compiled" code for Python modules of :file:`.pyc` files.
25Therefore, the Python maintainers reserve the right to modify the marshal format
26in backward incompatible ways should the need arise. If you're serializing and
Georg Brandl9afde1c2007-11-01 20:32:30 +000027de-serializing Python objects, use the :mod:`pickle` module instead -- the
28performance is comparable, version independence is guaranteed, and pickle
29supports a substantially wider range of objects than marshal.
Georg Brandl116aa622007-08-15 14:28:22 +000030
31.. warning::
32
33 The :mod:`marshal` module is not intended to be secure against erroneous or
34 maliciously constructed data. Never unmarshal data received from an
35 untrusted or unauthenticated source.
36
Tommy Beadlee9b84032016-06-02 19:26:51 -040037.. index:: object; code, code object
38
Georg Brandl116aa622007-08-15 14:28:22 +000039Not all Python object types are supported; in general, only objects whose value
40is independent from a particular invocation of Python can be written and read by
Georg Brandl35ac3a82009-09-03 12:34:10 +000041this module. The following types are supported: booleans, integers, floating
42point numbers, complex numbers, strings, bytes, bytearrays, tuples, lists, sets,
43frozensets, dictionaries, and code objects, where it should be understood that
44tuples, lists, sets, frozensets and dictionaries are only supported as long as
Martin Panter3ee62702016-06-04 04:57:19 +000045the values contained therein are themselves supported. The
Georg Brandl35ac3a82009-09-03 12:34:10 +000046singletons :const:`None`, :const:`Ellipsis` and :exc:`StopIteration` can also be
47marshalled and unmarshalled.
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -070048For format *version* lower than 3, recursive lists, sets and dictionaries cannot
49be written (see below).
Georg Brandl116aa622007-08-15 14:28:22 +000050
Georg Brandl116aa622007-08-15 14:28:22 +000051There are functions that read/write files as well as functions operating on
Serhiy Storchakac611a5b2017-03-12 08:53:22 +020052bytes-like objects.
Georg Brandl116aa622007-08-15 14:28:22 +000053
54The module defines these functions:
55
56
57.. function:: dump(value, file[, version])
58
59 Write the value on the open file. The value must be a supported type. The
Serhiy Storchakac611a5b2017-03-12 08:53:22 +020060 file must be a writeable :term:`binary file`.
Georg Brandl116aa622007-08-15 14:28:22 +000061
62 If the value has (or contains an object that has) an unsupported type, a
63 :exc:`ValueError` exception is raised --- but garbage data will also be written
64 to the file. The object will not be properly read back by :func:`load`.
65
Georg Brandl55ac8f02007-09-01 13:51:09 +000066 The *version* argument indicates the data format that ``dump`` should use
67 (see below).
Georg Brandl116aa622007-08-15 14:28:22 +000068
69
70.. function:: load(file)
71
72 Read one value from the open file and return it. If no valid value is read
73 (e.g. because the data has a different Python version's incompatible marshal
74 format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. The
Serhiy Storchakac611a5b2017-03-12 08:53:22 +020075 file must be a readable :term:`binary file`.
Georg Brandl116aa622007-08-15 14:28:22 +000076
Georg Brandle720c0a2009-04-27 16:20:50 +000077 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +000078
79 If an object containing an unsupported type was marshalled with :func:`dump`,
80 :func:`load` will substitute ``None`` for the unmarshallable type.
81
82
83.. function:: dumps(value[, version])
84
Serhiy Storchakac611a5b2017-03-12 08:53:22 +020085 Return the bytes object that would be written to a file by ``dump(value, file)``. The
Georg Brandl116aa622007-08-15 14:28:22 +000086 value must be a supported type. Raise a :exc:`ValueError` exception if value
87 has (or contains an object that has) an unsupported type.
88
Georg Brandl55ac8f02007-09-01 13:51:09 +000089 The *version* argument indicates the data format that ``dumps`` should use
90 (see below).
Georg Brandl116aa622007-08-15 14:28:22 +000091
92
Serhiy Storchakac611a5b2017-03-12 08:53:22 +020093.. function:: loads(bytes)
Georg Brandl116aa622007-08-15 14:28:22 +000094
Serhiy Storchakac611a5b2017-03-12 08:53:22 +020095 Convert the :term:`bytes-like object` to a value. If no valid value is found, raise
96 :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. Extra bytes in the
97 input are ignored.
Georg Brandl116aa622007-08-15 14:28:22 +000098
99
100In addition, the following constants are defined:
101
102.. data:: version
103
Georg Brandle6bcc912008-05-12 18:05:20 +0000104 Indicates the format that the module uses. Version 0 is the historical
105 format, version 1 shares interned strings and version 2 uses a binary format
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700106 for floating point numbers.
107 Version 3 adds support for object instancing and recursion.
Benjamin Petersonb4610002015-01-04 16:29:48 -0600108 The current version is 4.
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111.. rubric:: Footnotes
112
113.. [#] The name of this module stems from a bit of terminology used by the designers of
114 Modula-3 (amongst others), who use the term "marshalling" for shipping of data
115 around in a self-contained form. Strictly speaking, "to marshal" means to
116 convert some data from internal to external form (in an RPC buffer for instance)
117 and "unmarshalling" for the reverse process.
118