diff options
Diffstat (limited to 'devdocs/python~3.12/library%2Fmarshal.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Fmarshal.html | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fmarshal.html b/devdocs/python~3.12/library%2Fmarshal.html new file mode 100644 index 00000000..ba2ce477 --- /dev/null +++ b/devdocs/python~3.12/library%2Fmarshal.html @@ -0,0 +1,23 @@ + <span id="marshal-internal-python-object-serialization"></span><h1>marshal — Internal Python object serialization</h1> <p>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). <a class="footnote-reference brackets" href="#id2" id="id1">1</a></p> <p id="index-0">This is not a general “persistence” module. For general persistence and transfer of Python objects through RPC calls, see the modules <a class="reference internal" href="pickle#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code>pickle</code></a> and <a class="reference internal" href="shelve#module-shelve" title="shelve: Python object persistence."><code>shelve</code></a>. The <a class="reference internal" href="#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code>marshal</code></a> module exists mainly to support reading and writing the “pseudo-compiled” code for Python modules of <code>.pyc</code> 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 <a class="reference internal" href="pickle#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code>pickle</code></a> module instead – the performance is comparable, version independence is guaranteed, and pickle supports a substantially wider range of objects than marshal.</p> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>The <a class="reference internal" href="#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code>marshal</code></a> module is not intended to be secure against erroneous or maliciously constructed data. Never unmarshal data received from an untrusted or unauthenticated source.</p> </div> <p id="index-1">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 <a class="reference internal" href="constants#None" title="None"><code>None</code></a>, <a class="reference internal" href="constants#Ellipsis" title="Ellipsis"><code>Ellipsis</code></a> and <a class="reference internal" href="exceptions#StopIteration" title="StopIteration"><code>StopIteration</code></a> can also be marshalled and unmarshalled. For format <em>version</em> lower than 3, recursive lists, sets and dictionaries cannot be written (see below).</p> <p>There are functions that read/write files as well as functions operating on bytes-like objects.</p> <p>The module defines these functions:</p> <dl class="py function"> <dt class="sig sig-object py" id="marshal.dump"> +<code>marshal.dump(value, file[, version])</code> </dt> <dd> +<p>Write the value on the open file. The value must be a supported type. The file must be a writeable <a class="reference internal" href="../glossary#term-binary-file"><span class="xref std std-term">binary file</span></a>.</p> <p>If the value has (or contains an object that has) an unsupported type, a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> exception is raised — but garbage data will also be written to the file. The object will not be properly read back by <a class="reference internal" href="#marshal.load" title="marshal.load"><code>load()</code></a>.</p> <p>The <em>version</em> argument indicates the data format that <code>dump</code> should use (see below).</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>marshal.dumps</code> with arguments <code>value</code>, <code>version</code>.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="marshal.load"> +<code>marshal.load(file)</code> </dt> <dd> +<p>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 <a class="reference internal" href="exceptions#EOFError" title="EOFError"><code>EOFError</code></a>, <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> or <a class="reference internal" href="exceptions#TypeError" title="TypeError"><code>TypeError</code></a>. The file must be a readable <a class="reference internal" href="../glossary#term-binary-file"><span class="xref std std-term">binary file</span></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>marshal.load</code> with no arguments.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>If an object containing an unsupported type was marshalled with <a class="reference internal" href="#marshal.dump" title="marshal.dump"><code>dump()</code></a>, <a class="reference internal" href="#marshal.load" title="marshal.load"><code>load()</code></a> will substitute <code>None</code> for the unmarshallable type.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>This call used to raise a <code>code.__new__</code> audit event for each code object. Now it raises a single <code>marshal.load</code> event for the entire load operation.</p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="marshal.dumps"> +<code>marshal.dumps(value[, version])</code> </dt> <dd> +<p>Return the bytes object that would be written to a file by <code>dump(value, file)</code>. The value must be a supported type. Raise a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> exception if value has (or contains an object that has) an unsupported type.</p> <p>The <em>version</em> argument indicates the data format that <code>dumps</code> should use (see below).</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>marshal.dumps</code> with arguments <code>value</code>, <code>version</code>.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="marshal.loads"> +<code>marshal.loads(bytes)</code> </dt> <dd> +<p>Convert the <a class="reference internal" href="../glossary#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> to a value. If no valid value is found, raise <a class="reference internal" href="exceptions#EOFError" title="EOFError"><code>EOFError</code></a>, <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> or <a class="reference internal" href="exceptions#TypeError" title="TypeError"><code>TypeError</code></a>. Extra bytes in the input are ignored.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>marshal.loads</code> with argument <code>bytes</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>This call used to raise a <code>code.__new__</code> audit event for each code object. Now it raises a single <code>marshal.loads</code> event for the entire load operation.</p> </div> </dd> +</dl> <p>In addition, the following constants are defined:</p> <dl class="py data"> <dt class="sig sig-object py" id="marshal.version"> +<code>marshal.version</code> </dt> <dd> +<p>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.</p> </dd> +</dl> <h4 class="rubric">Footnotes</h4> <dl class="footnote brackets"> <dt class="label" id="id2"> +<code>1</code> </dt> <dd> +<p>The name of this module stems from a bit of terminology used by the designers of Modula-3 (amongst others), who use the term “marshalling” for shipping of data around in a self-contained form. Strictly speaking, “to marshal” means to convert some data from internal to external form (in an RPC buffer for instance) and “unmarshalling” for the reverse process.</p> </dd> </dl> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/marshal.html" class="_attribution-link">https://docs.python.org/3.12/library/marshal.html</a> + </p> +</div> |
