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


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

警告

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 (if allow_code is true), 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=version, /, *, allow_code=True)

向打开的文件写入值。 值必须为受支持的类型。 文件必须为可写的 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(). Code objects are only supported if allow_code is true.

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

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

在 3.13 版本发生变更: Added the allow_code parameter.

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

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. Code objects are only supported if allow_code is true. The file must be a readable binary file.

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

备注

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

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

在 3.13 版本发生变更: Added the allow_code parameter.

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

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. Code objects are only supported if allow_code is true.

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

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

在 3.13 版本发生变更: Added the allow_code parameter.

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

Convert the bytes-like object to a value. If no valid value is found, raise EOFError, ValueError or TypeError. Code objects are only supported if allow_code is true. Extra bytes in the input are ignored.

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

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

在 3.13 版本发生变更: Added the allow_code parameter.

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

marshal.version

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

备注