blob: 9312f9896380cc8ddee52762a7145e265a8e4acc [file] [log] [blame]
Fred Drake20417b71997-12-17 14:17:35 +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}
Fred Drake3c3d7ce1998-01-08 04:00:30 +00005\index{External Data Representation}
Fred Drake20417b71997-12-17 14:17:35 +00006\index{RFC!1014}
Guido van Rossum40006cf1996-08-19 22:58:03 +00007
8\renewcommand{\indexsubitem}{(in module xdrlib)}
9
10
11The \code{xdrlib} module supports the External Data Representation
12Standard as described in RFC 1014, written by Sun Microsystems,
13Inc. June 1987. It supports most of the data types described in the
Fred Drakeae18e9f1997-10-24 21:14:36 +000014RFC.
Guido van Rossum40006cf1996-08-19 22:58:03 +000015
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
Fred Drake3c3d7ce1998-01-08 04:00:30 +000035calling the appropriate \code{pack_\var{type}()} method. Each method
Guido van Rossum40006cf1996-08-19 22:58:03 +000036takes a single argument, the value to pack. The following simple data
Fred Drake3c3d7ce1998-01-08 04:00:30 +000037type packing methods are supported: \code{pack_uint()}, \code{pack_int()},
38\code{pack_enum()}, \code{pack_bool()}, \code{pack_uhyper()},
39and \code{pack_hyper()}.
Guido van Rossum40006cf1996-08-19 22:58:03 +000040
Guido van Rossum40006cf1996-08-19 22:58:03 +000041\begin{funcdesc}{pack_float}{value}
42Packs the single-precision floating point number \var{value}.
43\end{funcdesc}
44
45\begin{funcdesc}{pack_double}{value}
46Packs the double-precision floating point number \var{value}.
47\end{funcdesc}
48
49The following methods support packing strings, bytes, and opaque data:
50
Fred Drake3c3d7ce1998-01-08 04:00:30 +000051\begin{funcdesc}{pack_fstring}{n, s}
Guido van Rossum40006cf1996-08-19 22:58:03 +000052Packs a fixed length string, \var{s}. \var{n} is the length of the
53string but it is \emph{not} packed into the data buffer. The string
54is padded with null bytes if necessary to guaranteed 4 byte alignment.
55\end{funcdesc}
56
Fred Drake3c3d7ce1998-01-08 04:00:30 +000057\begin{funcdesc}{pack_fopaque}{n, data}
Guido van Rossum40006cf1996-08-19 22:58:03 +000058Packs a fixed length opaque data stream, similarly to
Fred Drake3c3d7ce1998-01-08 04:00:30 +000059\code{pack_fstring()}.
Guido van Rossum40006cf1996-08-19 22:58:03 +000060\end{funcdesc}
61
62\begin{funcdesc}{pack_string}{s}
63Packs a variable length string, \var{s}. The length of the string is
64first packed as an unsigned integer, then the string data is packed
Fred Drake3c3d7ce1998-01-08 04:00:30 +000065with \code{pack_fstring()}.
Guido van Rossum40006cf1996-08-19 22:58:03 +000066\end{funcdesc}
67
68\begin{funcdesc}{pack_opaque}{data}
69Packs a variable length opaque data string, similarly to
Fred Drake3c3d7ce1998-01-08 04:00:30 +000070\code{pack_string()}.
Guido van Rossum40006cf1996-08-19 22:58:03 +000071\end{funcdesc}
72
73\begin{funcdesc}{pack_bytes}{bytes}
Fred Drake3c3d7ce1998-01-08 04:00:30 +000074Packs a variable length byte stream, similarly to \code{pack_string()}.
Guido van Rossum40006cf1996-08-19 22:58:03 +000075\end{funcdesc}
76
77The following methods support packing arrays and lists:
78
79\begin{funcdesc}{pack_list}{list\, pack_item}
80Packs a \var{list} of homogeneous items. This method is useful for
81lists with an indeterminate size; i.e. the size is not available until
82the entire list has been walked. For each item in the list, an
83unsigned integer \code{1} is packed first, followed by the data value
84from the list. \var{pack_item} is the function that is called to pack
85the individual item. At the end of the list, an unsigned integer
86\code{0} is packed.
87\end{funcdesc}
88
89\begin{funcdesc}{pack_farray}{n\, array\, pack_item}
90Packs a fixed length list (\var{array}) of homogeneous items. \var{n}
91is the length of the list; it is \emph{not} packed into the buffer,
Fred Drake3c3d7ce1998-01-08 04:00:30 +000092but a \code{ValueError} exception is raised if \code{len(\var{array})} is not
Guido van Rossum40006cf1996-08-19 22:58:03 +000093equal to \var{n}. As above, \var{pack_item} is the function used to
94pack each element.
95\end{funcdesc}
96
97\begin{funcdesc}{pack_array}{list\, pack_item}
98Packs a variable length \var{list} of homogeneous items. First, the
99length of the list is packed as an unsigned integer, then each element
Fred Drake3c3d7ce1998-01-08 04:00:30 +0000100is packed as in \code{pack_farray()} above.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000101\end{funcdesc}
102
103\subsection{Unpacker Objects}
104
105\code{Unpacker} is the complementary class which unpacks XDR data
106values from a string buffer, and has the following methods:
107
108\begin{funcdesc}{__init__}{data}
109Instantiates an \code{Unpacker} object with the string buffer
110\var{data}.
111\end{funcdesc}
112
113\begin{funcdesc}{reset}{data}
114Resets the string buffer with the given \var{data}.
115\end{funcdesc}
116
117\begin{funcdesc}{get_position}{}
118Returns the current unpack position in the data buffer.
119\end{funcdesc}
120
121\begin{funcdesc}{set_position}{position}
122Sets the data buffer unpack position to \var{position}. You should be
123careful about using \code{get_position()} and \code{set_position()}.
124\end{funcdesc}
125
Barry Warsaw102dc411996-12-04 22:05:42 +0000126\begin{funcdesc}{get_buffer}{}
127Returns the current unpack data buffer as a string.
128\end{funcdesc}
129
Guido van Rossum40006cf1996-08-19 22:58:03 +0000130\begin{funcdesc}{done}{}
131Indicates unpack completion. Raises an \code{xdrlib.Error} exception
132if all of the data has not been unpacked.
133\end{funcdesc}
134
135In addition, every data type that can be packed with a \code{Packer},
136can be unpacked with an \code{Unpacker}. Unpacking methods are of the
Fred Drake3c3d7ce1998-01-08 04:00:30 +0000137form \code{unpack_\var{type}()}, and take no arguments. They return the
Fred Drake040e5651997-10-24 21:15:55 +0000138unpacked object.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000139
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000140\begin{funcdesc}{unpack_float}{}
141Unpacks a single-precision floating point number.
142\end{funcdesc}
143
144\begin{funcdesc}{unpack_double}{}
145Unpacks a double-precision floating point number, similarly to
Fred Drake3c3d7ce1998-01-08 04:00:30 +0000146\code{unpack_float()}.
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000147\end{funcdesc}
148
Guido van Rossum40006cf1996-08-19 22:58:03 +0000149In addition, the following methods unpack strings, bytes, and opaque
150data:
151
152\begin{funcdesc}{unpack_fstring}{n}
153Unpacks and returns a fixed length string. \var{n} is the number of
154characters expected. Padding with null bytes to guaranteed 4 byte
155alignment is assumed.
156\end{funcdesc}
157
158\begin{funcdesc}{unpack_fopaque}{n}
159Unpacks and returns a fixed length opaque data stream, similarly to
Fred Drake3c3d7ce1998-01-08 04:00:30 +0000160\code{unpack_fstring()}.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000161\end{funcdesc}
162
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000163\begin{funcdesc}{unpack_string}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000164Unpacks and returns a variable length string. The length of the
165string is first unpacked as an unsigned integer, then the string data
Fred Drake3c3d7ce1998-01-08 04:00:30 +0000166is unpacked with \code{unpack_fstring()}.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000167\end{funcdesc}
168
169\begin{funcdesc}{unpack_opaque}{}
170Unpacks and returns a variable length opaque data string, similarly to
Fred Drake3c3d7ce1998-01-08 04:00:30 +0000171\code{unpack_string()}.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000172\end{funcdesc}
173
174\begin{funcdesc}{unpack_bytes}{}
175Unpacks and returns a variable length byte stream, similarly to
Fred Drake3c3d7ce1998-01-08 04:00:30 +0000176\code{unpack_string()}.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000177\end{funcdesc}
178
179The following methods support unpacking arrays and lists:
180
181\begin{funcdesc}{unpack_list}{unpack_item}
182Unpacks and returns a list of homogeneous items. The list is unpacked
183one element at a time
184by first unpacking an unsigned integer flag. If the flag is \code{1},
185then the item is unpacked and appended to the list. A flag of
186\code{0} indicates the end of the list. \var{unpack_item} is the
187function that is called to unpack the items.
188\end{funcdesc}
189
190\begin{funcdesc}{unpack_farray}{n\, unpack_item}
191Unpacks and returns (as a list) a fixed length array of homogeneous
192items. \var{n} is number of list elements to expect in the buffer.
193As above, \var{unpack_item} is the function used to unpack each element.
194\end{funcdesc}
195
196\begin{funcdesc}{unpack_array}{unpack_item}
197Unpacks and returns a variable length \var{list} of homogeneous items.
198First, the length of the list is unpacked as an unsigned integer, then
Fred Drake3c3d7ce1998-01-08 04:00:30 +0000199each element is unpacked as in \code{unpack_farray()} above.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000200\end{funcdesc}
201
202\subsection{Exceptions}
Fred Drake4b3f0311996-12-13 22:04:31 +0000203\nodename{Exceptions in xdrlib module}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000204
205Exceptions in this module are coded as class instances:
206
207\begin{excdesc}{Error}
208The base exception class. \code{Error} has a single public data
209member \code{msg} containing the description of the error.
210\end{excdesc}
211
212\begin{excdesc}{ConversionError}
213Class derived from \code{Error}. Contains no additional instance
214variables.
215\end{excdesc}
216
217Here is an example of how you would catch one of these exceptions:
218
Guido van Rossume47da0a1997-07-17 16:34:52 +0000219\bcode\begin{verbatim}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000220import xdrlib
221p = xdrlib.Packer()
222try:
223 p.pack_double(8.01)
224except xdrlib.ConversionError, instance:
225 print 'packing the double failed:', instance.msg
Guido van Rossume47da0a1997-07-17 16:34:52 +0000226\end{verbatim}\ecode