Commit ad2e08df authored by Jeremy Hylton's avatar Jeremy Hylton

Add simpler implementations of p64() and u64() for Python 2.2.

parent eabb0131
...@@ -83,35 +83,53 @@ ...@@ -83,35 +83,53 @@
# #
############################################################################## ##############################################################################
import sys
import TimeStamp, time, struct import TimeStamp, time, struct
t32 = 1L << 32 if sys.version >= (2, 2):
def p64(v, pack=struct.pack): # Note that the distinction between ints and longs is blurred in
"""Pack an integer or long into a 8-byte string""" # Python 2.2. So make u64() and U64() the same.
if v < t32:
h = 0
else:
h, v = divmod(v, t32)
return pack(">II", h, v)
def u64(v, unpack=struct.unpack): def p64(v, pack=struct.pack):
"""Unpack an 8-byte string into a 64-bit (or long) integer""" """Pack an integer or long into a 8-byte string"""
h, v = unpack(">ii", v) return pack(">Q", v)
if v < 0:
v = t32 + v
if h:
if h < 0:
h= t32 + h
v = (long(h) << 32) + v
return v
def U64(v, unpack=struct.unpack): def u64(v, unpack=struct.unpack):
"""Same as u64 but always returns a long.""" """Unpack an 8-byte string into a 64-bit long integer."""
h, v = unpack(">II", v) return unpack(">Q", v)[0]
if h:
v = (long(h) << 32) + v U64 = u64
return v
else:
t32 = 1L << 32
def p64(v, pack=struct.pack):
"""Pack an integer or long into a 8-byte string"""
if v < t32:
h = 0
else:
h, v = divmod(v, t32)
return pack(">II", h, v)
def u64(v, unpack=struct.unpack):
"""Unpack an 8-byte string into a 64-bit (or long) integer."""
h, v = unpack(">ii", v)
if v < 0:
v = t32 + v
if h:
if h < 0:
h= t32 + h
v = (long(h) << 32) + v
return v
def U64(v, unpack=struct.unpack):
"""Same as u64 but always returns a long."""
h, v = unpack(">II", v)
if h:
v = (long(h) << 32) + v
return v
def cp(f1, f2, l): def cp(f1, f2, l):
read = f1.read read = f1.read
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment