diff options
Diffstat (limited to 'Lib/xdrlib.py')
| -rw-r--r-- | Lib/xdrlib.py | 41 |
1 files changed, 19 insertions, 22 deletions
diff --git a/Lib/xdrlib.py b/Lib/xdrlib.py index ef172dd..2963915 100644 --- a/Lib/xdrlib.py +++ b/Lib/xdrlib.py @@ -5,10 +5,7 @@ See: RFC 1014 """ import struct -try: - from cStringIO import StringIO as _StringIO -except ImportError: - from StringIO import StringIO as _StringIO +from io import BytesIO __all__ = ["Error", "Packer", "Unpacker", "ConversionError"] @@ -16,7 +13,7 @@ __all__ = ["Error", "Packer", "Unpacker", "ConversionError"] class Error(Exception): """Exception class for this module. Use: - except xdrlib.Error, var: + except xdrlib.Error as var: # var has the Error instance for the exception Public ivars: @@ -43,7 +40,7 @@ class Packer: self.reset() def reset(self): - self.__buf = _StringIO() + self.__buf = BytesIO() def get_buffer(self): return self.__buf.getvalue() @@ -59,31 +56,31 @@ class Packer: pack_enum = pack_int def pack_bool(self, x): - if x: self.__buf.write('\0\0\0\1') - else: self.__buf.write('\0\0\0\0') + if x: self.__buf.write(b'\0\0\0\1') + else: self.__buf.write(b'\0\0\0\0') def pack_uhyper(self, x): - self.pack_uint(x>>32 & 0xffffffffL) - self.pack_uint(x & 0xffffffffL) + self.pack_uint(x>>32 & 0xffffffff) + self.pack_uint(x & 0xffffffff) pack_hyper = pack_uhyper def pack_float(self, x): try: self.__buf.write(struct.pack('>f', x)) - except struct.error, msg: - raise ConversionError, msg + except struct.error as msg: + raise ConversionError(msg) def pack_double(self, x): try: self.__buf.write(struct.pack('>d', x)) - except struct.error, msg: - raise ConversionError, msg + except struct.error as msg: + raise ConversionError(msg) def pack_fstring(self, n, s): if n < 0: - raise ValueError, 'fstring size must be nonnegative' + raise ValueError('fstring size must be nonnegative') data = s[:n] n = ((n+3)//4)*4 - data = data + (n - len(data)) * '\0' + data = data + (n - len(data)) * b'\0' self.__buf.write(data) pack_fopaque = pack_fstring @@ -104,7 +101,7 @@ class Packer: def pack_farray(self, n, list, pack_item): if len(list) != n: - raise ValueError, 'wrong array size' + raise ValueError('wrong array size') for item in list: pack_item(item) @@ -166,12 +163,12 @@ class Unpacker: def unpack_uhyper(self): hi = self.unpack_uint() lo = self.unpack_uint() - return long(hi)<<32 | lo + return int(hi)<<32 | lo def unpack_hyper(self): x = self.unpack_uhyper() - if x >= 0x8000000000000000L: - x = x - 0x10000000000000000L + if x >= 0x8000000000000000: + x = x - 0x10000000000000000 return x def unpack_float(self): @@ -192,7 +189,7 @@ class Unpacker: def unpack_fstring(self, n): if n < 0: - raise ValueError, 'fstring size must be nonnegative' + raise ValueError('fstring size must be nonnegative') i = self.__pos j = i + (n+3)//4*4 if j > len(self.__buf): @@ -215,7 +212,7 @@ class Unpacker: x = self.unpack_uint() if x == 0: break if x != 1: - raise ConversionError, '0 or 1 expected, got %r' % (x,) + raise ConversionError('0 or 1 expected, got %r' % (x,)) item = unpack_item() list.append(item) return list |
