1:mod:`xdrlib` --- Encode and decode XDR data
2============================================
3
4.. module:: xdrlib
5   :synopsis: Encoders and decoders for the External Data Representation (XDR).
6   :deprecated:
7
8**Source code:** :source:`Lib/xdrlib.py`
9
10.. index::
11   single: XDR
12   single: External Data Representation
13
14.. deprecated-removed:: 3.11 3.13
15   The :mod:`xdrlib` module is deprecated
16   (see :pep:`PEP 594 <594#xdrlib>` for details).
17
18--------------
19
20The :mod:`xdrlib` module supports the External Data Representation Standard as
21described in :rfc:`1014`, written by Sun Microsystems, Inc. June 1987.  It
22supports most of the data types described in the RFC.
23
24The :mod:`xdrlib` module defines two classes, one for packing variables into XDR
25representation, and another for unpacking from XDR representation.  There are
26also two exception classes.
27
28
29.. class:: Packer()
30
31   :class:`Packer` is the class for packing data into XDR representation. The
32   :class:`Packer` class is instantiated with no arguments.
33
34
35.. class:: Unpacker(data)
36
37   ``Unpacker`` is the complementary class which unpacks XDR data values from a
38   string buffer.  The input buffer is given as *data*.
39
40
41.. seealso::
42
43   :rfc:`1014` - XDR: External Data Representation Standard
44      This RFC defined the encoding of data which was XDR at the time this module was
45      originally written.  It has apparently been obsoleted by :rfc:`1832`.
46
47   :rfc:`1832` - XDR: External Data Representation Standard
48      Newer RFC that provides a revised definition of XDR.
49
50
51.. _xdr-packer-objects:
52
53Packer Objects
54--------------
55
56:class:`Packer` instances have the following methods:
57
58
59.. method:: Packer.get_buffer()
60
61   Returns the current pack buffer as a string.
62
63
64.. method:: Packer.reset()
65
66   Resets the pack buffer to the empty string.
67
68In general, you can pack any of the most common XDR data types by calling the
69appropriate ``pack_type()`` method.  Each method takes a single argument, the
70value to pack.  The following simple data type packing methods are supported:
71:meth:`pack_uint`, :meth:`pack_int`, :meth:`pack_enum`, :meth:`pack_bool`,
72:meth:`pack_uhyper`, and :meth:`pack_hyper`.
73
74
75.. method:: Packer.pack_float(value)
76
77   Packs the single-precision floating point number *value*.
78
79
80.. method:: Packer.pack_double(value)
81
82   Packs the double-precision floating point number *value*.
83
84The following methods support packing strings, bytes, and opaque data:
85
86
87.. method:: Packer.pack_fstring(n, s)
88
89   Packs a fixed length string, *s*.  *n* is the length of the string but it is
90   *not* packed into the data buffer.  The string is padded with null bytes if
91   necessary to guaranteed 4 byte alignment.
92
93
94.. method:: Packer.pack_fopaque(n, data)
95
96   Packs a fixed length opaque data stream, similarly to :meth:`pack_fstring`.
97
98
99.. method:: Packer.pack_string(s)
100
101   Packs a variable length string, *s*.  The length of the string is first packed
102   as an unsigned integer, then the string data is packed with
103   :meth:`pack_fstring`.
104
105
106.. method:: Packer.pack_opaque(data)
107
108   Packs a variable length opaque data string, similarly to :meth:`pack_string`.
109
110
111.. method:: Packer.pack_bytes(bytes)
112
113   Packs a variable length byte stream, similarly to :meth:`pack_string`.
114
115The following methods support packing arrays and lists:
116
117
118.. method:: Packer.pack_list(list, pack_item)
119
120   Packs a *list* of homogeneous items.  This method is useful for lists with an
121   indeterminate size; i.e. the size is not available until the entire list has
122   been walked.  For each item in the list, an unsigned integer ``1`` is packed
123   first, followed by the data value from the list.  *pack_item* is the function
124   that is called to pack the individual item.  At the end of the list, an unsigned
125   integer ``0`` is packed.
126
127   For example, to pack a list of integers, the code might appear like this::
128
129      import xdrlib
130      p = xdrlib.Packer()
131      p.pack_list([1, 2, 3], p.pack_int)
132
133
134.. method:: Packer.pack_farray(n, array, pack_item)
135
136   Packs a fixed length list (*array*) of homogeneous items.  *n* is the length of
137   the list; it is *not* packed into the buffer, but a :exc:`ValueError` exception
138   is raised if ``len(array)`` is not equal to *n*.  As above, *pack_item* is the
139   function used to pack each element.
140
141
142.. method:: Packer.pack_array(list, pack_item)
143
144   Packs a variable length *list* of homogeneous items.  First, the length of the
145   list is packed as an unsigned integer, then each element is packed as in
146   :meth:`pack_farray` above.
147
148
149.. _xdr-unpacker-objects:
150
151Unpacker Objects
152----------------
153
154The :class:`Unpacker` class offers the following methods:
155
156
157.. method:: Unpacker.reset(data)
158
159   Resets the string buffer with the given *data*.
160
161
162.. method:: Unpacker.get_position()
163
164   Returns the current unpack position in the data buffer.
165
166
167.. method:: Unpacker.set_position(position)
168
169   Sets the data buffer unpack position to *position*.  You should be careful about
170   using :meth:`get_position` and :meth:`set_position`.
171
172
173.. method:: Unpacker.get_buffer()
174
175   Returns the current unpack data buffer as a string.
176
177
178.. method:: Unpacker.done()
179
180   Indicates unpack completion.  Raises an :exc:`Error` exception if all of the
181   data has not been unpacked.
182
183In addition, every data type that can be packed with a :class:`Packer`, can be
184unpacked with an :class:`Unpacker`.  Unpacking methods are of the form
185``unpack_type()``, and take no arguments.  They return the unpacked object.
186
187
188.. method:: Unpacker.unpack_float()
189
190   Unpacks a single-precision floating point number.
191
192
193.. method:: Unpacker.unpack_double()
194
195   Unpacks a double-precision floating point number, similarly to
196   :meth:`unpack_float`.
197
198In addition, the following methods unpack strings, bytes, and opaque data:
199
200
201.. method:: Unpacker.unpack_fstring(n)
202
203   Unpacks and returns a fixed length string.  *n* is the number of characters
204   expected.  Padding with null bytes to guaranteed 4 byte alignment is assumed.
205
206
207.. method:: Unpacker.unpack_fopaque(n)
208
209   Unpacks and returns a fixed length opaque data stream, similarly to
210   :meth:`unpack_fstring`.
211
212
213.. method:: Unpacker.unpack_string()
214
215   Unpacks and returns a variable length string.  The length of the string is first
216   unpacked as an unsigned integer, then the string data is unpacked with
217   :meth:`unpack_fstring`.
218
219
220.. method:: Unpacker.unpack_opaque()
221
222   Unpacks and returns a variable length opaque data string, similarly to
223   :meth:`unpack_string`.
224
225
226.. method:: Unpacker.unpack_bytes()
227
228   Unpacks and returns a variable length byte stream, similarly to
229   :meth:`unpack_string`.
230
231The following methods support unpacking arrays and lists:
232
233
234.. method:: Unpacker.unpack_list(unpack_item)
235
236   Unpacks and returns a list of homogeneous items.  The list is unpacked one
237   element at a time by first unpacking an unsigned integer flag.  If the flag is
238   ``1``, then the item is unpacked and appended to the list.  A flag of ``0``
239   indicates the end of the list.  *unpack_item* is the function that is called to
240   unpack the items.
241
242
243.. method:: Unpacker.unpack_farray(n, unpack_item)
244
245   Unpacks and returns (as a list) a fixed length array of homogeneous items.  *n*
246   is number of list elements to expect in the buffer. As above, *unpack_item* is
247   the function used to unpack each element.
248
249
250.. method:: Unpacker.unpack_array(unpack_item)
251
252   Unpacks and returns a variable length *list* of homogeneous items. First, the
253   length of the list is unpacked as an unsigned integer, then each element is
254   unpacked as in :meth:`unpack_farray` above.
255
256
257.. _xdr-exceptions:
258
259Exceptions
260----------
261
262Exceptions in this module are coded as class instances:
263
264
265.. exception:: Error
266
267   The base exception class.  :exc:`Error` has a single public attribute
268   :attr:`msg` containing the description of the error.
269
270
271.. exception:: ConversionError
272
273   Class derived from :exc:`Error`.  Contains no additional instance variables.
274
275Here is an example of how you would catch one of these exceptions::
276
277   import xdrlib
278   p = xdrlib.Packer()
279   try:
280       p.pack_double(8.01)
281   except xdrlib.ConversionError as instance:
282       print('packing the double failed:', instance.msg)
283
284