1 """Implements (a subset of) Sun XDR -- eXternal Data Representation.
9 from cStringIO import StringIO as _StringIO
11 from StringIO import StringIO as _StringIO
13 __all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
16 class Error(Exception):
17 """Exception class for this module. Use:
19 except xdrlib.Error, var:
20 # var has the Error instance for the exception
23 msg -- contains the message
26 def __init__(self, msg):
34 class ConversionError(Error):
40 """Pack various data representations into a buffer."""
46 self.__buf = _StringIO()
49 return self.__buf.getvalue()
50 # backwards compatibility
53 def pack_uint(self, x):
54 self.__buf.write(struct.pack('>L', x))
56 def pack_int(self, x):
57 self.__buf.write(struct.pack('>l', x))
61 def pack_bool(self, x):
62 if x: self.__buf.write('\0\0\0\1')
63 else: self.__buf.write('\0\0\0\0')
65 def pack_uhyper(self, x):
66 self.pack_uint(x>>32 & 0xffffffffL)
67 self.pack_uint(x & 0xffffffffL)
69 pack_hyper = pack_uhyper
71 def pack_float(self, x):
72 try: self.__buf.write(struct.pack('>f', x))
73 except struct.error, msg:
74 raise ConversionError, msg
76 def pack_double(self, x):
77 try: self.__buf.write(struct.pack('>d', x))
78 except struct.error, msg:
79 raise ConversionError, msg
81 def pack_fstring(self, n, s):
83 raise ValueError, 'fstring size must be nonnegative'
86 data = data + (n - len(data)) * '\0'
87 self.__buf.write(data)
89 pack_fopaque = pack_fstring
91 def pack_string(self, s):
94 self.pack_fstring(n, s)
96 pack_opaque = pack_string
97 pack_bytes = pack_string
99 def pack_list(self, list, pack_item):
105 def pack_farray(self, n, list, pack_item):
107 raise ValueError, 'wrong array size'
111 def pack_array(self, list, pack_item):
114 self.pack_farray(n, list, pack_item)
119 """Unpacks various data representations from the given buffer."""
121 def __init__(self, data):
124 def reset(self, data):
128 def get_position(self):
131 def set_position(self, position):
132 self.__pos = position
134 def get_buffer(self):
138 if self.__pos < len(self.__buf):
139 raise Error('unextracted data remains')
141 def unpack_uint(self):
144 data = self.__buf[i:j]
147 x = struct.unpack('>L', data)[0]
150 except OverflowError:
153 def unpack_int(self):
156 data = self.__buf[i:j]
159 return struct.unpack('>l', data)[0]
161 unpack_enum = unpack_int
163 def unpack_bool(self):
164 return bool(self.unpack_int())
166 def unpack_uhyper(self):
167 hi = self.unpack_uint()
168 lo = self.unpack_uint()
169 return long(hi)<<32 | lo
171 def unpack_hyper(self):
172 x = self.unpack_uhyper()
173 if x >= 0x8000000000000000L:
174 x = x - 0x10000000000000000L
177 def unpack_float(self):
180 data = self.__buf[i:j]
183 return struct.unpack('>f', data)[0]
185 def unpack_double(self):
188 data = self.__buf[i:j]
191 return struct.unpack('>d', data)[0]
193 def unpack_fstring(self, n):
195 raise ValueError, 'fstring size must be nonnegative'
198 if j > len(self.__buf):
201 return self.__buf[i:i+n]
203 unpack_fopaque = unpack_fstring
205 def unpack_string(self):
206 n = self.unpack_uint()
207 return self.unpack_fstring(n)
209 unpack_opaque = unpack_string
210 unpack_bytes = unpack_string
212 def unpack_list(self, unpack_item):
215 x = self.unpack_uint()
218 raise ConversionError, '0 or 1 expected, got %r' % (x,)
223 def unpack_farray(self, n, unpack_item):
226 list.append(unpack_item())
229 def unpack_array(self, unpack_item):
230 n = self.unpack_uint()
231 return self.unpack_farray(n, unpack_item)