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):
/* "[" + "1" + ", 2" * (len - 1) + "]" */
Py_ssize_t prealloc = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
PyUnicodeWriter *writer = PyUnicodeWriter_Create(prealloc);
+ PyObject *item = NULL;
if (writer == NULL) {
goto error;
}
/* Do repr() on each element. Note that this may mutate the list,
so must refetch the list size on each iteration. */
for (Py_ssize_t i = 0; i < Py_SIZE(v); ++i) {
+ item = list_get_item_ref(v, i);
+ if (item == NULL) {
+ // List truncated while iterating on it
+ PyErr_Clear();
+ break;
+ }
+
if (i > 0) {
if (PyUnicodeWriter_WriteChar(writer, ',') < 0) {
goto error;
}
}
- if (PyUnicodeWriter_WriteRepr(writer, v->ob_item[i]) < 0) {
+ if (PyUnicodeWriter_WriteRepr(writer, item) < 0) {
goto error;
}
+ Py_CLEAR(item);
}
if (PyUnicodeWriter_WriteChar(writer, ']') < 0) {
return PyUnicodeWriter_Finish(writer);
error:
+ Py_XDECREF(item);
PyUnicodeWriter_Discard(writer);
Py_ReprLeave((PyObject *)v);
return NULL;