"""Implements (a subset of) Sun XDR -- eXternal Data Representation.
from cStringIO
import StringIO
as _StringIO
from StringIO
import StringIO
as _StringIO
__all__
= ["Error", "Packer", "Unpacker", "ConversionError"]
"""Exception class for this module. Use:
except xdrlib.Error, var:
# var has the Error instance for the exception
msg -- contains the message
class ConversionError(Error
):
"""Pack various data representations into a buffer."""
return self
.__buf
.getvalue()
# backwards compatibility
self
.__buf
.write(struct
.pack('>L', x
))
if x
: self
.__buf
.write('\0\0\0\1')
else: self
.__buf
.write('\0\0\0\0')
def pack_uhyper(self
, x
):
self
.pack_uint(x
>>32 & 0xffffffffL
)
self
.pack_uint(x
& 0xffffffffL
)
try: self
.__buf
.write(struct
.pack('>f', x
))
except struct
.error
, msg
:
raise ConversionError
, msg
def pack_double(self
, x
):
try: self
.__buf
.write(struct
.pack('>d', x
))
except struct
.error
, msg
:
raise ConversionError
, msg
def pack_fstring(self
, n
, s
):
raise ValueError, 'fstring size must be nonnegative'
data
= data
+ (n
- len(data
)) * '\0'
pack_fopaque
= pack_fstring
def pack_string(self
, s
):
pack_opaque
= pack_string
def pack_list(self
, list, pack_item
):
def pack_farray(self
, n
, list, pack_item
):
raise ValueError, 'wrong array size'
def pack_array(self
, list, pack_item
):
self
.pack_farray(n
, list, pack_item
)
"""Unpacks various data representations from the given buffer."""
def __init__(self
, data
):
def set_position(self
, position
):
if self
.__pos
< len(self
.__buf
):
raise Error('unextracted data remains')
x
= struct
.unpack('>L', data
)[0]
return struct
.unpack('>l', data
)[0]
if x
>= 0x8000000000000000L
:
x
= x
- 0x10000000000000000L
return struct
.unpack('>f', data
)[0]
return struct
.unpack('>d', data
)[0]
def unpack_fstring(self
, n
):
raise ValueError, 'fstring size must be nonnegative'
unpack_fopaque
= unpack_fstring
return self
.unpack_fstring(n
)
unpack_opaque
= unpack_string
unpack_bytes
= unpack_string
def unpack_list(self
, unpack_item
):
raise ConversionError
, '0 or 1 expected, got %r' % (x
,)
def unpack_farray(self
, n
, unpack_item
):
list.append(unpack_item())
def unpack_array(self
, unpack_item
):
return self
.unpack_farray(n
, unpack_item
)
(p
.pack_bool
, ('hello',)),
(p
.pack_string
, ('hello world',)),
(p
.pack_list
, (range(5), p
.pack_uint
)),
(p
.pack_array
, (['what', 'is', 'hapnin', 'doctor'], p
.pack_string
)),
succeedlist
= [1] * len(packtest
)
for method
, args
in packtest
:
print 'pack test', count
,
except ConversionError
, var
:
print 'ConversionError:', var
.msg
(up
.unpack_uint
, (), lambda x
: x
== 9),
(up
.unpack_bool
, (), lambda x
: not x
),
(up
.unpack_bool
, (), lambda x
: x
),
(up
.unpack_uhyper
, (), lambda x
: x
== 45L),
(up
.unpack_float
, (), lambda x
: 1.89 < x
< 1.91),
(up
.unpack_double
, (), lambda x
: 1.89 < x
< 1.91),
(up
.unpack_string
, (), lambda x
: x
== 'hello world'),
(up
.unpack_list
, (up
.unpack_uint
,), lambda x
: x
== range(5)),
(up
.unpack_array
, (up
.unpack_string
,),
lambda x
: x
== ['what', 'is', 'hapnin', 'doctor']),
for method
, args
, pred
in unpacktest
:
print 'unpack test', count
,
print pred(x
) and 'succeeded' or 'failed', ':', x
except ConversionError
, var
:
print 'ConversionError:', var
.msg
if __name__
== '__main__':