Author: mattip <[email protected]>
Branch:
Changeset: r82686:7ac45ccc8658
Date: 2016-03-04 11:53 +0200
http://bitbucket.org/pypy/pypy/changeset/7ac45ccc8658/
Log: merge issue-2248, which fixes float.__int__()
diff --git a/pypy/module/__builtin__/test/test_classobj.py
b/pypy/module/__builtin__/test/test_classobj.py
--- a/pypy/module/__builtin__/test/test_classobj.py
+++ b/pypy/module/__builtin__/test/test_classobj.py
@@ -452,7 +452,6 @@
assert a + 1 == 2
assert a + 1.1 == 2
-
def test_binaryop_calls_coerce_always(self):
l = []
class A:
@@ -1076,6 +1075,16 @@
assert (D() > A()) == 'D:A.gt'
assert (D() >= A()) == 'D:A.ge'
+ def test_override___int__(self):
+ class F(float):
+ def __int__(self):
+ return 666
+ f = F(-12.3)
+ assert int(f) == 666
+ # on cpython, this calls float_trunc() in floatobject.c
+ # which ends up calling PyFloat_AS_DOUBLE((PyFloatObject*) f)
+ assert float.__int__(f) == -12
+
class AppTestOldStyleClassBytesDict(object):
def setup_class(cls):
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
@@ -160,15 +160,11 @@
return self.floatval
def int(self, space):
+ # this is a speed-up only, for space.int(w_float).
if (type(self) is not W_FloatObject and
space.is_overloaded(self, space.w_float, '__int__')):
return W_Root.int(self, space)
- try:
- value = ovfcheck_float_to_int(self.floatval)
- except OverflowError:
- return space.long(self)
- else:
- return space.newint(value)
+ return self.descr_trunc(space)
def is_w(self, space, w_other):
from rpython.rlib.longlong2float import float2longlong
@@ -424,9 +420,8 @@
"cannot convert float NaN to integer")
def descr_trunc(self, space):
- whole = math.modf(self.floatval)[1]
try:
- value = ovfcheck_float_to_int(whole)
+ value = ovfcheck_float_to_int(self.floatval)
except OverflowError:
return self.descr_long(space)
else:
@@ -661,7 +656,7 @@
__format__ = interp2app(W_FloatObject.descr_format),
__coerce__ = interp2app(W_FloatObject.descr_coerce),
__nonzero__ = interp2app(W_FloatObject.descr_nonzero),
- __int__ = interp2app(W_FloatObject.int),
+ __int__ = interp2app(W_FloatObject.descr_trunc),
__float__ = interp2app(W_FloatObject.descr_float),
__long__ = interp2app(W_FloatObject.descr_long),
__trunc__ = interp2app(W_FloatObject.descr_trunc),
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit