| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="module-warnings.html" /> |
| 13 | <link rel="prev" href="module-copy.html" /> |
| 14 | <link rel="parent" href="python.html" /> |
| 15 | <link rel="next" href="module-warnings.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3.19 marshal -- Internal Python object serialization</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="3.18 copy " |
| 25 | href="module-copy.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" |
| 28 | href="python.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="3.20 warnings " |
| 31 | href="module-warnings.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-copy.html">3.18 copy </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="module-warnings.html">3.20 warnings </A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION0051900000000000000000"> |
| 56 | 3.19 <tt class="module">marshal</tt> -- |
| 57 | Internal Python object serialization</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-marshal"></A> |
| 62 | |
| 63 | <P> |
| 64 | This module contains functions that can read and write Python |
| 65 | values in a binary format. The format is specific to Python, but |
| 66 | independent of machine architecture issues (e.g., you can write a |
| 67 | Python value to a file on a PC, transport the file to a Sun, and read |
| 68 | it back there). Details of the format are undocumented on purpose; |
| 69 | it may change between Python versions (although it rarely |
| 70 | does).<A NAME="tex2html24" |
| 71 | HREF="#foot9695"><SUP>3.12</SUP></A> |
| 72 | <P> |
| 73 | This is not a general ``persistence'' module. For general persistence |
| 74 | and transfer of Python objects through RPC calls, see the modules |
| 75 | <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 |
| 76 | mainly to support reading and writing the ``pseudo-compiled'' code for |
| 77 | Python modules of <span class="file">.pyc</span> files. Therefore, the Python |
| 78 | maintainers reserve the right to modify the marshal format in backward |
| 79 | incompatible ways should the need arise. If you're serializing and |
| 80 | de-serializing Python objects, use the <tt class="module">pickle</tt> module instead. |
| 81 | <a id='l2h-686' xml:id='l2h-686'></a> |
| 82 | <a id='l2h-680' xml:id='l2h-680'></a> |
| 83 | <P> |
| 84 | <div class="warning"><b class="label">Warning:</b> |
| 85 | |
| 86 | The <tt class="module">marshal</tt> module is not intended to be secure against |
| 87 | erroneous or maliciously constructed data. Never unmarshal data |
| 88 | received from an untrusted or unauthenticated source. |
| 89 | </div> |
| 90 | |
| 91 | <P> |
| 92 | Not all Python object types are supported; in general, only objects |
| 93 | whose value is independent from a particular invocation of Python can |
| 94 | be written and read by this module. The following types are supported: |
| 95 | <code>None</code>, integers, long integers, floating point numbers, |
| 96 | strings, Unicode objects, tuples, lists, dictionaries, and code |
| 97 | objects, where it should be understood that tuples, lists and |
| 98 | dictionaries are only supported as long as the values contained |
| 99 | therein are themselves supported; and recursive lists and dictionaries |
| 100 | should not be written (they will cause infinite loops). |
| 101 | |
| 102 | <P> |
| 103 | <strong>Caveat:</strong> On machines where C's <code>long int</code> type has more than |
| 104 | 32 bits (such as the DEC Alpha), it is possible to create plain Python |
| 105 | integers that are longer than 32 bits. |
| 106 | If such an integer is marshaled and read back in on a machine where |
| 107 | C's <code>long int</code> type has only 32 bits, a Python long integer object |
| 108 | is returned instead. While of a different type, the numeric value is |
| 109 | the same. (This behavior is new in Python 2.2. In earlier versions, |
| 110 | all but the least-significant 32 bits of the value were lost, and a |
| 111 | warning message was printed.) |
| 112 | |
| 113 | <P> |
| 114 | There are functions that read/write files as well as functions |
| 115 | operating on strings. |
| 116 | |
| 117 | <P> |
| 118 | The module defines these functions: |
| 119 | |
| 120 | <P> |
| 121 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 122 | <td><nobr><b><tt id='l2h-681' xml:id='l2h-681' class="function">dump</tt></b>(</nobr></td> |
| 123 | <td><var>value, file</var><big>[</big><var>, version</var><big>]</big><var></var>)</td></tr></table></dt> |
| 124 | <dd> |
| 125 | Write the value on the open file. The value must be a supported |
| 126 | type. The file must be an open file object such as |
| 127 | <code>sys.stdout</code> or returned by <tt class="function">open()</tt> or |
| 128 | <tt class="function">posix.popen()</tt>. It must be opened in binary mode |
| 129 | (<code>'wb'</code> or <code>'w+b'</code>). |
| 130 | |
| 131 | <P> |
| 132 | If the value has (or contains an object that has) an unsupported type, |
| 133 | a <tt class="exception">ValueError</tt> exception is raised -- but garbage data |
| 134 | will also be written to the file. The object will not be properly |
| 135 | read back by <tt class="function">load()</tt>. |
| 136 | |
| 137 | <P> |
| 138 | |
| 139 | <span class="versionnote">New in version 2.4: |
| 140 | The <var>version</var> argument indicates the data |
| 141 | format that <code>dump</code> should use (see below).</span> |
| 142 | |
| 143 | </dl> |
| 144 | |
| 145 | <P> |
| 146 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 147 | <td><nobr><b><tt id='l2h-682' xml:id='l2h-682' class="function">load</tt></b>(</nobr></td> |
| 148 | <td><var>file</var>)</td></tr></table></dt> |
| 149 | <dd> |
| 150 | Read one value from the open file and return it. If no valid value |
| 151 | is read, raise <tt class="exception">EOFError</tt>, <tt class="exception">ValueError</tt> or |
| 152 | <tt class="exception">TypeError</tt>. The file must be an open file object opened |
| 153 | in binary mode (<code>'rb'</code> or <code>'r+b'</code>). |
| 154 | |
| 155 | <P> |
| 156 | <span class="warning"><b class="label">Warning:</b> |
| 157 | If an object containing an unsupported type was |
| 158 | marshalled with <tt class="function">dump()</tt>, <tt class="function">load()</tt> will substitute |
| 159 | <code>None</code> for the unmarshallable type.</span> |
| 160 | </dl> |
| 161 | |
| 162 | <P> |
| 163 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 164 | <td><nobr><b><tt id='l2h-683' xml:id='l2h-683' class="function">dumps</tt></b>(</nobr></td> |
| 165 | <td><var>value</var><big>[</big><var>, version</var><big>]</big><var></var>)</td></tr></table></dt> |
| 166 | <dd> |
| 167 | Return the string that would be written to a file by |
| 168 | <code>dump(<var>value</var>, <var>file</var>)</code>. The value must be a supported |
| 169 | type. Raise a <tt class="exception">ValueError</tt> exception if value has (or |
| 170 | contains an object that has) an unsupported type. |
| 171 | |
| 172 | <P> |
| 173 | |
| 174 | <span class="versionnote">New in version 2.4: |
| 175 | The <var>version</var> argument indicates the data |
| 176 | format that <code>dumps</code> should use (see below).</span> |
| 177 | |
| 178 | </dl> |
| 179 | |
| 180 | <P> |
| 181 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 182 | <td><nobr><b><tt id='l2h-684' xml:id='l2h-684' class="function">loads</tt></b>(</nobr></td> |
| 183 | <td><var>string</var>)</td></tr></table></dt> |
| 184 | <dd> |
| 185 | Convert the string to a value. If no valid value is found, raise |
| 186 | <tt class="exception">EOFError</tt>, <tt class="exception">ValueError</tt> or |
| 187 | <tt class="exception">TypeError</tt>. Extra characters in the string are ignored. |
| 188 | </dl> |
| 189 | |
| 190 | <P> |
| 191 | In addition, the following constants are defined: |
| 192 | |
| 193 | <P> |
| 194 | <dl><dt><b><tt id='l2h-685' xml:id='l2h-685'>version</tt></b></dt> |
| 195 | <dd> |
| 196 | Indicates the format that the module uses. Version 0 is the |
| 197 | historical format, version 1 (added in Python 2.4) shares |
| 198 | interned strings. The current version is 1. |
| 199 | |
| 200 | <P> |
| 201 | |
| 202 | <span class="versionnote">New in version 2.4.</span> |
| 203 | |
| 204 | </dd></dl> |
| 205 | <BR><HR><H4>Footnotes</H4> |
| 206 | <DL> |
| 207 | <DT><A NAME="foot9695">... |
| 208 | does).</A><A |
| 209 | href="module-marshal.html#tex2html24"><SUP>3.12</SUP></A></DT> |
| 210 | <DD>The name of this module stems from a bit of |
| 211 | terminology used by the designers of Modula-3 (amongst others), who |
| 212 | use the term ``marshalling'' for shipping of data around in a |
| 213 | self-contained form. Strictly speaking, ``to marshal'' means to |
| 214 | convert some data from internal to external form (in an RPC buffer for |
| 215 | instance) and ``unmarshalling'' for the reverse process. |
| 216 | |
| 217 | </DD> |
| 218 | </DL> |
| 219 | <DIV CLASS="navigation"> |
| 220 | <div class='online-navigation'> |
| 221 | <p></p><hr /> |
| 222 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 223 | <tr> |
| 224 | <td class='online-navigation'><a rel="prev" title="3.18 copy " |
| 225 | href="module-copy.html"><img src='../icons/previous.png' |
| 226 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 227 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" |
| 228 | href="python.html"><img src='../icons/up.png' |
| 229 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 230 | <td class='online-navigation'><a rel="next" title="3.20 warnings " |
| 231 | href="module-warnings.html"><img src='../icons/next.png' |
| 232 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 233 | <td align="center" width="100%">Python Library Reference</td> |
| 234 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 235 | href="contents.html"><img src='../icons/contents.png' |
| 236 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 237 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 238 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 239 | <td class='online-navigation'><a rel="index" title="Index" |
| 240 | href="genindex.html"><img src='../icons/index.png' |
| 241 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 242 | </tr></table> |
| 243 | <div class='online-navigation'> |
| 244 | <b class="navlabel">Previous:</b> |
| 245 | <a class="sectref" rel="prev" href="module-copy.html">3.18 copy </A> |
| 246 | <b class="navlabel">Up:</b> |
| 247 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> |
| 248 | <b class="navlabel">Next:</b> |
| 249 | <a class="sectref" rel="next" href="module-warnings.html">3.20 warnings </A> |
| 250 | </div> |
| 251 | </div> |
| 252 | <hr /> |
| 253 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 254 | </DIV> |
| 255 | <!--End of Navigation Panel--> |
| 256 | <ADDRESS> |
| 257 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 258 | </ADDRESS> |
| 259 | </BODY> |
| 260 | </HTML> |