~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.1-docs-html/library/marshal.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>12.4. marshal — Internal Python object serialization &mdash; Python 3.5.1 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '3.5.1',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
23
 
    <script type="text/javascript" src="../_static/jquery.js"></script>
24
 
    <script type="text/javascript" src="../_static/underscore.js"></script>
25
 
    <script type="text/javascript" src="../_static/doctools.js"></script>
26
 
    <script type="text/javascript" src="../_static/sidebar.js"></script>
27
 
    <link rel="search" type="application/opensearchdescription+xml"
28
 
          title="Search within Python 3.5.1 documentation"
29
 
          href="../_static/opensearch.xml"/>
30
 
    <link rel="author" title="About these documents" href="../about.html" />
31
 
    <link rel="copyright" title="Copyright" href="../copyright.html" />
32
 
    <link rel="top" title="Python 3.5.1 documentation" href="../contents.html" />
33
 
    <link rel="up" title="12. Data Persistence" href="persistence.html" />
34
 
    <link rel="next" title="12.5. dbm — Interfaces to Unix “databases”" href="dbm.html" />
35
 
    <link rel="prev" title="12.3. shelve — Python object persistence" href="shelve.html" />
36
 
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
 
    <script type="text/javascript" src="../_static/copybutton.js"></script>
38
 
    <script type="text/javascript" src="../_static/version_switch.js"></script>
39
 
    
40
 
 
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="dbm.html" title="12.5. dbm — Interfaces to Unix “databases”"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
57
 
          <a href="shelve.html" title="12.3. shelve — Python object persistence"
58
 
             accesskey="P">previous</a> |</li>
59
 
        <li><img src="../_static/py.png" alt=""
60
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
61
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">3.5.1</span>
64
 
          <a href="../index.html">Documentation </a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
68
 
          <li class="nav-item nav-item-2"><a href="persistence.html" accesskey="U">12. Data Persistence</a> &raquo;</li> 
69
 
      </ul>
70
 
    </div>    
71
 
 
72
 
    <div class="document">
73
 
      <div class="documentwrapper">
74
 
        <div class="bodywrapper">
75
 
          <div class="body" role="main">
76
 
            
77
 
  <div class="section" id="module-marshal">
78
 
<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>
79
 
<p>This module contains functions that can read and write Python values in a binary
80
 
format.  The format is specific to Python, but independent of machine
81
 
architecture issues (e.g., you can write a Python value to a file on a PC,
82
 
transport the file to a Sun, and read it back there).  Details of the format are
83
 
undocumented on purpose; it may change between Python versions (although it
84
 
rarely does). <a class="footnote-reference" href="#id2" id="id1">[1]</a></p>
85
 
<p id="index-0">This is not a general &#8220;persistence&#8221; module.  For general persistence and
86
 
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
87
 
<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
88
 
writing the &#8220;pseudo-compiled&#8221; code for Python modules of <code class="file docutils literal"><span class="pre">.pyc</span></code> files.
89
 
Therefore, the Python maintainers reserve the right to modify the marshal format
90
 
in backward incompatible ways should the need arise.  If you&#8217;re serializing and
91
 
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
92
 
performance is comparable, version independence is guaranteed, and pickle
93
 
supports a substantially wider range of objects than marshal.</p>
94
 
<div class="admonition warning">
95
 
<p class="first admonition-title">Warning</p>
96
 
<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
97
 
maliciously constructed data.  Never unmarshal data received from an
98
 
untrusted or unauthenticated source.</p>
99
 
</div>
100
 
<p>Not all Python object types are supported; in general, only objects whose value
101
 
is independent from a particular invocation of Python can be written and read by
102
 
this module.  The following types are supported: booleans, integers, floating
103
 
point numbers, complex numbers, strings, bytes, bytearrays, tuples, lists, sets,
104
 
frozensets, dictionaries, and code objects, where it should be understood that
105
 
tuples, lists, sets, frozensets and dictionaries are only supported as long as
106
 
the values contained therein are themselves supported.
107
 
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
108
 
marshalled and unmarshalled.
109
 
For format <em>version</em> lower than 3, recursive lists, sets and dictionaries cannot
110
 
be written (see below).</p>
111
 
<p>There are functions that read/write files as well as functions operating on
112
 
strings.</p>
113
 
<p>The module defines these functions:</p>
114
 
<dl class="function">
115
 
<dt id="marshal.dump">
116
 
<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>
117
 
<dd><p>Write the value on the open file.  The value must be a supported type.  The
118
 
file must be an open file object such as <code class="docutils literal"><span class="pre">sys.stdout</span></code> or returned by
119
 
<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>
120
 
or <code class="docutils literal"><span class="pre">'w+b'</span></code>).</p>
121
 
<p>If the value has (or contains an object that has) an unsupported type, a
122
 
<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
123
 
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>
124
 
<p>The <em>version</em> argument indicates the data format that <code class="docutils literal"><span class="pre">dump</span></code> should use
125
 
(see below).</p>
126
 
</dd></dl>
127
 
 
128
 
<dl class="function">
129
 
<dt id="marshal.load">
130
 
<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>
131
 
<dd><p>Read one value from the open file and return it.  If no valid value is read
132
 
