diff options
author | Raymond Hettinger <python@rcn.com> | 2006-12-30 04:01:17 +0000 |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2006-12-30 04:01:17 +0000 |
commit | 5399910eba1ef3580aebf49482b1367f14fa6c48 (patch) | |
tree | d853caaf0b19b05ecef8330a704a64ce657bde37 /Objects/setobject.c | |
parent | SF bug #1623890, fix argument name in docstring (diff) | |
download | cpython-5399910eba1ef3580aebf49482b1367f14fa6c48.tar.gz cpython-5399910eba1ef3580aebf49482b1367f14fa6c48.tar.bz2 cpython-5399910eba1ef3580aebf49482b1367f14fa6c48.zip |
For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Diffstat (limited to 'Objects/setobject.c')
-rw-r--r-- | Objects/setobject.c | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/Objects/setobject.c b/Objects/setobject.c index d33fff93fe2..507a07bdcc7 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -572,34 +572,54 @@ set_tp_print(PySetObject *so, FILE *fp, int flags) Py_ssize_t pos=0; char *emit = ""; /* No separator emitted on first pass */ char *separator = ", "; + int status = Py_ReprEnter((PyObject*)so); + + if (status != 0) { + if (status < 0) + return status; + fprintf(fp, "%s(...)", so->ob_type->tp_name); + return 0; + } fprintf(fp, "%s([", so->ob_type->tp_name); while (set_next(so, &pos, &entry)) { fputs(emit, fp); emit = separator; - if (PyObject_Print(entry->key, fp, 0) != 0) + if (PyObject_Print(entry->key, fp, 0) != 0) { + Py_ReprLeave((PyObject*)so); return -1; + } } fputs("])", fp); + Py_ReprLeave((PyObject*)so); return 0; } static PyObject * set_repr(PySetObject *so) { - PyObject *keys, *result, *listrepr; + PyObject *keys, *result=NULL, *listrepr; + int status = Py_ReprEnter((PyObject*)so); + + if (status != 0) { + if (status < 0) + return NULL; + return PyString_FromFormat("%s(...)", so->ob_type->tp_name); + } keys = PySequence_List((PyObject *)so); if (keys == NULL) - return NULL; + goto done; listrepr = PyObject_Repr(keys); Py_DECREF(keys); if (listrepr == NULL) - return NULL; + goto done; result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name, PyString_AS_STRING(listrepr)); Py_DECREF(listrepr); +done: + Py_ReprLeave((PyObject*)so); return result; } |