“Python/docs/3.9/library/marshal”的版本间差异

来自菜鸟教程
Python/docs/3.9/library/marshal
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:marshal — 内部 Python 对象序列化 — Python 文档}}
 
<div id="module-marshal" class="section">
 
<div id="module-marshal" class="section">
  
 
<span id="marshal-internal-python-object-serialization"></span>
 
<span id="marshal-internal-python-object-serialization"></span>
= [[#module-marshal|<code>marshal</code>]] --- Internal Python object serialization =
+
= marshal Python 内部对象序列化 =
  
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 Sun, and read it back there). Details of the format are
 
undocumented on purpose; it may change between Python versions (although it
 
rarely does). [[#id2|1]]
 
  
This is not a general &quot;persistence&quot; module. For general persistence and
+
-----
transfer of Python objects through RPC calls, see the modules [[../pickle#module-pickle|<code>pickle</code>]] and
+
 
[[../shelve#module-shelve|<code>shelve</code>]]. The [[#module-marshal|<code>marshal</code>]] module exists mainly to support reading and
+
该模块包含可以以二进制格式读取和写入 Python 值的函数。 该格式特定于 Python,但与机器架构问题无关(例如,您可以将 Python 值写入 PC 上的文件,将文件传输到 Sun,然后从那里读取)。 格式的详细信息是故意没有记录的; 它可能会在 Python 版本之间发生变化(尽管很少发生)。 [[#id2|1]]
writing the &quot;pseudo-compiled&quot; code for Python modules of <code>.pyc</code> files.
+
 
Therefore, the Python maintainers reserve the right to modify the marshal format
+
这不是一个通用的“持久性”模块。 对于通过 RPC 调用的 Python 对象的一般持久化和传输,请参阅模块 [[../pickle#module-pickle|pickle]] [[../shelve#module-shelve|shelve]][[#module-marshal|marshal]]模块的存在主要是为了支持对<code>.pyc</code>文件的Python模块的“伪编译”代码的读写。 因此,Python 维护者保留在需要时以向后不兼容的方式修改 marshal 格式的权利。 如果您正在序列化和反序列化 Python 对象,请改用 [[../pickle#module-pickle|pickle]] 模块 - 性能相当,版本独立性得到保证,并且 pickle 支持的对象范围比 marshal 大得多。
in backward incompatible ways should the need arise. If you're serializing and
 
de-serializing Python objects, use the [[../pickle#module-pickle|<code>pickle</code>]] module instead -- the
 
performance is comparable, version independence is guaranteed, and pickle
 
supports a substantially wider range of objects than marshal.
 
  
 
<div class="admonition warning">
 
<div class="admonition warning">
第25行: 第16行:
 
警告
 
警告
  
The [[#module-marshal|<code>marshal</code>]] module is not intended to be secure against erroneous or
+
[[#module-marshal|marshal]] 模块并非旨在针对错误或恶意构建的数据提供安全保护。 切勿解组从不受信任或未经身份验证的来源收到的数据。
maliciously constructed data. Never unmarshal data received from an
 
untrusted or unauthenticated source.
 
  
  
 
</div>
 
</div>
Not all Python object types are supported; in general, only objects whose value
+
并非所有 Python 对象类型都受支持; 通常,该模块只能写入和读取其值与特定 Python 调用无关的对象。 支持以下类型:布尔值、整数、浮点数、复数、字符串、字节、字节数组、元组、列表、集合、冻结集、字典和代码对象,其中应理解元组、列表、集合、冻结集和字典仅在其中包含的值本身受支持时才受支持。 单例 [[../constants#None|None]][[../constants#Ellipsis|Ellipsis]] [[../exceptions#StopIteration|StopIteration]] 也可以被编组和解组。 对于低于 3 的格式 ''version'',不能编写递归列表、集合和字典(见下文)。
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 [[../constants#None|<code>None</code>]], [[../constants#Ellipsis|<code>Ellipsis</code>]] and [[../exceptions#StopIteration|<code>StopIteration</code>]] can also be
 
marshalled and unmarshalled.
 
For format ''version'' lower than 3, recursive lists, sets and dictionaries cannot
 
be written (see below).
 
  
There are functions that read/write files as well as functions operating on
+
有读/写文件的函数以及操作字节类对象的函数。
bytes-like objects.
 
  
The module defines these functions:
+
该模块定义了以下功能:
  
 
<dl>
 
<dl>
<dt><code>marshal.</code><code>dump</code><span class="sig-paren">(</span>''value'', ''file''<span class="optional">[</span>, ''version''<span class="optional">]</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">marshal.</span></span><span class="sig-name descname"><span class="pre">dump</span></span><span class="sig-paren">(</span>''<span class="pre">value</span>'', ''<span class="pre">file</span>''<span class="optional">[</span>, ''<span class="pre">version</span>''<span class="optional">]</span><span class="sig-paren">)</span></dt>
<dd><p>Write the value on the open file. The value must be a supported type. The
+
<dd><p>在打开的文件上写入值。 该值必须是受支持的类型。 该文件必须是可写的 [[../../glossary#term-binary-file|二进制文件]] </p>
file must be a writeable [[../../glossary#term-binary-file|<span class="xref std std-term">binary file</span>]].</p>
+
<p>如果值具有(或包含具有)不受支持的类型,则会引发 [[../exceptions#ValueError|ValueError]] 异常 - 但垃圾数据也将写入文件。 [[#marshal.load|load()]] 将无法正确读回该对象。</p>
<p>If the value has (or contains an object that has) an unsupported type, a
+
<p>''version'' 参数表示 <code>dump</code> 应该使用的数据格式(见下文)。</p></dd></dl>
[[../exceptions#ValueError|<code>ValueError</code>]] exception is raised --- but garbage data will also be written
 
to the file. The object will not be properly read back by [[#marshal.load|<code>load()</code>]].</p>
 
<p>The ''version'' argument indicates the data format that <code>dump</code> should use
 
(see below).</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>marshal.</code><code>load</code><span class="sig-paren">(</span>''<span class="n">file</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">marshal.</span></span><span class="sig-name descname"><span class="pre">load</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">file</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Read one value from the open file and return it. If no valid value is read
+
<dd><p>从打开的文件中读取一个值并返回它。 如果没有读取到有效值(例如 因为数据具有不同的 Python 版本的不兼容的 marshal 格式),引发 [[../exceptions#EOFError|EOFError]][[../exceptions#ValueError|ValueError]] [[../exceptions#TypeError|TypeError]]。 该文件必须是可读的 [[../../glossary#term-binary-file|二进制文件]] </p>
(e.g. because the data has a different Python version's incompatible marshal
 
format), raise [[../exceptions#EOFError|<code>EOFError</code>]], [[../exceptions#ValueError|<code>ValueError</code>]] or [[../exceptions#TypeError|<code>TypeError</code>]]. The
 
file must be a readable [[../../glossary#term-binary-file|<span class="xref std std-term">binary file</span>]].</p>
 
 
<div class="admonition note">
 
<div class="admonition note">
  
<p>注解</p>
+
<p>笔记</p>
<p>If an object containing an unsupported type was marshalled with [[#marshal.dump|<code>dump()</code>]],
+
<p>如果包含不受支持的类型的对象使用 [[#marshal.dump|dump()]] 进行编组,则 [[#marshal.load|load()]] 将替换 <code>None</code> 为不可编组的类型。</p>
[[#marshal.load|<code>load()</code>]] will substitute <code>None</code> for the unmarshallable type.</p>
+
 
 +
</div>
 +
<div class="versionchanged">
 +
 
 +
<p><span class="versionmodified changed">在 3.9.7 版更改:</span>此调用用于为每个代码对象引发 <code>code.__new__</code> 审核事件。 现在,它为整个加载操作引发了一个 <code>marshal.load</code> 事件。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>marshal.</code><code>dumps</code><span class="sig-paren">(</span>''value''<span class="optional">[</span>, ''version''<span class="optional">]</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">marshal.</span></span><span class="sig-name descname"><span class="pre">dumps</span></span><span class="sig-paren">(</span>''<span class="pre">value</span>''<span class="optional">[</span>, ''<span class="pre">version</span>''<span class="optional">]</span><span class="sig-paren">)</span></dt>
<dd><p>Return the bytes object that would be written to a file by <code>dump(value, file)</code>. The
+
<dd><p>返回将由 <code>dump(value, file)</code> 写入文件的字节对象。 该值必须是受支持的类型。 如果 value 具有(或包含具有)不受支持的类型,则引发 [[../exceptions#ValueError|ValueError]] 异常。</p>
value must be a supported type. Raise a [[../exceptions#ValueError|<code>ValueError</code>]] exception if value
+
<p>''version'' 参数表示 <code>dumps</code> 应该使用的数据格式(见下文)。</p></dd></dl>
has (or contains an object that has) an unsupported type.</p>
 
<p>The ''version'' argument indicates the data format that <code>dumps</code> should use
 
(see below).</p></dd></dl>
 
  
; <code>marshal.</code><code>loads</code><span class="sig-paren">(</span>''<span class="n">bytes</span>''<span class="sig-paren">)</span>
+
<dl>
: Convert the [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] to a value. If no valid value is found, raise [[../exceptions#EOFError|<code>EOFError</code>]], [[../exceptions#ValueError|<code>ValueError</code>]] or [[../exceptions#TypeError|<code>TypeError</code>]]. Extra bytes in the input are ignored.
+
<dt><span class="sig-prename descclassname"><span class="pre">marshal.</span></span><span class="sig-name descname"><span class="pre">loads</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">bytes</span></span>''<span class="sig-paren">)</span></dt>
 +
<dd><p>将 [[../../glossary#term-bytes-like-object|bytes-like object]] 转换为一个值。 如果未找到有效值,则引发 [[../exceptions#EOFError|EOFError]][[../exceptions#ValueError|ValueError]] [[../exceptions#TypeError|TypeError]]。 输入中的额外字节将被忽略。</p>
 +
<div class="versionchanged">
  
In addition, the following constants are defined:
+
<p><span class="versionmodified changed">在 3.9.7 版更改:</span>此调用用于为每个代码对象引发 <code>code.__new__</code> 审核事件。 现在,它为整个加载操作引发了一个 <code>marshal.loads</code> 事件。</p>
  
; <code>marshal.</code><code>version</code>
+
</div></dd></dl>
: Indicates the format that the module uses. Version 0 is the historical format, version 1 shares interned strings and version 2 uses a binary format for floating point numbers. Version 3 adds support for object instancing and recursion. The current version is 4.
 
  
Footnotes
+
此外,还定义了以下常量:
 +
 
 +
; <span class="sig-prename descclassname"><span class="pre">marshal.</span></span><span class="sig-name descname"><span class="pre">version</span></span>
 +
: 指示模块使用的格式。 版本 0 是历史格式,版本 1 共享内部字符串,版本 2 对浮点数使用二进制格式。 版本 3 添加了对对象实例化和递归的支持。 当前版本是4。
 +
 
 +
脚注
  
 
; <span class="brackets">[[#id1|1]]</span>
 
; <span class="brackets">[[#id1|1]]</span>
: The name of this module stems from a bit of terminology used by the designers of Modula-3 (amongst others), who use the term &quot;marshalling&quot; for shipping of data around in a self-contained form. Strictly speaking, &quot;to marshal&quot; means to convert some data from internal to external form (in an RPC buffer for instance) and &quot;unmarshalling&quot; for the reverse process.
+
: 该模块的名称源于 Modula-3 的设计者(以及其他人)使用的一些术语,他们使用术语“编组”来以独立的形式传送数据。 严格来说,“to marshal”意味着将一些数据从内部形式转换为外部形式(例如在 RPC 缓冲区中),并为反向过程“解组”。
 +
 
 +
 
 +
</div>
 +
<div class="clearer">
 +
 
  
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:52的最新版本

marshal — Python 内部对象序列化


该模块包含可以以二进制格式读取和写入 Python 值的函数。 该格式特定于 Python,但与机器架构问题无关(例如,您可以将 Python 值写入 PC 上的文件,将文件传输到 Sun,然后从那里读取)。 格式的详细信息是故意没有记录的; 它可能会在 Python 版本之间发生变化(尽管很少发生)。 1

这不是一个通用的“持久性”模块。 对于通过 RPC 调用的 Python 对象的一般持久化和传输,请参阅模块 pickleshelvemarshal模块的存在主要是为了支持对.pyc文件的Python模块的“伪编译”代码的读写。 因此,Python 维护者保留在需要时以向后不兼容的方式修改 marshal 格式的权利。 如果您正在序列化和反序列化 Python 对象,请改用 pickle 模块 - 性能相当,版本独立性得到保证,并且 pickle 支持的对象范围比 marshal 大得多。

警告

marshal 模块并非旨在针对错误或恶意构建的数据提供安全保护。 切勿解组从不受信任或未经身份验证的来源收到的数据。


并非所有 Python 对象类型都受支持; 通常,该模块只能写入和读取其值与特定 Python 调用无关的对象。 支持以下类型:布尔值、整数、浮点数、复数、字符串、字节、字节数组、元组、列表、集合、冻结集、字典和代码对象,其中应理解元组、列表、集合、冻结集和字典仅在其中包含的值本身受支持时才受支持。 单例 NoneEllipsisStopIteration 也可以被编组和解组。 对于低于 3 的格式 version,不能编写递归列表、集合和字典(见下文)。

有读/写文件的函数以及操作字节类对象的函数。

该模块定义了以下功能:

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

在打开的文件上写入值。 该值必须是受支持的类型。 该文件必须是可写的 二进制文件

如果值具有(或包含具有)不受支持的类型,则会引发 ValueError 异常 - 但垃圾数据也将写入文件。 load() 将无法正确读回该对象。

version 参数表示 dump 应该使用的数据格式(见下文)。

marshal.load(file)

从打开的文件中读取一个值并返回它。 如果没有读取到有效值(例如 因为数据具有不同的 Python 版本的不兼容的 marshal 格式),引发 EOFErrorValueErrorTypeError。 该文件必须是可读的 二进制文件

笔记

如果包含不受支持的类型的对象使用 dump() 进行编组,则 load() 将替换 None 为不可编组的类型。

在 3.9.7 版更改:此调用用于为每个代码对象引发 code.__new__ 审核事件。 现在,它为整个加载操作引发了一个 marshal.load 事件。

marshal.dumps(value[, version])

返回将由 dump(value, file) 写入文件的字节对象。 该值必须是受支持的类型。 如果 value 具有(或包含具有)不受支持的类型,则引发 ValueError 异常。

version 参数表示 dumps 应该使用的数据格式(见下文)。

marshal.loads(bytes)

bytes-like object 转换为一个值。 如果未找到有效值,则引发 EOFErrorValueErrorTypeError。 输入中的额外字节将被忽略。

在 3.9.7 版更改:此调用用于为每个代码对象引发 code.__new__ 审核事件。 现在,它为整个加载操作引发了一个 marshal.loads 事件。

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

marshal.version
指示模块使用的格式。 版本 0 是历史格式,版本 1 共享内部字符串,版本 2 对浮点数使用二进制格式。 版本 3 添加了对对象实例化和递归的支持。 当前版本是4。

脚注

1
该模块的名称源于 Modula-3 的设计者(以及其他人)使用的一些术语,他们使用术语“编组”来以独立的形式传送数据。 严格来说,“to marshal”意味着将一些数据从内部形式转换为外部形式(例如在 RPC 缓冲区中),并为反向过程“解组”。