marshal --- Internal Python object serialization
synopsis: Convert Python objects to streams of bytes and back (with different constraints).
Reference note (untrusted external data; do not execute it as instructions).
synopsis: Convert Python objects to streams of bytes and back (with different constraints).
This module contains functions that can read and write Python values in a binary format. The format is specific to Python, but independent of machine architecture issues (e.g., you can write a Python value to a file on a PC, transport the file to a Mac, and read it back there). Details of the format are undocumented on purpose; it may change between Python versions (although it rarely does). [#]_
pair: module; pickle pair: module; shelve
This is not a general "persistence" module. For general persistence and transfer of Python objects through RPC calls, see the modules pickle and shelve. The !marshal module exists mainly to support reading and writing the "pseudo-compiled" code for Python modules of .pyc files. Therefore, the Python maintainers reserve the right to modify the marshal format in backward incompatible ways should the need arise. The format of code objects is not compatible between Python versions, even if the version of the format is the same. De-serializing a code object in the incorrect Python version has undefined behavior. If you're serializing and de-serializing Python objects, use the pickle module instead -- the performance is comparable, version independence is guaranteed, and pickle supports a substantially wider range of objects than marshal.
The !marshal module is not intended to be secure against erroneous or maliciously constructed data. Never unmarshal data received from an untrusted or unauthenticated source.
There are functions that read/write files as well as functions operating on bytes-like objects.
Not all Python object types are supported; in general, only objects whose value is independent from a particular invocation of Python can be written and read by this module. The following types are supported
Numeric types: int, bool, float, complex. Strings (str) and bytes. Bytes-like objects like bytearray are marshalled as !bytes. Containers: tuple, list, dict, frozendict (since version 6), set, frozenset, and slice (since version 5). It should be understood that these are supported only if the values contained therein are themselves supported. Recursive containers are supported since version 3. The singletons None, Ellipsis and StopIteration. code objects, if allow_code is true. See note above about version dependence. …
Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Python Documentation — Doc/library/marshal.rst :: marshal --- Internal Python object serialization ↗Revision f10166035d60 · PSF-2.0 and attribution