Sophie

Sophie

distrib > Mageia > 6 > armv5tl > by-pkgid > a600cd26dfe6bfd8c11f12bce5cb0eee > files > 820

python3-docs-3.5.3-1.1.mga6.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>12.4. marshal — Internal Python object serialization &mdash; Python 3.5.3 documentation</title>
    
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.5.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.5.3 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 3.5.3 documentation" href="../contents.html" />
    <link rel="up" title="12. Data Persistence" href="persistence.html" />
    <link rel="next" title="12.5. dbm — Interfaces to Unix “databases”" href="dbm.html" />
    <link rel="prev" title="12.3. shelve — Python object persistence" href="shelve.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/version_switch.js"></script>
    
    
 

  </head>
  <body role="document">  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="dbm.html" title="12.5. dbm — Interfaces to Unix “databases”"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="shelve.html" title="12.3. shelve — Python object persistence"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.5.3</span>
          <a href="../index.html">Documentation </a> &raquo;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li class="nav-item nav-item-2"><a href="persistence.html" accesskey="U">12. Data Persistence</a> &raquo;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-marshal">
<span id="marshal-internal-python-object-serialization"></span><h1>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> &#8212; Internal Python object serialization<a class="headerlink" href="#module-marshal" title="Permalink to this headline">¶</a></h1>
<hr class="docutils" />
<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" href="#id2" id="id1">[1]</a></p>
<p id="index-0">This is not a general &#8220;persistence&#8221; module.  For general persistence and
transfer of Python objects through RPC calls, see the modules <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> and
<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>.  The <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> module exists mainly to support reading and
writing the &#8220;pseudo-compiled&#8221; code for Python modules of <code class="file docutils literal"><span class="pre">.pyc</span></code> files.
Therefore, the Python maintainers reserve the right to modify the marshal format
in backward incompatible ways should the need arise.  If you&#8217;re serializing and
de-serializing Python objects, use the <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> module instead &#8211; 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="first admonition-title">Warning</p>
<p class="last">The <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> 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.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> and <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> 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
strings.</p>
<p>The module defines these functions:</p>
<dl class="function">
<dt id="marshal.dump">
<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><a class="headerlink" href="#marshal.dump" title="Permalink to this definition">¶</a></dt>
<dd><p>Write the value on the open file.  The value must be a supported type.  The
file must be an open file object such as <code class="docutils literal"><span class="pre">sys.stdout</span></code> or returned by
<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> or <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>.  It must be opened in binary mode (<code class="docutils literal"><span class="pre">'wb'</span></code>
or <code class="docutils literal"><span class="pre">'w+b'</span></code>).</p>
<p>If the value has (or contains an object that has) an unsupported type, 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> exception is raised &#8212; 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 class="xref py py-func docutils literal"><span class="pre">load()</span></code></a>.</p>
<p>The <em>version</em> argument indicates the data format that <code class="docutils literal"><span class="pre">dump</span></code> should use
(see below).</p>
</dd></dl>

<dl class="function">
<dt id="marshal.load">
<code class="descclassname">marshal.</code><code class="descname">load</code><span class="sig-paren">(</span><em>file</em><span class="sig-paren">)</span><a class="headerlink" href="#marshal.load" title="Permalink to this definition">¶</a></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&#8217;s incompatible marshal
format), raise <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> or <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>.  The
file must be an open file object opened in binary mode (<code class="docutils literal"><span class="pre">'rb'</span></code> or
<code class="docutils literal"><span class="pre">'r+b'</span></code>).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If an object containing an unsupported type was marshalled with <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> will substitute <code class="docutils literal"><span class="pre">None</span></code> for the unmarshallable type.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="marshal.dumps">
<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><a class="headerlink" href="#marshal.dumps" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the string that would be written to a file by <code class="docutils literal"><span class="pre">dump(value,</span> <span class="pre">file)</span></code>.  The
value must be a supported type.  Raise 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> 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 class="docutils literal"><span class="pre">dumps</span></code> should use
(see below).</p>
</dd></dl>

<dl class="function">
<dt id="marshal.loads">
<code class="descclassname">marshal.</code><code class="descname">loads</code><span class="sig-paren">(</span><em>string</em><span class="sig-paren">)</span><a class="headerlink" href="#marshal.loads" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert the string to a value.  If no valid value is found, raise
<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> or <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>.  Extra characters in the
string are ignored.</p>
</dd></dl>

<p>In addition, the following constants are defined:</p>
<dl class="data">
<dt id="marshal.version">
<code class="descclassname">marshal.</code><code class="descname">version</code><a class="headerlink" href="#marshal.version" title="Permalink to this definition">¶</a></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>

<p class="rubric">Footnotes</p>
<table class="docutils footnote" frame="void" id="id2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>The name of this module stems from a bit of terminology used by the designers of
Modula-3 (amongst others), who use the term &#8220;marshalling&#8221; for shipping of data
around in a self-contained form. Strictly speaking, &#8220;to marshal&#8221; means to
convert some data from internal to external form (in an RPC buffer for instance)
and &#8220;unmarshalling&#8221; for the reverse process.</td></tr>
</tbody>
</table>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="shelve.html"
                        title="previous chapter">12.3. <code class="docutils literal"><span class="pre">shelve</span></code> &#8212; Python object persistence</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="dbm.html"
                        title="next chapter">12.5. <code class="docutils literal"><span class="pre">dbm</span></code> &#8212; Interfaces to Unix &#8220;databases&#8221;</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">Report a Bug</a></li>
      <li><a href="../_sources/library/marshal.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="dbm.html" title="12.5. dbm — Interfaces to Unix “databases”"
             >next</a> |</li>
        <li class="right" >
          <a href="shelve.html" title="12.3. shelve — Python object persistence"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.5.3</span>
          <a href="../index.html">Documentation </a> &raquo;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li class="nav-item nav-item-2"><a href="persistence.html" >12. Data Persistence</a> &raquo;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 2001-2017, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Jan 20, 2017.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
    </div>

  </body>
</html>