(e.g. because the data has a different Python version&#8217;s incompatible marshal
133
 
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
134
 
file must be an open file object opened in binary mode (<code class="docutils literal"><span class="pre">'rb'</span></code> or
135
 
<code class="docutils literal"><span class="pre">'r+b'</span></code>).</p>
136
 
<div class="admonition note">
137
 
<p class="first admonition-title">Note</p>
138
 
<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>,
139
 
<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>
140
 
</div>
141
 
</dd></dl>
142
 
 
143
 
<dl class="function">
144
 
<dt id="marshal.dumps">
145
 
<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>
146
 
<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
147
 
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
148
 
has (or contains an object that has) an unsupported type.</p>
149
 
<p>The <em>version</em> argument indicates the data format that <code class="docutils literal"><span class="pre">dumps</span></code> should use
150
 
(see below).</p>
151
 
</dd></dl>
152
 
 
153
 
<dl class="function">
154
 
<dt id="marshal.loads">
155
 
<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>
156
 
<dd><p>Convert the string to a value.  If no valid value is found, raise
157
 
<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
158
 
string are ignored.</p>
159
 
</dd></dl>
160
 
 
161
 
<p>In addition, the following constants are defined:</p>
162
 
<dl class="data">
163
 
<dt id="marshal.version">
164
 
<code class="descclassname">marshal.</code><code class="descname">version</code><a class="headerlink" href="#marshal.version" title="Permalink to this definition">¶</a></dt>
165
 
<dd><p>Indicates the format that the module uses. Version 0 is the historical
166
 
format, version 1 shares interned strings and version 2 uses a binary format
167
 
for floating point numbers.
168
 
Version 3 adds support for object instancing and recursion.
169
 
The current version is 4.</p>
170
 
</dd></dl>
171
 
 
172
 
<p class="rubric">Footnotes</p>
173
 
<table class="docutils footnote" frame="void" id="id2" rules="none">
174
 
<colgroup><col class="label" /><col /></colgroup>
175
 
<tbody valign="top">
176
 
<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
177
 
Modula-3 (amongst others), who use the term &#8220;marshalling&#8221; for shipping of data
178
 
around in a self-contained form. Strictly speaking, &#8220;to marshal&#8221; means to
179
 
convert some data from internal to external form (in an RPC buffer for instance)
180
 
and &#8220;unmarshalling&#8221; for the reverse process.</td></tr>
181
 
</tbody>
182
 
</table>
183
 
</div>
184
 
 
185
 
 
186
 
          </div>
187
 
        </div>
188
 
      </div>
189
 
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
190
 
        <div class="sphinxsidebarwrapper">
191
 
  <h4>Previous topic</h4>
192
 
  <p class="topless"><a href="shelve.html"
193
 
                        title="previous chapter">12.3. <code class="docutils literal"><span class="pre">shelve</span></code> &#8212; Python object persistence</a></p>
194
 
  <h4>Next topic</h4>
195
 
  <p class="topless"><a href="dbm.html"
196
 
                        title="next chapter">12.5. <code class="docutils literal"><span class="pre">dbm</span></code> &#8212; Interfaces to Unix &#8220;databases&#8221;</a></p>
197
 
<h3>This Page</h3>
198
 
<ul class="this-page-menu">
199
 
  <li><a href="../bugs.html">Report a Bug</a></li>
200
 
  <li><a href="../_sources/library/marshal.txt"
201
 
         rel="nofollow">Show Source</a></li>
202
 
</ul>
203
 
 
204
 
<div id="searchbox" style="display: none" role="search">
205
 
  <h3>Quick search</h3>
206
 
    <form class="search" action="../search.html" method="get">
207
 
      <input type="text" name="q" />
208
 
      <input type="submit" value="Go" />
209
 
      <input type="hidden" name="check_keywords" value="yes" />
210
 
      <input type="hidden" name="area" value="default" />
211
 
    </form>
212
 
    <p class="searchtip" style="font-size: 90%">
213
 
    Enter search terms or a module, class or function name.
214
 
    </p>
215
 
</div>
216
 
<script type="text/javascript">$('#searchbox').show(0);</script>
217
 
        </div>
218
 
      </div>
219
 
      <div class="clearer"></div>
220
 
    </div>  
221
 
    <div class="related" role="navigation" aria-label="related navigation">
222
 
      <h3>Navigation</h3>
223
 
      <ul>
224
 
        <li class="right" style="margin-right: 10px">
225
 
          <a href="../genindex.html" title="General Index"
226
 
             >index</a></li>
227
 
        <li class="right" >
228
 
          <a href="../py-modindex.html" title="Python Module Index"
229
 
             >modules</a> |</li>
230
 
        <li class="right" >
231
 
          <a href="dbm.html" title="12.5. dbm — Interfaces to Unix “databases”"
232
 
             >next</a> |</li>
233
 
        <li class="right" >
234
 
          <a href="shelve.html" title="12.3. shelve — Python object persistence"
235
 
             >previous</a> |</li>
236
 
        <li><img src="../_static/py.png" alt=""
237
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
238
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
239
 
        <li>
240
 
          <span class="version_switcher_placeholder">3.5.1</span>
241
 
          <a href="../index.html">Documentation </a> &raquo;
242
 
        </li>
243
 
 
244
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
245
 
          <li class="nav-item nav-item-2"><a href="persistence.html" >12. Data Persistence</a> &raquo;</li> 
246
 
      </ul>
247
 
    </div>  
248
 
    <div class="footer">
249
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
250
 
    <br />
251
 
    The Python Software Foundation is a non-profit corporation.
252
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
253
 
    <br />
254
 
    Last updated on Jan 22, 2016.
255
 
    <a href="../bugs.html">Found a bug</a>?
256
 
    <br />
257
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
258
 
    </div>
259
 
 
260
 
  </body>
261
 
</html>
 
 
b'\\ No newline at end of file'