Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +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} |
| 5 | |
| 6 | \renewcommand{\indexsubitem}{(in module xdrlib)} |
| 7 | |
| 8 | |
| 9 | The \code{xdrlib} module supports the External Data Representation |
| 10 | Standard as described in RFC 1014, written by Sun Microsystems, |
| 11 | Inc. June 1987. It supports most of the data types described in the |
| 12 | RFC, although some, most notably \code{float} and \code{double} are |
| 13 | only supported on those operating systems that provide an XDR |
| 14 | library. |
| 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 |
| 35 | calling the appropriate \code{pack_\var{type}} method. Each method |
| 36 | takes a single argument, the value to pack. The following simple data |
| 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}. |
| 40 | |
| 41 | The following methods pack floating point numbers, however they |
| 42 | require C library support. Without the optional C built-in module, |
| 43 | both of these methods will raise an \code{xdrlib.ConversionError} |
| 44 | exception. See the note at the end of this chapter for details. |
| 45 | |
| 46 | \begin{funcdesc}{pack_float}{value} |
| 47 | Packs the single-precision floating point number \var{value}. |
| 48 | \end{funcdesc} |
| 49 | |
| 50 | \begin{funcdesc}{pack_double}{value} |
| 51 | Packs the double-precision floating point number \var{value}. |
| 52 | \end{funcdesc} |
| 53 | |
| 54 | The following methods support packing strings, bytes, and opaque data: |
| 55 | |
| 56 | \begin{funcdesc}{pack_fstring}{n\, s} |
| 57 | Packs a fixed length string, \var{s}. \var{n} is the length of the |
| 58 | string but it is \emph{not} packed into the data buffer. The string |
| 59 | is padded with null bytes if necessary to guaranteed 4 byte alignment. |
| 60 | \end{funcdesc} |
| 61 | |
| 62 | \begin{funcdesc}{pack_fopaque}{n\, data} |
| 63 | Packs a fixed length opaque data stream, similarly to |
| 64 | \code{pack_fstring}. |
| 65 | \end{funcdesc} |
| 66 | |
| 67 | \begin{funcdesc}{pack_string}{s} |
| 68 | Packs a variable length string, \var{s}. The length of the string is |
| 69 | first packed as an unsigned integer, then the string data is packed |
| 70 | with \code{pack_fstring}. |
| 71 | \end{funcdesc} |
| 72 | |
| 73 | \begin{funcdesc}{pack_opaque}{data} |
| 74 | Packs a variable length opaque data string, similarly to |
| 75 | \code{pack_string}. |
| 76 | \end{funcdesc} |
| 77 | |
| 78 | \begin{funcdesc}{pack_bytes}{bytes} |
| 79 | Packs a variable length byte stream, similarly to \code{pack_string}. |
| 80 | \end{funcdesc} |
| 81 | |
| 82 | The following methods support packing arrays and lists: |
| 83 | |
| 84 | \begin{funcdesc}{pack_list}{list\, pack_item} |
| 85 | Packs a \var{list} of homogeneous items. This method is useful for |
| 86 | lists with an indeterminate size; i.e. the size is not available until |
| 87 | the entire list has been walked. For each item in the list, an |
| 88 | unsigned integer \code{1} is packed first, followed by the data value |
| 89 | from the list. \var{pack_item} is the function that is called to pack |
| 90 | the individual item. At the end of the list, an unsigned integer |
| 91 | \code{0} is packed. |
| 92 | \end{funcdesc} |
| 93 | |
| 94 | \begin{funcdesc}{pack_farray}{n\, array\, pack_item} |
| 95 | Packs a fixed length list (\var{array}) of homogeneous items. \var{n} |
| 96 | is the length of the list; it is \emph{not} packed into the buffer, |
| 97 | but a \code{ValueError} exception is raised if \code{len(array)} is not |
| 98 | equal to \var{n}. As above, \var{pack_item} is the function used to |
| 99 | pack each element. |
| 100 | \end{funcdesc} |
| 101 | |
| 102 | \begin{funcdesc}{pack_array}{list\, pack_item} |
| 103 | Packs a variable length \var{list} of homogeneous items. First, the |
| 104 | length of the list is packed as an unsigned integer, then each element |
| 105 | is packed as in \code{pack_farray} above. |
| 106 | \end{funcdesc} |
| 107 | |
| 108 | \subsection{Unpacker Objects} |
| 109 | |
| 110 | \code{Unpacker} is the complementary class which unpacks XDR data |
| 111 | values from a string buffer, and has the following methods: |
| 112 | |
| 113 | \begin{funcdesc}{__init__}{data} |
| 114 | Instantiates an \code{Unpacker} object with the string buffer |
| 115 | \var{data}. |
| 116 | \end{funcdesc} |
| 117 | |
| 118 | \begin{funcdesc}{reset}{data} |
| 119 | Resets the string buffer with the given \var{data}. |
| 120 | \end{funcdesc} |
| 121 | |
| 122 | \begin{funcdesc}{get_position}{} |
| 123 | Returns the current unpack position in the data buffer. |
| 124 | \end{funcdesc} |
| 125 | |
| 126 | \begin{funcdesc}{set_position}{position} |
| 127 | Sets the data buffer unpack position to \var{position}. You should be |
| 128 | careful about using \code{get_position()} and \code{set_position()}. |
| 129 | \end{funcdesc} |
| 130 | |
Barry Warsaw | 102dc41 | 1996-12-04 22:05:42 +0000 | [diff] [blame] | 131 | \begin{funcdesc}{get_buffer}{} |
| 132 | Returns the current unpack data buffer as a string. |
| 133 | \end{funcdesc} |
| 134 | |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 135 | \begin{funcdesc}{done}{} |
| 136 | Indicates unpack completion. Raises an \code{xdrlib.Error} exception |
| 137 | if all of the data has not been unpacked. |
| 138 | \end{funcdesc} |
| 139 | |
| 140 | In addition, every data type that can be packed with a \code{Packer}, |
| 141 | can be unpacked with an \code{Unpacker}. Unpacking methods are of the |
| 142 | form \code{unpack_\var{type}}, and take no arguments. They return the |
| 143 | unpacked object. The same caveats apply for \code{unpack_float} and |
| 144 | \code{unpack_double} as above. |
| 145 | |
Guido van Rossum | 3f247ad | 1996-09-27 17:11:24 +0000 | [diff] [blame] | 146 | \begin{funcdesc}{unpack_float}{} |
| 147 | Unpacks a single-precision floating point number. |
| 148 | \end{funcdesc} |
| 149 | |
| 150 | \begin{funcdesc}{unpack_double}{} |
| 151 | Unpacks a double-precision floating point number, similarly to |
| 152 | \code{unpack_float}. |
| 153 | \end{funcdesc} |
| 154 | |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 155 | In addition, the following methods unpack strings, bytes, and opaque |
| 156 | data: |
| 157 | |
| 158 | \begin{funcdesc}{unpack_fstring}{n} |
| 159 | Unpacks and returns a fixed length string. \var{n} is the number of |
| 160 | characters expected. Padding with null bytes to guaranteed 4 byte |
| 161 | alignment is assumed. |
| 162 | \end{funcdesc} |
| 163 | |
| 164 | \begin{funcdesc}{unpack_fopaque}{n} |
| 165 | Unpacks and returns a fixed length opaque data stream, similarly to |
| 166 | \code{unpack_fstring}. |
| 167 | \end{funcdesc} |
| 168 | |
Guido van Rossum | 3f247ad | 1996-09-27 17:11:24 +0000 | [diff] [blame] | 169 | \begin{funcdesc}{unpack_string}{} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 170 | Unpacks and returns a variable length string. The length of the |
| 171 | string is first unpacked as an unsigned integer, then the string data |
| 172 | is unpacked with \code{unpack_fstring}. |
| 173 | \end{funcdesc} |
| 174 | |
| 175 | \begin{funcdesc}{unpack_opaque}{} |
| 176 | Unpacks and returns a variable length opaque data string, similarly to |
Guido van Rossum | 3f247ad | 1996-09-27 17:11:24 +0000 | [diff] [blame] | 177 | \code{unpack_string}. |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 178 | \end{funcdesc} |
| 179 | |
| 180 | \begin{funcdesc}{unpack_bytes}{} |
| 181 | Unpacks and returns a variable length byte stream, similarly to |
Guido van Rossum | 3f247ad | 1996-09-27 17:11:24 +0000 | [diff] [blame] | 182 | \code{unpack_string}. |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 183 | \end{funcdesc} |
| 184 | |
| 185 | The following methods support unpacking arrays and lists: |
| 186 | |
| 187 | \begin{funcdesc}{unpack_list}{unpack_item} |
| 188 | Unpacks and returns a list of homogeneous items. The list is unpacked |
| 189 | one element at a time |
| 190 | by first unpacking an unsigned integer flag. If the flag is \code{1}, |
| 191 | then the item is unpacked and appended to the list. A flag of |
| 192 | \code{0} indicates the end of the list. \var{unpack_item} is the |
| 193 | function that is called to unpack the items. |
| 194 | \end{funcdesc} |
| 195 | |
| 196 | \begin{funcdesc}{unpack_farray}{n\, unpack_item} |
| 197 | Unpacks and returns (as a list) a fixed length array of homogeneous |
| 198 | items. \var{n} is number of list elements to expect in the buffer. |
| 199 | As above, \var{unpack_item} is the function used to unpack each element. |
| 200 | \end{funcdesc} |
| 201 | |
| 202 | \begin{funcdesc}{unpack_array}{unpack_item} |
| 203 | Unpacks and returns a variable length \var{list} of homogeneous items. |
| 204 | First, the length of the list is unpacked as an unsigned integer, then |
| 205 | each element is unpacked as in \code{unpack_farray} above. |
| 206 | \end{funcdesc} |
| 207 | |
| 208 | \subsection{Exceptions} |
Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 209 | \nodename{Exceptions in xdrlib module} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 210 | |
| 211 | Exceptions in this module are coded as class instances: |
| 212 | |
| 213 | \begin{excdesc}{Error} |
| 214 | The base exception class. \code{Error} has a single public data |
| 215 | member \code{msg} containing the description of the error. |
| 216 | \end{excdesc} |
| 217 | |
| 218 | \begin{excdesc}{ConversionError} |
| 219 | Class derived from \code{Error}. Contains no additional instance |
| 220 | variables. |
| 221 | \end{excdesc} |
| 222 | |
| 223 | Here is an example of how you would catch one of these exceptions: |
| 224 | |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame^] | 225 | \bcode\begin{verbatim} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 226 | import xdrlib |
| 227 | p = xdrlib.Packer() |
| 228 | try: |
| 229 | p.pack_double(8.01) |
| 230 | except xdrlib.ConversionError, instance: |
| 231 | print 'packing the double failed:', instance.msg |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame^] | 232 | \end{verbatim}\ecode |
| 233 | % |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 234 | \subsection{Supporting Floating Point Data} |
| 235 | |
| 236 | Packing and unpacking floating point data, |
| 237 | i.e. \code{Packer.pack_float}, \code{Packer.pack_double}, |
| 238 | \code{Unpacker.unpack_float}, and \code{Unpacker.unpack_double}, are |
| 239 | only supported with the helper built-in \code{_xdr} module, which |
| 240 | relies on your operating system having the appropriate XDR library |
| 241 | routines. |
| 242 | |
| 243 | If you have built the Python interpeter with the \code{_xdr} module, |
| 244 | or have built the \code{_xdr} module as a shared library, |
| 245 | \code{xdrlib} will use these to pack and unpack floating point |
| 246 | numbers. Otherwise, using these routines will raise a |
| 247 | \code{ConversionError} exception. |
| 248 | |
| 249 | See the Python installation instructions for details on building the |
| 250 | \code{_xdr} module. |