~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.12-docs-html/library/xdrlib.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>13.5. xdrlib — Encode and decode XDR data &mdash; Python 2.7.12 documentation</title>
 
10
    
 
11
    <link rel="stylesheet" href="../_static/classic.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:     '2.7.12',
 
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 2.7.12 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 2.7.12 documentation" href="../contents.html" />
 
33
    <link rel="up" title="13. File Formats" href="fileformats.html" />
 
34
    <link rel="next" title="13.6. plistlib — Generate and parse Mac OS X .plist files" href="plistlib.html" />
 
35
    <link rel="prev" title="13.4. netrc — netrc file processing" href="netrc.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="plistlib.html" title="13.6. plistlib — Generate and parse Mac OS X .plist files"
 
55
             accesskey="N">next</a> |</li>
 
56
        <li class="right" >
 
57
          <a href="netrc.html" title="13.4. netrc — netrc file processing"
 
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">2.7.12</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="fileformats.html" accesskey="U">13. File Formats</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-xdrlib">
 
78
<span id="xdrlib-encode-and-decode-xdr-data"></span><h1>13.5. <a class="reference internal" href="#module-xdrlib" title="xdrlib: Encoders and decoders for the External Data Representation (XDR)."><code class="xref py py-mod docutils literal"><span class="pre">xdrlib</span></code></a> &#8212; Encode and decode XDR data<a class="headerlink" href="#module-xdrlib" title="Permalink to this headline">¶</a></h1>
 
79
<p id="index-0"><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/2.7/Lib/xdrlib.py">Lib/xdrlib.py</a></p>
 
80
<hr class="docutils" />
 
81
<p>The <a class="reference internal" href="#module-xdrlib" title="xdrlib: Encoders and decoders for the External Data Representation (XDR)."><code class="xref py py-mod docutils literal"><span class="pre">xdrlib</span></code></a> module supports the External Data Representation Standard as
 
82
described in <span class="target" id="index-1"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1014.html"><strong>RFC 1014</strong></a>, written by Sun Microsystems, Inc. June 1987.  It
 
83
supports most of the data types described in the RFC.</p>
 
84
<p>The <a class="reference internal" href="#module-xdrlib" title="xdrlib: Encoders and decoders for the External Data Representation (XDR)."><code class="xref py py-mod docutils literal"><span class="pre">xdrlib</span></code></a> module defines two classes, one for packing variables into XDR
 
85
representation, and another for unpacking from XDR representation.  There are
 
86
also two exception classes.</p>
 
87
<dl class="class">
 
88
<dt id="xdrlib.Packer">
 
89
<em class="property">class </em><code class="descclassname">xdrlib.</code><code class="descname">Packer</code><a class="headerlink" href="#xdrlib.Packer" title="Permalink to this definition">¶</a></dt>
 
90
<dd><p><a class="reference internal" href="#xdrlib.Packer" title="xdrlib.Packer"><code class="xref py py-class docutils literal"><span class="pre">Packer</span></code></a> is the class for packing data into XDR representation. The
 
91
<a class="reference internal" href="#xdrlib.Packer" title="xdrlib.Packer"><code class="xref py py-class docutils literal"><span class="pre">Packer</span></code></a> class is instantiated with no arguments.</p>
 
92
</dd></dl>
 
93
 
 
94
<dl class="class">
 
95
<dt id="xdrlib.Unpacker">
 
96
<em class="property">class </em><code class="descclassname">xdrlib.</code><code class="descname">Unpacker</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker" title="Permalink to this definition">¶</a></dt>
 
97
<dd><p><code class="docutils literal"><span class="pre">Unpacker</span></code> is the complementary class which unpacks XDR data values from a
 
98
string buffer.  The input buffer is given as <em>data</em>.</p>
 
99
</dd></dl>
 
100
 
 
101
<div class="admonition seealso">
 
102
<p class="first admonition-title">See also</p>
 
103
<dl class="last docutils">
 
104
<dt><span class="target" id="index-2"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1014.html"><strong>RFC 1014</strong></a> - XDR: External Data Representation Standard</dt>
 
105
<dd>This RFC defined the encoding of data which was XDR at the time this module was
 
106
originally written.  It has apparently been obsoleted by <span class="target" id="index-3"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1832.html"><strong>RFC 1832</strong></a>.</dd>
 
107
<dt><span class="target" id="index-4"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1832.html"><strong>RFC 1832</strong></a> - XDR: External Data Representation Standard</dt>
 
