1 """Implements (a subset of) Sun XDR -- eXternal Data Representation.
11 """Exception class for this module. Use:
13 except xdrlib.Error, var:
14 # var has the Error instance for the exception
17 msg -- contains the message
20 def __init__(self
, msg
):
28 class ConversionError(Error
):
34 """Pack various data representations into a buffer."""
44 # backwards compatibility
47 def pack_uint(self
, x
):
48 self
.__buf
= self
.__buf
+ struct
.pack('>L', x
)
53 def pack_bool(self
, x
):
54 if x
: self
.__buf
= self
.__buf
+ '\0\0\0\1'
55 else: self
.__buf
= self
.__buf
+ '\0\0\0\0'
57 def pack_uhyper(self
, x
):
58 self
.pack_uint(x
>>32 & 0xffffffffL
)
59 self
.pack_uint(x
& 0xffffffffL
)
61 pack_hyper
= pack_uhyper
63 def pack_float(self
, x
):
64 try: self
.__buf
= self
.__buf
+ struct
.pack('>f', x
)
65 except struct
.error
, msg
:
66 raise ConversionError
, msg
68 def pack_double(self
, x
):
69 try: self
.__buf
= self
.__buf
+ struct
.pack('>d', x
)
70 except struct
.error
, msg
:
71 raise ConversionError
, msg
73 def pack_fstring(self
, n
, s
):
75 raise ValueError, 'fstring size must be nonnegative'
78 data
= data
+ (n
- len(data
)) * '\0'
79 self
.__buf
= self
.__buf
+ data
81 pack_fopaque
= pack_fstring
83 def pack_string(self
, s
):
86 self
.pack_fstring(n
, s
)
88 pack_opaque
= pack_string
89 pack_bytes
= pack_string
91 def pack_list(self
, list, pack_item
):
97 def pack_farray(self
, n
, list, pack_item
):
99 raise ValueError, 'wrong array size'
103 def pack_array(self
, list, pack_item
):
106 self
.pack_farray(n
, list, pack_item
)
111 """Unpacks various data representations from the given buffer."""
113 def __init__(self
, data
):
116 def reset(self
, data
):
120 def get_position(self
):
123 def set_position(self
, position
):
124 self
.__pos
= position
126 def get_buffer(self
):
130 if self
.__pos
< len(self
.__buf
):
131 raise Error('unextracted data remains')
133 def unpack_uint(self
):
136 data
= self
.__buf
[i
:j
]
139 x
= struct
.unpack('>L', data
)[0]
142 except OverflowError:
145 def unpack_int(self
):
148 data
= self
.__buf
[i
:j
]
151 return struct
.unpack('>l', data
)[0]
153 unpack_enum
= unpack_int
154 unpack_bool
= unpack_int
156 def unpack_uhyper(self
):
157 hi
= self
.unpack_uint()
158 lo
= self
.unpack_uint()
159 return long(hi
)<<32 | lo
161 def unpack_hyper(self
):
162 x
= self
.unpack_uhyper()
163 if x
>= 0x8000000000000000L
:
164 x
= x
- 0x10000000000000000L
167 def unpack_float(self
):
170 data
= self
.__buf
[i
:j
]
173 return struct
.unpack('>f', data
)[0]
175 def unpack_double(self
):
178 data
= self
.__buf
[i
:j
]
181 return struct
.unpack('>d', data
)[0]
183 def unpack_fstring(self
, n
):
185 raise ValueError, 'fstring size must be nonnegative'
188 if j
> len(self
.__buf
):
191 return self
.__buf
[i
:i
+n
]
193 unpack_fopaque
= unpack_fstring
195 def unpack_string(self
):
196 n
= self
.unpack_uint()
197 return self
.unpack_fstring(n
)
199 unpack_opaque
= unpack_string
200 unpack_bytes
= unpack_string
202 def unpack_list(self
, unpack_item
):
205 x
= self
.unpack_uint()
208 raise ConversionError
, '0 or 1 expected, got ' + `x`
213 def unpack_farray(self
, n
, unpack_item
):
216 list.append(unpack_item())
219 def unpack_array(self
, unpack_item
):
220 n
= self
.unpack_uint()
221 return self
.unpack_farray(n
, unpack_item
)
229 (p
.pack_bool
, (None,)),
230 (p
.pack_bool
, ('hello',)),
231 (p
.pack_uhyper
, (45L,)),
232 (p
.pack_float
, (1.9,)),
233 (p
.pack_double
, (1.9,)),
234 (p
.pack_string
, ('hello world',)),
235 (p
.pack_list
, (range(5), p
.pack_uint
)),
236 (p
.pack_array
, (['what', 'is', 'hapnin', 'doctor'], p
.pack_string
)),
238 succeedlist
= [1] * len(packtest
)
240 for method
, args
in packtest
:
241 print 'pack test', count
,
245 except ConversionError
, var
:
246 print 'ConversionError:', var
.msg
247 succeedlist
[count
] = 0
249 data
= p
.get_buffer()
253 (up
.unpack_uint
, (), lambda x
: x
== 9),
254 (up
.unpack_bool
, (), lambda x
: not x
),
255 (up
.unpack_bool
, (), lambda x
: x
),
256 (up
.unpack_uhyper
, (), lambda x
: x
== 45L),
257 (up
.unpack_float
, (), lambda x
: 1.89 < x
< 1.91),
258 (up
.unpack_double
, (), lambda x
: 1.89 < x
< 1.91),
259 (up
.unpack_string
, (), lambda x
: x
== 'hello world'),
260 (up
.unpack_list
, (up
.unpack_uint
,), lambda x
: x
== range(5)),
261 (up
.unpack_array
, (up
.unpack_string
,),
262 lambda x
: x
== ['what', 'is', 'hapnin', 'doctor']),
265 for method
, args
, pred
in unpacktest
:
266 print 'unpack test', count
,
268 if succeedlist
[count
]:
269 x
= apply(method
, args
)
270 print pred(x
) and 'succeeded' or 'failed', ':', x
273 except ConversionError
, var
:
274 print 'ConversionError:', var
.msg
278 if __name__
== '__main__':