| Fred Drake | 20417b7 | 1997-12-17 14:17:35 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{xdrlib}} | 
| Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-xdrlib} | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 3 | \stmodindex{xdrlib} | 
|  | 4 | \index{XDR} | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 5 | \index{External Data Representation} | 
| Fred Drake | 20417b7 | 1997-12-17 14:17:35 +0000 | [diff] [blame] | 6 | \index{RFC!1014} | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 7 |  | 
|  | 8 | \renewcommand{\indexsubitem}{(in module xdrlib)} | 
|  | 9 |  | 
|  | 10 |  | 
|  | 11 | The \code{xdrlib} module supports the External Data Representation | 
|  | 12 | Standard as described in RFC 1014, written by Sun Microsystems, | 
|  | 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 |  | 
|  | 16 | The \code{xdrlib} module defines two classes, one for packing | 
|  | 17 | variables into XDR representation, and another for unpacking from XDR | 
|  | 18 | representation.  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. | 
|  | 24 | The \code{Packer} class is instantiated with no arguments. | 
|  | 25 |  | 
|  | 26 | \begin{funcdesc}{get_buffer}{} | 
|  | 27 | Returns the current pack buffer as a string. | 
|  | 28 | \end{funcdesc} | 
|  | 29 |  | 
|  | 30 | \begin{funcdesc}{reset}{} | 
|  | 31 | Resets the pack buffer to the empty string. | 
|  | 32 | \end{funcdesc} | 
|  | 33 |  | 
|  | 34 | 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] | 35 | calling the appropriate \code{pack_\var{type}()} method.  Each method | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 36 | takes a single argument, the value to pack.  The following simple data | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 37 | type packing methods are supported: \code{pack_uint()}, \code{pack_int()}, | 
|  | 38 | \code{pack_enum()}, \code{pack_bool()}, \code{pack_uhyper()}, | 
|  | 39 | and \code{pack_hyper()}. | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 40 |  | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 41 | \begin{funcdesc}{pack_float}{value} | 
|  | 42 | Packs the single-precision floating point number \var{value}. | 
|  | 43 | \end{funcdesc} | 
|  | 44 |  | 
|  | 45 | \begin{funcdesc}{pack_double}{value} | 
|  | 46 | Packs the double-precision floating point number \var{value}. | 
|  | 47 | \end{funcdesc} | 
|  | 48 |  | 
|  | 49 | The following methods support packing strings, bytes, and opaque data: | 
|  | 50 |  | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 51 | \begin{funcdesc}{pack_fstring}{n, s} | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 52 | Packs a fixed length string, \var{s}.  \var{n} is the length of the | 
|  | 53 | string but it is \emph{not} packed into the data buffer.  The string | 
|  | 54 | is padded with null bytes if necessary to guaranteed 4 byte alignment. | 
|  | 55 | \end{funcdesc} | 
|  | 56 |  | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 57 | \begin{funcdesc}{pack_fopaque}{n, data} | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 58 | Packs a fixed length opaque data stream, similarly to | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 59 | \code{pack_fstring()}. | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 60 | \end{funcdesc} | 
|  | 61 |  | 
|  | 62 | \begin{funcdesc}{pack_string}{s} | 
|  | 63 | Packs a variable length string, \var{s}.  The length of the string is | 
|  | 64 | first packed as an unsigned integer, then the string data is packed | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 65 | with \code{pack_fstring()}. | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 66 | \end{funcdesc} | 
|  | 67 |  | 
|  | 68 | \begin{funcdesc}{pack_opaque}{data} | 
|  | 69 | Packs a variable length opaque data string, similarly to | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 70 | \code{pack_string()}. | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 71 | \end{funcdesc} | 
|  | 72 |  | 
|  | 73 | \begin{funcdesc}{pack_bytes}{bytes} | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 74 | Packs a variable length byte stream, similarly to \code{pack_string()}. | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 75 | \end{funcdesc} | 
|  | 76 |  | 
|  | 77 | The following methods support packing arrays and lists: | 
|  | 78 |  | 
|  | 79 | \begin{funcdesc}{pack_list}{list\, pack_item} | 
|  | 80 | Packs a \var{list} of homogeneous items.  This method is useful for | 
|  | 81 | lists with an indeterminate size; i.e. the size is not available until | 
|  | 82 | the entire list has been walked.  For each item in the list, an | 
|  | 83 | unsigned integer \code{1} is packed first, followed by the data value | 
|  | 84 | from the list.  \var{pack_item} is the function that is called to pack | 
|  | 85 | the 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} | 
|  | 90 | Packs a fixed length list (\var{array}) of homogeneous items.  \var{n} | 
|  | 91 | is the length of the list; it is \emph{not} packed into the buffer, | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 92 | but a \code{ValueError} exception is raised if \code{len(\var{array})} is not | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 93 | equal to \var{n}.  As above, \var{pack_item} is the function used to | 
|  | 94 | pack each element. | 
|  | 95 | \end{funcdesc} | 
|  | 96 |  | 
|  | 97 | \begin{funcdesc}{pack_array}{list\, pack_item} | 
|  | 98 | Packs a variable length \var{list} of homogeneous items.  First, the | 
|  | 99 | length of the list is packed as an unsigned integer, then each element | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 100 | is packed as in \code{pack_farray()} above. | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 101 | \end{funcdesc} | 
|  | 102 |  | 
|  | 103 | \subsection{Unpacker Objects} | 
|  | 104 |  | 
|  | 105 | \code{Unpacker} is the complementary class which unpacks XDR data | 
|  | 106 | values from a string buffer, and has the following methods: | 
|  | 107 |  | 
|  | 108 | \begin{funcdesc}{__init__}{data} | 
|  | 109 | Instantiates an \code{Unpacker} object with the string buffer | 
|  | 110 | \var{data}. | 
|  | 111 | \end{funcdesc} | 
|  | 112 |  | 
|  | 113 | \begin{funcdesc}{reset}{data} | 
|  | 114 | Resets the string buffer with the given \var{data}. | 
|  | 115 | \end{funcdesc} | 
|  | 116 |  | 
|  | 117 | \begin{funcdesc}{get_position}{} | 
|  | 118 | Returns the current unpack position in the data buffer. | 
|  | 119 | \end{funcdesc} | 
|  | 120 |  | 
|  | 121 | \begin{funcdesc}{set_position}{position} | 
|  | 122 | Sets the data buffer unpack position to \var{position}.  You should be | 
|  | 123 | careful about using \code{get_position()} and \code{set_position()}. | 
|  | 124 | \end{funcdesc} | 
|  | 125 |  | 
| Barry Warsaw | 102dc41 | 1996-12-04 22:05:42 +0000 | [diff] [blame] | 126 | \begin{funcdesc}{get_buffer}{} | 
|  | 127 | Returns the current unpack data buffer as a string. | 
|  | 128 | \end{funcdesc} | 
|  | 129 |  | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 130 | \begin{funcdesc}{done}{} | 
|  | 131 | Indicates unpack completion.  Raises an \code{xdrlib.Error} exception | 
|  | 132 | if all of the data has not been unpacked. | 
|  | 133 | \end{funcdesc} | 
|  | 134 |  | 
|  | 135 | In addition, every data type that can be packed with a \code{Packer}, | 
|  | 136 | can be unpacked with an \code{Unpacker}.  Unpacking methods are of the | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 137 | form \code{unpack_\var{type}()}, and take no arguments.  They return the | 
| Fred Drake | 040e565 | 1997-10-24 21:15:55 +0000 | [diff] [blame] | 138 | unpacked object. | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 139 |  | 
| Guido van Rossum | 3f247ad | 1996-09-27 17:11:24 +0000 | [diff] [blame] | 140 | \begin{funcdesc}{unpack_float}{} | 
|  | 141 | Unpacks a single-precision floating point number. | 
|  | 142 | \end{funcdesc} | 
|  | 143 |  | 
|  | 144 | \begin{funcdesc}{unpack_double}{} | 
|  | 145 | Unpacks a double-precision floating point number, similarly to | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 146 | \code{unpack_float()}. | 
| Guido van Rossum | 3f247ad | 1996-09-27 17:11:24 +0000 | [diff] [blame] | 147 | \end{funcdesc} | 
|  | 148 |  | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 149 | In addition, the following methods unpack strings, bytes, and opaque | 
|  | 150 | data: | 
|  | 151 |  | 
|  | 152 | \begin{funcdesc}{unpack_fstring}{n} | 
|  | 153 | Unpacks and returns a fixed length string.  \var{n} is the number of | 
|  | 154 | characters expected.  Padding with null bytes to guaranteed 4 byte | 
|  | 155 | alignment is assumed. | 
|  | 156 | \end{funcdesc} | 
|  | 157 |  | 
|  | 158 | \begin{funcdesc}{unpack_fopaque}{n} | 
|  | 159 | Unpacks and returns a fixed length opaque data stream, similarly to | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 160 | \code{unpack_fstring()}. | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 161 | \end{funcdesc} | 
|  | 162 |  | 
| Guido van Rossum | 3f247ad | 1996-09-27 17:11:24 +0000 | [diff] [blame] | 163 | \begin{funcdesc}{unpack_string}{} | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 164 | Unpacks and returns a variable length string.  The length of the | 
|  | 165 | string is first unpacked as an unsigned integer, then the string data | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 166 | is unpacked with \code{unpack_fstring()}. | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 167 | \end{funcdesc} | 
|  | 168 |  | 
|  | 169 | \begin{funcdesc}{unpack_opaque}{} | 
|  | 170 | Unpacks and returns a variable length opaque data string, similarly to | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 171 | \code{unpack_string()}. | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 172 | \end{funcdesc} | 
|  | 173 |  | 
|  | 174 | \begin{funcdesc}{unpack_bytes}{} | 
|  | 175 | Unpacks and returns a variable length byte stream, similarly to | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 176 | \code{unpack_string()}. | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 177 | \end{funcdesc} | 
|  | 178 |  | 
|  | 179 | The following methods support unpacking arrays and lists: | 
|  | 180 |  | 
|  | 181 | \begin{funcdesc}{unpack_list}{unpack_item} | 
|  | 182 | Unpacks and returns a list of homogeneous items.  The list is unpacked | 
|  | 183 | one element at a time | 
|  | 184 | by first unpacking an unsigned integer flag.  If the flag is \code{1}, | 
|  | 185 | then 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 | 
|  | 187 | function that is called to unpack the items. | 
|  | 188 | \end{funcdesc} | 
|  | 189 |  | 
|  | 190 | \begin{funcdesc}{unpack_farray}{n\, unpack_item} | 
|  | 191 | Unpacks and returns (as a list) a fixed length array of homogeneous | 
|  | 192 | items.  \var{n} is number of list elements to expect in the buffer. | 
|  | 193 | As above, \var{unpack_item} is the function used to unpack each element. | 
|  | 194 | \end{funcdesc} | 
|  | 195 |  | 
|  | 196 | \begin{funcdesc}{unpack_array}{unpack_item} | 
|  | 197 | Unpacks and returns a variable length \var{list} of homogeneous items. | 
|  | 198 | First, the length of the list is unpacked as an unsigned integer, then | 
| Fred Drake | 3c3d7ce | 1998-01-08 04:00:30 +0000 | [diff] [blame] | 199 | each element is unpacked as in \code{unpack_farray()} above. | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 200 | \end{funcdesc} | 
|  | 201 |  | 
|  | 202 | \subsection{Exceptions} | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 203 | \nodename{Exceptions in xdrlib module} | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 204 |  | 
|  | 205 | Exceptions in this module are coded as class instances: | 
|  | 206 |  | 
|  | 207 | \begin{excdesc}{Error} | 
|  | 208 | The base exception class.  \code{Error} has a single public data | 
|  | 209 | member \code{msg} containing the description of the error. | 
|  | 210 | \end{excdesc} | 
|  | 211 |  | 
|  | 212 | \begin{excdesc}{ConversionError} | 
|  | 213 | Class derived from \code{Error}.  Contains no additional instance | 
|  | 214 | variables. | 
|  | 215 | \end{excdesc} | 
|  | 216 |  | 
|  | 217 | Here is an example of how you would catch one of these exceptions: | 
|  | 218 |  | 
| Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 219 | \bcode\begin{verbatim} | 
| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 220 | import xdrlib | 
|  | 221 | p = xdrlib.Packer() | 
|  | 222 | try: | 
|  | 223 | p.pack_double(8.01) | 
|  | 224 | except xdrlib.ConversionError, instance: | 
|  | 225 | print 'packing the double failed:', instance.msg | 
| Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 226 | \end{verbatim}\ecode |