[pypy-svn] r27248 - in pypy/dist/pypy: interpreter interpreter/pyparser lib objspace objspace/flow objspace/std objspace/std/test rpython/lltypesystem rpython/ootypesystem/test rpython/test tool tool/algo translator/backendopt translator/c translator/c/test

arigo at codespeak.net arigo at codespeak.net
Mon May 15 19:33:13 CEST 2006


Author: arigo
Date: Mon May 15 19:33:06 2006
New Revision: 27248

Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/interpreter/nestedscope.py
   pypy/dist/pypy/interpreter/pyparser/syntaxtree.py
   pypy/dist/pypy/lib/_classobj.py
   pypy/dist/pypy/objspace/flow/framestate.py
   pypy/dist/pypy/objspace/logic.py
   pypy/dist/pypy/objspace/std/default.py
   pypy/dist/pypy/objspace/std/test/test_obj.py
   pypy/dist/pypy/rpython/lltypesystem/rclass.py
   pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py
   pypy/dist/pypy/rpython/test/test_rclass.py
   pypy/dist/pypy/tool/algo/unionref.py
   pypy/dist/pypy/tool/uid.py
   pypy/dist/pypy/translator/backendopt/escape.py
   pypy/dist/pypy/translator/c/exceptiontransform.py
   pypy/dist/pypy/translator/c/test/test_typed.py
Log:
Trying to generally fix id() issues.


Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Mon May 15 19:33:06 2006
@@ -3,8 +3,8 @@
 from pypy.interpreter.argument import Arguments, ArgumentsFromValuestack
 from pypy.interpreter.pycompiler import CPythonCompiler, PythonAstCompiler
 from pypy.interpreter.miscutils import ThreadLocals
-from pypy.tool.cache import Cache 
-from pypy.rpython.rarithmetic import r_uint
+from pypy.tool.cache import Cache
+from pypy.tool.uid import HUGEVAL_BYTES
 import os
 
 __all__ = ['ObjSpace', 'OperationError', 'Wrappable', 'W_Root']
@@ -48,9 +48,23 @@
             raise
 
     def getrepr(self, space, info):
-        id = space.int_w(space.id(self)) # xxx ids could be long
-        id = r_uint(id) # XXX what about sizeof(void*) > sizeof(long) !!
-        return space.wrap("<%s at 0x%x>" % (info, id))
+        # XXX slowish
+        w_id = space.id(self)
+        w_4 = space.wrap(4)
+        w_0x0F = space.wrap(0x0F)
+        i = 2 * HUGEVAL_BYTES
+        addrstring = [' '] * i
+        while True:
+            n = space.int_w(space.and_(w_id, w_0x0F))
+            n += ord('0')
+            if n > ord('9'):
+                n += (ord('a') - ord('9') - 1)
+            i -= 1
+            addrstring[i] = chr(n)
+            if i == 0:
+                break
+            w_id = space.rshift(w_id, w_4)
+        return space.wrap("<%s at 0x%s>" % (info, ''.join(addrstring)))
 
     def getslotvalue(self, index):
         raise NotImplementedError

Modified: pypy/dist/pypy/interpreter/nestedscope.py
==============================================================================
--- pypy/dist/pypy/interpreter/nestedscope.py	(original)
+++ pypy/dist/pypy/interpreter/nestedscope.py	Mon May 15 19:33:06 2006
@@ -3,6 +3,7 @@
 from pypy.interpreter import function, pycode, pyframe
 from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter.mixedmodule import MixedModule
+from pypy.tool.uid import uid
 
 class Cell(Wrappable):
     "A simple container for a wrapped value."
@@ -55,7 +56,7 @@
         else:
             content = repr(self.w_value)
         return "<%s(%s) at 0x%x>" % (self.__class__.__name__,
-                                     content, id(self))
+                                     content, uid(self))
 
 
 class PyNestedScopeFrame(PyInterpFrame):

Modified: pypy/dist/pypy/interpreter/pyparser/syntaxtree.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/syntaxtree.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/syntaxtree.py	Mon May 15 19:33:06 2006
@@ -5,7 +5,9 @@
 # except ImportError:
 # #    from pysymbol import sym_values
 #     from pytoken import tok_values
-    
+
+from pypy.tool.uid import uid
+
 class AbstractSyntaxVisitor(object):
     def visit_syntaxnode( self, node ):
         pass
@@ -45,7 +47,7 @@
         return "".join(treenodes)
 
     def __repr__(self):
-        return "<node [%s] at 0x%x>" % (self.name, id(self))
+        return "<node [%s] at 0x%x>" % (self.name, uid(self))
 
     def __str__(self):
         return "(%s)" % self.name

Modified: pypy/dist/pypy/lib/_classobj.py
==============================================================================
--- pypy/dist/pypy/lib/_classobj.py	(original)
+++ pypy/dist/pypy/lib/_classobj.py	Mon May 15 19:33:06 2006
@@ -336,7 +336,7 @@
                 raise TypeError("__hash__() should return an int")
             return ret
         else:
