Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_augassign.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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__`.
2 changes: 1 addition & 1 deletion Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Loading