diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-11-25 13:52:04 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-11-25 13:52:04 (GMT) |
commit | f9afda57ad7d0394531982b2c9de8301c5a54e45 (patch) | |
tree | 8a09bb1aa816b4daf6777e630333dfece309cdb5 /Lib | |
parent | c5f3b4285a8f3f90305d777a88a856a623c22cb1 (diff) | |
parent | 15095800a381a396cbc077cb5320203a8feae51a (diff) | |
download | cpython-f9afda57ad7d0394531982b2c9de8301c5a54e45.zip cpython-f9afda57ad7d0394531982b2c9de8301c5a54e45.tar.gz cpython-f9afda57ad7d0394531982b2c9de8301c5a54e45.tar.bz2 |
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.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_bytes.py | 11 | ||||
-rw-r--r-- | Lib/test/test_float.py | 15 | ||||
-rw-r--r-- | Lib/test/test_int.py | 7 | ||||
-rw-r--r-- | Lib/test/test_unicode.py | 11 |
4 files changed, 40 insertions, 4 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 53a80f4..8158f78 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -779,6 +779,14 @@ class BytesTest(BaseBytesTest, unittest.TestCase): def __index__(self): return 42 self.assertEqual(bytes(A()), b'a') + # Issue #24731 + class A: + def __bytes__(self): + return OtherBytesSubclass(b'abc') + self.assertEqual(bytes(A()), b'abc') + self.assertIs(type(bytes(A())), OtherBytesSubclass) + self.assertEqual(BytesSubclass(A()), b'abc') + self.assertIs(type(BytesSubclass(A())), BytesSubclass) # Test PyBytes_FromFormat() def test_from_format(self): @@ -1552,6 +1560,9 @@ class ByteArraySubclass(bytearray): class BytesSubclass(bytes): pass +class OtherBytesSubclass(bytes): + pass + class ByteArraySubclassTest(SubclassTest, unittest.TestCase): type2test = bytearray subclass2test = ByteArraySubclass diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 723beb3..48f7a70 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -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()) diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py index 3e4b4fc..b66c5d6 100644 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -24,6 +24,9 @@ L = [ ("\u0200", ValueError) ] +class IntSubclass(int): + pass + class IntTestCases(unittest.TestCase): def test_basic(self): @@ -441,6 +444,10 @@ class IntTestCases(unittest.TestCase): good_int = TruncReturnsIntSubclass() n = int(good_int) self.assertEqual(n, 1) + self.assertIs(type(n), bool) + n = IntSubclass(good_int) + self.assertEqual(n, 1) + self.assertIs(type(n), IntSubclass) def test_error_message(self): def check(s, base=None): diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 56a60df..ce8df40 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -43,6 +43,9 @@ def duplicate_string(text): """ return text.encode().decode() +class StrSubclass(str): + pass + class UnicodeTest(string_tests.CommonTest, string_tests.MixinStrUnicodeUserStringTest, string_tests.MixinStrUnicodeTest, @@ -1441,11 +1444,8 @@ class UnicodeTest(string_tests.CommonTest, 'unicode remains unicode' ) - class UnicodeSubclass(str): - pass - for text in ('ascii', '\xe9', '\u20ac', '\U0010FFFF'): - subclass = UnicodeSubclass(text) + subclass = StrSubclass(text) self.assertEqual(str(subclass), text) self.assertEqual(len(subclass), len(text)) if text == 'ascii': @@ -2199,6 +2199,9 @@ class UnicodeTest(string_tests.CommonTest, s = str(StrSubclassToStrSubclass("foo")) self.assertEqual(s, "foofoo") self.assertIs(type(s), StrSubclassToStrSubclass) + s = StrSubclass(StrSubclassToStrSubclass("foo")) + self.assertEqual(s, "foofoo") + self.assertIs(type(s), StrSubclass) def test_unicode_repr(self): class s1: |