mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
bpo-38610: Fix possible crashes in several list methods (GH-17022)
Hold strong references to list elements while calling PyObject_RichCompareBool().
This commit is contained in:
parent
09c482fad1
commit
d9e561d23d
3 changed files with 40 additions and 3 deletions
|
@ -171,5 +171,31 @@ class ListTest(list_tests.CommonTest):
|
|||
self.assertEqual(iter_size, sys.getsizeof(list([0] * 10)))
|
||||
self.assertEqual(iter_size, sys.getsizeof(list(range(10))))
|
||||
|
||||
def test_count_index_remove_crashes(self):
|
||||
# bpo-38610: The count(), index(), and remove() methods were not
|
||||
# holding strong references to list elements while calling
|
||||
# PyObject_RichCompareBool().
|
||||
class X:
|
||||
def __eq__(self, other):
|
||||
lst.clear()
|
||||
return NotImplemented
|
||||
|
||||
lst = [X()]
|
||||
with self.assertRaises(ValueError):
|
||||
lst.index(lst)
|
||||
|
||||
class L(list):
|
||||
def __eq__(self, other):
|
||||
str(other)
|
||||
return NotImplemented
|
||||
|
||||
lst = L([X()])
|
||||
lst.count(lst)
|
||||
|
||||
lst = L([X()])
|
||||
with self.assertRaises(ValueError):
|
||||
lst.remove(lst)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue