1 """Implements (a subset of) Sun XDR -- eXternal Data Representation.
9 __all__
= ["Error", "Packer", "Unpacker", "ConversionError"]
13 """Exception class for this module. Use:
15 except xdrlib.Error, var:
16 # var has the Error instance for the exception
19 msg -- contains the message
22 def __init__(self
, msg
):
30 class ConversionError(Error
):
36 """Pack various data representations into a buffer."""
46 # backwards compatibility
49 def pack_uint(self
, x
):
50 self
.__buf
= self
.__buf
+ struct
.pack('>L', x
)
55 def pack_bool(self
, x
):
56 if x
: self
.__buf
= self
.__buf
+ '\0\0\0\1'
57 else: self
.__buf
= self
.__buf
+ '\0\0\0\0'
59 def pack_uhyper(self
, x
):
60 self
.pack_uint(x
>>32 & 0xffffffffL
)
61 self
.pack_uint(x
& 0xffffffffL
)
63 pack_hyper
= pack_uhyper
65 def pack_float(self
, x
):
66 try: self
.__buf
= self
.__buf
+ struct
.pack('>f', x
)
67 except struct
.error
, msg
:
68 raise ConversionError
, msg
70 def pack_double(self
, x
):
71 try: self
.__buf
= self
.__buf
+ struct
.pack('>d', x
)
72 except struct
.error
, msg
:
73 raise ConversionError
, msg
75 def pack_fstring(self
, n
, s
):
77 raise ValueError, 'fstring size must be nonnegative'
80 data
= data
+ (n
- len(data
)) * '\0'
81 self
.__buf
= self
.__buf
+ data
83 pack_fopaque
= pack_fstring
85 def pack_string(self
, s
):
88 self
.pack_fstring(n
, s
)
90 pack_opaque
= pack_string
91 pack_bytes
= pack_string
93 def pack_list(self
, list, pack_item
):
99 def pack_farray(self
, n
, list, pack_item
):
101 raise ValueError, 'wrong array size'
105 def pack_array(self
, list, pack_item
):
108 self
.pack_farray(n
, list, pack_item
)
113 """Unpacks various data representations from the given buffer."""
115 def __init__(self
, data
):
118 def reset(self
, data
):
122 def get_position(self
):
125 def set_position(self
, position
):
126 self
.__pos
= position
128 def get_buffer(self
):
132 if self
.__pos
< len(self
.__buf
):
133 raise Error('unextracted data remains')
135 def unpack_uint(self
):
138 data
= self
.__buf
[i
:j
]
141 x
= struct
.unpack('>L', data
)[0]
144 except OverflowError:
147 def unpack_int(self
):
150 data
= self
.__buf
[i
:j
]
153 return struct
.unpack('>l', data
)[0]
155 unpack_enum
= unpack_int
156 unpack_bool
= unpack_int
158 def unpack_uhyper(self
):
159 hi
= self
.unpack_uint()
160 lo
= self
.unpack_uint()
161 return long(hi
)<<32 | lo
163 def unpack_hyper(self
):
164 x
= self
.unpack_uhyper()
165 if x
>= 0x8000000000000000L
:
166 x
= x
- 0x10000000000000000L
169 def unpack_float(self
):
172 data
= self
.__buf
[i
:j
]
175 return struct
.unpack('>f', data
)[0]
177 def unpack_double(self
):
180 data
= self
.__buf
[i
:j
]
183 return struct
.unpack('>d', data
)[0]
185 def unpack_fstring(self
, n
):
187 raise ValueError, 'fstring size must be nonnegative'
190 if j
> len(self
.__buf
):
193 return self
.__buf
[i
:i
+n
]
195 unpack_fopaque
= unpack_fstring
197 def unpack_string(self
):
198 n
= self
.unpack_uint()
199 return self
.unpack_fstring(n
)
201 unpack_opaque
= unpack_string
202 unpack_bytes
= unpack_string
204 def unpack_list(self
, unpack_item
):
207 x
= self
.unpack_uint()
210 raise ConversionError
, '0 or 1 expected, got ' + `x`
215 def unpack_farray(self
, n
, unpack_item
):
218 list.append(unpack_item())
221 def unpack_array(self
, unpack_item
):
222 n
= self
.unpack_uint()
223 return self
.unpack_farray(n
, unpack_item
)
231 (p
.pack_bool
, (None,)),
232 (p
.pack_bool
, ('hello',)),
233 (p
.pack_uhyper
, (45L,)),
234 (p
.pack_float
, (1.9,)),
235 (p
.pack_double
, (1.9,)),
236 (p
.pack_string
, ('hello world',)),
237 (p
.pack_list
, (range(5), p
.pack_uint
)),
238 (p
.pack_array
, (['what', 'is', 'hapnin', 'doctor'], p
.pack_string
)),
240 succeedlist
= [1] * len(packtest
)
242 for method
, args
in packtest
:
243 print 'pack test', count
,
247 except ConversionError
, var
:
248 print 'ConversionError:', var
.msg
249 succeedlist
[count
] = 0
251 data
= p
.get_buffer()
255 (up
.unpack_uint
, (), lambda x
: x
== 9),
256 (up
.unpack_bool
, (), lambda x
: not x
),
257 (up
.unpack_bool
, (), lambda x
: x
),
258 (up
.unpack_uhyper
, (), lambda x
: x
== 45L),
259 (up
.unpack_float
, (), lambda x
: 1.89 < x
< 1.91),
260 (up
.unpack_double
, (), lambda x
: 1.89 < x
< 1.91),
261 (up
.unpack_string
, (), lambda x
: x
== 'hello world'),
262 (up
.unpack_list
, (up
.unpack_uint
,), lambda x
: x
== range(5)),
263 (up
.unpack_array
, (up
.unpack_string
,),
264 lambda x
: x
== ['what', 'is', 'hapnin', 'doctor']),
267 for method
, args
, pred
in unpacktest
:
268 print 'unpack test', count
,
270 if succeedlist
[count
]:
271 x
= apply(method
, args
)
272 print pred(x
) and 'succeeded' or 'failed', ':', x
275 except ConversionError
, var
:
276 print 'ConversionError:', var
.msg
280 if __name__
== '__main__':