Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-backports > by-pkgid > 3ba3bd1608c672ba2129b098a48e9e4d > files > 733

python3-docs-3.2.2-3mdv2010.2.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>11.4. marshal — Internal Python object serialization &mdash; Python v3.2.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.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.2.2',
        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 v3.2.2 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 v3.2.2 documentation" href="../index.html" />
    <link rel="up" title="11. Data Persistence" href="persistence.html" />
    <link rel="next" title="11.5. dbm — Interfaces to Unix “databases”" href="dbm.html" />
    <link rel="prev" title="11.3. shelve — Python object persistence" href="shelve.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <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="11.5. dbm — Interfaces to Unix “databases”"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="shelve.html" title="11.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="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="persistence.html" accesskey="U">11. Data Persistence</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-marshal">
<span id="marshal-internal-python-object-serialization"></span><h1>11.4. <a class="reference internal" href="#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><tt class="xref py py-mod docutils literal"><span class="pre">marshal</span></tt></a> &#8212; Internal Python object serialization<a class="headerlink" href="#module-marshal" title="Permalink to this headline">¶</a></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" 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."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></a> and
<a class="reference internal" href="shelve.html#module-shelve" title="shelve: Python object persistence."><tt class="xref py py-mod docutils literal"><span class="pre">shelve</span></tt></a>.  The <a class="reference internal" href="#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><tt class="xref py py-mod docutils literal"><span class="pre">marshal</span></tt></a> module exists mainly to support reading and
writing the &#8220;pseudo-compiled&#8221; code for Python modules of <tt class="file docutils literal"><span class="pre">.pyc</span></tt> 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."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></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)."><tt class="xref py py-mod docutils literal"><span class="pre">marshal</span></tt></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>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; and recursive lists, sets
and dictionaries should not be written (they will cause infinite loops).  The
singletons <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const xref docutils literal"><span class="pre">None</span></tt></a>, <a class="reference internal" href="constants.html#Ellipsis" title="Ellipsis"><tt class="xref py py-const docutils literal"><span class="pre">Ellipsis</span></tt></a> and <a class="reference internal" href="exceptions.html#StopIteration" title="StopIteration"><tt class="xref py py-exc docutils literal"><span class="pre">StopIteration</span></tt></a> can also be
marshalled and unmarshalled.</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">
<tt class="descclassname">marshal.</tt><tt class="descname">dump</tt><big>(</big><em>value</em>, <em>file</em><span class="optional">[</span>, <em>version</em><span class="optional">]</span><big>)</big><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 <tt class="docutils literal"><span class="pre">sys.stdout</span></tt> or returned by
<a class="reference internal" href="functions.html#open" title="open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a> or <tt class="xref py py-func docutils literal"><span class="pre">os.popen()</span></tt>.  It must be opened in binary mode (<tt class="docutils literal"><span class="pre">'wb'</span></tt>
or <tt class="docutils literal"><span class="pre">'w+b'</span></tt>).</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"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">load()</span></tt></a>.</p>
<p>The <em>version</em> argument indicates the data format that <tt class="docutils literal"><span class="pre">dump</span></tt> should use
(see below).</p>
</dd></dl>

<dl class="function">
<dt id="marshal.load">
<tt class="descclassname">marshal.</tt><tt class="descname">load</tt><big>(</big><em>file</em><big>)</big><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"><tt class="xref py py-exc docutils literal"><span class="pre">EOFError</span></tt></a>, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> or <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>.  The
file must be an open file object opened in binary mode (<tt class="docutils literal"><span class="pre">'rb'</span></tt> or
<tt class="docutils literal"><span class="pre">'r+b'</span></tt>).</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"><tt class="xref py py-func docutils literal"><span class="pre">dump()</span></tt></a>,
<a class="reference internal" href="#marshal.load" title="marshal.load"><tt class="xref py py-func docutils literal"><span class="pre">load()</span></tt></a> will substitute <tt class="xref docutils literal"><span class="pre">None</span></tt> for the unmarshallable type.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="marshal.dumps">
<tt class="descclassname">marshal.</tt><tt class="descname">dumps</tt><big>(</big><em>value</em><span class="optional">[</span>, <em>version</em><span class="optional">]</span><big>)</big><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 <tt class="docutils literal"><span class="pre">dump(value,</span> <span class="pre">file)</span></tt>.  The
value must be a supported type.  Raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></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 <tt class="docutils literal"><span class="pre">dumps</span></tt> should use
(see below).</p>
</dd></dl>

<dl class="function">
<dt id="marshal.loads">
<tt class="descclassname">marshal.</tt><tt class="descname">loads</tt><big>(</big><em>string</em><big>)</big><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"><tt class="xref py py-exc docutils literal"><span class="pre">EOFError</span></tt></a>, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> or <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></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">
<tt class="descclassname">marshal.</tt><tt class="descname">version</tt><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. The current version is 2.</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">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="shelve.html"
                        title="previous chapter">11.3. <tt class="docutils literal docutils literal"><span class="pre">shelve</span></tt> &#8212; Python object persistence</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="dbm.html"
                        title="next chapter">11.5. <tt class="docutils literal docutils literal"><span class="pre">dbm</span></tt> &#8212; Interfaces to Unix &#8220;databases&#8221;</a></p>
<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 id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <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="11.5. dbm — Interfaces to Unix “databases”"
             >next</a> |</li>
        <li class="right" >
          <a href="shelve.html" title="11.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="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="persistence.html" >11. Data Persistence</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Sep 04, 2011.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>