summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_index.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_index.py')
-rw-r--r--Lib/test/test_index.py36
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