Initialized merge tracking via "svnmerge" with revisions "1-73579" from
[python/dscho.git] / Lib / xdrlib.py
blobb293e06a1084db3bf98ff58bea50347c6c8ec84b
1 """Implements (a subset of) Sun XDR -- eXternal Data Representation.
3 See: RFC 1014
5 """
7 import struct
8 from io import BytesIO
10 __all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
12 # exceptions
13 class Error(Exception):
14 """Exception class for this module. Use:
16 except xdrlib.Error, var:
17 # var has the Error instance for the exception
19 Public ivars:
20 msg -- contains the message
22 """
23 def __init__(self, msg):
24 self.msg = msg
25 def __repr__(self):
26 return repr(self.msg)
27 def __str__(self):
28 return str(self.msg)
31 class ConversionError(Error):
32 pass
36 class Packer:
37 """Pack various data representations into a buffer."""
39 def __init__(self):
40 self.reset()
42 def reset(self):
43 self.__buf = BytesIO()
45 def get_buffer(self):
46 return self.__buf.getvalue()
47 # backwards compatibility
48 get_buf = get_buffer
50 def pack_uint(self, x):
51 self.__buf.write(struct.pack('>L', x))
53 pack_int = pack_uint
54 pack_enum = pack_int
56 def pack_bool(self, x):
57 if x: self.__buf.write(b'\0\0\0\1')
58 else: self.__buf.write(b'\0\0\0\0')
60 def pack_uhyper(self, x):
61 self.pack_uint(x>>32 & 0xffffffff)
62 self.pack_uint(x & 0xffffffff)
64 pack_hyper = pack_uhyper
66 def pack_float(self, x):
67 try: self.__buf.write(struct.pack('>f', x))
68 except struct.error as msg:
69 raise ConversionError(msg)
71 def pack_double(self, x):
72 try: self.__buf.write(struct.pack('>d', x))
73 except struct.error as msg:
74 raise ConversionError(msg)
76 def pack_fstring(self, n, s):
77 if n < 0:
78 raise ValueError('fstring size must be nonnegative')
79 data = s[:n]
80 n = ((n+3)//4)*4
81 data = data + (n - len(data)) * b'\0'
82 self.__buf.write(data)
84 pack_fopaque = pack_fstring
86 def pack_string(self, s):
87 n = len(s)
88 self.pack_uint(n)
89 self.pack_fstring(n, s)
91 pack_opaque = pack_string
92 pack_bytes = pack_string
94 def pack_list(self, list, pack_item):
95 for item in list:
96 self.pack_uint(1)
97 pack_item(item)
98 self.pack_uint(0)
100 def pack_farray(self, n, list, pack_item):
101 if len(list) != n:
102 raise ValueError('wrong array size')
103 for item in list:
104 pack_item(item)
106 def pack_array(self, list, pack_item):
107 n = len(list)
108 self.pack_uint(n)
109 self.pack_farray(n, list, pack_item)
113 class Unpacker:
114 """Unpacks various data representations from the given buffer."""
116 def __init__(self, data):
117 self.reset(data)
119 def reset(self, data):
120 self.__buf = data
121 self.__pos = 0
123 def get_position(self):
124 return self.__pos
126 def set_position(self, position):
127 self.__pos = position
129 def get_buffer(self):
130 return self.__buf
132 def done(self):
133 if self.__pos < len(self.__buf):
134 raise Error('unextracted data remains')
136 def unpack_uint(self):
137 i = self.__pos
138 self.__pos = j = i+4
139 data = self.__buf[i:j]
140 if len(data) < 4:
141 raise EOFError
142 x = struct.unpack('>L', data)[0]
143 try:
144 return int(x)
145 except OverflowError:
146 return x
148 def unpack_int(self):
149 i = self.__pos
150 self.__pos = j = i+4
151 data = self.__buf[i:j]
152 if len(data) < 4:
153 raise EOFError
154 return struct.unpack('>l', data)[0]
156 unpack_enum = unpack_int
158 def unpack_bool(self):
159 return bool(self.unpack_int())
161 def unpack_uhyper(self):
162 hi = self.unpack_uint()
163 lo = self.unpack_uint()
164 return int(hi)<<32 | lo
166 def unpack_hyper(self):
167 x = self.unpack_uhyper()
168 if x >= 0x8000000000000000:
169 x = x - 0x10000000000000000
170 return x
172 def unpack_float(self):
173 i = self.__pos
174 self.__pos = j = i+4
175 data = self.__buf[i:j]
176 if len(data) < 4:
177 raise EOFError
178 return struct.unpack('>f', data)[0]
180 def unpack_double(self):
181 i = self.__pos
182 self.__pos = j = i+8
183 data = self.__buf[i:j]
184 if len(data) < 8:
185 raise EOFError
186 return struct.unpack('>d', data)[0]
188 def unpack_fstring(self, n):
189 if n < 0:
190 raise ValueError('fstring size must be nonnegative')
191 i = self.__pos
192 j = i + (n+3)//4*4
193 if j > len(self.__buf):
194 raise EOFError
195 self.__pos = j
196 return self.__buf[i:i+n]
198 unpack_fopaque = unpack_fstring
200 def unpack_string(self):
201 n = self.unpack_uint()
202 return self.unpack_fstring(n)
204 unpack_opaque = unpack_string
205 unpack_bytes = unpack_string
207 def unpack_list(self, unpack_item):
208 list = []
209 while 1:
210 x = self.unpack_uint()
211 if x == 0: break
212 if x != 1:
213 raise ConversionError('0 or 1 expected, got %r' % (x,))
214 item = unpack_item()
215 list.append(item)
216 return list
218 def unpack_farray(self, n, unpack_item):
219 list = []
220 for i in range(n):
221 list.append(unpack_item())
222 return list
224 def unpack_array(self, unpack_item):
225 n = self.unpack_uint()
226 return self.unpack_farray(n, unpack_item)