Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{xdrlib} --- |
Fred Drake | 2d34cd3 | 2000-10-10 17:02:34 +0000 | [diff] [blame] | 2 | Encode and decode XDR data} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 2d34cd3 | 2000-10-10 17:02:34 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{xdrlib} |
Fred Drake | c18a6f3 | 1998-08-07 16:01:15 +0000 | [diff] [blame] | 5 | \modulesynopsis{Encoders and decoders for the External Data |
Fred Drake | 2d34cd3 | 2000-10-10 17:02:34 +0000 | [diff] [blame] | 6 | Representation (XDR).} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 8 | \index{XDR} |
Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 9 | \index{External Data Representation} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 10 | |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 11 | The \module{xdrlib} module supports the External Data Representation |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 12 | Standard as described in \rfc{1014}, written by Sun Microsystems, |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 13 | Inc. June 1987. It supports most of the data types described in the |
Fred Drake | ae18e9f | 1997-10-24 21:14:36 +0000 | [diff] [blame] | 14 | RFC. |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 15 | |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 16 | The \module{xdrlib} module defines two classes, one for packing |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 17 | variables into XDR representation, and another for unpacking from XDR |
| 18 | representation. There are also two exception classes. |
| 19 | |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 20 | \begin{classdesc}{Packer}{} |
| 21 | \class{Packer} is the class for packing data into XDR representation. |
| 22 | The \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 |
| 27 | values from a string buffer. The input buffer is given as |
| 28 | \var{data}. |
| 29 | \end{classdesc} |
| 30 | |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 31 | |
Fred Drake | 2d34cd3 | 2000-10-10 17:02:34 +0000 | [diff] [blame] | 32 | \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 Hettinger | 8ef3d8d | 2003-01-06 18:41:01 +0000 | [diff] [blame] | 36 | apparently been obsoleted by \rfc{1832}.} |
Fred Drake | 2d34cd3 | 2000-10-10 17:02:34 +0000 | [diff] [blame] | 37 | |
| 38 | \seerfc{1832}{XDR: External Data Representation Standard}{Newer RFC |
| 39 | that provides a revised definition of XDR.} |
| 40 | \end{seealso} |
| 41 | |
| 42 | |
Fred Drake | c18a6f3 | 1998-08-07 16:01:15 +0000 | [diff] [blame] | 43 | \subsection{Packer Objects \label{xdr-packer-objects}} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 44 | |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 45 | \class{Packer} instances have the following methods: |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 46 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 47 | \begin{methoddesc}[Packer]{get_buffer}{} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 48 | Returns the current pack buffer as a string. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 49 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 50 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 51 | \begin{methoddesc}[Packer]{reset}{} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 52 | Resets the pack buffer to the empty string. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 53 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 54 | |
| 55 | In general, you can pack any of the most common XDR data types by |
Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 56 | calling the appropriate \code{pack_\var{type}()} method. Each method |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 57 | takes a single argument, the value to pack. The following simple data |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 58 | type 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 Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 61 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 62 | \begin{methoddesc}[Packer]{pack_float}{value} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 63 | Packs the single-precision floating point number \var{value}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 64 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 65 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 66 | \begin{methoddesc}[Packer]{pack_double}{value} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 67 | Packs the double-precision floating point number \var{value}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 68 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 69 | |
| 70 | The following methods support packing strings, bytes, and opaque data: |
| 71 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 72 | \begin{methoddesc}[Packer]{pack_fstring}{n, s} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 73 | Packs a fixed length string, \var{s}. \var{n} is the length of the |
| 74 | string but it is \emph{not} packed into the data buffer. The string |
| 75 | is padded with null bytes if necessary to guaranteed 4 byte alignment. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 76 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 77 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 78 | \begin{methoddesc}[Packer]{pack_fopaque}{n, data} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 79 | Packs a fixed length opaque data stream, similarly to |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 80 | \method{pack_fstring()}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 81 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 82 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 83 | \begin{methoddesc}[Packer]{pack_string}{s} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 84 | Packs a variable length string, \var{s}. The length of the string is |
| 85 | first packed as an unsigned integer, then the string data is packed |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 86 | with \method{pack_fstring()}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 87 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 88 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 89 | \begin{methoddesc}[Packer]{pack_opaque}{data} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 90 | Packs a variable length opaque data string, similarly to |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 91 | \method{pack_string()}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 92 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 93 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 94 | \begin{methoddesc}[Packer]{pack_bytes}{bytes} |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 95 | Packs a variable length byte stream, similarly to \method{pack_string()}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 96 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 97 | |
| 98 | The following methods support packing arrays and lists: |
| 99 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 100 | \begin{methoddesc}[Packer]{pack_list}{list, pack_item} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 101 | Packs a \var{list} of homogeneous items. This method is useful for |
| 102 | lists with an indeterminate size; i.e. the size is not available until |
| 103 | the entire list has been walked. For each item in the list, an |
| 104 | unsigned integer \code{1} is packed first, followed by the data value |
| 105 | from the list. \var{pack_item} is the function that is called to pack |
| 106 | the individual item. At the end of the list, an unsigned integer |
| 107 | \code{0} is packed. |
Fred Drake | ad56daf | 1999-04-20 13:41:14 +0000 | [diff] [blame] | 108 | |
| 109 | For example, to pack a list of integers, the code might appear like |
| 110 | this: |
| 111 | |
| 112 | \begin{verbatim} |
| 113 | import xdrlib |
| 114 | p = xdrlib.Packer() |
| 115 | p.pack_list([1, 2, 3], p.pack_int) |
| 116 | \end{verbatim} |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 117 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 118 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 119 | \begin{methoddesc}[Packer]{pack_farray}{n, array, pack_item} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 120 | Packs a fixed length list (\var{array}) of homogeneous items. \var{n} |
| 121 | is the length of the list; it is \emph{not} packed into the buffer, |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 122 | but 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 Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 125 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 126 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 127 | \begin{methoddesc}[Packer]{pack_array}{list, pack_item} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 128 | Packs a variable length \var{list} of homogeneous items. First, the |
| 129 | length of the list is packed as an unsigned integer, then each element |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 130 | is packed as in \method{pack_farray()} above. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 131 | \end{methoddesc} |
| 132 | |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 133 | |
Fred Drake | c18a6f3 | 1998-08-07 16:01:15 +0000 | [diff] [blame] | 134 | \subsection{Unpacker Objects \label{xdr-unpacker-objects}} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 135 | |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 136 | The \class{Unpacker} class offers the following methods: |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 137 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 138 | \begin{methoddesc}[Unpacker]{reset}{data} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 139 | Resets the string buffer with the given \var{data}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 140 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 141 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 142 | \begin{methoddesc}[Unpacker]{get_position}{} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 143 | Returns the current unpack position in the data buffer. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 144 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 145 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 146 | \begin{methoddesc}[Unpacker]{set_position}{position} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 147 | Sets the data buffer unpack position to \var{position}. You should be |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 148 | careful about using \method{get_position()} and \method{set_position()}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 149 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 150 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 151 | \begin{methoddesc}[Unpacker]{get_buffer}{} |
Barry Warsaw | 102dc41 | 1996-12-04 22:05:42 +0000 | [diff] [blame] | 152 | Returns the current unpack data buffer as a string. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 153 | \end{methoddesc} |
Barry Warsaw | 102dc41 | 1996-12-04 22:05:42 +0000 | [diff] [blame] | 154 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 155 | \begin{methoddesc}[Unpacker]{done}{} |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 156 | Indicates unpack completion. Raises an \exception{Error} exception |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 157 | if all of the data has not been unpacked. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 158 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 159 | |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 160 | In addition, every data type that can be packed with a \class{Packer}, |
| 161 | can be unpacked with an \class{Unpacker}. Unpacking methods are of the |
Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 162 | form \code{unpack_\var{type}()}, and take no arguments. They return the |
Fred Drake | 040e565 | 1997-10-24 21:15:55 +0000 | [diff] [blame] | 163 | unpacked object. |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 164 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 165 | \begin{methoddesc}[Unpacker]{unpack_float}{} |
Guido van Rossum | 3f247ad | 1996-09-27 17:11:24 +0000 | [diff] [blame] | 166 | Unpacks a single-precision floating point number. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 167 | \end{methoddesc} |
Guido van Rossum | 3f247ad | 1996-09-27 17:11:24 +0000 | [diff] [blame] | 168 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 169 | \begin{methoddesc}[Unpacker]{unpack_double}{} |
Guido van Rossum | 3f247ad | 1996-09-27 17:11:24 +0000 | [diff] [blame] | 170 | Unpacks a double-precision floating point number, similarly to |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 171 | \method{unpack_float()}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 172 | \end{methoddesc} |
Guido van Rossum | 3f247ad | 1996-09-27 17:11:24 +0000 | [diff] [blame] | 173 | |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 174 | In addition, the following methods unpack strings, bytes, and opaque |
| 175 | data: |
| 176 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 177 | \begin{methoddesc}[Unpacker]{unpack_fstring}{n} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 178 | Unpacks and returns a fixed length string. \var{n} is the number of |
| 179 | characters expected. Padding with null bytes to guaranteed 4 byte |
| 180 | alignment is assumed. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 181 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 182 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 183 | \begin{methoddesc}[Unpacker]{unpack_fopaque}{n} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 184 | Unpacks and returns a fixed length opaque data stream, similarly to |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 185 | \method{unpack_fstring()}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 186 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 187 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 188 | \begin{methoddesc}[Unpacker]{unpack_string}{} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 189 | Unpacks and returns a variable length string. The length of the |
| 190 | string is first unpacked as an unsigned integer, then the string data |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 191 | is unpacked with \method{unpack_fstring()}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 192 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 193 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 194 | \begin{methoddesc}[Unpacker]{unpack_opaque}{} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 195 | Unpacks and returns a variable length opaque data string, similarly to |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 196 | \method{unpack_string()}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 197 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 198 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 199 | \begin{methoddesc}[Unpacker]{unpack_bytes}{} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 200 | Unpacks and returns a variable length byte stream, similarly to |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 201 | \method{unpack_string()}. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 202 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 203 | |
| 204 | The following methods support unpacking arrays and lists: |
| 205 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 206 | \begin{methoddesc}[Unpacker]{unpack_list}{unpack_item} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 207 | Unpacks and returns a list of homogeneous items. The list is unpacked |
| 208 | one element at a time |
| 209 | by first unpacking an unsigned integer flag. If the flag is \code{1}, |
| 210 | then 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 |
| 212 | function that is called to unpack the items. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 213 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 214 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 215 | \begin{methoddesc}[Unpacker]{unpack_farray}{n, unpack_item} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 216 | Unpacks and returns (as a list) a fixed length array of homogeneous |
| 217 | items. \var{n} is number of list elements to expect in the buffer. |
| 218 | As above, \var{unpack_item} is the function used to unpack each element. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 219 | \end{methoddesc} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 220 | |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 221 | \begin{methoddesc}[Unpacker]{unpack_array}{unpack_item} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 222 | Unpacks and returns a variable length \var{list} of homogeneous items. |
| 223 | First, the length of the list is unpacked as an unsigned integer, then |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 224 | each element is unpacked as in \method{unpack_farray()} above. |
Fred Drake | 6250205 | 1998-04-12 03:31:31 +0000 | [diff] [blame] | 225 | \end{methoddesc} |
| 226 | |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 227 | |
Fred Drake | c18a6f3 | 1998-08-07 16:01:15 +0000 | [diff] [blame] | 228 | \subsection{Exceptions \label{xdr-exceptions}} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 229 | |
| 230 | Exceptions in this module are coded as class instances: |
| 231 | |
| 232 | \begin{excdesc}{Error} |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 233 | The base exception class. \exception{Error} has a single public data |
| 234 | member \member{msg} containing the description of the error. |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 235 | \end{excdesc} |
| 236 | |
| 237 | \begin{excdesc}{ConversionError} |
Fred Drake | ff79a21 | 1998-03-14 06:30:13 +0000 | [diff] [blame] | 238 | Class derived from \exception{Error}. Contains no additional instance |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 239 | variables. |
| 240 | \end{excdesc} |
| 241 | |
| 242 | Here is an example of how you would catch one of these exceptions: |
| 243 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 244 | \begin{verbatim} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 245 | import xdrlib |
| 246 | p = xdrlib.Packer() |
| 247 | try: |
| 248 | p.pack_double(8.01) |
| 249 | except xdrlib.ConversionError, instance: |
| 250 | print 'packing the double failed:', instance.msg |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 251 | \end{verbatim} |