Skip to content

Commit 33f71f8

Browse files
miss-islingtonBroyojoStanFromIreland
authored
[3.15] gh-161968: Fall back to the right operand's sq_repeat in PyNumber_InPlaceMultiply (GH-161969) (#162009)
(cherry picked from commit fb08c03) Co-authored-by: David Andrews <dha@xoba.com> Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 2673045 commit 33f71f8

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎Lib/test/test_augassign.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ def testSequences(self):
6464
self.assertEqual(x, [1, 2, 1, 2, 3])
6565
self.assertTrue(x is y)
6666

67+
def testSequenceRepeatRightOperand(self):
68+
# gh-161968
69+
class MyInt(int):
70+
pass
71+
class Index:
72+
def __index__(self):
73+
return 2
74+
75+
seq = [1]
76+
x = MyInt(2)
77+
x *= seq
78+
self.assertEqual(x, [1, 1])
79+
# The right operand is not modified in place.
80+
self.assertEqual(seq, [1])
81+
82+
x = Index()
83+
x *= 'a'
84+
self.assertEqual(x, 'aa')
85+
6786
def testCustomMethods1(self):
6887

6988
class aug_test:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``x *= seq`` raising :exc:`TypeError` when ``x`` is an instance of an
2+
:class:`int` subclass or of a class defining :meth:`~object.__index__`.

‎Objects/abstract.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
13431343
if (f != NULL)
13441344
return sequence_repeat(f, v, w);
13451345
}
1346-
else if (mw != NULL) {
1346+
if (mw != NULL) {
13471347
/* Note that the right hand operand should not be
13481348
* mutated in this case so sq_inplace_repeat is not
13491349
* used. */

0 commit comments

Comments
 (0)