blob: 6157c9d65a1c22b31ca0d09e0541497560b2d793 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{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}
Guido van Rossum40006cf1996-08-19 22:58:03 +00006
Guido van Rossum40006cf1996-08-19 22:58:03 +00007
8
Fred Drakeff79a211998-03-14 06:30:13 +00009The \module{xdrlib} module supports the External Data Representation
Fred Drakec5891241998-02-09 19:16:20 +000010Standard as described in \rfc{1014}, written by Sun Microsystems,
Guido van Rossum40006cf1996-08-19 22:58:03 +000011Inc. 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
Fred Drakeff79a211998-03-14 06:30:13 +000014The \module{xdrlib} module defines two classes, one for packing
Guido van Rossum40006cf1996-08-19 22:58:03 +000015variables into XDR representation, and another for unpacking from XDR
16representation. There are also two exception classes.
17
Fred Drakeff79a211998-03-14 06:30:13 +000018\begin{classdesc}{Packer}{}
19\class{Packer} is the class for packing data into XDR representation.
20The \class{Packer} class is instantiated with no arguments.
21\end{classdesc}
22
23\begin{classdesc}{Unpacker}{data}
24\code{Unpacker} is the complementary class which unpacks XDR data
25values from a string buffer. The input buffer is given as
26\var{data}.
27\end{classdesc}
28
Guido van Rossum40006cf1996-08-19 22:58:03 +000029
30\subsection{Packer Objects}
Fred Drakeff79a211998-03-14 06:30:13 +000031\label{xdr-packer-objects}
Guido van Rossum40006cf1996-08-19 22:58:03 +000032
Fred Drakeff79a211998-03-14 06:30:13 +000033\class{Packer} instances have the following methods:
Guido van Rossum40006cf1996-08-19 22:58:03 +000034
Fred Drake62502051998-04-12 03:31:31 +000035\begin{methoddesc}[Packer]{get_buffer}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +000036Returns the current pack buffer as a string.
Fred Drake62502051998-04-12 03:31:31 +000037\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000038
Fred Drake62502051998-04-12 03:31:31 +000039\begin{methoddesc}[Packer]{reset}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +000040Resets the pack buffer to the empty string.
Fred Drake62502051998-04-12 03:31:31 +000041\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000042
43In general, you can pack any of the most common XDR data types by
Fred Drake3c3d7ce1998-01-08 04:00:30 +000044calling the appropriate \code{pack_\var{type}()} method. Each method
Guido van Rossum40006cf1996-08-19 22:58:03 +000045takes a single argument, the value to pack. The following simple data
Fred Drakeff79a211998-03-14 06:30:13 +000046type packing methods are supported: \method{pack_uint()},
47\method{pack_int()}, \method{pack_enum()}, \method{pack_bool()},
48\method{pack_uhyper()}, and \method{pack_hyper()}.
Guido van Rossum40006cf1996-08-19 22:58:03 +000049
Fred Drake62502051998-04-12 03:31:31 +000050\begin{methoddesc}[Packer]{pack_float}{value}
Guido van Rossum40006cf1996-08-19 22:58:03 +000051Packs the single-precision floating point number \var{value}.
Fred Drake62502051998-04-12 03:31:31 +000052\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000053
Fred Drake62502051998-04-12 03:31:31 +000054\begin{methoddesc}[Packer]{pack_double}{value}
Guido van Rossum40006cf1996-08-19 22:58:03 +000055Packs the double-precision floating point number \var{value}.
Fred Drake62502051998-04-12 03:31:31 +000056\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000057
58The following methods support packing strings, bytes, and opaque data:
59
Fred Drake62502051998-04-12 03:31:31 +000060\begin{methoddesc}[Packer]{pack_fstring}{n, s}
Guido van Rossum40006cf1996-08-19 22:58:03 +000061Packs a fixed length string, \var{s}. \var{n} is the length of the
62string but it is \emph{not} packed into the data buffer. The string
63is padded with null bytes if necessary to guaranteed 4 byte alignment.
Fred Drake62502051998-04-12 03:31:31 +000064\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000065
Fred Drake62502051998-04-12 03:31:31 +000066\begin{methoddesc}[Packer]{pack_fopaque}{n, data}
Guido van Rossum40006cf1996-08-19 22:58:03 +000067Packs a fixed length opaque data stream, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +000068\method{pack_fstring()}.
Fred Drake62502051998-04-12 03:31:31 +000069\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000070
Fred Drake62502051998-04-12 03:31:31 +000071\begin{methoddesc}[Packer]{pack_string}{s}
Guido van Rossum40006cf1996-08-19 22:58:03 +000072Packs a variable length string, \var{s}. The length of the string is
73first packed as an unsigned integer, then the string data is packed
Fred Drakeff79a211998-03-14 06:30:13 +000074with \method{pack_fstring()}.
Fred Drake62502051998-04-12 03:31:31 +000075\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000076
Fred Drake62502051998-04-12 03:31:31 +000077\begin{methoddesc}[Packer]{pack_opaque}{data}
Guido van Rossum40006cf1996-08-19 22:58:03 +000078Packs a variable length opaque data string, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +000079\method{pack_string()}.
Fred Drake62502051998-04-12 03:31:31 +000080\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000081
Fred Drake62502051998-04-12 03:31:31 +000082\begin{methoddesc}[Packer]{pack_bytes}{bytes}
Fred Drakeff79a211998-03-14 06:30:13 +000083Packs a variable length byte stream, similarly to \method{pack_string()}.
Fred Drake62502051998-04-12 03:31:31 +000084\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000085
86The following methods support packing arrays and lists:
87
Fred Drake62502051998-04-12 03:31:31 +000088\begin{methoddesc}[Packer]{pack_list}{list, pack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +000089Packs a \var{list} of homogeneous items. This method is useful for
90lists with an indeterminate size; i.e. the size is not available until
91the entire list has been walked. For each item in the list, an
92unsigned integer \code{1} is packed first, followed by the data value
93from the list. \var{pack_item} is the function that is called to pack
94the individual item. At the end of the list, an unsigned integer
95\code{0} is packed.
Fred Drake62502051998-04-12 03:31:31 +000096\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000097
Fred Drake62502051998-04-12 03:31:31 +000098\begin{methoddesc}[Packer]{pack_farray}{n, array, pack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +000099Packs a fixed length list (\var{array}) of homogeneous items. \var{n}
100is the length of the list; it is \emph{not} packed into the buffer,
Fred Drakeff79a211998-03-14 06:30:13 +0000101but a \exception{ValueError} exception is raised if
102\code{len(\var{array})} is not equal to \var{n}. As above,
103\var{pack_item} is the function used to pack each element.
Fred Drake62502051998-04-12 03:31:31 +0000104\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000105
Fred Drake62502051998-04-12 03:31:31 +0000106\begin{methoddesc}[Packer]{pack_array}{list, pack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000107Packs a variable length \var{list} of homogeneous items. First, the
108length of the list is packed as an unsigned integer, then each element
Fred Drakeff79a211998-03-14 06:30:13 +0000109is packed as in \method{pack_farray()} above.
Fred Drake62502051998-04-12 03:31:31 +0000110\end{methoddesc}
111
Guido van Rossum40006cf1996-08-19 22:58:03 +0000112
113\subsection{Unpacker Objects}
Fred Drakeff79a211998-03-14 06:30:13 +0000114\label{xdr-unpacker-objects}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000115
Fred Drakeff79a211998-03-14 06:30:13 +0000116The \class{Unpacker} class offers the following methods:
Guido van Rossum40006cf1996-08-19 22:58:03 +0000117
Fred Drake62502051998-04-12 03:31:31 +0000118\begin{methoddesc}[Unpacker]{reset}{data}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000119Resets the string buffer with the given \var{data}.
Fred Drake62502051998-04-12 03:31:31 +0000120\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000121
Fred Drake62502051998-04-12 03:31:31 +0000122\begin{methoddesc}[Unpacker]{get_position}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000123Returns the current unpack position in the data buffer.
Fred Drake62502051998-04-12 03:31:31 +0000124\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000125
Fred Drake62502051998-04-12 03:31:31 +0000126\begin{methoddesc}[Unpacker]{set_position}{position}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000127Sets the data buffer unpack position to \var{position}. You should be
Fred Drakeff79a211998-03-14 06:30:13 +0000128careful about using \method{get_position()} and \method{set_position()}.
Fred Drake62502051998-04-12 03:31:31 +0000129\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000130
Fred Drake62502051998-04-12 03:31:31 +0000131\begin{methoddesc}[Unpacker]{get_buffer}{}
Barry Warsaw102dc411996-12-04 22:05:42 +0000132Returns the current unpack data buffer as a string.
Fred Drake62502051998-04-12 03:31:31 +0000133\end{methoddesc}
Barry Warsaw102dc411996-12-04 22:05:42 +0000134
Fred Drake62502051998-04-12 03:31:31 +0000135\begin{methoddesc}[Unpacker]{done}{}
Fred Drakeff79a211998-03-14 06:30:13 +0000136Indicates unpack completion. Raises an \exception{Error} exception
Guido van Rossum40006cf1996-08-19 22:58:03 +0000137if all of the data has not been unpacked.
Fred Drake62502051998-04-12 03:31:31 +0000138\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000139
Fred Drakeff79a211998-03-14 06:30:13 +0000140In addition, every data type that can be packed with a \class{Packer},
141can be unpacked with an \class{Unpacker}. Unpacking methods are of the
Fred Drake3c3d7ce1998-01-08 04:00:30 +0000142form \code{unpack_\var{type}()}, and take no arguments. They return the
Fred Drake040e5651997-10-24 21:15:55 +0000143unpacked object.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000144
Fred Drake62502051998-04-12 03:31:31 +0000145\begin{methoddesc}[Unpacker]{unpack_float}{}
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000146Unpacks a single-precision floating point number.
Fred Drake62502051998-04-12 03:31:31 +0000147\end{methoddesc}
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000148
Fred Drake62502051998-04-12 03:31:31 +0000149\begin{methoddesc}[Unpacker]{unpack_double}{}
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000150Unpacks a double-precision floating point number, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +0000151\method{unpack_float()}.
Fred Drake62502051998-04-12 03:31:31 +0000152\end{methoddesc}
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000153
Guido van Rossum40006cf1996-08-19 22:58:03 +0000154In addition, the following methods unpack strings, bytes, and opaque
155data:
156
Fred Drake62502051998-04-12 03:31:31 +0000157\begin{methoddesc}[Unpacker]{unpack_fstring}{n}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000158Unpacks and returns a fixed length string. \var{n} is the number of
159characters expected. Padding with null bytes to guaranteed 4 byte
160alignment is assumed.
Fred Drake62502051998-04-12 03:31:31 +0000161\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000162
Fred Drake62502051998-04-12 03:31:31 +0000163\begin{methoddesc}[Unpacker]{unpack_fopaque}{n}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000164Unpacks and returns a fixed length opaque data stream, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +0000165\method{unpack_fstring()}.
Fred Drake62502051998-04-12 03:31:31 +0000166\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000167
Fred Drake62502051998-04-12 03:31:31 +0000168\begin{methoddesc}[Unpacker]{unpack_string}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000169Unpacks and returns a variable length string. The length of the
170string is first unpacked as an unsigned integer, then the string data
Fred Drakeff79a211998-03-14 06:30:13 +0000171is unpacked with \method{unpack_fstring()}.
Fred Drake62502051998-04-12 03:31:31 +0000172\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000173
Fred Drake62502051998-04-12 03:31:31 +0000174\begin{methoddesc}[Unpacker]{unpack_opaque}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000175Unpacks and returns a variable length opaque data string, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +0000176\method{unpack_string()}.
Fred Drake62502051998-04-12 03:31:31 +0000177\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000178
Fred Drake62502051998-04-12 03:31:31 +0000179\begin{methoddesc}[Unpacker]{unpack_bytes}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000180Unpacks and returns a variable length byte stream, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +0000181\method{unpack_string()}.
Fred Drake62502051998-04-12 03:31:31 +0000182\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000183
184The following methods support unpacking arrays and lists:
185
Fred Drake62502051998-04-12 03:31:31 +0000186\begin{methoddesc}[Unpacker]{unpack_list}{unpack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000187Unpacks and returns a list of homogeneous items. The list is unpacked
188one element at a time
189by first unpacking an unsigned integer flag. If the flag is \code{1},
190then the item is unpacked and appended to the list. A flag of
191\code{0} indicates the end of the list. \var{unpack_item} is the
192function that is called to unpack the items.
Fred Drake62502051998-04-12 03:31:31 +0000193\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000194
Fred Drake62502051998-04-12 03:31:31 +0000195\begin{methoddesc}[Unpacker]{unpack_farray}{n, unpack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000196Unpacks and returns (as a list) a fixed length array of homogeneous
197items. \var{n} is number of list elements to expect in the buffer.
198As above, \var{unpack_item} is the function used to unpack each element.
Fred Drake62502051998-04-12 03:31:31 +0000199\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000200
Fred Drake62502051998-04-12 03:31:31 +0000201\begin{methoddesc}[Unpacker]{unpack_array}{unpack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000202Unpacks and returns a variable length \var{list} of homogeneous items.
203First, the length of the list is unpacked as an unsigned integer, then
Fred Drakeff79a211998-03-14 06:30:13 +0000204each element is unpacked as in \method{unpack_farray()} above.
Fred Drake62502051998-04-12 03:31:31 +0000205\end{methoddesc}
206
Guido van Rossum40006cf1996-08-19 22:58:03 +0000207
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}
Fred Drakeff79a211998-03-14 06:30:13 +0000214The base exception class. \exception{Error} has a single public data
215member \member{msg} containing the description of the error.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000216\end{excdesc}
217
218\begin{excdesc}{ConversionError}
Fred Drakeff79a211998-03-14 06:30:13 +0000219Class derived from \exception{Error}. Contains no additional instance
Guido van Rossum40006cf1996-08-19 22:58:03 +0000220variables.
221\end{excdesc}
222
223Here is an example of how you would catch one of these exceptions:
224
Fred Drake19479911998-02-13 06:58:54 +0000225\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
Fred Drake19479911998-02-13 06:58:54 +0000232\end{verbatim}