diff Python/ceval.c @ 6147:b76a44163e0f legacy-trunk

cmp_exception gets promoted (essentially) to the C API function PyErr_GivenExceptionMatches(). set_exc_info(): make sure to normalize exceptions. do_raise(): Use PyErr_NormalizeException() if type is a class. loop_subscript(): Use PyErr_ExceptionMatches() instead of raw pointer compare for PyExc_IndexError.
author Barry Warsaw <barry@python.org>
date Fri, 22 Aug 1997 21:26:19 +0000
parents a287c5014e66
children 647e60fb3bac
line wrap: on
line diff
--- a/Python/ceval.c	Fri Aug 22 21:22:58 1997 +0000
+++ b/Python/ceval.c	Fri Aug 22 21:26:19 1997 +0000
@@ -84,7 +84,6 @@
 static PyObject *apply_slice Py_PROTO((PyObject *, PyObject *, PyObject *));
 static int assign_slice Py_PROTO((PyObject *, PyObject *,
 				  PyObject *, PyObject *));
-static int cmp_exception Py_PROTO((PyObject *, PyObject *));
 static int cmp_member Py_PROTO((PyObject *, PyObject *));
 static PyObject *cmp_outcome Py_PROTO((int, PyObject *, PyObject *));
 static int import_from Py_PROTO((PyObject *, PyObject *, PyObject *));
@@ -1872,6 +1871,9 @@
 {
 	PyFrameObject *frame;
 	PyObject *tmp_type, *tmp_value, *tmp_tb;
+
+	PyErr_NormalizeException(&type, &value, &tb);
+
 	frame = tstate->frame;
 	if (frame->f_exc_type == NULL) {
 		/* This frame didn't catch an exception before */
@@ -2000,44 +2002,12 @@
 		Py_DECREF(tmp);
 	}
 
-	/* Now switch on the exception's type */
-	if (PyString_Check(type)) {
+	if (PyString_Check(type))
 		;
-	}
-	else if (PyClass_Check(type)) {
-		/* Raising a class.  If the value is an instance, it
-		   better be an instance of the class.  If it is not,
-		   it will be used to create an instance. */
-		if (PyInstance_Check(value)) {
-			PyObject *inclass = (PyObject*)
-				(((PyInstanceObject*)value)->in_class);
-			if (!PyClass_IsSubclass(inclass, type)) {
-				PyErr_SetString(PyExc_TypeError,
- "raise <class>, <instance> requires that <instance> is a member of <class>");
-				goto raise_error;
-			}
-		}
-		else {
-			/* Go instantiate the class */
-			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 raise_error;
-			res = PyEval_CallObject(type, args);
-			Py_DECREF(args);
-			if (res == NULL)
-				goto raise_error;
-			Py_DECREF(value);
-			value = res;
-		}
-	}
+
+	else if (PyClass_Check(type))
+		PyErr_NormalizeException(&type, &value, &tb);
+
 	else if (PyInstance_Check(type)) {
 		/* Raising an instance.  The value should be a dummy. */
 		if (value != Py_None) {
@@ -2465,7 +2435,7 @@
 	v = (*sq->sq_item)(v, i);
 	if (v)
 		return v;
-	if (PyErr_Occurred() == PyExc_IndexError)
+	if (PyErr_ExceptionMatches(PyExc_IndexError))
 		PyErr_Clear();
 	return NULL;
 }
@@ -2521,25 +2491,6 @@
 }
 
 static int
-cmp_exception(err, v)
-	PyObject *err, *v;
-{
-	if (PyTuple_Check(v)) {
-		int i, n;
-		n = PyTuple_Size(v);
-		for (i = 0; i < n; i++) {
-			/* Test recursively */
-			if (cmp_exception(err, PyTuple_GET_ITEM(v, i)))
-				return 1;
-		}
-		return 0;
-	}
-	if (PyClass_Check(v) && PyClass_Check(err))
-		return PyClass_IsSubclass(err, v);
-	return err == v;
-}
-
-static int
 cmp_member(v, w)
 	PyObject *v, *w;
 {
@@ -2613,7 +2564,7 @@
 			res = !res;
 		break;
 	case EXC_MATCH:
-		res = cmp_exception(v, w);
+		res = PyErr_GivenExceptionMatches(v, w);
 		break;
 	default:
 		cmp = PyObject_Compare(v, w);