[3.8] bpo-39453: Fix contains method of list to hold strong references (GH-18204)

This commit is contained in:
Dong-hee Na 2020-02-17 18:13:52 +09:00 committed by GitHub
parent 988aeba94b
commit f64abd1056
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 3 deletions

View File

@ -216,6 +216,13 @@ class ListTest(list_tests.CommonTest):
with self.assertRaises(ValueError):
lst.remove(lst)
# bpo-39453: list.__contains__ was not holding strong references
# to list elements while calling PyObject_RichCompareBool().
lst = [X(), X()]
3 in lst
lst = [X(), X()]
X() in lst
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,2 @@
Fixed a possible crash in :meth:`list.__contains__` when a list is changed
during comparing items. Patch by Dong-hee Na.

View File

@ -445,12 +445,16 @@ list_length(PyListObject *a)
static int
list_contains(PyListObject *a, PyObject *el)
{
PyObject *item;
Py_ssize_t i;
int cmp;
for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
Py_EQ);
for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) {
item = PyList_GET_ITEM(a, i);
Py_INCREF(item);
cmp = PyObject_RichCompareBool(el, item, Py_EQ);
Py_DECREF(item);
}
return cmp;
}