]> granicus.if.org Git - python/commitdiff
- Issue #1686386: Tuple's tp_repr did not take into account the possibility of
authorGuido van Rossum <guido@python.org>
Thu, 10 Apr 2008 22:43:58 +0000 (22:43 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 10 Apr 2008 22:43:58 +0000 (22:43 +0000)
  having a self-referential tuple, which is possible from C code.  Nor did
  object's tp_str consider that a type's tp_str could do something that could
  lead to an inifinite recursion.  Py_ReprEnter() and Py_EnterRecursiveCall(),
  respectively, fixed the issues.  (Backport of r58288 from trunk to 2.5.)

Misc/NEWS
Objects/object.c
Objects/tupleobject.c

index aeee3f8f5558b186fbb98a41ade71018fd68f884..b9ce278e3e0d1d30d8e70c550f519faf4daee834 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,12 @@ What's New in Python 2.5.3?
 Core and builtins
 -----------------
 
+- Issue #1686386: Tuple's tp_repr did not take into account the possibility of
+  having a self-referential tuple, which is possible from C code.  Nor did
+  object's tp_str consider that a type's tp_str could do something that could
+  lead to an inifinite recursion.  Py_ReprEnter() and Py_EnterRecursiveCall(),
+  respectively, fixed the issues.  (Backport of r58288 from trunk.)
+
 - Patch #1442: properly report exceptions when the PYTHONSTARTUP file
   cannot be executed.
 
index 71e5641e969129bea7e228ae3f46ba7e83342afc..af7be01fbb6888a469db7f1d0a569a561d4a302a 100644 (file)
@@ -403,7 +403,12 @@ _PyObject_Str(PyObject *v)
        if (v->ob_type->tp_str == NULL)
                return PyObject_Repr(v);
 
+       /* It is possible for a type to have a tp_str representation that loops
+          infinitely. */
+       if (Py_EnterRecursiveCall(" while getting the str of an object"))
+               return NULL;
        res = (*v->ob_type->tp_str)(v);
+       Py_LeaveRecursiveCall();
        if (res == NULL)
                return NULL;
        type_ok = PyString_Check(res);
@@ -2141,4 +2146,3 @@ _PyTrash_destroy_chain(void)
 #ifdef __cplusplus
 }
 #endif
-
index 6f3711f1f457f290cc05d532a2e644c506b1b495..56a1024fa1d206f50b1f83afbf198b6a666690b6 100644 (file)
@@ -208,6 +208,15 @@ tuplerepr(PyTupleObject *v)
        PyObject *s, *temp;
        PyObject *pieces, *result = NULL;
 
+       /* While not mutable, it is still possible to end up with a cycle in a
+          tuple through an object that stores itself within a tuple (and thus
+          infinitely asks for the repr of itself). This should only be
+          possible within a type. */
+       i = Py_ReprEnter((PyObject *)v);
+       if (i != 0) {
+               return i > 0 ? PyString_FromString("(...)") : NULL;
+       }
+
        n = v->ob_size;
        if (n == 0)
                return PyString_FromString("()");
@@ -218,7 +227,10 @@ tuplerepr(PyTupleObject *v)
 
        /* Do repr() on each element. */
        for (i = 0; i < n; ++i) {
+               if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
+                       goto Done;
                s = PyObject_Repr(v->ob_item[i]);
+               Py_LeaveRecursiveCall();
                if (s == NULL)
                        goto Done;
                PyTuple_SET_ITEM(pieces, i, s);
@@ -253,6 +265,7 @@ tuplerepr(PyTupleObject *v)
 
 Done:
        Py_DECREF(pieces);
+       Py_ReprLeave((PyObject *)v);
        return result;
 }