blob: a219f40539f7b44af9ef2011f62cc9730ee06b50 [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
39The following methods pack floating point numbers, however they
40require C library support. Without the optional C built-in module,
41both of these methods will raise an \code{xdrlib.ConversionError}
42exception. See the note at the end of this chapter for details.
43
44\begin{funcdesc}{pack_float}{value}
45Packs the single-precision floating point number \var{value}.
46\end{funcdesc}
47
48\begin{funcdesc}{pack_double}{value}
49Packs the double-precision floating point number \var{value}.
50\end{funcdesc}
51
52The following methods support packing strings, bytes, and opaque data:
53
54\begin{funcdesc}{pack_fstring}{n\, s}
55Packs a fixed length string, \var{s}. \var{n} is the length of the
56string but it is \emph{not} packed into the data buffer. The string
57is padded with null bytes if necessary to guaranteed 4 byte alignment.
58\end{funcdesc}
59
60\begin{funcdesc}{pack_fopaque}{n\, data}
61Packs a fixed length opaque data stream, similarly to
62\code{pack_fstring}.
63\end{funcdesc}
64
65\begin{funcdesc}{pack_string}{s}
66Packs a variable length string, \var{s}. The length of the string is
67first packed as an unsigned integer, then the string data is packed
68with \code{pack_fstring}.
69\end{funcdesc}
70
71\begin{funcdesc}{pack_opaque}{data}
72Packs a variable length opaque data string, similarly to
73\code{pack_string}.
74\end{funcdesc}
75
76\begin{funcdesc}{pack_bytes}{bytes}
77Packs a variable length byte stream, similarly to \code{pack_string}.
78\end{funcdesc}
79
80The following methods support packing arrays and lists:
81
82\begin{funcdesc}{pack_list}{list\, pack_item}
83Packs a \var{list} of homogeneous items. This method is useful for
84lists with an indeterminate size; i.e. the size is not available until
85the entire list has been walked. For each item in the list, an
86unsigned integer \code{1} is packed first, followed by the data value
87from the list. \var{pack_item} is the function that is called to pack
88the individual item. At the end of the list, an unsigned integer
89\code{0} is packed.
90\end{funcdesc}
91
92\begin{funcdesc}{pack_farray}{n\, array\, pack_item}
93Packs a fixed length list (\var{array}) of homogeneous items. \var{n}
94is the length of the list; it is \emph{not} packed into the buffer,
95but a \code{ValueError} exception is raised if \code{len(array)} is not
96equal to \var{n}. As above, \var{pack_item} is the function used to
97pack each element.
98\end{funcdesc}
99
100\begin{funcdesc}{pack_array}{list\, pack_item}
101Packs a variable length \var{list} of homogeneous items. First, the
102length of the list is packed as an unsigned integer, then each element
103is packed as in \code{pack_farray} above.
104\end{funcdesc}
105
106\subsection{Unpacker Objects}
107
108\code{Unpacker} is the complementary class which unpacks XDR data
109values from a string buffer, and has the following methods:
110
111\begin{funcdesc}{__init__}{data}
112Instantiates an \code{Unpacker} object with the string buffer
113\var{data}.
114\end{funcdesc}
115
116\begin{funcdesc}{reset}{data}
117Resets the string buffer with the given \var{data}.
118\end{funcdesc}
119
120\begin{funcdesc}{get_position}{}
121Returns the current unpack position in the data buffer.
122\end{funcdesc}
123
124\begin{funcdesc}{set_position}{position}
125Sets the data buffer unpack position to \var{position}. You should be
126careful about using \code{get_position()} and \code{set_position()}.
127\end{funcdesc}
128
Barry Warsaw102dc411996-12-04 22:05:42 +0000129\begin{funcdesc}{get_buffer}{}
130Returns the current unpack data buffer as a string.
131\end{funcdesc}
132
Guido van Rossum40006cf1996-08-19 22:58:03 +0000133\begin{funcdesc}{done}{}
134Indicates unpack completion. Raises an \code{xdrlib.Error} exception
135if all of the data has not been unpacked.
136\end{funcdesc}
137
138In addition, every data type that can be packed with a \code{Packer},
139can be unpacked with an \code{Unpacker}. Unpacking methods are of the
140form \code{unpack_\var{type}}, and take no arguments. They return the
141unpacked object. The same caveats apply for \code{unpack_float} and
142\code{unpack_double} as above.
143
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000144\begin{funcdesc}{unpack_float}{}
145Unpacks a single-precision floating point number.
146\end{funcdesc}
147
148\begin{funcdesc}{unpack_double}{}
149Unpacks a double-precision floating point number, similarly to
150\code{unpack_float}.
151\end{funcdesc}
152
Guido van Rossum40006cf1996-08-19 22:58:03 +0000153In addition, the following methods unpack strings, bytes, and opaque
154data:
155
156\begin{funcdesc}{unpack_fstring}{n}
157Unpacks and returns a fixed length string. \var{n} is the number of
158characters expected. Padding with null bytes to guaranteed 4 byte
159alignment is assumed.
160\end{funcdesc}
161
162\begin{funcdesc}{unpack_fopaque}{n}
163Unpacks and returns a fixed length opaque data stream, similarly to
164\code{unpack_fstring}.
165\end{funcdesc}
166
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000167\begin{funcdesc}{unpack_string}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000168Unpacks and returns a variable length string. The length of the
169string is first unpacked as an unsigned integer, then the string data
170is unpacked with \code{unpack_fstring}.
171\end{funcdesc}
172
173\begin{funcdesc}{unpack_opaque}{}
174Unpacks and returns a variable length opaque data string, similarly to
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000175\code{unpack_string}.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000176\end{funcdesc}
177
178\begin{funcdesc}{unpack_bytes}{}
179Unpacks and returns a variable length byte stream, similarly to
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000180\code{unpack_string}.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000181\end{funcdesc}
182
183The following methods support unpacking arrays and lists:
184
185\begin{funcdesc}{unpack_list}{unpack_item}
186Unpacks and returns a list of homogeneous items. The list is unpacked
187one element at a time
188by first unpacking an unsigned integer flag. If the flag is \code{1},
189then the item is unpacked and appended to the list. A flag of
190\code{0} indicates the end of the list. \var{unpack_item} is the
191function that is called to unpack the items.
192\end{funcdesc}
193
194\begin{funcdesc}{unpack_farray}{n\, unpack_item}
195Unpacks and returns (as a list) a fixed length array of homogeneous
196items. \var{n} is number of list elements to expect in the buffer.
197As above, \var{unpack_item} is the function used to unpack each element.
198\end{funcdesc}
199
200\begin{funcdesc}{unpack_array}{unpack_item}
201Unpacks and returns a variable length \var{list} of homogeneous items.
202First, the length of the list is unpacked as an unsigned integer, then
203each element is unpacked as in \code{unpack_farray} above.
204\end{funcdesc}
205
206\subsection{Exceptions}
Fred Drake4b3f0311996-12-13 22:04:31 +0000207\nodename{Exceptions in xdrlib module}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000208
209Exceptions in this module are coded as class instances:
210
211\begin{excdesc}{Error}
212The base exception class. \code{Error} has a single public data
213member \code{msg} containing the description of the error.
214\end{excdesc}
215
216\begin{excdesc}{ConversionError}
217Class derived from \code{Error}. Contains no additional instance
218variables.
219\end{excdesc}
220
221Here is an example of how you would catch one of these exceptions:
222
Guido van Rossume47da0a1997-07-17 16:34:52 +0000223\bcode\begin{verbatim}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000224import xdrlib
225p = xdrlib.Packer()
226try:
227 p.pack_double(8.01)
228except xdrlib.ConversionError, instance:
229 print 'packing the double failed:', instance.msg
Guido van Rossume47da0a1997-07-17 16:34:52 +0000230\end{verbatim}\ecode