Skip to content

Commit f41021e

Browse files
test_numeric_tower: add regression test for __index__ mutation
1 parent 19e1acb commit f41021e

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lib/test/test_numeric_tower.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,29 @@ def test_complex(self):
225225
self.assertRaises(TypeError, op, z, v)
226226
self.assertRaises(TypeError, op, v, z)
227227

228+
class IndexMutationDuringNumericOpTest(unittest.TestCase):
229+
230+
def test_index_mutates_lhs_type_during_operation(self):
231+
import array
228232

233+
class Good(array.array):
234+
pass
235+
236+
class Hide(type):
237+
def mro(cls):
238+
return (cls, object)
239+
240+
class Bad(Good, metaclass=Hide):
241+
pass
242+
243+
arr = Good('b', b'x')
244+
245+
class Count:
246+
def __index__(self):
247+
arr.__class__ = Bad
248+
return 2
249+
250+
with self.assertRaises(TypeError):
251+
arr * Count()
229252
if __name__ == '__main__':
230253
unittest.main()

0 commit comments

Comments
 (0)