Issue #24731: Fixed crash on converting objects with special methods

__bytes__, __trunc__, and __float__ returning instances of subclasses of
bytes, int, and float to subclasses of bytes, int, and float correspondingly.
This commit is contained in:
Serhiy Storchaka 2015-11-25 15:47:01 +02:00
parent a49de6be36
commit 15095800a3
8 changed files with 50 additions and 10 deletions

View file

@ -25,6 +25,12 @@ requires_setformat = unittest.skipUnless(hasattr(float, "__setformat__"),
test_dir = os.path.dirname(__file__) or os.curdir
format_testfile = os.path.join(test_dir, 'formatfloat_testcases.txt')
class FloatSubclass(float):
pass
class OtherFloatSubclass(float):
pass
class GeneralFloatCases(unittest.TestCase):
def test_float(self):
@ -167,6 +173,15 @@ class GeneralFloatCases(unittest.TestCase):
return ""
self.assertRaises(TypeError, time.sleep, Foo5())
# Issue #24731
class F:
def __float__(self):
return OtherFloatSubclass(42.)
self.assertAlmostEqual(float(F()), 42.)
self.assertIs(type(float(F())), OtherFloatSubclass)
self.assertAlmostEqual(FloatSubclass(F()), 42.)
self.assertIs(type(FloatSubclass(F())), FloatSubclass)
def test_is_integer(self):
self.assertFalse((1.1).is_integer())
self.assertTrue((1.).is_integer())