Issue #17576: Deprecation warning emitted now when __int__() or __index__()

return not int instance.  Introduced _PyLong_FromNbInt() and refactored
PyLong_As*() functions.
This commit is contained in:
Serhiy Storchaka 2013-12-11 21:07:54 +02:00
parent acd17304d2
commit 31a655411a
7 changed files with 313 additions and 187 deletions

View file

@ -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