108
<dd>Newer RFC that provides a revised definition of XDR.</dd>
 
109
</dl>
 
110
</div>
 
111
<div class="section" id="packer-objects">
 
112
<span id="xdr-packer-objects"></span><h2>13.5.1. Packer Objects<a class="headerlink" href="#packer-objects" title="Permalink to this headline">¶</a></h2>
 
113
<p><a class="reference internal" href="#xdrlib.Packer" title="xdrlib.Packer"><code class="xref py py-class docutils literal"><span class="pre">Packer</span></code></a> instances have the following methods:</p>
 
114
<dl class="method">
 
115
<dt id="xdrlib.Packer.get_buffer">
 
116
<code class="descclassname">Packer.</code><code class="descname">get_buffer</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Packer.get_buffer" title="Permalink to this definition">¶</a></dt>
 
117
<dd><p>Returns the current pack buffer as a string.</p>
 
118
</dd></dl>
 
119
 
 
120
<dl class="method">
 
121
<dt id="xdrlib.Packer.reset">
 
122
<code class="descclassname">Packer.</code><code class="descname">reset</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Packer.reset" title="Permalink to this definition">¶</a></dt>
 
123
<dd><p>Resets the pack buffer to the empty string.</p>
 
124
</dd></dl>
 
125
 
 
126
<p>In general, you can pack any of the most common XDR data types by calling the
 
127
appropriate <code class="docutils literal"><span class="pre">pack_type()</span></code> method.  Each method takes a single argument, the
 
128
value to pack.  The following simple data type packing methods are supported:
 
129
<code class="xref py py-meth docutils literal"><span class="pre">pack_uint()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">pack_int()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">pack_enum()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">pack_bool()</span></code>,
 
130
<code class="xref py py-meth docutils literal"><span class="pre">pack_uhyper()</span></code>, and <code class="xref py py-meth docutils literal"><span class="pre">pack_hyper()</span></code>.</p>
 
131
<dl class="method">
 
132
<dt id="xdrlib.Packer.pack_float">
 
133
<code class="descclassname">Packer.</code><code class="descname">pack_float</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Packer.pack_float" title="Permalink to this definition">¶</a></dt>
 
134
<dd><p>Packs the single-precision floating point number <em>value</em>.</p>
 
135
</dd></dl>
 
136
 
 
137
<dl class="method">
 
138
<dt id="xdrlib.Packer.pack_double">
 
139
<code class="descclassname">Packer.</code><code class="descname">pack_double</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Packer.pack_double" title="Permalink to this definition">¶</a></dt>
 
140
<dd><p>Packs the double-precision floating point number <em>value</em>.</p>
 
141
</dd></dl>
 
142
 
 
143
<p>The following methods support packing strings, bytes, and opaque data:</p>
 
144
<dl class="method">
 
145
<dt id="xdrlib.Packer.pack_fstring">
 
