summaryrefslogtreecommitdiffstats
path: root/Python/errors.c
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>1997-08-22 21:22:58 (GMT)
committerBarry Warsaw <barry@python.org>1997-08-22 21:22:58 (GMT)
commitc0dc92af7d41ecf1d60f4ab29452d7179c039d8c (patch)
tree207d717b9b91dac3b1b7a605ed2292cc87830e69 /Python/errors.c
parentcde8b1ba0c0948f2cec00d11821214e06ca419cb (diff)
downloadcpython-c0dc92af7d41ecf1d60f4ab29452d7179c039d8c.zip
cpython-c0dc92af7d41ecf1d60f4ab29452d7179c039d8c.tar.gz
cpython-c0dc92af7d41ecf1d60f4ab29452d7179c039d8c.tar.bz2
Three new C API functions:
- int PyErr_GivenExceptionMatches(obj1, obj2) Returns 1 if obj1 and obj2 are the same object, or if obj1 is an instance of type obj2, or of a class derived from obj2 - int PyErr_ExceptionMatches(obj) Higher level wrapper around PyErr_GivenExceptionMatches() which uses PyErr_Occurred() as obj1. This will be the more commonly called function. - void PyErr_NormalizeException(typeptr, valptr, tbptr) Normalizes exceptions, and places the normalized values in the arguments. If type is not a class, this does nothing. If type is a class, then it makes sure that value is an instance of the class by: 1. if instance is of the type, or a class derived from type, it does nothing. 2. otherwise it instantiates the class, using the value as an argument. If value is None, it uses an empty arg tuple, and if the value is a tuple, it uses just that.
Diffstat (limited to 'Python/errors.c')
-rw-r--r--Python/errors.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/Python/errors.c b/Python/errors.c
index 6109387..48649fd 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -115,6 +115,111 @@ PyErr_Occurred()
return tstate->curexc_type;
}
+
+int
+PyErr_GivenExceptionMatches(err, exc)
+ PyObject *err, *exc;
+{
+ if (PyTuple_Check(exc)) {
+ int i, n;
+ n = PyTuple_Size(exc);
+ for (i = 0; i < n; i++) {
+ /* Test recursively */
+ if (PyErr_GivenExceptionMatches(
+ err, PyTuple_GET_ITEM(exc, i)))
+ {
+ return 1;
+ }
+ }
+ return 0;
+ }
+ /* err might be an instance, so check its class. */
+ if (PyInstance_Check(err))
+ err = (PyObject*)((PyInstanceObject*)err)->in_class;
+
+ if (PyClass_Check(err) && PyClass_Check(exc))
+ return PyClass_IsSubclass(err, exc);
+
+ return err == exc;
+}
+
+
+int
+PyErr_ExceptionMatches(exc)
+ PyObject *exc;
+{
+ return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
+}
+
+
+/* Used in many places to normalize a raised exception, including in
+ eval_code2(), do_raise(), and PyErr_Print()
+*/
+void
+PyErr_NormalizeException(exc, val, tb)
+ PyObject **exc;
+ PyObject **val;
+ PyObject **tb;
+{
+ PyObject *type = *exc;
+ PyObject *value = *val;
+ PyObject *inclass = NULL;
+
+ /* If PyErr_SetNone() was used, the value will have been actually
+ set to NULL.
+ */
+ if (!value) {
+ value = Py_None;
+ Py_INCREF(value);
+ }
+
+ if (PyInstance_Check(value))
+ inclass = (PyObject*)((PyInstanceObject*)value)->in_class;
+
+ /* Normalize the exception so that if the type is a class, the
+ value will be an instance.
+ */
+ if (PyClass_Check(type)) {
+ /* if the value was not an instance, or is not an instance
+ whose class is (or is derived from) type, then use the
+ value as an argument to instantiation of the type
+ class.
+ */
+ if (!inclass || !PyClass_IsSubclass(inclass, type)) {
+ PyObject *args, *res;
+
+ if (value == Py_None)
+ args = Py_BuildValue("()");
+ else if (PyTuple_Check(value)) {
+ Py_INCREF(value);
+ args = value;
+ }
+ else
+ args = Py_BuildValue("(O)", value);
+
+ if (args == NULL)
+ goto finally;
+ res = PyEval_CallObject(type, args);
+ Py_DECREF(args);
+ if (res == NULL)
+ goto finally;
+ Py_DECREF(value);
+ value = res;
+ }
+ }
+ *exc = type;
+ *val = value;
+ return;
+finally:
+ Py_DECREF(*exc);
+ Py_DECREF(*val);
+ Py_XDECREF(*tb);
+ PyErr_Fetch(exc, val, tb);
+ /* normalize recursively */
+ PyErr_NormalizeException(exc, val, tb);
+}
+
+
void
PyErr_Fetch(p_type, p_value, p_traceback)
PyObject **p_type;