mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 23:14:06 +08:00
1 line
12 KiB
HTML
1 line
12 KiB
HTML
<div class="body" role="main"><div class="section" id="module-marshal"><h1><span class="yiyi-st" id="yiyi-10">12.4. <a class="reference internal" href="#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code class="xref py py-mod docutils literal"><span class="pre">marshal</span></code></a> — Python 内部对象序列化</span></h1><p><span class="yiyi-st" id="yiyi-11">此模块包含可以用二进制格式读取和写入Python值的函数。</span><span class="yiyi-st" id="yiyi-12">该格式特定于Python,但不依赖于机器架构问题(例如,你可以将Python值写入PC上的文件,将文件传输到Sun,然后读回它)。</span><span class="yiyi-st" id="yiyi-13">格式的细节有意没写成文档;它可能会在Python版本之间变化(虽然很少这样做)。</span><span class="yiyi-st" id="yiyi-14"><a class="footnote-reference" href="#id2" id="id1">[1]</a></span></p><p id="index-0"><span class="yiyi-st" id="yiyi-15">这不是一个通用的“持久性”模块。</span><span class="yiyi-st" id="yiyi-16">对于通过RPC调用的通用的 Python 对象的持久性和传输,请参见模块<a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal"><span class="pre">pickle</span></code></a>和<a class="reference internal" href="shelve.html#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal"><span class="pre">shelve</span></code></a>。</span><span class="yiyi-st" id="yiyi-17"><a class="reference internal" href="#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code class="xref py py-mod docutils literal"><span class="pre">marshal</span></code></a>模块主要用于支持读取和写入Python模块的<code class="file docutils literal"><span class="pre">.pyc</span></code>文件的“伪编译”代码。</span><span class="yiyi-st" id="yiyi-18">因此,Python维护者保留在需要时以向后不兼容的方式修改元组格式的权利。</span><span class="yiyi-st" id="yiyi-19">如果你要序列化和反序列化Python对象,使用<a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal"><span class="pre">pickle</span></code></a>模块 - 性能是可比的,版本独立性得到保证,pickle支持比marshal更宽的对象范围。</span></p><div class="admonition warning"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-20">警告</span></p><p class="last"><span class="yiyi-st" id="yiyi-21"><a class="reference internal" href="#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code class="xref py py-mod docutils literal"><span class="pre">marshal</span></code></a>模块并非旨在防止错误或恶意构造的数据。</span><span class="yiyi-st" id="yiyi-22">切勿对从不受信任或未经身份验证的来源接收的数据进行解组。</span></p></div><p id="index-1"><span class="yiyi-st" id="yiyi-23">不是所有的Python对象类型都支持;一般来说,只有其值独立于Python的特定调用的对象才能被该模块写入和读取。</span><span class="yiyi-st" id="yiyi-24">支持以下类型:布尔,整数,浮点数,复数,字符串,字节,字节数,元组,列表,集合,冻结,字典和代码对象,其中应当理解元组,列表,集,并且只支持字典,只要其中包含的值本身被支持。</span><span class="yiyi-st" id="yiyi-25">单例<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a>,<a class="reference internal" href="constants.html#Ellipsis" title="Ellipsis"><code class="xref py py-const docutils literal"><span class="pre">Ellipsis</span></code></a>和<a class="reference internal" href="exceptions.html#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal"><span class="pre">StopIteration</span></code></a>也可以编组和取消组合。</span><span class="yiyi-st" id="yiyi-26">对于<em>版本</em>低于3的格式,不能写入递归列表,集合和字典(见下文)。</span></p><p><span class="yiyi-st" id="yiyi-27">有一些函数读/写文件以及对字符串操作的函数。</span></p><p><span class="yiyi-st" id="yiyi-28">模块定义了这些功能:</span></p><dl class="function"><dt id="marshal.dump"><span class="yiyi-st" id="yiyi-29"> <code class="descclassname">marshal.</code><code class="descname">dump</code><span class="sig-paren">(</span><em>value</em>, <em>file</em><span class="optional">[</span>, <em>version</em><span class="optional">]</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-30">在打开的文件上写入值。</span><span class="yiyi-st" id="yiyi-31">该值必须是受支持的类型。</span><span class="yiyi-st" id="yiyi-32">文件必须是打开的文件对象,例如<code class="docutils literal"><span class="pre">sys.stdout</span></code>或由<a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a>或<a class="reference internal" href="os.html#os.popen" title="os.popen"><code class="xref py py-func docutils literal"><span class="pre">os.popen()</span></code></a>返回。</span><span class="yiyi-st" id="yiyi-33">它必须以二进制模式打开(<code class="docutils literal"><span class="pre">'wb'</span></code>或<code class="docutils literal"><span class="pre">'w+b'</span></code>)。</span></p><p><span class="yiyi-st" id="yiyi-34">如果值具有(或包含具有)不受支持类型的对象,则会引发<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a>异常 - 但垃圾数据也将写入文件。</span><span class="yiyi-st" id="yiyi-35">该对象将不会被<a class="reference internal" href="#marshal.load" title="marshal.load"><code class="xref py py-func docutils literal"><span class="pre">load()</span></code></a>正确读取。</span></p><p><span class="yiyi-st" id="yiyi-36"><em>版本</em>参数表示<code class="docutils literal"><span class="pre">dump</span></code>应使用的数据格式(见下文)。</span></p></dd></dl><dl class="function"><dt id="marshal.load"><span class="yiyi-st" id="yiyi-37"> <code class="descclassname">marshal.</code><code class="descname">load</code><span class="sig-paren">(</span><em>file</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-38">从打开的文件中读取一个值并返回。</span><span class="yiyi-st" id="yiyi-39">如果没有读取有效值(例如,</span><span class="yiyi-st" id="yiyi-40">因为数据具有不同的Python版本不兼容的封送格式),引发<a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal"><span class="pre">EOFError</span></code></a>,<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a>或<a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a>。</span><span class="yiyi-st" id="yiyi-41">该文件必须是以二进制模式打开的打开文件对象(<code class="docutils literal"><span class="pre">'rb'</span></code>或<code class="docutils literal"><span class="pre">'r+b'</span></code>)。</span></p><div class="admonition note"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-42">注意</span></p><p class="last"><span class="yiyi-st" id="yiyi-43">如果包含不支持类型的对象与<a class="reference internal" href="#marshal.dump" title="marshal.dump"><code class="xref py py-func docutils literal"><span class="pre">dump()</span></code></a>编组,<a class="reference internal" href="#marshal.load" title="marshal.load"><code class="xref py py-func docutils literal"><span class="pre">load()</span></code></a>将替换<code class="docutils literal"><span class="pre">None</span></code></span></p></div></dd></dl><dl class="function"><dt id="marshal.dumps"><span class="yiyi-st" id="yiyi-44"> <code class="descclassname">marshal.</code><code class="descname">dumps</code><span class="sig-paren">(</span><em>value</em><span class="optional">[</span>, <em>version</em><span class="optional">]</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-45">返回<code class="docutils literal"><span class="pre">dump(value,</span> <span class="pre">文件)</span></code>将写入文件的字符串。</span><span class="yiyi-st" id="yiyi-46">该值必须是受支持的类型。</span><span class="yiyi-st" id="yiyi-47">如果值具有(或包含具有)不受支持类型的对象,则引发<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a>异常。</span></p><p><span class="yiyi-st" id="yiyi-48"><em>版本</em>参数表示<code class="docutils literal"><span class="pre">dumps</span></code>应使用的数据格式(见下文)。</span></p></dd></dl><dl class="function"><dt id="marshal.loads"><span class="yiyi-st" id="yiyi-49"> <code class="descclassname">marshal.</code><code class="descname">loads</code><span class="sig-paren">(</span><em>string</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-50">将字符串转换为值。</span><span class="yiyi-st" id="yiyi-51">如果没有找到有效值,引发<a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal"><span class="pre">EOFError</span></code></a>,<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a>或<a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a>。</span><span class="yiyi-st" id="yiyi-52">字符串中的额外字符将被忽略。</span></p></dd></dl><p><span class="yiyi-st" id="yiyi-53">此外,还定义了以下常数:</span></p><dl class="data"><dt id="marshal.version"><span class="yiyi-st" id="yiyi-54"> <code class="descclassname">marshal.</code><code class="descname">version</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-55">表示模块使用的格式。</span><span class="yiyi-st" id="yiyi-56">版本0是历史格式,版本1共享内部字符串,版本2使用二进制格式的浮点数。</span><span class="yiyi-st" id="yiyi-57">版本3增加了对对象实例化和递归的支持。</span><span class="yiyi-st" id="yiyi-58">当前版本为4。</span></p></dd></dl><p class="rubric"><span class="yiyi-st" id="yiyi-59">脚注</span></p><table class="docutils footnote" frame="void" id="id2" rules="none"><tbody valign="top"><tr><td class="label"><span class="yiyi-st" id="yiyi-60"><a class="fn-backref" href="#id1">[1]</a></span></td><td><span class="yiyi-st" id="yiyi-61">这个模块的名字来源于Modula-3的设计者使用的一些术语,他们使用术语“编组”以自包含形式传送数据。</span><span class="yiyi-st" id="yiyi-62">严格来说,“to marshal”意味着将一些数据从内部转换为外部形式(在实例的RPC缓冲区中),“反编译”用于反向过程。</span></td></tr></tbody></table></div></div> |