"""Implements (a subset of) Sun XDR -- eXternal Data Representation.
from cStringIO import StringIO as _StringIO
from StringIO import StringIO as _StringIO
from functools import wraps
__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):
def raise_conversion_error(function):
""" Wrap any raised struct.errors in a ConversionError. """
return function(self, value)
except struct.error as e:
raise ConversionError(e.args[0])
"""Pack various data representations into a buffer."""
return self.__buf.getvalue()
# backwards compatibility
self.__buf.write(struct.pack('>L', x))
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)
except (TypeError, struct.error) as e:
raise ConversionError(e.args[0])
self.pack_uint(x & 0xffffffffL)
except (TypeError, struct.error) as e:
raise ConversionError(e.args[0])
self.__buf.write(struct.pack('>f', x))
def pack_double(self, x):
self.__buf.write(struct.pack('>d', x))
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]
return bool(self.unpack_int())
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)