Author: Carl Friedrich Bolz <[email protected]>
Branch: list-strategies
Changeset: r47678:29e932a272a4
Date: 2011-09-29 14:28 +0200
http://bitbucket.org/pypy/pypy/changeset/29e932a272a4/
Log: implement is and id for complex, fix -0.0 behaviour
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -452,18 +452,33 @@
return self.newbool(self.is_w(w_one, w_two))
def is_w(self, w_one, w_two):
+ from pypy.rlib.rstruct import ieee
+ w_typeone = self.type(w_one)
# cannot use self.is_w here to not get infinite recursion
- w_typeone = self.type(w_one)
if w_typeone is self.w_int:
return (self.type(w_two) is self.w_int and
self.int_w(w_one) == self.int_w(w_two))
elif w_typeone is self.w_float:
- return (self.type(w_two) is self.w_float and
- self.float_w(w_one) == self.float_w(w_two))
+ if self.type(w_two) is not self.w_float:
+ return False
+ one = ieee.float_pack(self.float_w(w_one), 8)
+ two = ieee.float_pack(self.float_w(w_two), 8)
+ return one == two
elif w_typeone is self.w_long:
return (self.type(w_two) is self.w_long and
self.bigint_w(w_one).eq(self.bigint_w(w_two)))
- # XXX complex?
+ elif w_typeone is self.w_complex:
+ if self.type(w_two) is not self.w_complex:
+ return False
+ real1 = self.float_w(self.getattr(w_one, self.wrap("real")))
+ real2 = self.float_w(self.getattr(w_two, self.wrap("real")))
+ imag1 = self.float_w(self.getattr(w_one, self.wrap("imag")))
+ imag2 = self.float_w(self.getattr(w_two, self.wrap("imag")))
+ real1 = ieee.float_pack(real1, 8)
+ real2 = ieee.float_pack(real2, 8)
+ imag1 = ieee.float_pack(imag1, 8)
+ imag2 = ieee.float_pack(imag2, 8)
+ return real1 == real2 and imag1 == imag2
elif w_typeone is self.w_str:
return (self.type(w_two) is self.w_str and
self.str_w(w_one) is self.str_w(w_two))
@@ -473,7 +488,8 @@
return w_one is w_two
def id(self, w_obj):
- from pypy.rlib import objectmodel, rbigint
+ from pypy.rlib.rbigint import rbigint
+ from pypy.rlib import objectmodel
from pypy.rlib.rstruct import ieee
w_type = self.type(w_obj)
if w_type is self.w_int:
@@ -485,9 +501,16 @@
elif w_type is self.w_float:
tag = 5
val = ieee.float_pack(self.float_w(w_obj), 8)
- w_obj =
self.newlong_from_rbigint(rbigint.rbigint.fromrarith_int(val))
+ w_obj = self.newlong_from_rbigint(rbigint.fromrarith_int(val))
return self.or_(self.lshift(w_obj, self.wrap(3)), self.wrap(tag))
- # XXX complex?
+ elif w_type is self.w_complex:
+ real = self.float_w(self.getattr(w_obj, self.wrap("real")))
+ imag = self.float_w(self.getattr(w_obj, self.wrap("imag")))
+ tag = 5
+ real_b = rbigint.fromrarith_int(ieee.float_pack(real, 8))
+ imag_b = rbigint.fromrarith_int(ieee.float_pack(imag, 8))
+ val = real_b.lshift(8 *
8).or_(imag_b).lshift(3).or_(rbigint.fromint(3))
+ return self.newlong_from_rbigint(val)
elif w_type is self.w_str:
res = objectmodel.compute_unique_id(self.str_w(w_obj))
elif w_type is self.w_unicode:
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
@@ -120,18 +120,22 @@
assert 1 is not 1l
assert 1l is not 1.0
assert 1.1 is 1.1
+ assert 0.0 is not -0.0
for x in range(10):
assert x + 0.1 is x + 0.1
for x in range(10):
assert x + 1L is x + 1L
- #for x in range(10):
- # assert x+1j is x+1j
+ for x in range(10):
+ assert x+1j is x+1j
+ assert 1+x*1j is 1+x*1j
l = [1]
assert l[0] is l[0]
l = ["a"]
assert l[0] is l[0]
u = u"a"
assert self.unwrap_wrap_unicode(u) is u
+ s = "a"
+ assert self.unwrap_wrap_str(s) is s
def test_id(self):
assert id(1) == (1 << 3) + 1
@@ -149,3 +153,38 @@
assert id(self.unwrap_wrap_unicode(u)) == id(u)
s = "a"
assert id(self.unwrap_wrap_str(s)) == id(s)
+
+ def test_identity_vs_id(self):
+ import sys
+ l = range(-10, 10)
+ for i in range(10):
+ l.append(float(i))
+ l.append(i + 0.1)
+ l.append(long(i))
+ l.append(i + sys.maxint)
+ l.append(i - sys.maxint)
+ l.append(i + 1j)
+ l.append(1 + i * 1j)
+ s = str(i)
+ l.append(s)
+ l.append(self.unwrap_wrap_str(s))
+ u = unicode(s)
+ l.append(u)
+ l.append(self.unwrap_wrap_unicode(u))
+ l.append(-0.0)
+ l.append(None)
+ l.append(True)
+ l.append(False)
+ s = "s"
+ l.append(s)
+ l.append(self.unwrap_wrap_str(s))
+ s = u"s"
+ l.append(s)
+ l.append(self.unwrap_wrap_unicode(s))
+
+ for a in l:
+ for b in l:
+ assert (a is b) == (id(a) == id(b))
+ if a is b:
+ assert a == b
+
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit