marshal --- 内部 Python 对象序列化


此模块包含一些能以二进制格式来读写 Python 值的函数。 这种格式是 Python 专属的,但是独立于特定的机器架构(即你可以在一台 PC 上写入某个 Python 值,将文件传到一台 Mac 上并在那里读取它)。 这种格式的细节有意不带文档说明;可可能在不同 Python 版本之间发生改变(但这种情况极少发生)。 [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. 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.

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

并非所有 Python 对象类型都受支持;一般来说,只有其值独立于 Python 特定调用的对象才能被此模块写入和读取。支持以下类型:

在 3.4 版本发生变更:

  • 增加了格式版本3,支持序列化递归列表、集合和字典。

  • 增加了格式版本4,支持短字符串的有效表示。

在 3.14 版本发生变更: 增加了格式版本5,支持切片。

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

marshal.dump(value, file, version=version, /, *, allow_code=True)

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

如果值具有(或其包含的对象具有)不受支持的类型,则会引发 ValueError 异常 --- 但还是会向文件写入垃圾数据。 对象将不能使用 load() 正确地重新读取。 代码对象 仅在 allow_code 为真值时受到支持。

version 参数指明 dump 应当使用的数据格式(见下文)。

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

在 3.13 版本发生变更: 增加了 allow_code 形参。

marshal.load(file, /, *, allow_code=True)

从打开的文件读取一个值并返回它。 如果没有读取到有效的值(例如由于数据具有来自不同 Python 版本的不兼容的 marshal 格式),则会引发 EOFError, ValueErrorTypeError代码对象 仅在 allow_code 为真值时受到支持。 文件必须为可读的 binary file

引发一个不带参数的 审计事件 marshal.load

备注

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

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

在 3.13 版本发生变更: 增加了 allow_code 形参。

marshal.dumps(value, version=version, /, *, allow_code=True)

返回将通过 dump(value, file) 写入到文件的字节串对象。 值必须是受支持的类型。 如果值具有(或其包含的对象具有)不受支持的类型则会引发 ValueError 异常。 代码对象 仅在 allow_code 为真值时受到支持。

version 参数指明 dumps 应当使用的数据类型(见下文)。

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

在 3.13 版本发生变更: 增加了 allow_code 形参。

marshal.loads(bytes, /, *, allow_code=True)

bytes-like object 转换为一个值。 如果找不到有效的值,则会引发 EOFError, ValueErrorTypeError代码对象 仅在 allow_code 为真值时受支持。 输入的额外字节串会被忽略。

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

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

在 3.13 版本发生变更: 增加了 allow_code 形参。

此外,还定义了以下常量:

marshal.version

指明该模块使用的格式。版本0是历史上的第一个版本;后续版本添加了新特性。通常,新版本在引入时就会成为默认版本。

版本

可用始于

新的特性

1

Python 2.4

共享互联字符串

2

Python 2.5

浮点数的二进制表示

3

Python 3.4

支持对象实例化和递归

4

Python 3.4

短字符串的有效表示

5

Python 3.14

支持 slice 对象

备注