# `marshal` --- Internal Python object serialization

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). [1]

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. 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.

警告

`marshal` 模块对于错误或恶意构建的数据来说是不安全的。 永远不要 unmarshal 来自不受信任的或未经验证的来源的数据。

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: booleans, integers, floating point numbers, complex numbers, strings, bytes, bytearrays, tuples, lists, sets, frozensets, dictionaries, and code objects, where it should be understood that tuples, lists, sets, frozensets and dictionaries are only supported as long as the values contained therein are themselves supported. The singletons `None`, `Ellipsis` and `StopIteration` can also be marshalled and unmarshalled. For format version lower than 3, recursive lists, sets and dictionaries cannot be written (see below).

有些函数可以读/写文件，还有些函数可以操作字节类对象。

这个模块定义了以下函数：

marshal.dump(value, file[, version])

  向打开的文件写入值。 值必须为受支持的类型。 文件必须为可写的 binary file。

  If the value has (or contains an object that has) an unsupported type, a `ValueError` exception is raised --- but garbage data will also be written to the file. The object will not be properly read back by `load()`.

  version 参数指明 `dump` 应当使用的数据格式（见下文）。

  引发一个 审计事件 `marshal.dumps`，附带参数 `value`, `version`。

marshal.load(file)

  Read one value from the open file and return it. If no valid value is read (e.g. because the data has a different Python version's incompatible marshal format), raise `EOFError`, `ValueError` or `TypeError`. The file must be a readable binary file.

  引发一个 审计事件 `marshal.load`，没有附带参数。

  备注

  如果通过 `dump()` marshal 了一个包含不受支持类型的对象，`load()` 将为不可 marshal 的类型替换 `None`。

  在 3.10 版本发生变更: 使用此调用为每个代码对象引发一个 `code.__new__` 审计事件。 现在它会为整个载入操作引发单个 `marshal.load` 事件。

marshal.dumps(value[, version])

  Return the bytes object that would be written to a file by `dump(value, file)`. The value must be a supported type. Raise a `ValueError` exception if value has (or contains an object that has) an unsupported type.

  version 参数指明 `dumps` 应当使用的数据类型（见下文）。

  引发一个 审计事件 `marshal.dumps`，附带参数 `value`, `version`。

marshal.loads(bytes)

  Convert the bytes-like object to a value. If no valid value is found, raise `EOFError`, `ValueError` or `TypeError`. Extra bytes in the input are ignored.

  引发一个 审计事件 `marshal.loads`，附带参数 `bytes`。

  在 3.10 版本发生变更: 使用此调用为每个代码对象引发一个 `code.__new__` 审计事件。 现在它会为整个载入操作引发单个 `marshal.loads` 事件。

此外，还定义了以下常量：

marshal.version

  指明模块所使用的格式。 第 0 版为历史格式，第 1 版为共享固化的字符串，第 2 版对浮点数使用二进制格式。 第 3 版添加了对于对象实例化和递归的支持。 目前使用的为第 4 版。

备注

[1]

此模块的名称来源于 Modula-3 (及其他语言) 的设计者所使用的术语，他们使用术语 "marshal" 来表示以自包含的形式传输数据。 严格地说，将数据从内部形式转换为外部形式 (例如用于 RPC 缓冲区) 称为 "marshal" 而其逆过程则称为 "unmarshal"。