146
<code class="descclassname">Packer.</code><code class="descname">pack_fstring</code><span class="sig-paren">(</span><em>n</em>, <em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Packer.pack_fstring" title="Permalink to this definition">¶</a></dt>
 
147
<dd><p>Packs a fixed length string, <em>s</em>.  <em>n</em> is the length of the string but it is
 
148
<em>not</em> packed into the data buffer.  The string is padded with null bytes if
 
149
necessary to guaranteed 4 byte alignment.</p>
 
150
</dd></dl>
 
151
 
 
152
<dl class="method">
 
153
<dt id="xdrlib.Packer.pack_fopaque">
 
154
<code class="descclassname">Packer.</code><code class="descname">pack_fopaque</code><span class="sig-paren">(</span><em>n</em>, <em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Packer.pack_fopaque" title="Permalink to this definition">¶</a></dt>
 
155
<dd><p>Packs a fixed length opaque data stream, similarly to <a class="reference internal" href="#xdrlib.Packer.pack_fstring" title="xdrlib.Packer.pack_fstring"><code class="xref py py-meth docutils literal"><span class="pre">pack_fstring()</span></code></a>.</p>
 
156
</dd></dl>
 
157
 
 
158
<dl class="method">
 
159
<dt id="xdrlib.Packer.pack_string">
 
160
<code class="descclassname">Packer.</code><code class="descname">pack_string</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Packer.pack_string" title="Permalink to this definition">¶</a></dt>
 
161
<dd><p>Packs a variable length string, <em>s</em>.  The length of the string is first packed
 
162
as an unsigned integer, then the string data is packed with
 
163
<a class="reference internal" href="#xdrlib.Packer.pack_fstring" title="xdrlib.Packer.pack_fstring"><code class="xref py py-meth docutils literal"><span class="pre">pack_fstring()</span></code></a>.</p>
 
164
</dd></dl>
 
165
 
 
166
<dl class="method">
 
167
<dt id="xdrlib.Packer.pack_opaque">
 
168
<code class="descclassname">Packer.</code><code class="descname">pack_opaque</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Packer.pack_opaque" title="Permalink to this definition">¶</a></dt>
 
169
<dd><p>Packs a variable length opaque data string, similarly to <a class="reference internal" href="#xdrlib.Packer.pack_string" title="xdrlib.Packer.pack_string"><code class="xref py py-meth docutils literal"><span class="pre">pack_string()</span></code></a>.</p>
 
170
</dd></dl>
 
171
 
 
172
<dl class="method">
 
173
<dt id="xdrlib.Packer.pack_bytes">
 
174
<code class="descclassname">Packer.</code><code class="descname">pack_bytes</code><span class="sig-paren">(</span><em>bytes</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Packer.pack_bytes" title="Permalink to this definition">¶</a></dt>
 
175
<dd><p>Packs a variable length byte stream, similarly to <a class="reference internal" href="#xdrlib.Packer.pack_string" title="xdrlib.Packer.pack_string"><code class="xref py py-meth docutils literal"><span class="pre">pack_string()</span></code></a>.</p>
 
176
</dd></dl>
 
177
 
 
178
<p>The following methods support packing arrays and lists:</p>
 
179
<dl class="method">
 
180
<dt id="xdrlib.Packer.pack_list">
 
181
<code class="descclassname">Packer.</code><code class="descname">pack_list</code><span class="sig-paren">(</span><em>list</em>, <em>pack_item</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Packer.pack_list" title="Permalink to this definition">¶</a></dt>
 
182
<dd><p>Packs a <em>list</em> of homogeneous items.  This method is useful for lists with an
 
183
indeterminate size; i.e. the size is not available until the entire list has
 
184
been walked.  For each item in the list, an unsigned integer <code class="docutils literal"><span class="pre">1</span></code> is packed
 
185
first, followed by the data value from the list.  <em>pack_item</em> is the function
 
186
that is called to pack the individual item.  At the end of the list, an unsigned
 
187
integer <code class="docutils literal"><span class="pre">0</span></code> is packed.</p>
 
188
<p>For example, to pack a list of integers, the code might appear like this:</p>
 
189
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">xdrlib</span>
 
190
<span class="n">p</span> <span class="o">=</span> <span class="n">xdrlib</span><span class="o">.</span><span class="n">Packer</span><span class="p">()</span>
 
191
<span class="n">p</span><span class="o">.</span><span class="n">pack_list</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">p</span><span class="o">.</span><span class="n">pack_int</span><span class="p">)</span>
 
192
</pre></div>
 
193
</div>
 
194
</dd></dl>
 
195
 
 
196
<dl class="method">
 
197
<dt id="xdrlib.Packer.pack_farray">
 
198
<code class="descclassname">Packer.</code><code class="descname">pack_farray</code><span class="sig-paren">(</span><em>n</em>, <em>array</em>, <em>pack_item</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Packer.pack_farray" title="Permalink to this definition">¶</a></dt>
 
199
<dd><p>Packs a fixed length list (<em>array</em>) of homogeneous items.  <em>n</em> is the length of
 
200
the list; it is <em>not</em> packed into the buffer, but a <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> exception
 
201
is raised if <code class="docutils literal"><span class="pre">len(array)</span></code> is not equal to <em>n</em>.  As above, <em>pack_item</em> is the
 
202
function used to pack each element.</p>
 
203
</dd></dl>
 
204
 
 
205
<dl class="method">
 
206
<dt id="xdrlib.Packer.pack_array">
 
207
<code class="descclassname">Packer.</code><code class="descname">pack_array</code><span class="sig-paren">(</span><em>list</em>, <em>pack_item</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Packer.pack_array" title="Permalink to this definition">¶</a></dt>
 
208
<dd><p>Packs a variable length <em>list</em> of homogeneous items.  First, the length of the
 
209
list is packed as an unsigned integer, then each element is packed as in
 
210
<a class="reference internal" href="#xdrlib.Packer.pack_farray" title="xdrlib.Packer.pack_farray"><code class="xref py py-meth docutils literal"><span class="pre">pack_farray()</span></code></a> above.</p>
 
211
</dd></dl>
 
212
 
 
213
</div>
 
214
<div class="section" id="unpacker-objects">
 
215
<span id="xdr-unpacker-objects"></span><h2>13.5.2. Unpacker Objects<a class="headerlink" href="#unpacker-objects" title="Permalink to this headline">¶</a></h2>
 
216
<p>The <a class="reference internal" href="#xdrlib.Unpacker" title="xdrlib.Unpacker"><code class="xref py py-class docutils literal"><span class="pre">Unpacker</span></code></a> class offers the following methods:</p>
 
217
<dl class="method">
 
218
<dt id="xdrlib.Unpacker.reset">
 
219
<code class="descclassname">Unpacker.</code><code class="descname">reset</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.reset" title="Permalink to this definition">¶</a></dt>
 
220
<dd><p>Resets the string buffer with the given <em>data</em>.</p>
 
221
</dd></dl>
 
222
 
 
223
<dl class="method">
 
224
<dt id="xdrlib.Unpacker.get_position">
 
225
<code class="descclassname">Unpacker.</code><code class="descname">get_position</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.get_position" title="Permalink to this definition">¶</a></dt>
 
226
<dd><p>Returns the current unpack position in the data buffer.</p>
 
227
</dd></dl>
 
228
 
 
229
<dl class="method">
 
230
<dt id="xdrlib.Unpacker.set_position">
 
231
<code class="descclassname">Unpacker.</code><code class="descname">set_position</code><span class="sig-paren">(</span><em>position</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.set_position" title="Permalink to this definition">¶</a></dt>
 
232
<dd><p>Sets the data buffer unpack position to <em>position</em>.  You should be careful about
 
233
using <a class="reference internal" href="#xdrlib.Unpacker.get_position" title="xdrlib.Unpacker.get_position"><code class="xref py py-meth docutils literal"><span class="pre">get_position()</span></code></a> and <a class="reference internal" href="#xdrlib.Unpacker.set_position" title="xdrlib.Unpacker.set_position"><code class="xref py py-meth docutils literal"><span class="pre">set_position()</span></code></a>.</p>
 
234
</dd></dl>
 
235
 
 
236
<dl class="method">
 
237
<dt id="xdrlib.Unpacker.get_buffer">
 
238
<code class="descclassname">Unpacker.</code><code class="descname">get_buffer</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.get_buffer" title="Permalink to this definition">¶</a></dt>
 
239
<dd><p>Returns the current unpack data buffer as a string.</p>
 
240
</dd></dl>
 
241
 
 
242
<dl class="method">
 
243
<dt id="xdrlib.Unpacker.done">
 
244
<code class="descclassname">Unpacker.</code><code class="descname">done</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.done" title="Permalink to this definition">¶</a></dt>
 
245
<dd><p>Indicates unpack completion.  Raises an <a class="reference internal" href="#xdrlib.Error" title="xdrlib.Error"><code class="xref py py-exc docutils literal"><span class="pre">Error</span></code></a> exception if all of the
 
246
data has not been unpacked.</p>
 
247
</dd></dl>
 
248
 
 
249
<p>In addition, every data type that can be packed with a <a class="reference internal" href="#xdrlib.Packer" title="xdrlib.Packer"><code class="xref py py-class docutils literal"><span class="pre">Packer</span></code></a>, can be
 
250
unpacked with an <a class="reference internal" href="#xdrlib.Unpacker" title="xdrlib.Unpacker"><code class="xref py py-class docutils literal"><span class="pre">Unpacker</span></code></a>.  Unpacking methods are of the form
 
251
<code class="docutils literal"><span class="pre">unpack_type()</span></code>, and take no arguments.  They return the unpacked object.</p>
 
252
<dl class="method">
 
253
<dt id="xdrlib.Unpacker.unpack_float">
 
254
<code class="descclassname">Unpacker.</code><code class="descname">unpack_float</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.unpack_float" title="Permalink to this definition">¶</a></dt>
 
255
<dd><p>Unpacks a single-precision floating point number.</p>
 
256
</dd></dl>
 
257
 
 
258
<dl class="method">
 
259
<dt id="xdrlib.Unpacker.unpack_double">
 
260
<code class="descclassname">Unpacker.</code><code class="descname">unpack_double</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.unpack_double" title="Permalink to this definition">¶</a></dt>
 
261
<dd><p>Unpacks a double-precision floating point number, similarly to
 
262
<a class="reference internal" href="#xdrlib.Unpacker.unpack_float" title="xdrlib.Unpacker.unpack_float"><code class="xref py py-meth docutils literal"><span class="pre">unpack_float()</span></code></a>.</p>
 
263
</dd></dl>
 
264
 
 
265
<p>In addition, the following methods unpack strings, bytes, and opaque data:</p>
 
266
<dl class="method">
 
267
<dt id="xdrlib.Unpacker.unpack_fstring">
 
268
<code class="descclassname">Unpacker.</code><code class="descname">unpack_fstring</code><span class="sig-paren">(</span><em>n</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.unpack_fstring" title="Permalink to this definition">¶</a></dt>
 
269
<dd><p>Unpacks and returns a fixed length string.  <em>n</em> is the number of characters
 
270
expected.  Padding with null bytes to guaranteed 4 byte alignment is assumed.</p>
 
271
</dd></dl>
 
272
 
 
273
<dl class="method">
 
274
<dt id="xdrlib.Unpacker.unpack_fopaque">
 
275
<code class="descclassname">Unpacker.</code><code class="descname">unpack_fopaque</code><span class="sig-paren">(</span><em>n</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.unpack_fopaque" title="Permalink to this definition">¶</a></dt>
 
276
<dd><p>Unpacks and returns a fixed length opaque data stream, similarly to
 
277
<a class="reference internal" href="#xdrlib.Unpacker.unpack_fstring" title="xdrlib.Unpacker.unpack_fstring"><code class="xref py py-meth docutils literal"><span class="pre">unpack_fstring()</span></code></a>.</p>
 
278
</dd></dl>
 
279
 
 
280
<dl class="method">
 
281
<dt id="xdrlib.Unpacker.unpack_string">
 
282
<code class="descclassname">Unpacker.</code><code class="descname">unpack_string</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.unpack_string" title="Permalink to this definition">¶</a></dt>
 
283
<dd><p>Unpacks and returns a variable length string.  The length of the string is first
 
284
unpacked as an unsigned integer, then the string data is unpacked with
 
285
<a class="reference internal" href="#xdrlib.Unpacker.unpack_fstring" title="xdrlib.Unpacker.unpack_fstring"><code class="xref py py-meth docutils literal"><span class="pre">unpack_fstring()</span></code></a>.</p>
 
286
</dd></dl>
 
287
 
 
288
<dl class="method">
 
289
<dt id="xdrlib.Unpacker.unpack_opaque">
 
290
<code class="descclassname">Unpacker.</code><code class="descname">unpack_opaque</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.unpack_opaque" title="Permalink to this definition">¶</a></dt>
 
291
<dd><p>Unpacks and returns a variable length opaque data string, similarly to
 
292
<a class="reference internal" href="#xdrlib.Unpacker.unpack_string" title="xdrlib.Unpacker.unpack_string"><code class="xref py py-meth docutils literal"><span class="pre">unpack_string()</span></code></a>.</p>
 
293
</dd></dl>
 
294
 
 
295
<dl class="method">
 
296
<dt id="xdrlib.Unpacker.unpack_bytes">
 
297
<code class="descclassname">Unpacker.</code><code class="descname">unpack_bytes</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.unpack_bytes" title="Permalink to this definition">¶</a></dt>
 
298
<dd><p>Unpacks and returns a variable length byte stream, similarly to
 
299
<a class="reference internal" href="#xdrlib.Unpacker.unpack_string" title="xdrlib.Unpacker.unpack_string"><code class="xref py py-meth docutils literal"><span class="pre">unpack_string()</span></code></a>.</p>
 
300
</dd></dl>
 
301
 
 
302
<p>The following methods support unpacking arrays and lists:</p>
 
303
<dl class="method">
 
304
<dt id="xdrlib.Unpacker.unpack_list">
 
305
<code class="descclassname">Unpacker.</code><code class="descname">unpack_list</code><span class="sig-paren">(</span><em>unpack_item</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.unpack_list" title="Permalink to this definition">¶</a></dt>
 
306
<dd><p>Unpacks and returns a list of homogeneous items.  The list is unpacked one
 
307
element at a time by first unpacking an unsigned integer flag.  If the flag is
 
308
<code class="docutils literal"><span class="pre">1</span></code>, then the item is unpacked and appended to the list.  A flag of <code class="docutils literal"><span class="pre">0</span></code>
 
309
indicates the end of the list.  <em>unpack_item</em> is the function that is called to
 
310
unpack the items.</p>
 
311
</dd></dl>
 
312
 
 
313
<dl class="method">
 
314
<dt id="xdrlib.Unpacker.unpack_farray">
 
315
<code class="descclassname">Unpacker.</code><code class="descname">unpack_farray</code><span class="sig-paren">(</span><em>n</em>, <em>unpack_item</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.unpack_farray" title="Permalink to this definition">¶</a></dt>
 
316
<dd><p>Unpacks and returns (as a list) a fixed length array of homogeneous items.  <em>n</em>
 
317
is number of list elements to expect in the buffer. As above, <em>unpack_item</em> is
 
318
the function used to unpack each element.</p>
 
319
</dd></dl>
 
320
 
 
321
<dl class="method">
 
322
<dt id="xdrlib.Unpacker.unpack_array">
 
323
<code class="descclassname">Unpacker.</code><code class="descname">unpack_array</code><span class="sig-paren">(</span><em>unpack_item</em><span class="sig-paren">)</span><a class="headerlink" href="#xdrlib.Unpacker.unpack_array" title="Permalink to this definition">¶</a></dt>
 
324
<dd><p>Unpacks and returns a variable length <em>list</em> of homogeneous items. First, the
 
325
length of the list is unpacked as an unsigned integer, then each element is
 
326
unpacked as in <a class="reference internal" href="#xdrlib.Unpacker.unpack_farray" title="xdrlib.Unpacker.unpack_farray"><code class="xref py py-meth docutils literal"><span class="pre">unpack_farray()</span></code></a> above.</p>
 
327
</dd></dl>
 
328
 
 
329
</div>
 
330
<div class="section" id="exceptions">
 
331
<span id="xdr-exceptions"></span><h2>13.5.3. Exceptions<a class="headerlink" href="#exceptions" title="Permalink to this headline">¶</a></h2>
 
332
<p>Exceptions in this module are coded as class instances:</p>
 
333
<dl class="exception">
 
334
<dt id="xdrlib.Error">
 
335
<em class="property">exception </em><code class="descclassname">xdrlib.</code><code class="descname">Error</code><a class="headerlink" href="#xdrlib.Error" title="Permalink to this definition">¶</a></dt>
 
336
<dd><p>The base exception class.  <a class="reference internal" href="#xdrlib.Error" title="xdrlib.Error"><code class="xref py py-exc docutils literal"><span class="pre">Error</span></code></a> has a single public attribute
 
337
<code class="xref py py-attr docutils literal"><span class="pre">msg</span></code> containing the description of the error.</p>
 
338
</dd></dl>
 
339
 
 
340
<dl class="exception">
 
341
<dt id="xdrlib.ConversionError">
 
342
<em class="property">exception </em><code class="descclassname">xdrlib.</code><code class="descname">ConversionError</code><a class="headerlink" href="#xdrlib.ConversionError" title="Permalink to this definition">¶</a></dt>
 
343
<dd><p>Class derived from <a class="reference internal" href="#xdrlib.Error" title="xdrlib.Error"><code class="xref py py-exc docutils literal"><span class="pre">Error</span></code></a>.  Contains no additional instance variables.</p>
 
344
</dd></dl>
 
345
 
 
346
<p>Here is an example of how you would catch one of these exceptions:</p>
 
347
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">xdrlib</span>
 
348
<span class="n">p</span> <span class="o">=</span> <span class="n">xdrlib</span><span class="o">.</span><span class="n">Packer</span><span class="p">()</span>
 
349
<span class="k">try</span><span class="p">:</span>
 
350
    <span class="n">p</span><span class="o">.</span><span class="n">pack_double</span><span class="p">(</span><span class="mf">8.01</span><span class="p">)</span>
 
351
<span class="k">except</span> <span class="n">xdrlib</span><span class="o">.</span><span class="n">ConversionError</span> <span class="k">as</span> <span class="n">instance</span><span class="p">:</span>
 
352
    <span class="k">print</span> <span class="s1">&#39;packing the double failed:&#39;</span><span class="p">,</span> <span class="n">instance</span><span class="o">.</span><span class="n">msg</span>
 
353
</pre></div>
 
354
</div>
 
355
</div>
 
356
</div>
 
357
 
 
358
 
 
359
          </div>
 
360
        </div>
 
361
      </div>
 
362
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
 
363
        <div class="sphinxsidebarwrapper">
 
364
  <h3><a href="../contents.html">Table Of Contents</a></h3>
 
365
  <ul>
 
366
<li><a class="reference internal" href="#">13.5. <code class="docutils literal"><span class="pre">xdrlib</span></code> &#8212; Encode and decode XDR data</a><ul>
 
367
<li><a class="reference internal" href="#packer-objects">13.5.1. Packer Objects</a></li>
 
368
<li><a class="reference internal" href="#unpacker-objects">13.5.2. Unpacker Objects</a></li>
 
369
<li><a class="reference internal" href="#exceptions">13.5.3. Exceptions</a></li>
 
370
</ul>
 
371
</li>
 
372
</ul>
 
373
 
 
374
  <h4>Previous topic</h4>
 
375
  <p class="topless"><a href="netrc.html"
 
376
                        title="previous chapter">13.4. <code class="docutils literal"><span class="pre">netrc</span></code> &#8212; netrc file processing</a></p>
 
377
  <h4>Next topic</h4>
 
378
  <p class="topless"><a href="plistlib.html"
 
379
                        title="next chapter">13.6. <code class="docutils literal"><span class="pre">plistlib</span></code> &#8212; Generate and parse Mac OS X <code class="docutils literal"><span class="pre">.plist</span></code> files</a></p>
 
380
<h3>This Page</h3>
 
381
<ul class="this-page-menu">
 
382
  <li><a href="../bugs.html">Report a Bug</a></li>
 
383
  <li><a href="../_sources/library/xdrlib.txt"
 
384
         rel="nofollow">Show Source</a></li>
 
385
</ul>
 
386
 
 
387
<div id="searchbox" style="display: none" role="search">
 
388
  <h3>Quick search</h3>
 
389
    <form class="search" action="../search.html" method="get">
 
390
      <input type="text" name="q" />
 
391
      <input type="submit" value="Go" />
 
392
      <input type="hidden" name="check_keywords" value="yes" />
 
393
      <input type="hidden" name="area" value="default" />
 
394
    </form>
 
395
    <p class="searchtip" style="font-size: 90%">
 
396
    Enter search terms or a module, class or function name.
 
397
    </p>
 
398
</div>
 
399
<script type="text/javascript">$('#searchbox').show(0);</script>
 
400
        </div>
 
401
      </div>
 
402
      <div class="clearer"></div>
 
403
    </div>  
 
404
    <div class="related" role="navigation" aria-label="related navigation">
 
405
      <h3>Navigation</h3>
 
406
      <ul>
 
407
        <li class="right" style="margin-right: 10px">
 
408
          <a href="../genindex.html" title="General Index"
 
409
             >index</a></li>
 
410
        <li class="right" >
 
411
          <a href="../py-modindex.html" title="Python Module Index"
 
412
             >modules</a> |</li>
 
413
        <li class="right" >
 
414
          <a href="plistlib.html" title="13.6. plistlib — Generate and parse Mac OS X .plist files"
 
415
             >next</a> |</li>
 
416
        <li class="right" >
 
417
          <a href="netrc.html" title="13.4. netrc — netrc file processing"
 
418
             >previous</a> |</li>
 
419
        <li><img src="../_static/py.png" alt=""
 
420
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
421
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
422
        <li>
 
423
          <span class="version_switcher_placeholder">2.7.12</span>
 
424
          <a href="../index.html">Documentation</a> &raquo;
 
425
        </li>
 
426
 
 
427
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
428
          <li class="nav-item nav-item-2"><a href="fileformats.html" >13. File Formats</a> &raquo;</li> 
 
429
      </ul>
 
430
    </div>  
 
431
    <div class="footer">
 
432
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
 
433
    <br />
 
434
    The Python Software Foundation is a non-profit corporation.
 
435
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
436
    <br />
 
437
    Last updated on Sep 20, 2016.
 
438
    <a href="../bugs.html">Found a bug</a>?
 
439
    <br />
 
440
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
441
    </div>
 
442
 
 
443
  </body>
 
444
</html>
 
 
b'\\ No newline at end of file'