-            return id(self)
+            return object.__hash__(self)
 
     def __len__(self):
         ret = instance_getattr1(self,'__len__')()

Modified: pypy/dist/pypy/objspace/flow/framestate.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/framestate.py	(original)
+++ pypy/dist/pypy/objspace/flow/framestate.py	Mon May 15 19:33:06 2006
@@ -2,6 +2,7 @@
 from pypy.interpreter.error import OperationError
 from pypy.rpython.objectmodel import instantiate
 from pypy.objspace.flow.model import *
+from pypy.tool.uid import uid
 
 class FrameState:
     # XXX this class depends on the internal state of PyFrame objects
@@ -140,7 +141,7 @@
 
 class SpecTag(object):
     def __repr__(self):
-        return 'SpecTag(%d)' % id(self)
+        return 'SpecTag(0x%x)' % uid(self)
     def _freeze_(self):
         return True
 

Modified: pypy/dist/pypy/objspace/logic.py
==============================================================================
--- pypy/dist/pypy/objspace/logic.py	(original)
+++ pypy/dist/pypy/objspace/logic.py	Mon May 15 19:33:06 2006
@@ -2,6 +2,7 @@
 from pypy.interpreter import gateway, baseobjspace, argument
 from pypy.interpreter.error import OperationError
 from pypy.rpython.objectmodel import we_are_translated
+from pypy.tool.uid import uid
 
 # wrapped types, mm stuff
 from pypy.objspace.std.listobject import W_ListObject, W_TupleObject
@@ -150,7 +151,7 @@
             return not (self == other)
 
         def __repr__(self):
-            return '<greenlet %s>' % id(self)
+            return '<greenlet 0x%x>' % uid(self)
 
     def construct_coroutine():
         if we_are_translated():

Modified: pypy/dist/pypy/objspace/std/default.py
==============================================================================
--- pypy/dist/pypy/objspace/std/default.py	(original)
+++ pypy/dist/pypy/objspace/std/default.py	Mon May 15 19:33:06 2006
@@ -8,8 +8,7 @@
 
 def id__ANY(space, w_obj):
     #print 'id:', w_obj
-    from pypy.objspace.std.inttype import wrapint
-    return wrapint(id(w_obj))
+    return space.wrap(id(w_obj))
 
 # __init__ should succeed if called internally as a multimethod
 

Modified: pypy/dist/pypy/objspace/std/test/test_obj.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_obj.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_obj.py	Mon May 15 19:33:06 2006
@@ -2,8 +2,9 @@
 
 class AppTestObject: 
     def test_hash_builtin(self):
+        import sys
         o = object()
-        assert hash(o) == id(o) 
+        assert (hash(o) & sys.maxint) == (id(o) & sys.maxint)
 
     def test_hash_method(self):
         o = object()
@@ -18,8 +19,9 @@
         assert not hasattr(o, '__getnewargs__')
 
     def test_hash_subclass(self):
+        import sys
         class X(object):
             pass
         x = X()
-        assert hash(x) == id(x)
+        assert (hash(x) & sys.maxint) == (id(x) & sys.maxint)
         assert hash(x) == object.__hash__(x)

Modified: pypy/dist/pypy/rpython/lltypesystem/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rclass.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rclass.py	Mon May 15 19:33:06 2006
@@ -20,6 +20,7 @@
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.annotation import model as annmodel
 from pypy.rpython.objectmodel import UnboxedValue
+from pypy.rpython.rarithmetic import intmask
 
 #
 #  There is one "vtable" per user class, with the following structure:
@@ -807,7 +808,7 @@
         return 0    # for None
     cached = ins.hash_cache
     if cached == 0:
-       cached = ins.hash_cache = id(ins)
+       cached = ins.hash_cache = intmask(id(ins))
     return cached
 
 def ll_inst_type(obj):

Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py	Mon May 15 19:33:06 2006
@@ -2,6 +2,7 @@
 from pypy.rpython.ootypesystem import ootype
 from pypy.rpython.test.test_llinterp import get_interpreter
 import py
+import sys
 
 
 def check_only_ootype(graph):
@@ -446,7 +447,8 @@
         #     one line to the next
         current_identityhash = id(d2)
         instance_hash = hash(d2)
-        return current_identityhash == instance_hash
+        return ((current_identityhash & sys.maxint) ==
+                (instance_hash & sys.maxint))
     res = interpret(f1, [])
     assert res is True
 

Modified: pypy/dist/pypy/rpython/test/test_rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rclass.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rclass.py	Mon May 15 19:33:06 2006
@@ -1,4 +1,5 @@
 import py
