Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / module-marshal.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-warnings.html" />
<link rel="prev" href="module-copy.html" />
<link rel="parent" href="python.html" />
<link rel="next" href="module-warnings.html" />
<meta name='aesop' content='information' />
<title>3.19 marshal -- Internal Python object serialization</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3.18 copy "
href="module-copy.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
href="python.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.20 warnings "
href="module-warnings.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-copy.html">3.18 copy </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-warnings.html">3.20 warnings </A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0051900000000000000000">
3.19 <tt class="module">marshal</tt> --
Internal Python object serialization</A>
</H1>
<P>
<A NAME="module-marshal"></A>
<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 NAME="tex2html24"
HREF="#foot9695"><SUP>3.12</SUP></A>
<P>
This is not a general ``persistence'' module. For general persistence
and transfer of Python objects through RPC calls, see the modules
<tt class="module"><a href="module-pickle.html">pickle</a></tt> and <tt class="module"><a href="module-shelve.html">shelve</a></tt>. The <tt class="module">marshal</tt> module exists
mainly to support reading and writing the ``pseudo-compiled'' code for
Python modules of <span class="file">.pyc</span> 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 <tt class="module">pickle</tt> module instead.
<a id='l2h-686' xml:id='l2h-686'></a>
<a id='l2h-680' xml:id='l2h-680'></a>
<P>
<div class="warning"><b class="label">Warning:</b>
The <tt class="module">marshal</tt> module is not intended to be secure against
erroneous or maliciously constructed data. Never unmarshal data
received from an untrusted or unauthenticated source.
</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:
<code>None</code>, integers, long integers, floating point numbers,
strings, Unicode objects, tuples, lists, dictionaries, and code
objects, where it should be understood that tuples, lists and
dictionaries are only supported as long as the values contained
therein are themselves supported; and recursive lists and dictionaries
should not be written (they will cause infinite loops).
<P>
<strong>Caveat:</strong> On machines where C's <code>long int</code> 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's <code>long int</code> 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>
There are functions that read/write files as well as functions
operating on strings.
<P>
The module defines these functions:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-681' xml:id='l2h-681' class="function">dump</tt></b>(</nobr></td>
<td><var>value, file</var><big>[</big><var>, version</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
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>sys.stdout</code> or returned by <tt class="function">open()</tt> or
<tt class="function">posix.popen()</tt>. It must be opened in binary mode
(<code>'wb'</code> or <code>'w+b'</code>).
<P>
If the value has (or contains an object that has) an unsupported type,
a <tt class="exception">ValueError</tt> exception is raised -- but garbage data
will also be written to the file. The object will not be properly
read back by <tt class="function">load()</tt>.
<P>
<span class="versionnote">New in version 2.4:
The <var>version</var> argument indicates the data
format that <code>dump</code> should use (see below).</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-682' xml:id='l2h-682' class="function">load</tt></b>(</nobr></td>
<td><var>file</var>)</td></tr></table></dt>
<dd>
Read one value from the open file and return it. If no valid value
is read, raise <tt class="exception">EOFError</tt>, <tt class="exception">ValueError</tt> or
<tt class="exception">TypeError</tt>. The file must be an open file object opened
in binary mode (<code>'rb'</code> or <code>'r+b'</code>).
<P>
<span class="warning"><b class="label">Warning:</b>
If an object containing an unsupported type was
marshalled with <tt class="function">dump()</tt>, <tt class="function">load()</tt> will substitute
<code>None</code> for the unmarshallable type.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-683' xml:id='l2h-683' class="function">dumps</tt></b>(</nobr></td>
<td><var>value</var><big>[</big><var>, version</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return the string that would be written to a file by
<code>dump(<var>value</var>, <var>file</var>)</code>. The value must be a supported
type. Raise a <tt class="exception">ValueError</tt> exception if value has (or
contains an object that has) an unsupported type.
<P>
<span class="versionnote">New in version 2.4:
The <var>version</var> argument indicates the data
format that <code>dumps</code> should use (see below).</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-684' xml:id='l2h-684' class="function">loads</tt></b>(</nobr></td>
<td><var>string</var>)</td></tr></table></dt>
<dd>
Convert the string to a value. If no valid value is found, raise
<tt class="exception">EOFError</tt>, <tt class="exception">ValueError</tt> or
<tt class="exception">TypeError</tt>. Extra characters in the string are ignored.
</dl>
<P>
In addition, the following constants are defined:
<P>
<dl><dt><b><tt id='l2h-685' xml:id='l2h-685'>version</tt></b></dt>
<dd>
Indicates the format that the module uses. Version 0 is the
historical format, version 1 (added in Python 2.4) shares
interned strings. The current version is 1.
<P>
<span class="versionnote">New in version 2.4.</span>
</dd></dl>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot9695">...
does).</A><A
href="module-marshal.html#tex2html24"><SUP>3.12</SUP></A></DT>
<DD>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.
</DD>
</DL>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3.18 copy "
href="module-copy.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
href="python.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.20 warnings "
href="module-warnings.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-copy.html">3.18 copy </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-warnings.html">3.20 warnings </A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>