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()
stop = 0;
}
for (i = start; i < stop && i < Py_SIZE(self); i++) {
- int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
+ PyObject *obj = self->ob_item[i];
+ Py_INCREF(obj);
+ int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
+ Py_DECREF(obj);
if (cmp > 0)
return PyLong_FromSsize_t(i);
else if (cmp < 0)
Py_ssize_t i;
for (i = 0; i < Py_SIZE(self); i++) {
- int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
+ PyObject *obj = self->ob_item[i];
+ Py_INCREF(obj);
+ int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
+ Py_DECREF(obj);
if (cmp > 0)
count++;
else if (cmp < 0)
Py_ssize_t i;
for (i = 0; i < Py_SIZE(self); i++) {
- int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
+ PyObject *obj = self->ob_item[i];
+ Py_INCREF(obj);
+ int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
+ Py_DECREF(obj);
if (cmp > 0) {
if (list_ass_slice(self, i, i+1,
(PyObject *)NULL) == 0)