https://github.com/python/cpython/commit/fb08c03d5c49d0cd43ee0afac302609c8ab0b361
commit: fb08c03d5c49d0cd43ee0afac302609c8ab0b361
branch: main
author: David Andrews <[email protected]>
committer: StanFromIreland <[email protected]>
date: 2026-11-14T11:50:15Z
summary:

gh-161968: Fall back to the right operand's `sq_repeat` in 
`PyNumber_InPlaceMultiply` (#161969)

Co-authored-by: Stan Ulbrych <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-00-00-00.gh-issue-161968.ytuf5z.rst
M Lib/test/test_augassign.py
M Objects/abstract.c

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. */

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to