[Zodb-checkins] CVS: Zope3/src/ZODB - Connection.py:1.144 utils.py:1.21

Tim Peters tim.one at comcast.net
Tue Apr 6 17:47:43 EDT 2004


Update of /cvs-repository/Zope3/src/ZODB
In directory cvs.zope.org:/tmp/cvs-serv14114/src/ZODB

Modified Files:
	Connection.py utils.py 
Log Message:
ZODB.utils grows a new function positive_id(), which returns the id() of
an object as a non-negative integer.  Code trying to pass addresses to
an %x format uses positive_id(), and this avoids a Python FutureWarning
about applying %x to negative ints.  The primary difference between this
and the last stab is that positive_id() should work OK on 64-bit boxes
too.  What we really want here is C's %p format code, but in Python we
can't even reliably know the width of native addresses.


=== Zope3/src/ZODB/Connection.py 1.143 => 1.144 ===
--- Zope3/src/ZODB/Connection.py:1.143	Tue Apr  6 16:21:55 2004
+++ Zope3/src/ZODB/Connection.py	Tue Apr  6 17:47:12 2004
@@ -33,7 +33,7 @@
 from ZODB.POSException \
      import ConflictError, ReadConflictError, InvalidObjectReference
 from ZODB.TmpStore import TmpStore
-from ZODB.utils import oid_repr, z64
+from ZODB.utils import oid_repr, z64, positive_id
 from ZODB.serialize import ObjectWriter, ConnectionObjectReader, myhasattr
 
 global_reset_counter = 0
@@ -223,9 +223,7 @@
             ver = ' (in version %s)' % `self._version`
         else:
             ver = ''
-        # Force the address to look positive.  A negative address will
-        # show up as signed in Python 2.4, and in 2.3 raises FutureWarning.
-        return '<Connection at %08x%s>' % (id(self) & 0xffffffffL, ver)
+        return '<Connection at %08x%s>' % (positive_id(self), ver)
 
     def get(self, oid):
         """Return the persistent object with oid 'oid'.


=== Zope3/src/ZODB/utils.py 1.20 => 1.21 ===
--- Zope3/src/ZODB/utils.py:1.20	Mon Dec 29 16:24:16 2003
+++ Zope3/src/ZODB/utils.py	Tue Apr  6 17:47:12 2004
@@ -69,3 +69,26 @@
         return repr(oid)
 
 serial_repr = oid_repr
+
+# Addresses can "look negative" on some boxes, some of the time.  If you
+# feed a "negative address" to an %x format, Python 2.3 displays it as
+# unsigned, but produces a FutureWarning, because Python 2.4 will display
+# it as signed.  So when you want to prodce an address, use positive_id() to
+# obtain it.
+def positive_id(obj):
+    """Return id(obj) as a non-negative integer."""
+
+    result = id(obj)
+    if result < 0:
+        # This is a puzzle:  there's no way to know the natural width of
+        # addresses on this box (in particular, there's no necessary
+        # relation to sys.maxint).  Try 32 bits first (and on a 32-bit
+        # box, adding 2**32 gives a positive number with the same hex
+        # representation as the original result).
+        result += 1L << 32
+        if result < 0:
+            # Undo that, and try 64 bits.
+            result -= 1L << 32
+            result += 1L << 64
+            assert result >= 0 # else addresses are fatter than 64 bits
+    return result




More information about the Zodb-checkins mailing list