[Zope-Checkins] CVS: StandaloneZODB/ZODB - utils.py:1.8

Jeremy Hylton jeremy@zope.com
Fri, 5 Oct 2001 17:21:15 -0400


Update of /cvs-repository/StandaloneZODB/ZODB
In directory cvs.zope.org:/tmp/cvs-serv11905

Modified Files:
	utils.py 
Log Message:
Add simpler implementations of p64() and u64() for Python 2.2.


=== StandaloneZODB/ZODB/utils.py 1.7 => 1.8 ===
 ##############################################################################
 
+import sys
 import TimeStamp, time, struct
 
-t32 = 1L << 32
+if sys.version >= (2, 2):
 
-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
+    # Note that the distinction between ints and longs is blurred in
+    # Python 2.2.  So make u64() and U64() the same.
+
+    def p64(v, pack=struct.pack):
+        """Pack an integer or long into a 8-byte string"""
+        return pack(">Q", v)
+
+    def u64(v, unpack=struct.unpack):
+        """Unpack an 8-byte string into a 64-bit long integer."""
+        return unpack(">Q", v)[0]
+
+    U64 = u64
+
+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):
     read = f1.read