Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb376de

Browse files
committed
Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
(Jeremy will hardly recognize his patch :-)
1 parent 255443b commit fb376de

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

Objects/listobject.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,25 @@ list_print(op, fp, flags)
231231
int flags;
232232
{
233233
int i;
234+
235+
i = Py_ReprEnter((PyObject*)op);
236+
if (i != 0) {
237+
if (i < 0)
238+
return i;
239+
fprintf(fp, "[...]");
240+
return 0;
241+
}
234242
fprintf(fp, "[");
235243
for (i = 0; i < op->ob_size; i++) {
236244
if (i > 0)
237245
fprintf(fp, ", ");
238-
if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
246+
if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
247+
Py_ReprLeave((PyObject *)op);
239248
return -1;
249+
}
240250
}
241251
fprintf(fp, "]");
252+
Py_ReprLeave((PyObject *)op);
242253
return 0;
243254
}
244255

@@ -248,6 +259,13 @@ list_repr(v)
248259
{
249260
PyObject *s, *comma;
250261
int i;
262+
263+
i = Py_ReprEnter((PyObject*)v);
264+
if (i != 0) {
265+
if (i > 0)
266+
return PyString_FromString("[...]");
267+
return NULL;
268+
}
251269
s = PyString_FromString("[");
252270
comma = PyString_FromString(", ");
253271
for (i = 0; i < v->ob_size && s != NULL; i++) {
@@ -257,6 +275,7 @@ list_repr(v)
257275
}
258276
Py_XDECREF(comma);
259277
PyString_ConcatAndDel(&s, PyString_FromString("]"));
278+
Py_ReprLeave((PyObject *)v);
260279
return s;
261280
}
262281

0 commit comments

Comments
 (0)