[pypy-svn] r68456 - in pypy/branch/gc-hash/pypy: interpreter objspace objspace/std
arigo at codespeak.net
arigo at codespeak.net
Wed Oct 14 18:37:29 CEST 2009
Author: arigo
Date: Wed Oct 14 18:37:29 2009
New Revision: 68456
Modified:
pypy/branch/gc-hash/pypy/interpreter/typedef.py
pypy/branch/gc-hash/pypy/objspace/descroperation.py
pypy/branch/gc-hash/pypy/objspace/std/complextype.py
pypy/branch/gc-hash/pypy/objspace/std/floattype.py
pypy/branch/gc-hash/pypy/objspace/std/frozensettype.py
pypy/branch/gc-hash/pypy/objspace/std/inttype.py
pypy/branch/gc-hash/pypy/objspace/std/longtype.py
pypy/branch/gc-hash/pypy/objspace/std/objecttype.py
pypy/branch/gc-hash/pypy/objspace/std/stringtype.py
pypy/branch/gc-hash/pypy/objspace/std/tupletype.py
pypy/branch/gc-hash/pypy/objspace/std/unicodetype.py
Log:
Kill code in PyPy! Yay. More precisely, remove the careful code that
builds default hash functions, and just use the new compute_identity_hash().
It can be called on random objects without giving a size penalty in the
normal case.
Modified: pypy/branch/gc-hash/pypy/interpreter/typedef.py
==============================================================================
--- pypy/branch/gc-hash/pypy/interpreter/typedef.py (original)
+++ pypy/branch/gc-hash/pypy/interpreter/typedef.py Wed Oct 14 18:37:29 2009
@@ -9,7 +9,7 @@
DescrMismatch
from pypy.interpreter.error import OperationError
from pypy.tool.sourcetools import compile2, func_with_new_name
-from pypy.rlib.objectmodel import instantiate
+from pypy.rlib.objectmodel import instantiate, compute_identity_hash
from pypy.rlib.rarithmetic import intmask
from pypy.rlib.jit import hint
@@ -20,12 +20,9 @@
self.base = __base
self.hasdict = '__dict__' in rawdict
self.weakrefable = '__weakref__' in rawdict
- self.custom_hash = '__hash__' in rawdict
if __base is not None:
self.hasdict |= __base.hasdict
self.weakrefable |= __base.weakrefable
- self.custom_hash |= __base.custom_hash
- # NB. custom_hash is sometimes overridden manually by callers
self.rawdict = {}
self.acceptable_as_base_class = True
# xxx used by faking
@@ -50,39 +47,9 @@
# ____________________________________________________________
# Hash support
-def get_default_hash_function(cls):
- # go to the first parent class of 'cls' that has a typedef
- while 'typedef' not in cls.__dict__:
- cls = cls.__bases__[0]
- if cls is object:
- # not found: 'cls' must have been an abstract class,
- # no hash function is needed
- return None
- if cls.typedef.custom_hash:
- return None # the typedef says that instances have their own
- # hash, so we don't need a default RPython-level
- # hash function.
- try:
- hashfunction = _hashfunction_cache[cls]
- except KeyError:
- def hashfunction(w_obj):
- "Return the identity hash of 'w_obj'."
- assert isinstance(w_obj, cls)
- return hash(w_obj) # forces a hash_cache only on 'cls' instances
- hashfunction = func_with_new_name(hashfunction,
- 'hashfunction_for_%s' % (cls.__name__,))
- _hashfunction_cache[cls] = hashfunction
- return hashfunction
-get_default_hash_function._annspecialcase_ = 'specialize:memo'
-_hashfunction_cache = {}
-
def default_identity_hash(space, w_obj):
- fn = get_default_hash_function(w_obj.__class__)
- if fn is None:
- typename = space.type(w_obj).getname(space, '?')
- msg = "%s objects have no default hash" % (typename,)
- raise OperationError(space.w_TypeError, space.wrap(msg))
- return space.wrap(intmask(fn(w_obj)))
+ hash = compute_identity_hash(w_obj)
+ return space.wrap(intmask(hash))
def descr__hash__unhashable(space, w_obj):
typename = space.type(w_obj).getname(space, '?')
Modified: pypy/branch/gc-hash/pypy/objspace/descroperation.py
==============================================================================
--- pypy/branch/gc-hash/pypy/objspace/descroperation.py (original)
+++ pypy/branch/gc-hash/pypy/objspace/descroperation.py Wed Oct 14 18:37:29 2009
@@ -320,10 +320,10 @@
def hash(space, w_obj):
w_hash = space.lookup(w_obj, '__hash__')
if w_hash is None:
- if space.lookup(w_obj, '__eq__') is not None or \
- space.lookup(w_obj, '__cmp__') is not None:
- raise OperationError(space.w_TypeError,
- space.wrap("unhashable type"))
+ # xxx there used to be logic about "do we have __eq__ or __cmp__"
+ # here, but it does not really make sense, as 'object' has a
+ # default __hash__. This path should only be taken under very
+ # obscure circumstances.
return default_identity_hash(space, w_obj)
# XXX CPython has a special case for types with "__hash__ = None"
# to produce a nicer error message, namely "unhashable type: 'X'".
Modified: pypy/branch/gc-hash/pypy/objspace/std/complextype.py
==============================================================================
--- pypy/branch/gc-hash/pypy/objspace/std/complextype.py (original)
+++ pypy/branch/gc-hash/pypy/objspace/std/complextype.py Wed Oct 14 18:37:29 2009
@@ -222,5 +222,4 @@
imag = complexwprop('imagval'),
)
-complex_typedef.custom_hash = True
complex_typedef.registermethods(globals())
Modified: pypy/branch/gc-hash/pypy/objspace/std/floattype.py
==============================================================================
--- pypy/branch/gc-hash/pypy/objspace/std/floattype.py (original)
+++ pypy/branch/gc-hash/pypy/objspace/std/floattype.py Wed Oct 14 18:37:29 2009
@@ -50,4 +50,3 @@
Convert a string or number to a floating point number, if possible.''',
__new__ = newmethod(descr__new__),
)
-float_typedef.custom_hash = True
Modified: pypy/branch/gc-hash/pypy/objspace/std/frozensettype.py
==============================================================================
--- pypy/branch/gc-hash/pypy/objspace/std/frozensettype.py (original)
+++ pypy/branch/gc-hash/pypy/objspace/std/frozensettype.py Wed Oct 14 18:37:29 2009
@@ -55,5 +55,4 @@
__new__ = newmethod(descr__frozenset__new__),
)
-frozenset_typedef.custom_hash = True
frozenset_typedef.registermethods(globals())
Modified: pypy/branch/gc-hash/pypy/objspace/std/inttype.py
==============================================================================
--- pypy/branch/gc-hash/pypy/objspace/std/inttype.py (original)
+++ pypy/branch/gc-hash/pypy/objspace/std/inttype.py Wed Oct 14 18:37:29 2009
@@ -145,4 +145,3 @@
will be returned instead.''',
__new__ = newmethod(descr__new__),
)
-int_typedef.custom_hash = True
Modified: pypy/branch/gc-hash/pypy/objspace/std/longtype.py
==============================================================================
--- pypy/branch/gc-hash/pypy/objspace/std/longtype.py (original)
+++ pypy/branch/gc-hash/pypy/objspace/std/longtype.py Wed Oct 14 18:37:29 2009
@@ -77,4 +77,3 @@
converting a non-string.''',
__new__ = newmethod(descr__new__),
)
-long_typedef.custom_hash = True
Modified: pypy/branch/gc-hash/pypy/objspace/std/objecttype.py
==============================================================================
--- pypy/branch/gc-hash/pypy/objspace/std/objecttype.py (original)
+++ pypy/branch/gc-hash/pypy/objspace/std/objecttype.py Wed Oct 14 18:37:29 2009
@@ -182,5 +182,3 @@
__init__ = gateway.interp2app(descr__init__,
unwrap_spec=[gateway.ObjSpace,gateway.W_Root,gateway.Arguments]),
)
-
-object_typedef.custom_hash = False # object.__hash__ is not a custom hash
Modified: pypy/branch/gc-hash/pypy/objspace/std/stringtype.py
==============================================================================
--- pypy/branch/gc-hash/pypy/objspace/std/stringtype.py (original)
+++ pypy/branch/gc-hash/pypy/objspace/std/stringtype.py Wed Oct 14 18:37:29 2009
@@ -301,7 +301,6 @@
If the argument is a string, the return value is the same object.'''
)
-str_typedef.custom_hash = True
str_typedef.registermethods(globals())
# ____________________________________________________________
Modified: pypy/branch/gc-hash/pypy/objspace/std/tupletype.py
==============================================================================
--- pypy/branch/gc-hash/pypy/objspace/std/tupletype.py (original)
+++ pypy/branch/gc-hash/pypy/objspace/std/tupletype.py Wed Oct 14 18:37:29 2009
@@ -27,4 +27,3 @@
If the argument is a tuple, the return value is the same object.''',
__new__ = newmethod(descr__new__),
)
-tuple_typedef.custom_hash = True
Modified: pypy/branch/gc-hash/pypy/objspace/std/unicodetype.py
==============================================================================
--- pypy/branch/gc-hash/pypy/objspace/std/unicodetype.py (original)
+++ pypy/branch/gc-hash/pypy/objspace/std/unicodetype.py Wed Oct 14 18:37:29 2009
@@ -298,7 +298,6 @@
errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'.'''
)
-unicode_typedef.custom_hash = True
unicode_typedef.registermethods(globals())
unitypedef = unicode_typedef
More information about the Pypy-commit
mailing list