Commits

Alexander Hesse  committed 24e99ed

Moved pypy.tool.identity_dict and pypy.tool.uid to rpython

  • Participants
  • Parent commits 81f81b2
  • Branches split-rpython

Comments (0)

Files changed (30)

File pypy/interpreter/baseobjspace.py

 from pypy.interpreter.argument import Arguments
 from pypy.interpreter.miscutils import ThreadLocals
 from pypy.tool.cache import Cache
-from pypy.tool.uid import HUGEVAL_BYTES
+from rpython.tool.uid import HUGEVAL_BYTES
 from rpython.rlib import jit
 from rpython.rlib.debug import make_sure_not_resized
 from rpython.rlib.objectmodel import we_are_translated, newlist_hint,\

File pypy/interpreter/nestedscope.py

 from pypy.interpreter.mixedmodule import MixedModule
 from pypy.interpreter.astcompiler import consts
 from rpython.rlib import jit
-from pypy.tool.uid import uid
+from rpython.tool.uid import uid
 
 class Cell(Wrappable):
     "A simple container for a wrapped value."

File pypy/module/cpyext/test/test_cpyext.py

 from pypy.module.cpyext.pyobject import RefcountState
 from pypy.module.cpyext.pyobject import Py_DecRef, InvalidPointerException
 from rpython.translator.goal import autopath
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.identity_dict import identity_dict
 from pypy.tool import leakfinder
 
 @api.cpython_api([], api.PyObject)

