blob: 66fa4b4366f767a3ef9ca96a6ae45e9940dcbb67 [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
Fred Drakeae18e9f1997-10-24 21:14:36 +000012RFC.
Guido van Rossum40006cf1996-08-19 22:58:03 +000013
14The \code{xdrlib} module defines two classes, one for packing
15variables into XDR representation, and another for unpacking from XDR
16representation. There are also two exception classes.
17
18
19\subsection{Packer Objects}
20
21\code{Packer} is the class for packing data into XDR representation.
22The \code{Packer} class is instantiated with no arguments.
23
24\begin{funcdesc}{get_buffer}{}
25Returns the current pack buffer as a string.
26\end{funcdesc}
27
28\begin{funcdesc}{reset}{}
29Resets the pack buffer to the empty string.
30\end{funcdesc}
31
32In general, you can pack any of the most common XDR data types by
33calling the appropriate \code{pack_\var{type}} method. Each method
34takes a single argument, the value to pack. The following simple data
35type packing methods are supported: \code{pack_uint}, \code{pack_int},
36\code{pack_enum}, \code{pack_bool}, \code{pack_uhyper},
37and \code{pack_hyper}.
38
Guido van Rossum40006cf1996-08-19 22:58:03 +000039\begin{funcdesc}{pack_float}{value}
40Packs the single-precision floating point number \var{value}.
41\end{funcdesc}
42
43\begin{funcdesc}{pack_double}{value}
44Packs the double-precision floating point number \var{value}.
45\end{funcdesc}
46
47The following methods support packing strings, bytes, and opaque data:
48
49\begin{funcdesc}{pack_fstring}{n\, s}
50Packs a fixed length string, \var{s}. \var{n} is the length of the
51string but it is \emph{not} packed into the data buffer. The string
52is padded with null bytes if necessary to guaranteed 4 byte alignment.
53\end{funcdesc}
54
55\begin{funcdesc}{pack_fopaque}{n\, data}
56Packs a fixed length opaque data stream, similarly to
57\code{pack_fstring}.
58\end{funcdesc}
59
60\begin{funcdesc}{pack_string}{s}
61Packs a variable length string, \var{s}. The length of the string is
62first packed as an unsigned integer, then the string data is packed
63with \code{pack_fstring}.
64\end{funcdesc}
65
66\begin{funcdesc}{pack_opaque}{data}
67Packs a variable length opaque data string, similarly to
68\code{pack_string}.
69\end{funcdesc}
70
71\begin{funcdesc}{pack_bytes}{bytes}
72Packs a variable length byte stream, similarly to \code{pack_string}.
73\end{funcdesc}
74
75The following methods support packing arrays and lists:
76
77\begin{funcdesc}{pack_list}{list\, pack_item}
78Packs a \var{list} of homogeneous items. This method is useful for
79lists with an indeterminate size; i.e. the size is not available until
80the entire list has been walked. For each item in the list, an
81unsigned integer \code{1} is packed first, followed by the data value
82from the list. \var{pack_item} is the function that is called to pack
83the individual item. At the end of the list, an unsigned integer
84\code{0} is packed.
85\end{funcdesc}
86
87\begin{funcdesc}{pack_farray}{n\, array\, pack_item}
88Packs a fixed length list (\var{array}) of homogeneous items. \var{n}
89is the length of the list; it is \emph{not} packed into the buffer,
90but a \code{ValueError} exception is raised if \code{len(array)} is not
91equal to \var{n}. As above, \var{pack_item} is the function used to
92pack each element.
93\end{funcdesc}
94
95\begin{funcdesc}{pack_array}{list\, pack_item}
96Packs a variable length \var{list} of homogeneous items. First, the
97length of the list is packed as an unsigned integer, then each element
98is packed as in \code{pack_farray} above.
99\end{funcdesc}
100
101\subsection{Unpacker Objects}
102
103\code{Unpacker} is the complementary class which unpacks XDR data
104values from a string buffer, and has the following methods:
105
106\begin{funcdesc}{__init__}{data}
107Instantiates an \code{Unpacker} object with the string buffer
108\var{data}.
109\end{funcdesc}
110
111\begin{funcdesc}{reset}{data}
112Resets the string buffer with the given \var{data}.
113\end{funcdesc}
114
115\begin{funcdesc}{get_position}{}
116Returns the current unpack position in the data buffer.
117\end{funcdesc}
118
119\begin{funcdesc}{set_position}{position}
120Sets the data buffer unpack position to \var{position}. You should be
121careful about using \code{get_position()} and \code{set_position()}.
122\end{funcdesc}
123
Barry Warsaw102dc411996-12-04 22:05:42 +0000124\begin{funcdesc}{get_buffer}{}
125Returns the current unpack data buffer as a string.
126\end{funcdesc}
127
Guido van Rossum40006cf1996-08-19 22:58:03 +0000128\begin{funcdesc}{done}{}
129Indicates unpack completion. Raises an \code{xdrlib.Error} exception
130if all of the data has not been unpacked.
131\end{funcdesc}
132
133In addition, every data type that can be packed with a \code{Packer},
134can be unpacked with an \code{Unpacker}. Unpacking methods are of the
135form \code{unpack_\var{type}}, and take no arguments. They return the
Fred Drake040e5651997-10-24 21:15:55 +0000136unpacked object.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000137
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000138\begin{funcdesc}{unpack_float}{}
139Unpacks a single-precision floating point number.
140\end{funcdesc}
141
142\begin{funcdesc}{unpack_double}{}
143Unpacks a double-precision floating point number, similarly to
144\code{unpack_float}.
145\end{funcdesc}
146
Guido van Rossum40006cf1996-08-19 22:58:03 +0000147In addition, the following methods unpack strings, bytes, and opaque
148data:
149
150\begin{funcdesc}{unpack_fstring}{n}
151Unpacks and returns a fixed length string. \var{n} is the number of
152characters expected. Padding with null bytes to guaranteed 4 byte
153alignment is assumed.
154\end{funcdesc}
155
156\begin{funcdesc}{unpack_fopaque}{n}
157Unpacks and returns a fixed length opaque data stream, similarly to
158\code{unpack_fstring}.
159\end{funcdesc}
160
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000161\begin{funcdesc}{unpack_string}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000162Unpacks and returns a variable length string. The length of the
163string is first unpacked as an unsigned integer, then the string data
164is unpacked with \code{unpack_fstring}.
165\end{funcdesc}
166
167\begin{funcdesc}{unpack_opaque}{}
168Unpacks and returns a variable length opaque data string, similarly to
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000169\code{unpack_string}.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000170\end{funcdesc}
171
172\begin{funcdesc}{unpack_bytes}{}
173Unpacks and returns a variable length byte stream, similarly to
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000174\code{unpack_string}.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000175\end{funcdesc}
176
177The following methods support unpacking arrays and lists:
178
179\begin{funcdesc}{unpack_list}{unpack_item}
180Unpacks and returns a list of homogeneous items. The list is unpacked
181one element at a time
182by first unpacking an unsigned integer flag. If the flag is \code{1},
183then the item is unpacked and appended to the list. A flag of
184\code{0} indicates the end of the list. \var{unpack_item} is the
185function that is called to unpack the items.
186\end{funcdesc}
187
188\begin{funcdesc}{unpack_farray}{n\, unpack_item}
189Unpacks and returns (as a list) a fixed length array of homogeneous
190items. \var{n} is number of list elements to expect in the buffer.
191As above, \var{unpack_item} is the function used to unpack each element.
192\end{funcdesc}
193
194\begin{funcdesc}{unpack_array}{unpack_item}
195Unpacks and returns a variable length \var{list} of homogeneous items.
196First, the length of the list is unpacked as an unsigned integer, then
197each element is unpacked as in \code{unpack_farray} above.
198\end{funcdesc}
199
200\subsection{Exceptions}
Fred Drake4b3f0311996-12-13 22:04:31 +0000201\nodename{Exceptions in xdrlib module}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000202
203Exceptions in this module are coded as class instances:
204
205\begin{excdesc}{Error}
206The base exception class. \code{Error} has a single public data
207member \code{msg} containing the description of the error.
208\end{excdesc}
209
210\begin{excdesc}{ConversionError}
211Class derived from \code{Error}. Contains no additional instance
212variables.
213\end{excdesc}
214
215Here is an example of how you would catch one of these exceptions:
216
Guido van Rossume47da0a1997-07-17 16:34:52 +0000217\bcode\begin{verbatim}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000218import xdrlib
219p = xdrlib.Packer()
220try:
221 p.pack_double(8.01)
222except xdrlib.ConversionError, instance:
223 print 'packing the double failed:', instance.msg
Guido van Rossume47da0a1997-07-17 16:34:52 +0000224\end{verbatim}\ecode