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