From 5c25376ce3a6b8a54b3c18b2fa92d32e5814cd3c Mon Sep 17 00:00:00 2001 From: David Andrews Date: Sat, 14 Nov 2026 06:50:15 -0500 Subject: [PATCH] gh-161968: Fall back to the right operand's `sq_repeat` in `PyNumber_InPlaceMultiply` (GH-161969) (cherry picked from commit fb08c03d5c49d0cd43ee0afac302609c8ab0b361) Co-authored-by: David Andrews Co-authored-by: Stan Ulbrych --- Lib/test/test_augassign.py | 19 +++++++++++++++++++ ...-11-13-00-00-00.gh-issue-161968.ytuf5z.rst | 2 ++ Objects/abstract.c | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-00-00-00.gh-issue-161968.ytuf5z.rst 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 28f751965f36b9..b4d31040296780 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1343,7 +1343,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. */