blob: 57329d7994c9e401b2ff2f226dcb911d91ed5fe1 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{xdrlib} ---
2 Encode and decode XDR data.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{xdrlib}
4
Fred Drakec18a6f31998-08-07 16:01:15 +00005\modulesynopsis{Encoders and decoders for the External Data
Fred Drake295da241998-08-10 19:42:37 +00006Representation (XDR).}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Guido van Rossum40006cf1996-08-19 22:58:03 +00008\index{XDR}
Fred Drake3c3d7ce1998-01-08 04:00:30 +00009\index{External Data Representation}
Guido van Rossum40006cf1996-08-19 22:58:03 +000010
Guido van Rossum40006cf1996-08-19 22:58:03 +000011
Fred Drakeff79a211998-03-14 06:30:13 +000012The \module{xdrlib} module supports the External Data Representation
Fred Drakec5891241998-02-09 19:16:20 +000013Standard as described in \rfc{1014}, written by Sun Microsystems,
Guido van Rossum40006cf1996-08-19 22:58:03 +000014Inc. June 1987. It supports most of the data types described in the
Fred Drakeae18e9f1997-10-24 21:14:36 +000015RFC.
Guido van Rossum40006cf1996-08-19 22:58:03 +000016
Fred Drakeff79a211998-03-14 06:30:13 +000017The \module{xdrlib} module defines two classes, one for packing
Guido van Rossum40006cf1996-08-19 22:58:03 +000018variables into XDR representation, and another for unpacking from XDR
19representation. There are also two exception classes.
20
Fred Drakeff79a211998-03-14 06:30:13 +000021\begin{classdesc}{Packer}{}
22\class{Packer} is the class for packing data into XDR representation.
23The \class{Packer} class is instantiated with no arguments.
24\end{classdesc}
25
26\begin{classdesc}{Unpacker}{data}
27\code{Unpacker} is the complementary class which unpacks XDR data
28values from a string buffer. The input buffer is given as
29\var{data}.
30\end{classdesc}
31
Guido van Rossum40006cf1996-08-19 22:58:03 +000032
Fred Drakec18a6f31998-08-07 16:01:15 +000033\subsection{Packer Objects \label{xdr-packer-objects}}
Guido van Rossum40006cf1996-08-19 22:58:03 +000034
Fred Drakeff79a211998-03-14 06:30:13 +000035\class{Packer} instances have the following methods:
Guido van Rossum40006cf1996-08-19 22:58:03 +000036
Fred Drake62502051998-04-12 03:31:31 +000037\begin{methoddesc}[Packer]{get_buffer}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +000038Returns the current pack buffer as a string.
Fred Drake62502051998-04-12 03:31:31 +000039\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000040
Fred Drake62502051998-04-12 03:31:31 +000041\begin{methoddesc}[Packer]{reset}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +000042Resets the pack buffer to the empty string.
Fred Drake62502051998-04-12 03:31:31 +000043\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000044
45In general, you can pack any of the most common XDR data types by
Fred Drake3c3d7ce1998-01-08 04:00:30 +000046calling the appropriate \code{pack_\var{type}()} method. Each method
Guido van Rossum40006cf1996-08-19 22:58:03 +000047takes a single argument, the value to pack. The following simple data
Fred Drakeff79a211998-03-14 06:30:13 +000048type packing methods are supported: \method{pack_uint()},
49\method{pack_int()}, \method{pack_enum()}, \method{pack_bool()},
50\method{pack_uhyper()}, and \method{pack_hyper()}.
Guido van Rossum40006cf1996-08-19 22:58:03 +000051
Fred Drake62502051998-04-12 03:31:31 +000052\begin{methoddesc}[Packer]{pack_float}{value}
Guido van Rossum40006cf1996-08-19 22:58:03 +000053Packs the single-precision floating point number \var{value}.
Fred Drake62502051998-04-12 03:31:31 +000054\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000055
Fred Drake62502051998-04-12 03:31:31 +000056\begin{methoddesc}[Packer]{pack_double}{value}
Guido van Rossum40006cf1996-08-19 22:58:03 +000057Packs the double-precision floating point number \var{value}.
Fred Drake62502051998-04-12 03:31:31 +000058\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000059
60The following methods support packing strings, bytes, and opaque data:
61
Fred Drake62502051998-04-12 03:31:31 +000062\begin{methoddesc}[Packer]{pack_fstring}{n, s}
Guido van Rossum40006cf1996-08-19 22:58:03 +000063Packs a fixed length string, \var{s}. \var{n} is the length of the
64string but it is \emph{not} packed into the data buffer. The string
65is padded with null bytes if necessary to guaranteed 4 byte alignment.
Fred Drake62502051998-04-12 03:31:31 +000066\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000067
Fred Drake62502051998-04-12 03:31:31 +000068\begin{methoddesc}[Packer]{pack_fopaque}{n, data}
Guido van Rossum40006cf1996-08-19 22:58:03 +000069Packs a fixed length opaque data stream, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +000070\method{pack_fstring()}.
Fred Drake62502051998-04-12 03:31:31 +000071\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000072
Fred Drake62502051998-04-12 03:31:31 +000073\begin{methoddesc}[Packer]{pack_string}{s}
Guido van Rossum40006cf1996-08-19 22:58:03 +000074Packs a variable length string, \var{s}. The length of the string is
75first packed as an unsigned integer, then the string data is packed
Fred Drakeff79a211998-03-14 06:30:13 +000076with \method{pack_fstring()}.
Fred Drake62502051998-04-12 03:31:31 +000077\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000078
Fred Drake62502051998-04-12 03:31:31 +000079\begin{methoddesc}[Packer]{pack_opaque}{data}
Guido van Rossum40006cf1996-08-19 22:58:03 +000080Packs a variable length opaque data string, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +000081\method{pack_string()}.
Fred Drake62502051998-04-12 03:31:31 +000082\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000083
Fred Drake62502051998-04-12 03:31:31 +000084\begin{methoddesc}[Packer]{pack_bytes}{bytes}
Fred Drakeff79a211998-03-14 06:30:13 +000085Packs a variable length byte stream, similarly to \method{pack_string()}.
Fred Drake62502051998-04-12 03:31:31 +000086\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000087
88The following methods support packing arrays and lists:
89
Fred Drake62502051998-04-12 03:31:31 +000090\begin{methoddesc}[Packer]{pack_list}{list, pack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +000091Packs a \var{list} of homogeneous items. This method is useful for
92lists with an indeterminate size; i.e. the size is not available until
93the entire list has been walked. For each item in the list, an
94unsigned integer \code{1} is packed first, followed by the data value
95from the list. \var{pack_item} is the function that is called to pack
96the individual item. At the end of the list, an unsigned integer
97\code{0} is packed.
Fred Drakead56daf1999-04-20 13:41:14 +000098
99For example, to pack a list of integers, the code might appear like
100this:
101
102\begin{verbatim}
103import xdrlib
104p = xdrlib.Packer()
105p.pack_list([1, 2, 3], p.pack_int)
106\end{verbatim}
Fred Drake62502051998-04-12 03:31:31 +0000107\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000108
Fred Drake62502051998-04-12 03:31:31 +0000109\begin{methoddesc}[Packer]{pack_farray}{n, array, pack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000110Packs a fixed length list (\var{array}) of homogeneous items. \var{n}
111is the length of the list; it is \emph{not} packed into the buffer,
Fred Drakeff79a211998-03-14 06:30:13 +0000112but a \exception{ValueError} exception is raised if
113\code{len(\var{array})} is not equal to \var{n}. As above,
114\var{pack_item} is the function used to pack each element.
Fred Drake62502051998-04-12 03:31:31 +0000115\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000116
Fred Drake62502051998-04-12 03:31:31 +0000117\begin{methoddesc}[Packer]{pack_array}{list, pack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000118Packs a variable length \var{list} of homogeneous items. First, the
119length of the list is packed as an unsigned integer, then each element
Fred Drakeff79a211998-03-14 06:30:13 +0000120is packed as in \method{pack_farray()} above.
Fred Drake62502051998-04-12 03:31:31 +0000121\end{methoddesc}
122
Guido van Rossum40006cf1996-08-19 22:58:03 +0000123
Fred Drakec18a6f31998-08-07 16:01:15 +0000124\subsection{Unpacker Objects \label{xdr-unpacker-objects}}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000125
Fred Drakeff79a211998-03-14 06:30:13 +0000126The \class{Unpacker} class offers the following methods:
Guido van Rossum40006cf1996-08-19 22:58:03 +0000127
Fred Drake62502051998-04-12 03:31:31 +0000128\begin{methoddesc}[Unpacker]{reset}{data}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000129Resets the string buffer with the given \var{data}.
Fred Drake62502051998-04-12 03:31:31 +0000130\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000131
Fred Drake62502051998-04-12 03:31:31 +0000132\begin{methoddesc}[Unpacker]{get_position}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000133Returns the current unpack position in the data buffer.
Fred Drake62502051998-04-12 03:31:31 +0000134\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000135
Fred Drake62502051998-04-12 03:31:31 +0000136\begin{methoddesc}[Unpacker]{set_position}{position}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000137Sets the data buffer unpack position to \var{position}. You should be
Fred Drakeff79a211998-03-14 06:30:13 +0000138careful about using \method{get_position()} and \method{set_position()}.
Fred Drake62502051998-04-12 03:31:31 +0000139\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000140
Fred Drake62502051998-04-12 03:31:31 +0000141\begin{methoddesc}[Unpacker]{get_buffer}{}
Barry Warsaw102dc411996-12-04 22:05:42 +0000142Returns the current unpack data buffer as a string.
Fred Drake62502051998-04-12 03:31:31 +0000143\end{methoddesc}
Barry Warsaw102dc411996-12-04 22:05:42 +0000144
Fred Drake62502051998-04-12 03:31:31 +0000145\begin{methoddesc}[Unpacker]{done}{}
Fred Drakeff79a211998-03-14 06:30:13 +0000146Indicates unpack completion. Raises an \exception{Error} exception
Guido van Rossum40006cf1996-08-19 22:58:03 +0000147if all of the data has not been unpacked.
Fred Drake62502051998-04-12 03:31:31 +0000148\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000149
Fred Drakeff79a211998-03-14 06:30:13 +0000150In addition, every data type that can be packed with a \class{Packer},
151can be unpacked with an \class{Unpacker}. Unpacking methods are of the
Fred Drake3c3d7ce1998-01-08 04:00:30 +0000152form \code{unpack_\var{type}()}, and take no arguments. They return the
Fred Drake040e5651997-10-24 21:15:55 +0000153unpacked object.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000154
Fred Drake62502051998-04-12 03:31:31 +0000155\begin{methoddesc}[Unpacker]{unpack_float}{}
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000156Unpacks a single-precision floating point number.
Fred Drake62502051998-04-12 03:31:31 +0000157\end{methoddesc}
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000158
Fred Drake62502051998-04-12 03:31:31 +0000159\begin{methoddesc}[Unpacker]{unpack_double}{}
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000160Unpacks a double-precision floating point number, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +0000161\method{unpack_float()}.
Fred Drake62502051998-04-12 03:31:31 +0000162\end{methoddesc}
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000163
Guido van Rossum40006cf1996-08-19 22:58:03 +0000164In addition, the following methods unpack strings, bytes, and opaque
165data:
166
Fred Drake62502051998-04-12 03:31:31 +0000167\begin{methoddesc}[Unpacker]{unpack_fstring}{n}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000168Unpacks and returns a fixed length string. \var{n} is the number of
169characters expected. Padding with null bytes to guaranteed 4 byte
170alignment is assumed.
Fred Drake62502051998-04-12 03:31:31 +0000171\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000172
Fred Drake62502051998-04-12 03:31:31 +0000173\begin{methoddesc}[Unpacker]{unpack_fopaque}{n}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000174Unpacks and returns a fixed length opaque data stream, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +0000175\method{unpack_fstring()}.
Fred Drake62502051998-04-12 03:31:31 +0000176\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000177
Fred Drake62502051998-04-12 03:31:31 +0000178\begin{methoddesc}[Unpacker]{unpack_string}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000179Unpacks and returns a variable length string. The length of the
180string is first unpacked as an unsigned integer, then the string data
Fred Drakeff79a211998-03-14 06:30:13 +0000181is unpacked with \method{unpack_fstring()}.
Fred Drake62502051998-04-12 03:31:31 +0000182\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000183
Fred Drake62502051998-04-12 03:31:31 +0000184\begin{methoddesc}[Unpacker]{unpack_opaque}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000185Unpacks and returns a variable length opaque data string, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +0000186\method{unpack_string()}.
Fred Drake62502051998-04-12 03:31:31 +0000187\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000188
Fred Drake62502051998-04-12 03:31:31 +0000189\begin{methoddesc}[Unpacker]{unpack_bytes}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000190Unpacks and returns a variable length byte stream, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +0000191\method{unpack_string()}.
Fred Drake62502051998-04-12 03:31:31 +0000192\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000193
194The following methods support unpacking arrays and lists:
195
Fred Drake62502051998-04-12 03:31:31 +0000196\begin{methoddesc}[Unpacker]{unpack_list}{unpack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000197Unpacks and returns a list of homogeneous items. The list is unpacked
198one element at a time
199by first unpacking an unsigned integer flag. If the flag is \code{1},
200then the item is unpacked and appended to the list. A flag of
201\code{0} indicates the end of the list. \var{unpack_item} is the
202function that is called to unpack the items.
Fred Drake62502051998-04-12 03:31:31 +0000203\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000204
Fred Drake62502051998-04-12 03:31:31 +0000205\begin{methoddesc}[Unpacker]{unpack_farray}{n, unpack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000206Unpacks and returns (as a list) a fixed length array of homogeneous
207items. \var{n} is number of list elements to expect in the buffer.
208As above, \var{unpack_item} is the function used to unpack each element.
Fred Drake62502051998-04-12 03:31:31 +0000209\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000210
Fred Drake62502051998-04-12 03:31:31 +0000211\begin{methoddesc}[Unpacker]{unpack_array}{unpack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000212Unpacks and returns a variable length \var{list} of homogeneous items.
213First, the length of the list is unpacked as an unsigned integer, then
Fred Drakeff79a211998-03-14 06:30:13 +0000214each element is unpacked as in \method{unpack_farray()} above.
Fred Drake62502051998-04-12 03:31:31 +0000215\end{methoddesc}
216
Guido van Rossum40006cf1996-08-19 22:58:03 +0000217
Fred Drakec18a6f31998-08-07 16:01:15 +0000218\subsection{Exceptions \label{xdr-exceptions}}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000219
220Exceptions in this module are coded as class instances:
221
222\begin{excdesc}{Error}
Fred Drakeff79a211998-03-14 06:30:13 +0000223The base exception class. \exception{Error} has a single public data
224member \member{msg} containing the description of the error.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000225\end{excdesc}
226
227\begin{excdesc}{ConversionError}
Fred Drakeff79a211998-03-14 06:30:13 +0000228Class derived from \exception{Error}. Contains no additional instance
Guido van Rossum40006cf1996-08-19 22:58:03 +0000229variables.
230\end{excdesc}
231
232Here is an example of how you would catch one of these exceptions:
233
Fred Drake19479911998-02-13 06:58:54 +0000234\begin{verbatim}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000235import xdrlib
236p = xdrlib.Packer()
237try:
238 p.pack_double(8.01)
239except xdrlib.ConversionError, instance:
240 print 'packing the double failed:', instance.msg
Fred Drake19479911998-02-13 06:58:54 +0000241\end{verbatim}