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