diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-12-11 19:07:54 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-12-11 19:07:54 (GMT) |
commit | 31a655411a79b00517cdcd0a2752824d183db792 (patch) | |
tree | 78bcf4531d65f5b7eadf550642e8a842741e8108 /Lib/test/test_index.py | |
parent | acd17304d290bd0b2ad41e5922f81679fea3ab6e (diff) | |
download | cpython-31a655411a79b00517cdcd0a2752824d183db792.zip cpython-31a655411a79b00517cdcd0a2752824d183db792.tar.gz cpython-31a655411a79b00517cdcd0a2752824d183db792.tar.bz2 |
Issue #17576: Deprecation warning emitted now when __int__() or __index__()
return not int instance. Introduced _PyLong_FromNbInt() and refactored
PyLong_As*() functions.
Diffstat (limited to 'Lib/test/test_index.py')
-rw-r--r-- | Lib/test/test_index.py | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/Lib/test/test_index.py b/Lib/test/test_index.py index 66eedaa..a2ac321 100644 --- a/Lib/test/test_index.py +++ b/Lib/test/test_index.py @@ -9,7 +9,7 @@ class newstyle: class TrapInt(int): def __index__(self): - return self + return int(self) class BaseTestCase(unittest.TestCase): def setUp(self): @@ -55,6 +55,40 @@ class BaseTestCase(unittest.TestCase): self.assertRaises(TypeError, slice(self.o).indices, 0) self.assertRaises(TypeError, slice(self.n).indices, 0) + def test_int_subclass_with_index(self): + # __index__ should be used when computing indices, even for int + # subclasses. See issue #17576. + class MyInt(int): + def __index__(self): + return int(self) + 1 + + my_int = MyInt(7) + direct_index = my_int.__index__() + operator_index = operator.index(my_int) + self.assertEqual(direct_index, 8) + self.assertEqual(operator_index, 7) + # Both results should be of exact type int. + self.assertIs(type(direct_index), int) + #self.assertIs(type(operator_index), int) + + def test_index_returns_int_subclass(self): + class BadInt: + def __index__(self): + return True + + class BadInt2(int): + def __index__(self): + return True + + bad_int = BadInt() + with self.assertWarns(DeprecationWarning): + n = operator.index(bad_int) + self.assertEqual(n, 1) + + bad_int = BadInt2() + n = operator.index(bad_int) + self.assertEqual(n, 0) + class SeqTestCase: # This test case isn't run directly. It just defines common tests |