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

Skip to content

Commit 255443b

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

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

Objects/dictobject.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,9 @@ dictresize(mp, minused)
301301
insertdict(mp,ep->me_key,ep->me_hash,ep->me_value);
302302
}
303303
for (i = 0, ep = oldtable; i < oldsize; i++, ep++) {
304-
if (ep->me_value == NULL)
304+
if (ep->me_value == NULL) {
305305
Py_XDECREF(ep->me_key);
306+
}
306307
}
307308

308309
PyMem_XDEL(oldtable);
@@ -483,10 +484,12 @@ dict_dealloc(mp)
483484
register int i;
484485
register dictentry *ep;
485486
for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
486-
if (ep->me_key != NULL)
487+
if (ep->me_key != NULL) {
487488
Py_DECREF(ep->me_key);
488-
if (ep->me_value != NULL)
489+
}
490+
if (ep->me_value != NULL) {
489491
Py_DECREF(ep->me_value);
492+
}
490493
}
491494
PyMem_XDEL(mp->ma_table);
492495
PyMem_DEL(mp);
@@ -501,20 +504,34 @@ dict_print(mp, fp, flags)
501504
register int i;
502505
register int any;
503506
register dictentry *ep;
507+
508+
i = Py_ReprEnter((PyObject*)mp);
509+
if (i != 0) {
510+
if (i < 0)
511+
return i;
512+
fprintf(fp, "{...}");
513+
return 0;
514+
}
515+
504516
fprintf(fp, "{");
505517
any = 0;
506518
for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
507519
if (ep->me_value != NULL) {
508520
if (any++ > 0)
509521
fprintf(fp, ", ");
510-
if (PyObject_Print((PyObject *)ep->me_key, fp, 0) != 0)
522+
if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
523+
Py_ReprLeave((PyObject*)mp);
511524
return -1;
525+
}
512526
fprintf(fp, ": ");
513-
if (PyObject_Print(ep->me_value, fp, 0) != 0)
527+
if (PyObject_Print(ep->me_value, fp, 0) != 0) {
528+
Py_ReprLeave((PyObject*)mp);
514529
return -1;
530+
}
515531
}
516532
}
517533
fprintf(fp, "}");
534+
Py_ReprLeave((PyObject*)mp);
518535
return 0;
519536
}
520537

@@ -527,6 +544,14 @@ dict_repr(mp)
527544
register int i;
528545
register int any;
529546
register dictentry *ep;
547+
548+
i = Py_ReprEnter((PyObject*)mp);
549+
if (i != 0) {
550+
if (i > 0)
551+
return PyString_FromString("{...}");
552+
return NULL;
553+
}
554+
530555
v = PyString_FromString("{");
531556
sepa = PyString_FromString(", ");
532557
colon = PyString_FromString(": ");
@@ -541,6 +566,7 @@ dict_repr(mp)
541566
}
542567
}
543568
PyString_ConcatAndDel(&v, PyString_FromString("}"));
569+
Py_ReprLeave((PyObject*)mp);
544570
Py_XDECREF(sepa);
545571
Py_XDECREF(colon);
546572
return v;

0 commit comments

Comments
 (0)