Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8f1462e52e1797a02c97073eed0b7f92 > files > 801

python-docs-2.6.5-2.5mdv2010.2.i586.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.5. marshal — Internal Python object serialization &mdash; Python v2.6.5 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:     '2.6.5',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.5 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 v2.6.5 documentation" href="../index.html" />
    <link rel="up" title="12. Data Persistence" href="persistence.html" />
    <link rel="next" title="12.6. anydbm — Generic access to DBM-style databases" href="anydbm.html" />
    <link rel="prev" title="12.4. 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="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="anydbm.html" title="12.6. anydbm — Generic access to DBM-style databases"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="shelve.html" title="12.4. 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 v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="persistence.html" accesskey="U">12. 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">
<h1>12.5. <tt class="xref docutils literal"><span class="pre">marshal</span></tt> &#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-428">This is not a general &#8220;persistence&#8221; module.  For general persistence and
transfer of Python objects through RPC calls, see the modules <a title="Convert Python objects to streams of bytes and back." class="reference external" href="pickle.html#module-pickle"><tt class="xref docutils literal"><span class="pre">pickle</span></tt></a> and
<a title="Python object persistence." class="reference external" href="shelve.html#module-shelve"><tt class="xref docutils literal"><span class="pre">shelve</span></tt></a>.  The <tt class="xref docutils literal"><span class="pre">marshal</span></tt> module exists mainly to support reading and
writing the &#8220;pseudo-compiled&#8221; code for Python modules of <tt class="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 title="Convert Python objects to streams of bytes and back." class="reference external" href="pickle.html#module-pickle"><tt class="xref 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 <tt class="xref docutils literal"><span class="pre">marshal</span></tt> 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, long
integers, floating point numbers, complex numbers, strings, Unicode objects,
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 title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>, <a title="Ellipsis" class="reference external" href="constants.html#Ellipsis"><tt class="xref docutils literal"><span class="pre">Ellipsis</span></tt></a> and
<a title="exceptions.StopIteration" class="reference external" href="exceptions.html#exceptions.StopIteration"><tt class="xref docutils literal"><span class="pre">StopIteration</span></tt></a> can also be marshalled and unmarshalled.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">On machines where C&#8217;s <tt class="docutils literal"><span class="pre">long</span> <span class="pre">int</span></tt> type has more than 32 bits (such as the
DEC Alpha), it is possible to create plain Python integers that are longer
than 32 bits. If such an integer is marshaled and read back in on a machine
where C&#8217;s <tt class="docutils literal"><span class="pre">long</span> <span class="pre">int</span></tt> type has only 32 bits, a Python long integer object
is returned instead.  While of a different type, the numeric value is the
same.  (This behavior is new in Python 2.2.  In earlier versions, all but the
least-significant 32 bits of the value were lost, and a warning message was
printed.)</p>
</div>
<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 title="open" class="reference external" href="functions.html#open"><tt class="xref docutils literal"><span class="pre">open()</span></tt></a> or <a title="os.popen" class="reference external" href="os.html#os.popen"><tt class="xref docutils literal"><span class="pre">os.popen()</span></tt></a>.  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 title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref 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 title="marshal.load" class="reference internal" href="#marshal.load"><tt class="xref docutils literal"><span class="pre">load()</span></tt></a>.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.4: </span>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 title="exceptions.EOFError" class="reference external" href="exceptions.html#exceptions.EOFError"><tt class="xref docutils literal"><span class="pre">EOFError</span></tt></a>, <a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> or <a title="exceptions.TypeError" class="reference external" href="exceptions.html#exceptions.TypeError"><tt class="xref 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 title="marshal.dump" class="reference internal" href="#marshal.dump"><tt class="xref docutils literal"><span class="pre">dump()</span></tt></a>,
<a title="marshal.load" class="reference internal" href="#marshal.load"><tt class="xref 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 title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> exception if value
has (or contains an object that has) an unsupported type.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.4: </span>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>Convert the string to a value.  If no valid value is found, raise
<a title="exceptions.EOFError" class="reference external" href="exceptions.html#exceptions.EOFError"><tt class="xref docutils literal"><span class="pre">EOFError</span></tt></a>, <a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> or <a title="exceptions.TypeError" class="reference external" href="exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a>.  Extra characters in the
string are ignored.</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 (added in Python 2.4) shares interned strings and version 2 (added in
Python 2.5) uses a binary format for floating point numbers. The current version
is 2.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.4.</span></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">12.4. <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="anydbm.html"
                                  title="next chapter">12.6. <tt class="docutils literal docutils literal"><span class="pre">anydbm</span></tt> &#8212; Generic access to DBM-style databases</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <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="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="anydbm.html" title="12.6. anydbm — Generic access to DBM-style databases"
             >next</a> |</li>
        <li class="right" >
          <a href="shelve.html" title="12.4. 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 v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="persistence.html" >12. Data Persistence</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2010, 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 Mar 19, 2010.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5.
    </div>

  </body>
</html>