gh-132713: Fix repr(list) race condition (#132801)

Hold a strong reference to the item while calling repr(item).
This commit is contained in:
Victor Stinner 2025-04-22 22:09:35 +02:00 committed by GitHub
parent 722c501dba
commit a4ea80d523
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 1 deletions

View file

@ -118,6 +118,19 @@ class ListTest(list_tests.CommonTest):
with self.assertRaises((MemoryError, OverflowError)):
lst *= size
def test_repr_mutate(self):
class Obj:
@staticmethod
def __repr__():
try:
mylist.pop()
except IndexError:
pass
return 'obj'
mylist = [Obj() for _ in range(5)]
self.assertEqual(repr(mylist), '[obj, obj, obj]')
def test_repr_large(self):
# Check the repr of large list objects
def check(n):