File pypy/tool/algo/graphlib.py

   'edges' is a dict mapping vertices to a list of edges with its source.
   Note that we can usually use 'edges' as the set of 'vertices' too.
 """
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.identity_dict import identity_dict
 
 
 class Edge:

File pypy/tool/identity_dict.py

-try:
-    from __pypy__ import identity_dict as idict
-except ImportError:
-    idict = None
-
-from UserDict import DictMixin
-
-
-class IdentityDictPurePython(object, DictMixin):
-    __slots__ = "_dict _keys".split()
-
-    def __init__(self):
-        self._dict = {}
-        self._keys = {} # id(obj) -> obj
-
-    def __getitem__(self, arg):
-        return self._dict[id(arg)]
-
-    def __setitem__(self, arg, val):
-        self._keys[id(arg)] = arg
-        self._dict[id(arg)] = val
-
-    def __delitem__(self, arg):
-        del self._keys[id(arg)]
-        del self._dict[id(arg)]
-
-    def keys(self):
-        return self._keys.values()
-
-    def __contains__(self, arg):
-        return id(arg) in self._dict
-
-    def copy(self):
-        d = type(self)()
-        d.update(self.iteritems())
-        assert len(d) == len(self)
-        return d
-
-
-class IdentityDictPyPy(object, DictMixin):
-    __slots__ = ["_dict"]
-
-    def __init__(self):
-        self._dict = idict()
-
-    def __getitem__(self, arg):
-        return self._dict[arg]
-
-    def __setitem__(self, arg, val):
-        self._dict[arg] = val
-
-    def __delitem__(self, arg):
-        del self._dict[arg]
-
-    def keys(self):
-        return self._dict.keys()
-
-    def __contains__(self, arg):
-        return arg in self._dict
-
-    def copy(self):
-        d = type(self)()
-        d.update(self.iteritems())
-        assert len(d) == len(self)
-        return d
-
-if idict is None:
-    identity_dict = IdentityDictPurePython
-else:
-    identity_dict = IdentityDictPyPy
-

File pypy/tool/test/test_identitydict.py

 import py
-from pypy.tool.identity_dict import identity_dict, IdentityDictPurePython
+from rpython.tool.identity_dict import identity_dict, IdentityDictPurePython
 
 class TestIdentityDictNative:
     identity_dict = identity_dict

File pypy/tool/uid.py

-import struct, sys
-
-# This is temporary hack to run PyPy on PyPy
-# until PyPy's struct module handle P format character.
-try:
-    HUGEVAL_FMT   = 'P'
-    HUGEVAL_BYTES = struct.calcsize('P')
-except struct.error:
-    if sys.maxint <= 2147483647:
-        HUGEVAL_FMT   = 'l'
-        HUGEVAL_BYTES = 4
-    else:
-        HUGEVAL_FMT   = 'q'
-        HUGEVAL_BYTES = 8
-
-HUGEVAL = 256 ** HUGEVAL_BYTES
-
-
-def fixid(result):
-    if result < 0:
-        result += HUGEVAL
-    return result
-
-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):
-    """
-    A Hashable instance encapsulates any object, but is always usable as a
-    key in dictionaries.  This is based on id() for mutable objects and on
-    real hash/compare for immutable ones.
-    """
-    __slots__ = ["key", "value"]
-    
-    def __init__(self, value):
-        self.value = value     # a concrete value
-        # try to be smart about constant mutable or immutable values
-        key = type(self.value), self.value  # to avoid confusing e.g. 0 and 0.0
-        #
-        # we also have to avoid confusing 0.0 and -0.0 (needed e.g. for
-        # translating the cmath module)
-        if key[0] is float and not self.value:
-            from rpython.rlib.rfloat import copysign
-            if copysign(1., self.value) == -1.:    # -0.0
-                key = (float, "-0.0")
-        #
-        try:
-            hash(key)
-        except TypeError:
-            key = id(self.value)
-        self.key = key
-
-    def __eq__(self, other):
-        return self.__class__ is other.__class__ and self.key == other.key
-
-    def __ne__(self, other):
-        return not (self == other)
-
-    def __hash__(self):
-        return hash(self.key)
-
-    def __repr__(self):
-        return '(%s)' % (self,)
-
-    def __str__(self):
-        # try to limit the size of the repr to make it more readable
-        r = repr(self.value)
-        if (r.startswith('<') and r.endswith('>') and
-            hasattr(self.value, '__name__')):
-            r = '%s %s' % (type(self.value).__name__, self.value.__name__)
-        elif len(r) > 60 or (len(r) > 30 and type(self.value) is not str):
-            r = r[:22] + '...' + r[-7:]
-        return r

File rpython/annotator/bookkeeper.py

 from rpython.rtyper.lltypesystem import lltype, llmemory
 from rpython.rtyper.ootypesystem import ootype
 from rpython.rtyper import extregistry
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.identity_dict import identity_dict
 
 class Stats(object):
 

File rpython/annotator/specialize.py

 # specialization support
 import py
-from pypy.tool.uid import uid
+from rpython.tool.uid import uid
 from rpython.tool.sourcetools import func_with_new_name
 from pypy.tool.algo.unionfind import UnionFind
 from rpython.flowspace.model import Block, Link, Variable, SpaceOperation

File rpython/flowspace/model.py

 # the below object/attribute model evolved from
 # a discussion in Berlin, 4th of october 2003
 import py
-from pypy.tool.uid import uid, Hashable
+from rpython.tool.uid import uid, Hashable
 from rpython.tool.sourcetools import PY_IDENTIFIER, nice_repr_for_func
 from rpython.rlib.rarithmetic import is_valid_int, r_longlong, r_ulonglong, r_uint
 

File rpython/rlib/unroll.py

-from pypy.tool.uid import uid
+from rpython.tool.uid import uid
 
 # Support for explicit specialization: in code using global constants
 # that are instances of SpecTag, code paths are not merged when

File rpython/rtyper/extregistry.py

 import weakref
 import UserDict
-from pypy.tool.uid import Hashable
+from rpython.tool.uid import Hashable
 
 
 class AutoRegisteringType(type):

File rpython/rtyper/lltypesystem/ll2ctypes.py

 from rpython.rtyper.lltypesystem import lltype, llmemory
 from rpython.rtyper.extfunc import ExtRegistryEntry
 from rpython.rlib.objectmodel import Symbolic, ComputedIntSymbolic
-from pypy.tool.uid import fixid
+from rpython.tool.uid import fixid
 from rpython.rlib.rarithmetic import r_singlefloat, r_longfloat, base_int, intmask
 from rpython.rlib.rarithmetic import is_emulated_long, maxint
 from rpython.annotator import model as annmodel

File rpython/rtyper/lltypesystem/llmemory.py

 import weakref
 from rpython.rlib.objectmodel import Symbolic
 from rpython.rtyper.lltypesystem import lltype
-from pypy.tool.uid import uid
+from rpython.tool.uid import uid
 from rpython.rlib.rarithmetic import is_valid_int
 
 

File rpython/rtyper/lltypesystem/lltype.py

                                    r_ulonglong, r_longlong, r_longfloat, r_longlonglong,
                                    base_int, normalizedinttype, longlongmask, longlonglongmask)
 from rpython.rlib.objectmodel import Symbolic
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.identity_dict import identity_dict
 from pypy.tool import leakfinder
 from types import NoneType
 from rpython.rlib.rarithmetic import maxint, is_valid_int, is_emulated_long

File rpython/rtyper/lltypesystem/rclass.py

 from rpython.annotator import model as annmodel
 from rpython.rlib.rarithmetic import intmask
 from rpython.rlib import objectmodel
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.identity_dict import identity_dict
 from rpython.rtyper.lltypesystem.lloperation import llop
 
 #

File rpython/rtyper/lltypesystem/test/test_lltype.py

 import py
 from rpython.rtyper.lltypesystem.lltype import *
 from rpython.rtyper.lltypesystem import lltype, rffi
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.identity_dict import identity_dict
 from pypy.tool import leakfinder
 
 def isweak(p, T):

File rpython/rtyper/memory/gctypelayout.py

 from rpython.rtyper.lltypesystem.lloperation import llop
 from rpython.rlib.debug import ll_assert
 from rpython.rlib.rarithmetic import intmask
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.identity_dict import identity_dict
 
 
 class GCData(object):

File rpython/rtyper/memory/support.py

 from rpython.rlib.objectmodel import free_non_gc_object, we_are_translated
 from rpython.rlib.rarithmetic import r_uint, LONG_BIT
 from rpython.rlib.debug import ll_assert
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.identity_dict import identity_dict
 
 
 def mangle_hash(i):

File rpython/rtyper/ootypesystem/ootype.py

 from rpython.rtyper.lltypesystem.lltype import identityhash
 from rpython.rlib.rarithmetic import intmask
 from rpython.rlib import objectmodel
-from pypy.tool.uid import uid
+from rpython.tool.uid import uid
 
 
 STATICNESS = True

File rpython/rtyper/ootypesystem/rclass.py

 from rpython.rtyper.exceptiondata import standardexceptions
 from rpython.tool.pairtype import pairtype
 from rpython.tool.sourcetools import func_with_new_name
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.identity_dict import identity_dict
 
 OBJECT = ootype.ROOT
 META = ootype.Instance("Meta", ootype.ROOT,

File rpython/tool/identity_dict.py

+try:
+    from __pypy__ import identity_dict as idict
+except ImportError:
+    idict = None
+
+from UserDict import DictMixin
+
+
+class IdentityDictPurePython(object, DictMixin):
+    __slots__ = "_dict _keys".split()
+
+    def __init__(self):
+        self._dict = {}
+        self._keys = {} # id(obj) -> obj
+
+    def __getitem__(self, arg):
+        return self._dict[id(arg)]
+
+    def __setitem__(self, arg, val):
+        self._keys[id(arg)] = arg
+        self._dict[id(arg)] = val
+
+    def __delitem__(self, arg):
+        del self._keys[id(arg)]
+        del self._dict[id(arg)]
+
+    def keys(self):
+        return self._keys.values()
+
+    def __contains__(self, arg):
+        return id(arg) in self._dict
+
+    def copy(self):
+        d = type(self)()
+        d.update(self.iteritems())
+        assert len(d) == len(self)
+        return d
+
+
+class IdentityDictPyPy(object, DictMixin):
+    __slots__ = ["_dict"]
+
+    def __init__(self):
+        self._dict = idict()
+
+    def __getitem__(self, arg):
+        return self._dict[arg]
+
+    def __setitem__(self, arg, val):
+        self._dict[arg] = val
+
+    def __delitem__(self, arg):
+        del self._dict[arg]
+
+    def keys(self):
+        return self._dict.keys()
+
+    def __contains__(self, arg):
+        return arg in self._dict
+
+    def copy(self):
+        d = type(self)()
+        d.update(self.iteritems())
+        assert len(d) == len(self)
+        return d
+
+if idict is None:
+    identity_dict = IdentityDictPurePython
+else:
+    identity_dict = IdentityDictPyPy
+

File rpython/tool/uid.py

+import struct, sys
+
+# This is temporary hack to run PyPy on PyPy
+# until PyPy's struct module handle P format character.
+try:
+    HUGEVAL_FMT   = 'P'
+    HUGEVAL_BYTES = struct.calcsize('P')
+except struct.error:
+    if sys.maxint <= 2147483647:
+        HUGEVAL_FMT   = 'l'
+        HUGEVAL_BYTES = 4
+    else:
+        HUGEVAL_FMT   = 'q'
+        HUGEVAL_BYTES = 8
+
+HUGEVAL = 256 ** HUGEVAL_BYTES
+
+
+def fixid(result):
+    if result < 0:
+        result += HUGEVAL
+    return result
+
+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):
+    """
+    A Hashable instance encapsulates any object, but is always usable as a
+    key in dictionaries.  This is based on id() for mutable objects and on
+    real hash/compare for immutable ones.
+    """
+    __slots__ = ["key", "value"]
+    
+    def __init__(self, value):
+        self.value = value     # a concrete value
+        # try to be smart about constant mutable or immutable values
+        key = type(self.value), self.value  # to avoid confusing e.g. 0 and 0.0
+        #
+        # we also have to avoid confusing 0.0 and -0.0 (needed e.g. for
+        # translating the cmath module)
+        if key[0] is float and not self.value:
+            from rpython.rlib.rfloat import copysign
+            if copysign(1., self.value) == -1.:    # -0.0
+                key = (float, "-0.0")
+        #
+        try:
+            hash(key)
+        except TypeError:
+            key = id(self.value)
+        self.key = key
+
+    def __eq__(self, other):
+        return self.__class__ is other.__class__ and self.key == other.key
+
+    def __ne__(self, other):
+        return not (self == other)
+
+    def __hash__(self):
+        return hash(self.key)
+
+    def __repr__(self):
+        return '(%s)' % (self,)
+
+    def __str__(self):
+        # try to limit the size of the repr to make it more readable
+        r = repr(self.value)
+        if (r.startswith('<') and r.endswith('>') and
+            hasattr(self.value, '__name__')):
+            r = '%s %s' % (type(self.value).__name__, self.value.__name__)
+        elif len(r) > 60 or (len(r) > 30 and type(self.value) is not str):
+            r = r[:22] + '...' + r[-7:]
+        return r

File rpython/translator/backendopt/escape.py

 from rpython.flowspace.model import Variable
 from rpython.rtyper.lltypesystem import lltype
 from rpython.translator.simplify import get_graph
-from pypy.tool.uid import uid
+from rpython.tool.uid import uid
 
 
 class CreationPoint(object):

File rpython/translator/c/database.py

 from rpython.translator.c.support import log, barebonearray
 from rpython.translator.c.extfunc import do_the_getting
 from rpython.translator.c import gc
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.identity_dict import identity_dict
 
 
 class NoCorrespondingNode(Exception):

File rpython/translator/c/funcgen.py

 from rpython.rtyper.lltypesystem.llmemory import Address
 from rpython.translator.backendopt.ssa import SSI_to_SSA
 from rpython.translator.backendopt.innerloop import find_inner_loops
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.identity_dict import identity_dict
 
 
 LOCALVAR = 'l_%s'

File rpython/translator/oosupport/function.py

 from rpython.rtyper.ootypesystem import ootype
 from rpython.translator.oosupport.treebuilder import SubOperation
 from rpython.translator.oosupport.metavm import InstructionList, StoreResult
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.identity_dict import identity_dict
 
 
 def render_sub_op(sub_op, db, generator):

File rpython/translator/tool/graphpage.py

 from rpython.annotator.model import SomePBC
 from rpython.annotator.description import MethodDesc
 from rpython.annotator.classdef import ClassDef
-from pypy.tool.uid import uid
+from rpython.tool.uid import uid
 from rpython.tool.udir import udir
 
 from dotviewer.graphpage import GraphPage as BaseGraphPage

File rpython/translator/tool/lltracker.py

 from rpython.rtyper.lltypesystem import lltype, llmemory
 from rpython.rtyper.memory.gcheader import header2obj
 from rpython.translator.tool.reftracker import BaseRefTrackerPage, MARKER
-from pypy.tool.uid import uid
-from pypy.tool.identity_dict import identity_dict
+from rpython.tool.uid import uid
+from rpython.tool.identity_dict import identity_dict
 
 
 class LLRefTrackerPage(BaseRefTrackerPage):

File rpython/translator/tool/reftracker.py

 import autopath, sys, os, types
 import gc
 from rpython.translator.tool.graphpage import GraphPage, DotGen
-from pypy.tool.uid import uid
+from rpython.tool.uid import uid
 
 
 MARKER = object()