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