blob: d0863d93277d12cf82f45290f63a7903ae36aad0 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{xdrlib} ---
Fred Drake2d34cd32000-10-10 17:02:34 +00002 Encode and decode XDR data}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake2d34cd32000-10-10 17:02:34 +00004\declaremodule{standard}{xdrlib}
Fred Drakec18a6f31998-08-07 16:01:15 +00005\modulesynopsis{Encoders and decoders for the External Data
Fred Drake2d34cd32000-10-10 17:02:34 +00006 Representation (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
Fred Drakeff79a211998-03-14 06:30:13 +000011The \module{xdrlib} module supports the External Data Representation
Fred Drakec5891241998-02-09 19:16:20 +000012Standard as described in \rfc{1014}, written by Sun Microsystems,
Guido van Rossum40006cf1996-08-19 22:58:03 +000013Inc. 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
Fred Drakeff79a211998-03-14 06:30:13 +000016The \module{xdrlib} module defines two classes, one for packing
Guido van Rossum40006cf1996-08-19 22:58:03 +000017variables into XDR representation, and another for unpacking from XDR
18representation. There are also two exception classes.
19
Fred Drakeff79a211998-03-14 06:30:13 +000020\begin{classdesc}{Packer}{}
21\class{Packer} is the class for packing data into XDR representation.
22The \class{Packer} class is instantiated with no arguments.
23\end{classdesc}
24
25\begin{classdesc}{Unpacker}{data}
26\code{Unpacker} is the complementary class which unpacks XDR data
27values from a string buffer. The input buffer is given as
28\var{data}.
29\end{classdesc}
30
Guido van Rossum40006cf1996-08-19 22:58:03 +000031
Fred Drake2d34cd32000-10-10 17:02:34 +000032\begin{seealso}
33 \seerfc{1014}{XDR: External Data Representation Standard}{This RFC
34 defined the encoding of data which was XDR at the time
35 this module was originally written. It has
Raymond Hettinger8ef3d8d2003-01-06 18:41:01 +000036 apparently been obsoleted by \rfc{1832}.}
Fred Drake2d34cd32000-10-10 17:02:34 +000037
38 \seerfc{1832}{XDR: External Data Representation Standard}{Newer RFC
39 that provides a revised definition of XDR.}
40\end{seealso}
41
42
Fred Drakec18a6f31998-08-07 16:01:15 +000043\subsection{Packer Objects \label{xdr-packer-objects}}
Guido van Rossum40006cf1996-08-19 22:58:03 +000044
Fred Drakeff79a211998-03-14 06:30:13 +000045\class{Packer} instances have the following methods:
Guido van Rossum40006cf1996-08-19 22:58:03 +000046
Fred Drake62502051998-04-12 03:31:31 +000047\begin{methoddesc}[Packer]{get_buffer}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +000048Returns the current pack buffer as a string.
Fred Drake62502051998-04-12 03:31:31 +000049\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000050
Fred Drake62502051998-04-12 03:31:31 +000051\begin{methoddesc}[Packer]{reset}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +000052Resets the pack buffer to the empty string.
Fred Drake62502051998-04-12 03:31:31 +000053\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000054
55In general, you can pack any of the most common XDR data types by
Fred Drake3c3d7ce1998-01-08 04:00:30 +000056calling the appropriate \code{pack_\var{type}()} method. Each method
Guido van Rossum40006cf1996-08-19 22:58:03 +000057takes a single argument, the value to pack. The following simple data
Fred Drakeff79a211998-03-14 06:30:13 +000058type packing methods are supported: \method{pack_uint()},
59\method{pack_int()}, \method{pack_enum()}, \method{pack_bool()},
60\method{pack_uhyper()}, and \method{pack_hyper()}.
Guido van Rossum40006cf1996-08-19 22:58:03 +000061
Fred Drake62502051998-04-12 03:31:31 +000062\begin{methoddesc}[Packer]{pack_float}{value}
Guido van Rossum40006cf1996-08-19 22:58:03 +000063Packs the single-precision floating point number \var{value}.
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_double}{value}
Guido van Rossum40006cf1996-08-19 22:58:03 +000067Packs the double-precision floating point number \var{value}.
Fred Drake62502051998-04-12 03:31:31 +000068\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000069
70The following methods support packing strings, bytes, and opaque data:
71
Fred Drake62502051998-04-12 03:31:31 +000072\begin{methoddesc}[Packer]{pack_fstring}{n, s}
Guido van Rossum40006cf1996-08-19 22:58:03 +000073Packs a fixed length string, \var{s}. \var{n} is the length of the
74string but it is \emph{not} packed into the data buffer. The string
75is padded with null bytes if necessary to guaranteed 4 byte alignment.
Fred Drake62502051998-04-12 03:31:31 +000076\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000077
Fred Drake62502051998-04-12 03:31:31 +000078\begin{methoddesc}[Packer]{pack_fopaque}{n, data}
Guido van Rossum40006cf1996-08-19 22:58:03 +000079Packs a fixed length opaque data stream, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +000080\method{pack_fstring()}.
Fred Drake62502051998-04-12 03:31:31 +000081\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000082
Fred Drake62502051998-04-12 03:31:31 +000083\begin{methoddesc}[Packer]{pack_string}{s}
Guido van Rossum40006cf1996-08-19 22:58:03 +000084Packs a variable length string, \var{s}. The length of the string is
85first packed as an unsigned integer, then the string data is packed
Fred Drakeff79a211998-03-14 06:30:13 +000086with \method{pack_fstring()}.
Fred Drake62502051998-04-12 03:31:31 +000087\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000088
Fred Drake62502051998-04-12 03:31:31 +000089\begin{methoddesc}[Packer]{pack_opaque}{data}
Guido van Rossum40006cf1996-08-19 22:58:03 +000090Packs a variable length opaque data string, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +000091\method{pack_string()}.
Fred Drake62502051998-04-12 03:31:31 +000092\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000093
Fred Drake62502051998-04-12 03:31:31 +000094\begin{methoddesc}[Packer]{pack_bytes}{bytes}
Fred Drakeff79a211998-03-14 06:30:13 +000095Packs a variable length byte stream, similarly to \method{pack_string()}.
Fred Drake62502051998-04-12 03:31:31 +000096\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +000097
98The following methods support packing arrays and lists:
99
Fred Drake62502051998-04-12 03:31:31 +0000100\begin{methoddesc}[Packer]{pack_list}{list, pack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000101Packs a \var{list} of homogeneous items. This method is useful for
102lists with an indeterminate size; i.e. the size is not available until
103the entire list has been walked. For each item in the list, an
104unsigned integer \code{1} is packed first, followed by the data value
105from the list. \var{pack_item} is the function that is called to pack
106the individual item. At the end of the list, an unsigned integer
107\code{0} is packed.
Fred Drakead56daf1999-04-20 13:41:14 +0000108
109For example, to pack a list of integers, the code might appear like
110this:
111
112\begin{verbatim}
113import xdrlib
114p = xdrlib.Packer()
115p.pack_list([1, 2, 3], p.pack_int)
116\end{verbatim}
Fred Drake62502051998-04-12 03:31:31 +0000117\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000118
Fred Drake62502051998-04-12 03:31:31 +0000119\begin{methoddesc}[Packer]{pack_farray}{n, array, pack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000120Packs a fixed length list (\var{array}) of homogeneous items. \var{n}
121is the length of the list; it is \emph{not} packed into the buffer,
Fred Drakeff79a211998-03-14 06:30:13 +0000122but a \exception{ValueError} exception is raised if
123\code{len(\var{array})} is not equal to \var{n}. As above,
124\var{pack_item} is the function used to pack each element.
Fred Drake62502051998-04-12 03:31:31 +0000125\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000126
Fred Drake62502051998-04-12 03:31:31 +0000127\begin{methoddesc}[Packer]{pack_array}{list, pack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000128Packs a variable length \var{list} of homogeneous items. First, the
129length of the list is packed as an unsigned integer, then each element
Fred Drakeff79a211998-03-14 06:30:13 +0000130is packed as in \method{pack_farray()} above.
Fred Drake62502051998-04-12 03:31:31 +0000131\end{methoddesc}
132
Guido van Rossum40006cf1996-08-19 22:58:03 +0000133
Fred Drakec18a6f31998-08-07 16:01:15 +0000134\subsection{Unpacker Objects \label{xdr-unpacker-objects}}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000135
Fred Drakeff79a211998-03-14 06:30:13 +0000136The \class{Unpacker} class offers the following methods:
Guido van Rossum40006cf1996-08-19 22:58:03 +0000137
Fred Drake62502051998-04-12 03:31:31 +0000138\begin{methoddesc}[Unpacker]{reset}{data}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000139Resets the string buffer with the given \var{data}.
Fred Drake62502051998-04-12 03:31:31 +0000140\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000141
Fred Drake62502051998-04-12 03:31:31 +0000142\begin{methoddesc}[Unpacker]{get_position}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000143Returns the current unpack position in the data buffer.
Fred Drake62502051998-04-12 03:31:31 +0000144\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000145
Fred Drake62502051998-04-12 03:31:31 +0000146\begin{methoddesc}[Unpacker]{set_position}{position}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000147Sets the data buffer unpack position to \var{position}. You should be
Fred Drakeff79a211998-03-14 06:30:13 +0000148careful about using \method{get_position()} and \method{set_position()}.
Fred Drake62502051998-04-12 03:31:31 +0000149\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000150
Fred Drake62502051998-04-12 03:31:31 +0000151\begin{methoddesc}[Unpacker]{get_buffer}{}
Barry Warsaw102dc411996-12-04 22:05:42 +0000152Returns the current unpack data buffer as a string.
Fred Drake62502051998-04-12 03:31:31 +0000153\end{methoddesc}
Barry Warsaw102dc411996-12-04 22:05:42 +0000154
Fred Drake62502051998-04-12 03:31:31 +0000155\begin{methoddesc}[Unpacker]{done}{}
Fred Drakeff79a211998-03-14 06:30:13 +0000156Indicates unpack completion. Raises an \exception{Error} exception
Guido van Rossum40006cf1996-08-19 22:58:03 +0000157if all of the data has not been unpacked.
Fred Drake62502051998-04-12 03:31:31 +0000158\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000159
Fred Drakeff79a211998-03-14 06:30:13 +0000160In addition, every data type that can be packed with a \class{Packer},
161can be unpacked with an \class{Unpacker}. Unpacking methods are of the
Fred Drake3c3d7ce1998-01-08 04:00:30 +0000162form \code{unpack_\var{type}()}, and take no arguments. They return the
Fred Drake040e5651997-10-24 21:15:55 +0000163unpacked object.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000164
Fred Drake62502051998-04-12 03:31:31 +0000165\begin{methoddesc}[Unpacker]{unpack_float}{}
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000166Unpacks a single-precision floating point number.
Fred Drake62502051998-04-12 03:31:31 +0000167\end{methoddesc}
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000168
Fred Drake62502051998-04-12 03:31:31 +0000169\begin{methoddesc}[Unpacker]{unpack_double}{}
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000170Unpacks a double-precision floating point number, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +0000171\method{unpack_float()}.
Fred Drake62502051998-04-12 03:31:31 +0000172\end{methoddesc}
Guido van Rossum3f247ad1996-09-27 17:11:24 +0000173
Guido van Rossum40006cf1996-08-19 22:58:03 +0000174In addition, the following methods unpack strings, bytes, and opaque
175data:
176
Fred Drake62502051998-04-12 03:31:31 +0000177\begin{methoddesc}[Unpacker]{unpack_fstring}{n}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000178Unpacks and returns a fixed length string. \var{n} is the number of
179characters expected. Padding with null bytes to guaranteed 4 byte
180alignment is assumed.
Fred Drake62502051998-04-12 03:31:31 +0000181\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000182
Fred Drake62502051998-04-12 03:31:31 +0000183\begin{methoddesc}[Unpacker]{unpack_fopaque}{n}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000184Unpacks and returns a fixed length opaque data stream, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +0000185\method{unpack_fstring()}.
Fred Drake62502051998-04-12 03:31:31 +0000186\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000187
Fred Drake62502051998-04-12 03:31:31 +0000188\begin{methoddesc}[Unpacker]{unpack_string}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000189Unpacks and returns a variable length string. The length of the
190string is first unpacked as an unsigned integer, then the string data
Fred Drakeff79a211998-03-14 06:30:13 +0000191is unpacked with \method{unpack_fstring()}.
Fred Drake62502051998-04-12 03:31:31 +0000192\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000193
Fred Drake62502051998-04-12 03:31:31 +0000194\begin{methoddesc}[Unpacker]{unpack_opaque}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000195Unpacks and returns a variable length opaque data string, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +0000196\method{unpack_string()}.
Fred Drake62502051998-04-12 03:31:31 +0000197\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000198
Fred Drake62502051998-04-12 03:31:31 +0000199\begin{methoddesc}[Unpacker]{unpack_bytes}{}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000200Unpacks and returns a variable length byte stream, similarly to
Fred Drakeff79a211998-03-14 06:30:13 +0000201\method{unpack_string()}.
Fred Drake62502051998-04-12 03:31:31 +0000202\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000203
204The following methods support unpacking arrays and lists:
205
Fred Drake62502051998-04-12 03:31:31 +0000206\begin{methoddesc}[Unpacker]{unpack_list}{unpack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000207Unpacks and returns a list of homogeneous items. The list is unpacked
208one element at a time
209by first unpacking an unsigned integer flag. If the flag is \code{1},
210then the item is unpacked and appended to the list. A flag of
211\code{0} indicates the end of the list. \var{unpack_item} is the
212function that is called to unpack the items.
Fred Drake62502051998-04-12 03:31:31 +0000213\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000214
Fred Drake62502051998-04-12 03:31:31 +0000215\begin{methoddesc}[Unpacker]{unpack_farray}{n, unpack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000216Unpacks and returns (as a list) a fixed length array of homogeneous
217items. \var{n} is number of list elements to expect in the buffer.
218As above, \var{unpack_item} is the function used to unpack each element.
Fred Drake62502051998-04-12 03:31:31 +0000219\end{methoddesc}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000220
Fred Drake62502051998-04-12 03:31:31 +0000221\begin{methoddesc}[Unpacker]{unpack_array}{unpack_item}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000222Unpacks and returns a variable length \var{list} of homogeneous items.
223First, the length of the list is unpacked as an unsigned integer, then
Fred Drakeff79a211998-03-14 06:30:13 +0000224each element is unpacked as in \method{unpack_farray()} above.
Fred Drake62502051998-04-12 03:31:31 +0000225\end{methoddesc}
226
Guido van Rossum40006cf1996-08-19 22:58:03 +0000227
Fred Drakec18a6f31998-08-07 16:01:15 +0000228\subsection{Exceptions \label{xdr-exceptions}}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000229
230Exceptions in this module are coded as class instances:
231
232\begin{excdesc}{Error}
Fred Drakeff79a211998-03-14 06:30:13 +0000233The base exception class. \exception{Error} has a single public data
234member \member{msg} containing the description of the error.
Guido van Rossum40006cf1996-08-19 22:58:03 +0000235\end{excdesc}
236
237\begin{excdesc}{ConversionError}
Fred Drakeff79a211998-03-14 06:30:13 +0000238Class derived from \exception{Error}. Contains no additional instance
Guido van Rossum40006cf1996-08-19 22:58:03 +0000239variables.
240\end{excdesc}
241
242Here is an example of how you would catch one of these exceptions:
243
Fred Drake19479911998-02-13 06:58:54 +0000244\begin{verbatim}
Guido van Rossum40006cf1996-08-19 22:58:03 +0000245import xdrlib
246p = xdrlib.Packer()
247try:
248 p.pack_double(8.01)
249except xdrlib.ConversionError, instance:
250 print 'packing the double failed:', instance.msg
Fred Drake19479911998-02-13 06:58:54 +0000251\end{verbatim}