diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-02-25 15:57:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-25 15:57:58 (GMT) |
commit | 6a44f6eef3d0958d88882347190b3e2d1222c2e9 (patch) | |
tree | bb6474b1e094a672837c3333a081fa4d5d7638f2 /Lib/test | |
parent | d90a141bb947b68601f8d1f37bc98f7b524f0e01 (diff) | |
download | cpython-6a44f6eef3d0958d88882347190b3e2d1222c2e9.zip cpython-6a44f6eef3d0958d88882347190b3e2d1222c2e9.tar.gz cpython-6a44f6eef3d0958d88882347190b3e2d1222c2e9.tar.bz2 |
bpo-36048: Use __index__() instead of __int__() for implicit conversion if available. (GH-11952)
Deprecate using the __int__() method in implicit conversions of Python
numbers to C integers.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/datetimetester.py | 7 | ||||
-rw-r--r-- | Lib/test/test_array.py | 2 | ||||
-rw-r--r-- | Lib/test/test_getargs2.py | 109 | ||||
-rw-r--r-- | Lib/test/test_int.py | 3 | ||||
-rw-r--r-- | Lib/test/test_zlib.py | 2 |
5 files changed, 110 insertions, 13 deletions
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 958b336..715f0ea 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -4918,8 +4918,9 @@ class Oddballs(unittest.TestCase): for xx in [decimal.Decimal(10), decimal.Decimal('10.9'), Number(10)]: - self.assertEqual(datetime(10, 10, 10, 10, 10, 10, 10), - datetime(xx, xx, xx, xx, xx, xx, xx)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(datetime(10, 10, 10, 10, 10, 10, 10), + datetime(xx, xx, xx, xx, xx, xx, xx)) with self.assertRaisesRegex(TypeError, '^an integer is required ' r'\(got type str\)$'): @@ -4927,7 +4928,7 @@ class Oddballs(unittest.TestCase): f10 = Number(10.9) with self.assertRaisesRegex(TypeError, '^__int__ returned non-int ' - r'\(type float\)$'): + r'\(type float\)$'): datetime(10, 10, f10) class Float(float): diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 7f402f8..2399980 100644 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -1242,6 +1242,8 @@ class IntegerNumberTest(NumberTest): class Intable: def __init__(self, num): self._num = num + def __index__(self): + return self._num def __int__(self): return self._num def __sub__(self, other): diff --git a/Lib/test/test_getargs2.py b/Lib/test/test_getargs2.py index d9bcb1d..07e2d15 100644 --- a/Lib/test/test_getargs2.py +++ b/Lib/test/test_getargs2.py @@ -53,6 +53,27 @@ LLONG_MAX = 2**63-1 LLONG_MIN = -2**63 ULLONG_MAX = 2**64-1 +class Index: + def __index__(self): + return 99 + +class IndexIntSubclass(int): + def __index__(self): + return 99 + +class BadIndex: + def __index__(self): + return 1.0 + +class BadIndex2: + def __index__(self): + return True + +class BadIndex3(int): + def __index__(self): + return True + + class Int: def __int__(self): return 99 @@ -134,7 +155,14 @@ class Unsigned_TestCase(unittest.TestCase): from _testcapi import getargs_b # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX) self.assertRaises(TypeError, getargs_b, 3.14) - self.assertEqual(99, getargs_b(Int())) + self.assertEqual(99, getargs_b(Index())) + self.assertEqual(0, getargs_b(IndexIntSubclass())) + self.assertRaises(TypeError, getargs_b, BadIndex()) + with self.assertWarns(DeprecationWarning): + self.assertEqual(1, getargs_b(BadIndex2())) + self.assertEqual(0, getargs_b(BadIndex3())) + with self.assertWarns(DeprecationWarning): + self.assertEqual(99, getargs_b(Int())) self.assertEqual(0, getargs_b(IntSubclass())) self.assertRaises(TypeError, getargs_b, BadInt()) with self.assertWarns(DeprecationWarning): @@ -153,7 +181,14 @@ class Unsigned_TestCase(unittest.TestCase): from _testcapi import getargs_B # B returns 'unsigned char', no range checking self.assertRaises(TypeError, getargs_B, 3.14) - self.assertEqual(99, getargs_B(Int())) + self.assertEqual(99, getargs_B(Index())) + self.assertEqual(0, getargs_B(IndexIntSubclass())) + self.assertRaises(TypeError, getargs_B, BadIndex()) + with self.assertWarns(DeprecationWarning): + self.assertEqual(1, getargs_B(BadIndex2())) + self.assertEqual(0, getargs_B(BadIndex3())) + with self.assertWarns(DeprecationWarning): + self.assertEqual(99, getargs_B(Int())) self.assertEqual(0, getargs_B(IntSubclass())) self.assertRaises(TypeError, getargs_B, BadInt()) with self.assertWarns(DeprecationWarning): @@ -172,7 +207,14 @@ class Unsigned_TestCase(unittest.TestCase): from _testcapi import getargs_H # H returns 'unsigned short', no range checking self.assertRaises(TypeError, getargs_H, 3.14) - self.assertEqual(99, getargs_H(Int())) + self.assertEqual(99, getargs_H(Index())) + self.assertEqual(0, getargs_H(IndexIntSubclass())) + self.assertRaises(TypeError, getargs_H, BadIndex()) + with self.assertWarns(DeprecationWarning): + self.assertEqual(1, getargs_H(BadIndex2())) + self.assertEqual(0, getargs_H(BadIndex3())) + with self.assertWarns(DeprecationWarning): + self.assertEqual(99, getargs_H(Int())) self.assertEqual(0, getargs_H(IntSubclass())) self.assertRaises(TypeError, getargs_H, BadInt()) with self.assertWarns(DeprecationWarning): @@ -192,7 +234,14 @@ class Unsigned_TestCase(unittest.TestCase): from _testcapi import getargs_I # I returns 'unsigned int', no range checking self.assertRaises(TypeError, getargs_I, 3.14) - self.assertEqual(99, getargs_I(Int())) + self.assertEqual(99, getargs_I(Index())) + self.assertEqual(0, getargs_I(IndexIntSubclass())) + self.assertRaises(TypeError, getargs_I, BadIndex()) + with self.assertWarns(DeprecationWarning): + self.assertEqual(1, getargs_I(BadIndex2())) + self.assertEqual(0, getargs_I(BadIndex3())) + with self.assertWarns(DeprecationWarning): + self.assertEqual(99, getargs_I(Int())) self.assertEqual(0, getargs_I(IntSubclass())) self.assertRaises(TypeError, getargs_I, BadInt()) with self.assertWarns(DeprecationWarning): @@ -213,6 +262,11 @@ class Unsigned_TestCase(unittest.TestCase): # k returns 'unsigned long', no range checking # it does not accept float, or instances with __int__ self.assertRaises(TypeError, getargs_k, 3.14) + self.assertRaises(TypeError, getargs_k, Index()) + self.assertEqual(0, getargs_k(IndexIntSubclass())) + self.assertRaises(TypeError, getargs_k, BadIndex()) + self.assertRaises(TypeError, getargs_k, BadIndex2()) + self.assertEqual(0, getargs_k(BadIndex3())) self.assertRaises(TypeError, getargs_k, Int()) self.assertEqual(0, getargs_k(IntSubclass())) self.assertRaises(TypeError, getargs_k, BadInt()) @@ -233,7 +287,14 @@ class Signed_TestCase(unittest.TestCase): from _testcapi import getargs_h # h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX) self.assertRaises(TypeError, getargs_h, 3.14) - self.assertEqual(99, getargs_h(Int())) + self.assertEqual(99, getargs_h(Index())) + self.assertEqual(0, getargs_h(IndexIntSubclass())) + self.assertRaises(TypeError, getargs_h, BadIndex()) + with self.assertWarns(DeprecationWarning): + self.assertEqual(1, getargs_h(BadIndex2())) + self.assertEqual(0, getargs_h(BadIndex3())) + with self.assertWarns(DeprecationWarning): + self.assertEqual(99, getargs_h(Int())) self.assertEqual(0, getargs_h(IntSubclass())) self.assertRaises(TypeError, getargs_h, BadInt()) with self.assertWarns(DeprecationWarning): @@ -252,7 +313,14 @@ class Signed_TestCase(unittest.TestCase): from _testcapi import getargs_i # i returns 'int', and does range checking (INT_MIN ... INT_MAX) self.assertRaises(TypeError, getargs_i, 3.14) - self.assertEqual(99, getargs_i(Int())) + self.assertEqual(99, getargs_i(Index())) + self.assertEqual(0, getargs_i(IndexIntSubclass())) + self.assertRaises(TypeError, getargs_i, BadIndex()) + with self.assertWarns(DeprecationWarning): + self.assertEqual(1, getargs_i(BadIndex2())) + self.assertEqual(0, getargs_i(BadIndex3())) + with self.assertWarns(DeprecationWarning): + self.assertEqual(99, getargs_i(Int())) self.assertEqual(0, getargs_i(IntSubclass())) self.assertRaises(TypeError, getargs_i, BadInt()) with self.assertWarns(DeprecationWarning): @@ -271,7 +339,14 @@ class Signed_TestCase(unittest.TestCase): from _testcapi import getargs_l # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX) self.assertRaises(TypeError, getargs_l, 3.14) - self.assertEqual(99, getargs_l(Int())) + self.assertEqual(99, getargs_l(Index())) + self.assertEqual(0, getargs_l(IndexIntSubclass())) + self.assertRaises(TypeError, getargs_l, BadIndex()) + with self.assertWarns(DeprecationWarning): + self.assertEqual(1, getargs_l(BadIndex2())) + self.assertEqual(0, getargs_l(BadIndex3())) + with self.assertWarns(DeprecationWarning): + self.assertEqual(99, getargs_l(Int())) self.assertEqual(0, getargs_l(IntSubclass())) self.assertRaises(TypeError, getargs_l, BadInt()) with self.assertWarns(DeprecationWarning): @@ -291,6 +366,12 @@ class Signed_TestCase(unittest.TestCase): # n returns 'Py_ssize_t', and does range checking # (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX) self.assertRaises(TypeError, getargs_n, 3.14) + self.assertEqual(99, getargs_n(Index())) + self.assertEqual(0, getargs_n(IndexIntSubclass())) + self.assertRaises(TypeError, getargs_n, BadIndex()) + with self.assertWarns(DeprecationWarning): + self.assertEqual(1, getargs_n(BadIndex2())) + self.assertEqual(0, getargs_n(BadIndex3())) self.assertRaises(TypeError, getargs_n, Int()) self.assertEqual(0, getargs_n(IntSubclass())) self.assertRaises(TypeError, getargs_n, BadInt()) @@ -313,7 +394,14 @@ class LongLong_TestCase(unittest.TestCase): # ... LLONG_MAX) self.assertRaises(TypeError, getargs_L, 3.14) self.assertRaises(TypeError, getargs_L, "Hello") - self.assertEqual(99, getargs_L(Int())) + self.assertEqual(99, getargs_L(Index())) + self.assertEqual(0, getargs_L(IndexIntSubclass())) + self.assertRaises(TypeError, getargs_L, BadIndex()) + with self.assertWarns(DeprecationWarning): + self.assertEqual(1, getargs_L(BadIndex2())) + self.assertEqual(0, getargs_L(BadIndex3())) + with self.assertWarns(DeprecationWarning): + self.assertEqual(99, getargs_L(Int())) self.assertEqual(0, getargs_L(IntSubclass())) self.assertRaises(TypeError, getargs_L, BadInt()) with self.assertWarns(DeprecationWarning): @@ -332,6 +420,11 @@ class LongLong_TestCase(unittest.TestCase): from _testcapi import getargs_K # K return 'unsigned long long', no range checking self.assertRaises(TypeError, getargs_K, 3.14) + self.assertRaises(TypeError, getargs_K, Index()) + self.assertEqual(0, getargs_K(IndexIntSubclass())) + self.assertRaises(TypeError, getargs_K, BadIndex()) + self.assertRaises(TypeError, getargs_K, BadIndex2()) + self.assertEqual(0, getargs_K(BadIndex3())) self.assertRaises(TypeError, getargs_K, Int()) self.assertEqual(0, getargs_K(IntSubclass())) self.assertRaises(TypeError, getargs_K, BadInt()) diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py index c048b71..307ca36 100644 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -385,7 +385,8 @@ class IntTestCases(unittest.TestCase): class TruncReturnsNonInt(base): def __trunc__(self): return Integral() - self.assertEqual(int(TruncReturnsNonInt()), 42) + with self.assertWarns(DeprecationWarning): + self.assertEqual(int(TruncReturnsNonInt()), 42) class NonIntegral(trunc_result_base): def __trunc__(self): diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py index bf5d64c..f828b4c 100644 --- a/Lib/test/test_zlib.py +++ b/Lib/test/test_zlib.py @@ -914,7 +914,7 @@ LAERTES class CustomInt: - def __int__(self): + def __index__(self): return 100 |