diff --git a/Lib/test/test_augassign.py b/Lib/test/test_augassign.py index 5930d9e7a29fa2..8cef5a970a5213 100644 --- a/Lib/test/test_augassign.py +++ b/Lib/test/test_augassign.py @@ -64,6 +64,25 @@ def testSequences(self): self.assertEqual(x, [1, 2, 1, 2, 3]) self.assertTrue(x is y) + def testSequenceRepeatRightOperand(self): + # gh-161968 + class MyInt(int): + pass + class Index: + def __index__(self): + return 2 + + seq = [1] + x = MyInt(2) + x *= seq + self.assertEqual(x, [1, 1]) + # The right operand is not modified in place. + self.assertEqual(seq, [1]) + + x = Index() + x *= 'a' + self.assertEqual(x, 'aa') + def testCustomMethods1(self): class aug_test: diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-00-00-00.gh-issue-161968.ytuf5z.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-00-00-00.gh-issue-161968.ytuf5z.rst new file mode 100644 index 00000000000000..12d61b2eeee74e --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-00-00-00.gh-issue-161968.ytuf5z.rst @@ -0,0 +1,2 @@ +Fix ``x *= seq`` raising :exc:`TypeError` when ``x`` is an instance of an +:class:`int` subclass or of a class defining :meth:`~object.__index__`. diff --git a/Objects/abstract.c b/Objects/abstract.c index da2a0b0c285b52..6bb1ac572b0c6e 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1344,7 +1344,7 @@ PyNumber_InPlaceMultiply(PyObject *v, PyObject *w) if (f != NULL) return sequence_repeat(f, v, w); } - else if (mw != NULL) { + if (mw != NULL) { /* Note that the right hand operand should not be * mutated in this case so sq_inplace_repeat is not * used. */