blob: 0de56957a5c612374538e8c5a48b04da8749e6f0 [file] [log] [blame]
Guido van Rossum40006cf1996-08-19 22:58:03 +00001\section{Standard module \sectcode{xdrlib}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-xdrlib}
Guido van Rossum40006cf1996-08-19 22:58:03 +00003\stmodindex{xdrlib}
4\index{XDR}
5
6\renewcommand{\indexsubitem}{(in module xdrlib)}
7
8
9The \code{xdrlib} module supports the External Data Representation
10Standard as described in RFC 1014, written by Sun Microsystems,
11Inc. June 1987. It supports most of the data types described in the
12RFC, although some, most notably \code{float} and \code{double} are
13only supported on those operating systems that provide an XDR
14library.
15
16The \code{xdrlib} module defines two classes, one for packing
17variables into XDR representation, and another for unpacking from XDR
18representation. There are also two exception classes.
19
20
21\subsection{Packer Objects}
22
23\code{Packer} is the class for packing data into XDR representation.
24The \code{Packer} class is instantiated with no arguments.
25
26\begin{funcdesc}{get_buffer}{}
27Returns the current pack buffer as a string.
28\end{funcdesc}
29
30\begin{funcdesc}{reset}{}
31Resets the pack buffer to the empty string.
32\end{funcdesc}
33
34In general, you can pack any of the most common XDR data types by
35calling the appropriate \code{pack_\var{type}} method. Each method
36takes a single argument, the value to pack. The following simple data
37type packing methods are supported: \code{pack_uint}, \code{pack_int},
38\code{pack_enum}, \code{pack_bool}, \code{pack_uhyper},
39and \code{pack_hyper}.
40
41The following methods pack floating point numbers, however they
42require C library support. Without the optional C built-in module,
43both of these methods will raise an \code{xdrlib.ConversionError}
44exception. See the note at the end of this chapter for details.
45
46\begin{funcdesc}{pack_float}{value}
47Packs the single-precision floating point number \var{value}.
48\end{funcdesc}
49
50\begin{funcdesc}{pack_double}{value}
51Packs the double-precision floating point number \var{value}.
52\end{funcdesc}
53
54The following methods support packing strings, bytes, and opaque data:
55
56\begin{funcdesc}{pack_fstring}{n\, s}
57Packs a fixed length string, \var{s}. \var{n} is the length of the
58string but it is \emph{not} packed into the data buffer. The string
59is padded with null bytes if necessary to guaranteed 4 byte alignment.
60\end{funcdesc}
61
62\begin{funcdesc}{pack_fopaque}{n\, data}
63Packs a fixed length opaque data stream, similarly to
64\code{pack_fstring}.
65\end{funcdesc}
66
67\begin{funcdesc}{pack_string}{s}
68Packs a variable length string, \var{s}. The length of the string is
69first packed as an unsigned integer, then the string data is packed
70with \code{pack_fstring}.
71\end{funcdesc}
72
73\begin{funcdesc}{pack_opaque}{data}
74Packs a variable length opaque data string, similarly to
75\code{pack_string}.
76\end{funcdesc}
77
78\begin{funcdesc}{pack_bytes}{bytes}
79Packs a variable length byte stream, similarly to \code{pack_string}.
80\end{funcdesc}
81
82The following methods support packing arrays and lists:
83
84\begin{funcdesc}{pack_list}{list\, pack_item}
85Packs a \var{list} of homogeneous items. This method is useful for
86lists with an indeterminate size; i.e. the size is not available until
87the entire list has been walked. For each item in the list, an
88unsigned integer \code{1} is packed first, followed by the data value
89from the list. \var{pack_item} is the function that is called to pack
90the individual item. At the end of the list, an unsigned integer
91\code{0} is packed.
92\end{funcdesc}
93
94\begin{funcdesc}{pack_farray}{n\, array\, pack_item}
95Packs a fixed length list (\var{array}) of homogeneous items. \var{n}
96is the length of the list; it is \emph{not} packed into the buffer,
97but a \code{ValueError} exception is raised if \code{len(array)} is not
98equal to \var{n}. As above, \var{pack_item} is the function used to
99pack each element.
100\end{funcdesc}
101
102\begin{funcdesc}{pack_array}{list\, pack_item}
103Packs a variable length \var{list} of homogeneous items. First, the
104length of the list is packed as an unsigned integer, then each element
105is packed as in \code{pack_farray} above.
106\end{funcdesc}
107
108\subsection{Unpacker Objects}
109
110\code{Unpacker} is the complementary class which unpacks XDR data
111values from a string buffer, and has the following methods:
112
113\begin{funcdesc}{__init__}{data}
114Instantiates an \code{Unpacker} object with the string buffer
115\var{data}.
116\end{funcdesc}
117
118\begin{funcdesc}{reset}{data}
119Resets the string buffer with the given \var{data}.
120\end{funcdesc}
121
122\begin{funcdesc}{get_position}{}
123Returns the current unpack position in the data buffer.
124\end{funcdesc}
125
126\begin{funcdesc}{set_position}{position}
127Sets the data buffer unpack position to \var{position}. You should be
128careful about using \code{get_position()} and \code{set_position()}.
129\end{funcdesc}
130
Barry Warsaw102dc411996-12-04 22:05:42 +0000131\begin{funcdesc}{get_buffer}{}
132Returns the current unpack data buffer as a string.
133\end{funcdesc}
134
Guido van Rossum40006cf1996-08-19 22:58:03 +0000135\begin{funcdesc}{done}{}
136Indicates unpack completion. Raises an \code{xdrlib.Error} exception
137if all of the data has not been unpacked.
138\end{funcdesc}
139
140In addition, every data type that can be packed with a \code{Packer},
141can be unpacked with an \code{Unpacker}. Unpacking methods are of the
142form \code{unpack_\var{type}}, and take no arguments. They return the
143unpacked object. The same caveats apply for \code{unpack_float} and
144\code{unpack_double} as above.
145
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000146\begin{funcdesc}{unpack_float}{}
147Unpacks a single-precision floating point number.
148\end{funcdesc}
149
150\begin{funcdesc}{unpack_double}{}
151Unpacks a double-precision floating point number, similarly to
152\code{unpack_float}.
153\end{funcdesc}
154
Guido van Rossum40006cf1996-08-19 22:58:03 +0000155In addition, the following methods unpack strings, bytes, and opaque
156data:
157
158\begin{funcdesc}{unpack_fstring}{n}
159Unpacks and returns a fixed length string. \var{n} is the number of
160characters expected. Padding with null bytes to guaranteed 4 byte
161alignment is assumed.
162\end{funcdesc}
163
164\begin{funcdesc}{unpack_fopaque}{n}
165Unpacks and returns a fixed length opaque data stream, similarly to
166\code{unpack_fstring}.
167\end{funcdesc}
168
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000169\begin{funcdesc}{unpack_string}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000170Unpacks and returns a variable length string. The length of the
171string is first unpacked as an unsigned integer, then the string data
172is unpacked with \code{unpack_fstring}.
173\end{funcdesc}
174
175\begin{funcdesc}{unpack_opaque}{}
176Unpacks and returns a variable length opaque data string, similarly to
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000177\code{unpack_string}.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000178\end{funcdesc}
179
180\begin{funcdesc}{unpack_bytes}{}
181Unpacks and returns a variable length byte stream, similarly to
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000182\code{unpack_string}.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000183\end{funcdesc}
184
185The following methods support unpacking arrays and lists:
186
187\begin{funcdesc}{unpack_list}{unpack_item}
188Unpacks and returns a list of homogeneous items. The list is unpacked
189one element at a time
190by first unpacking an unsigned integer flag. If the flag is \code{1},
191then the item is unpacked and appended to the list. A flag of
192\code{0} indicates the end of the list. \var{unpack_item} is the
193function that is called to unpack the items.
194\end{funcdesc}
195
196\begin{funcdesc}{unpack_farray}{n\, unpack_item}
197Unpacks and returns (as a list) a fixed length array of homogeneous
198items. \var{n} is number of list elements to expect in the buffer.
199As above, \var{unpack_item} is the function used to unpack each element.
200\end{funcdesc}
201
202\begin{funcdesc}{unpack_array}{unpack_item}
203Unpacks and returns a variable length \var{list} of homogeneous items.
204First, the length of the list is unpacked as an unsigned integer, then
205each element is unpacked as in \code{unpack_farray} above.
206\end{funcdesc}
207
208\subsection{Exceptions}
Fred Drake4b3f0311996-12-13 22:04:31 +0000209\nodename{Exceptions in xdrlib module}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000210
211Exceptions in this module are coded as class instances:
212
213\begin{excdesc}{Error}
214The base exception class. \code{Error} has a single public data
215member \code{msg} containing the description of the error.
216\end{excdesc}
217
218\begin{excdesc}{ConversionError}
219Class derived from \code{Error}. Contains no additional instance
220variables.
221\end{excdesc}
222
223Here is an example of how you would catch one of these exceptions:
224
Guido van Rossume47da0a1997-07-17 16:34:52 +0000225\bcode\begin{verbatim}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000226import xdrlib
227p = xdrlib.Packer()
228try:
229 p.pack_double(8.01)
230except xdrlib.ConversionError, instance:
231 print 'packing the double failed:', instance.msg
Guido van Rossume47da0a1997-07-17 16:34:52 +0000232\end{verbatim}\ecode
233%
Guido van Rossum40006cf1996-08-19 22:58:03 +0000234\subsection{Supporting Floating Point Data}
235
236Packing and unpacking floating point data,
237i.e. \code{Packer.pack_float}, \code{Packer.pack_double},
238\code{Unpacker.unpack_float}, and \code{Unpacker.unpack_double}, are
239only supported with the helper built-in \code{_xdr} module, which
240relies on your operating system having the appropriate XDR library
241routines.
242
243If you have built the Python interpeter with the \code{_xdr} module,
244or have built the \code{_xdr} module as a shared library,
245\code{xdrlib} will use these to pack and unpack floating point
246numbers. Otherwise, using these routines will raise a
247\code{ConversionError} exception.
248
249See the Python installation instructions for details on building the
250\code{_xdr} module.