]> granicus.if.org Git - python/commitdiff
Issue #18408: PyObject_Str(), PyObject_Repr() and type_call() now fail with an
authorVictor Stinner <victor.stinner@gmail.com>
Wed, 17 Jul 2013 23:49:30 +0000 (01:49 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Wed, 17 Jul 2013 23:49:30 +0000 (01:49 +0200)
assertion error if they are called with an exception set (PyErr_Occurred()).

As PyEval_EvalFrameEx(), they may clear the current exception and so the caller
looses its exception.

Objects/object.c
Objects/typeobject.c

index e957d9d7b0c9f45349b44ab57456165670dddea8..47d3ebd2ca7b49a59db17d8e0175a6bc76286d7c 100644 (file)
@@ -377,6 +377,14 @@ PyObject_Repr(PyObject *v)
     if (Py_TYPE(v)->tp_repr == NULL)
         return PyUnicode_FromFormat("<%s object at %p>",
                                     v->ob_type->tp_name, v);
+
+#ifdef Py_DEBUG
+    /* PyObject_Repr() must not be called with an exception set,
+       because it may clear it (directly or indirectly) and so the
+       caller looses its exception */
+    assert(!PyErr_Occurred());
+#endif
+
     res = (*v->ob_type->tp_repr)(v);
     if (res == NULL)
         return NULL;
@@ -408,6 +416,7 @@ PyObject_Str(PyObject *v)
 #endif
     if (v == NULL)
         return PyUnicode_FromString("<NULL>");
+
     if (PyUnicode_CheckExact(v)) {
 #ifndef Py_DEBUG
         if (PyUnicode_READY(v) < 0)
@@ -419,6 +428,13 @@ PyObject_Str(PyObject *v)
     if (Py_TYPE(v)->tp_str == NULL)
         return PyObject_Repr(v);
 
+#ifdef Py_DEBUG
+    /* PyObject_Str() must not be called with an exception set,
+       because it may clear it (directly or indirectly) and so the
+       caller looses its exception */
+    assert(!PyErr_Occurred());
+#endif
+
     /* 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"))
index c970ada1df0802b2a10031f3c2df225e5256aad9..0eea38443f0c9c1b0ba853522ccd0d14aa58c2bc 100644 (file)
@@ -736,6 +736,13 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return NULL;
     }
 
+#ifdef Py_DEBUG
+    /* type_call() must not be called with an exception set,
+       because it may clear it (directly or indirectly) and so the
+       caller looses its exception */
+    assert(!PyErr_Occurred());
+#endif
+
     obj = type->tp_new(type, args, kwds);
     if (obj != NULL) {
         /* Ugly exception: when the call was type(something),