Author: Armin Rigo <[email protected]>
Branch:
Changeset: r50515:46c074e2da29
Date: 2011-12-14 20:37 +0100
http://bitbucket.org/pypy/pypy/changeset/46c074e2da29/
Log: issue963 resolved
Similarly to not calling compute_unique_id() on the immutable
objects, we should avoid calling compute_identity_hash() on them.
Test and fix.
diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -190,8 +190,8 @@
def is_w(self, space, w_other):
return self is w_other
- def unique_id(self, space):
- return space.wrap(compute_unique_id(self))
+ def immutable_unique_id(self, space):
+ return None
def str_w(self, space):
w_msg = typed_unwrap_error_msg(space, "string", self)
@@ -706,7 +706,10 @@
return w_two.is_w(self, w_one)
def id(self, w_obj):
- return w_obj.unique_id(self)
+ w_result = w_obj.immutable_unique_id(self)
+ if w_result is None:
+ w_result = self.wrap(compute_unique_id(w_obj))
+ return w_result
def hash_w(self, w_obj):
"""shortcut for space.int_w(space.hash(w_obj))"""
diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -54,7 +54,11 @@
# Hash support
def default_identity_hash(space, w_obj):
- return space.wrap(compute_identity_hash(w_obj))
+ w_unique_id = w_obj.immutable_unique_id(space)
+ if w_unique_id is None: # common case
+ return space.wrap(compute_identity_hash(w_obj))
+ else:
+ return space.hash(w_unique_id)
# ____________________________________________________________
#
diff --git a/pypy/objspace/std/complexobject.py
b/pypy/objspace/std/complexobject.py
--- a/pypy/objspace/std/complexobject.py
+++ b/pypy/objspace/std/complexobject.py
@@ -31,9 +31,9 @@
imag2 = float2longlong(imag2)
return real1 == real2 and imag1 == imag2
- def unique_id(self, space):
+ def immutable_unique_id(self, space):
if self.user_overridden_class:
- return W_Object.unique_id(self, space)
+ return None
from pypy.rlib.longlong2float import float2longlong
from pypy.objspace.std.model import IDTAG_COMPLEX as tag
real = space.float_w(space.getattr(self, space.wrap("real")))
diff --git a/pypy/objspace/std/floatobject.py b/pypy/objspace/std/floatobject.py
--- a/pypy/objspace/std/floatobject.py
+++ b/pypy/objspace/std/floatobject.py
@@ -34,9 +34,9 @@
two = float2longlong(space.float_w(w_other))
return one == two
- def unique_id(self, space):
+ def immutable_unique_id(self, space):
if self.user_overridden_class:
- return W_Object.unique_id(self, space)
+ return None
from pypy.rlib.longlong2float import float2longlong
from pypy.objspace.std.model import IDTAG_FLOAT as tag
val = float2longlong(space.float_w(self))
diff --git a/pypy/objspace/std/intobject.py b/pypy/objspace/std/intobject.py
--- a/pypy/objspace/std/intobject.py
+++ b/pypy/objspace/std/intobject.py
@@ -26,9 +26,9 @@
return self is w_other
return space.int_w(self) == space.int_w(w_other)
- def unique_id(self, space):
+ def immutable_unique_id(self, space):
if self.user_overridden_class:
- return W_Object.unique_id(self, space)
+ return None
from pypy.objspace.std.model import IDTAG_INT as tag
b = space.bigint_w(self)
b = b.lshift(3).or_(rbigint.fromint(tag))
diff --git a/pypy/objspace/std/longobject.py b/pypy/objspace/std/longobject.py
--- a/pypy/objspace/std/longobject.py
+++ b/pypy/objspace/std/longobject.py
@@ -18,9 +18,9 @@
return self is w_other
return space.bigint_w(self).eq(space.bigint_w(w_other))
- def unique_id(self, space):
+ def immutable_unique_id(self, space):
if self.user_overridden_class:
- return W_Object.unique_id(self, space)
+ return None
from pypy.objspace.std.model import IDTAG_LONG as tag
b = space.bigint_w(self)
b = b.lshift(3).or_(rbigint.fromint(tag))
diff --git a/pypy/objspace/std/stringobject.py
b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -32,9 +32,9 @@
return False
return space.str_w(self) is space.str_w(w_other)
- def unique_id(self, space):
+ def immutable_unique_id(self, space):
if self.user_overridden_class:
- return W_Object.unique_id(self, space)
+ return None
return space.wrap(compute_unique_id(space.str_w(self)))
diff --git a/pypy/objspace/std/test/test_obj.py
b/pypy/objspace/std/test/test_obj.py
--- a/pypy/objspace/std/test/test_obj.py
+++ b/pypy/objspace/std/test/test_obj.py
@@ -253,6 +253,12 @@
y = 2j
assert id(x) != id(y)
+ def test_object_hash_immutable(self):
+ x = 42
+ y = 40
+ y += 2
+ assert object.__hash__(x) == object.__hash__(y)
+
def test_isinstance_shortcut():
from pypy.objspace.std import objspace
diff --git a/pypy/objspace/std/unicodeobject.py
b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -32,9 +32,9 @@
return False
return space.unicode_w(self) is space.unicode_w(w_other)
- def unique_id(self, space):
+ def immutable_unique_id(self, space):
if self.user_overridden_class:
- return W_Object.unique_id(self, space)
+ return None
return space.wrap(compute_unique_id(space.unicode_w(self)))
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit