Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4249f54

Browse files
committed
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.
1 parent c0dc92a commit 4249f54

1 file changed

Lines changed: 10 additions & 59 deletions

File tree

Python/ceval.c

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ static int slice_index Py_PROTO((PyObject *, int *));
8484
static PyObject *apply_slice Py_PROTO((PyObject *, PyObject *, PyObject *));
8585
static int assign_slice Py_PROTO((PyObject *, PyObject *,
8686
PyObject *, PyObject *));
87-
static int cmp_exception Py_PROTO((PyObject *, PyObject *));
8887
static int cmp_member Py_PROTO((PyObject *, PyObject *));
8988
static PyObject *cmp_outcome Py_PROTO((int, PyObject *, PyObject *));
9089
static int import_from Py_PROTO((PyObject *, PyObject *, PyObject *));
@@ -1872,6 +1871,9 @@ set_exc_info(tstate, type, value, tb)
18721871
{
18731872
PyFrameObject *frame;
18741873
PyObject *tmp_type, *tmp_value, *tmp_tb;
1874+
1875+
PyErr_NormalizeException(&type, &value, &tb);
1876+
18751877
frame = tstate->frame;
18761878
if (frame->f_exc_type == NULL) {
18771879
/* This frame didn't catch an exception before */
@@ -2000,44 +2002,12 @@ do_raise(type, value, tb)
20002002
Py_DECREF(tmp);
20012003
}
20022004

2003-
/* Now switch on the exception's type */
2004-
if (PyString_Check(type)) {
2005+
if (PyString_Check(type))
20052006
;
2006-
}
2007-
else if (PyClass_Check(type)) {
2008-
/* Raising a class. If the value is an instance, it
2009-
better be an instance of the class. If it is not,
2010-
it will be used to create an instance. */
2011-
if (PyInstance_Check(value)) {
2012-
PyObject *inclass = (PyObject*)
2013-
(((PyInstanceObject*)value)->in_class);
2014-
if (!PyClass_IsSubclass(inclass, type)) {
2015-
PyErr_SetString(PyExc_TypeError,
2016-
"raise <class>, <instance> requires that <instance> is a member of <class>");
2017-
goto raise_error;
2018-
}
2019-
}
2020-
else {
2021-
/* Go instantiate the class */
2022-
PyObject *args, *res;
2023-
if (value == Py_None)
2024-
args = Py_BuildValue("()");
2025-
else if (PyTuple_Check(value)) {
2026-
Py_INCREF(value);
2027-
args = value;
2028-
}
2029-
else
2030-
args = Py_BuildValue("(O)", value);
2031-
if (args == NULL)
2032-
goto raise_error;
2033-
res = PyEval_CallObject(type, args);
2034-
Py_DECREF(args);
2035-
if (res == NULL)
2036-
goto raise_error;
2037-
Py_DECREF(value);
2038-
value = res;
2039-
}
2040-
}
2007+
2008+
else if (PyClass_Check(type))
2009+
PyErr_NormalizeException(&type, &value, &tb);
2010+
20412011
else if (PyInstance_Check(type)) {
20422012
/* Raising an instance. The value should be a dummy. */
20432013
if (value != Py_None) {
@@ -2465,7 +2435,7 @@ loop_subscript(v, w)
24652435
v = (*sq->sq_item)(v, i);
24662436
if (v)
24672437
return v;
2468-
if (PyErr_Occurred() == PyExc_IndexError)
2438+
if (PyErr_ExceptionMatches(PyExc_IndexError))
24692439
PyErr_Clear();
24702440
return NULL;
24712441
}
@@ -2520,25 +2490,6 @@ assign_slice(u, v, w, x) /* u[v:w] = x */
25202490
return PySequence_SetSlice(u, ilow, ihigh, x);
25212491
}
25222492

2523-
static int
2524-
cmp_exception(err, v)
2525-
PyObject *err, *v;
2526-
{
2527-
if (PyTuple_Check(v)) {
2528-
int i, n;
2529-
n = PyTuple_Size(v);
2530-
for (i = 0; i < n; i++) {
2531-
/* Test recursively */
2532-
if (cmp_exception(err, PyTuple_GET_ITEM(v, i)))
2533-
return 1;
2534-
}
2535-
return 0;
2536-
}
2537-
if (PyClass_Check(v) && PyClass_Check(err))
2538-
return PyClass_IsSubclass(err, v);
2539-
return err == v;
2540-
}
2541-
25422493
static int
25432494
cmp_member(v, w)
25442495
PyObject *v, *w;
@@ -2613,7 +2564,7 @@ cmp_outcome(op, v, w)
26132564
res = !res;
26142565
break;
26152566
case EXC_MATCH:
2616-
res = cmp_exception(v, w);
2567+
res = PyErr_GivenExceptionMatches(v, w);
26172568
break;
26182569
default:
26192570
cmp = PyObject_Compare(v, w);

0 commit comments

Comments
 (0)