Support packing longs.

This commit is contained in:
Guido van Rossum 1992-12-15 21:43:59 +00:00
parent ad5f862125
commit 38625352bb
1 changed files with 10 additions and 4 deletions

View File

@ -4,6 +4,9 @@
import struct import struct
Long = type(0L)
class Packer: class Packer:
def init(self): def init(self):
@ -20,9 +23,12 @@ class Packer:
self.buf = self.buf + \ self.buf = self.buf + \
(chr(int(x>>24 & 0xff)) + chr(int(x>>16 & 0xff)) + \ (chr(int(x>>24 & 0xff)) + chr(int(x>>16 & 0xff)) + \
chr(int(x>>8 & 0xff)) + chr(int(x & 0xff))) chr(int(x>>8 & 0xff)) + chr(int(x & 0xff)))
if struct.pack('i', 1) == '\0\0\0\1': if struct.pack('l', 1) == '\0\0\0\1':
def pack_uint(self, x): def pack_uint(self, x):
self.buf = self.buf + struct.pack('i', x) if type(x) == Long:
x = int((x + 0x80000000L) % 0x100000000L \
- 0x80000000L)
self.buf = self.buf + struct.pack('l', x)
pack_int = pack_uint pack_int = pack_uint
@ -33,8 +39,8 @@ class Packer:
else: self.buf = self.buf + '\0\0\0\0' else: self.buf = self.buf + '\0\0\0\0'
def pack_uhyper(self, x): def pack_uhyper(self, x):
self.pack_uint(x>>32 & 0xffffffff) self.pack_uint(int(x>>32 & 0xffffffff))
self.pack_uint(x & 0xffffffff) self.pack_uint(int(x & 0xffffffff))
pack_hyper = pack_uhyper pack_hyper = pack_uhyper