Issue #24731: Fixed crash on converting objects with special methods
__str__, __trunc__, and __float__ returning instances of subclasses of
str, long, and float to subclasses of str, long, and float correspondingly.
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index ffa4774..b65d24c 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -79,6 +79,12 @@
(unichr(0x200), ValueError),
]
+class LongSubclass(long):
+ pass
+
+class OtherLongSubclass(long):
+ pass
+
class LongTest(test_int.IntLongCommonTests, unittest.TestCase):
ntype = long
@@ -539,6 +545,17 @@
self.fail("Failed to raise TypeError with %s" %
((base, trunc_result_base),))
+ class TruncReturnsLongSubclass(base):
+ def __long__(self):
+ return OtherLongSubclass(42L)
+ good_int = TruncReturnsLongSubclass()
+ n = long(good_int)
+ self.assertEqual(n, 42L)
+ self.assertIs(type(n), OtherLongSubclass)
+ n = LongSubclass(good_int)
+ self.assertEqual(n, 42L)
+ self.assertIs(type(n), LongSubclass)
+
def test_misc(self):
# check the extremes in int<->long conversion