+import sys
 from pypy.translator.translator import TranslationContext, graphof
 from pypy.rpython.lltypesystem.lltype import *
 from pypy.rpython.ootypesystem import ootype
@@ -356,7 +357,8 @@
         d = D()
         def f():
             d2 = D()
-            x = hash(d2) == id(d2) # xxx check for this CPython peculiarity for now
+            # xxx check for this CPython peculiarity for now:
+            x = (hash(d2) & sys.maxint) == (id(d2) & sys.maxint)
             return x, hash(c)+hash(d)
 
         res = interpret(f, [], type_system=self.ts)

Modified: pypy/dist/pypy/tool/algo/unionref.py
==============================================================================
--- pypy/dist/pypy/tool/algo/unionref.py	(original)
+++ pypy/dist/pypy/tool/algo/unionref.py	Mon May 15 19:33:06 2006
@@ -18,6 +18,7 @@
 """
 
 import UserDict
+from pypy.tool.uid import uid
 
 
 class UnionRef(object):
@@ -112,7 +113,7 @@
         return result
 
     def __repr__(self):
-        return "<UnionDict at %s>" % id(self)
+        return "<UnionDict at 0x%x>" % uid(self)
 
     def __getitem__(self, key):
         return self._data[key]()

Modified: pypy/dist/pypy/tool/uid.py
==============================================================================
--- pypy/dist/pypy/tool/uid.py	(original)
+++ pypy/dist/pypy/tool/uid.py	Mon May 15 19:33:06 2006
@@ -1,23 +1,32 @@
-import struct
+import struct, sys
 
 # This is temporary hack to run PyPy on PyPy
 # until PyPy's struct module handle P format character.
 try:
-    HUGEVAL = 256 ** struct.calcsize('P')
+    HUGEVAL_BYTES = struct.calcsize('P')
 except struct.error:
-    HUGEVAL = 0
+    if sys.maxint <= 2147483647:
+        HUGEVAL_BYTES = 4
+    else:
+        HUGEVAL_BYTES = 8
+
+HUGEVAL = 256 ** HUGEVAL_BYTES
+
 
 def fixid(result):
     if result < 0:
         result += HUGEVAL
     return result
 
-def uid(obj):
-    """
-    Return the id of an object as an unsigned number so that its hex
-    representation makes sense
-    """
-    return fixid(id(obj))
+if sys.version_info < (2, 5):
+    def uid(obj):
+        """
+        Return the id of an object as an unsigned number so that its hex
+        representation makes sense
+        """
+        return fixid(id(obj))
+else:
+    uid = id    # guaranteed to be positive from CPython 2.5 onwards
 
 
 class Hashable(object):

Modified: pypy/dist/pypy/translator/backendopt/escape.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/escape.py	(original)
+++ pypy/dist/pypy/translator/backendopt/escape.py	Mon May 15 19:33:06 2006
@@ -4,6 +4,7 @@
 from pypy.translator.simplify import get_graph
 from pypy.rpython.rmodel import inputconst
 from pypy.translator.backendopt import support
+from pypy.tool.uid import uid
 
 class CreationPoint(object):
     def __init__(self, creation_method="?"):
@@ -16,8 +17,8 @@
             self.malloced = False
 
     def __repr__(self):
-        return ("CreationPoint(<%s>, %s, esc=%s, cha=%s)" %
-                (id(self), self.creation_method, self.escapes, self.changes))
+        return ("CreationPoint(<0x%x>, %s, esc=%s, cha=%s)" %
+                (uid(self), self.creation_method, self.escapes, self.changes))
 
 class VarState(object):
     def __init__(self, crep=None):

Modified: pypy/dist/pypy/translator/c/exceptiontransform.py
==============================================================================
--- pypy/dist/pypy/translator/c/exceptiontransform.py	(original)
+++ pypy/dist/pypy/translator/c/exceptiontransform.py	Mon May 15 19:33:06 2006
@@ -137,8 +137,8 @@
         from the current graph with a special value (False/-1/-1.0/null).
         Because of the added exitswitch we need an additional block.
         """
-        assert id(graph) not in self.seen_graphs
-        self.seen_graphs[id(graph)] = True
+        assert graph not in self.seen_graphs
+        self.seen_graphs[graph] = True
         join_blocks(graph)
         # collect the blocks before changing them
         for block in list(graph.iterblocks()):

Modified: pypy/dist/pypy/translator/c/test/test_typed.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_typed.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_typed.py	Mon May 15 19:33:06 2006
@@ -594,7 +594,8 @@
         d = D()
         def fn():
             d2 = D()
-            x = hash(d2) == id(d2) # xxx check for this CPython peculiarity for now
+            # xxx check for this CPython peculiarity for now:
+            x = (hash(d2) & sys.maxint) == (id(d2) & sys.maxint)
             return x, hash(c)+hash(d)
         
         f = self.getcompiled(fn)



More information about the Pypy-commit mailing list