]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Restore changeset 5bd9db528aed (issue #18408)
authorVictor Stinner <vstinner@wyplay.com>
Mon, 26 Aug 2013 12:05:19 +0000 (14:05 +0200)
committerVictor Stinner <vstinner@wyplay.com>
Mon, 26 Aug 2013 12:05:19 +0000 (14:05 +0200)
"Issue #18408: PyObject_Str(), PyObject_Repr() and type_call() now fail with an
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 006f0d4dbc343eef1253b04c194e33f89e195a97..693d8c73b1c622cb077add5b35717fcde54e4f9e 100644 (file)
@@ -449,6 +449,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;
@@ -491,6 +499,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 c6ff0193e7f4d1820a99008a3870d79d44ae6a1a..3ff42da1dd6512df69a8543fd9e400027b76c600 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),