mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
allow classes as exceptions
This commit is contained in:
parent
8ae87c0489
commit
8bf7c484c1
1 changed files with 26 additions and 4 deletions
|
@ -783,13 +783,33 @@ eval_code(co, globals, locals, owner, arg)
|
||||||
u = w;
|
u = w;
|
||||||
w = gettupleitem(u, 0);
|
w = gettupleitem(u, 0);
|
||||||
INCREF(w);
|
INCREF(w);
|
||||||
|
INCREF(w);
|
||||||
DECREF(u);
|
DECREF(u);
|
||||||
}
|
}
|
||||||
if (!is_stringobject(w))
|
if (is_stringobject(w)) {
|
||||||
err_setstr(TypeError,
|
|
||||||
"exceptions must be strings");
|
|
||||||
else
|
|
||||||
err_setval(w, v);
|
err_setval(w, v);
|
||||||
|
} else if (is_classobject(w)) {
|
||||||
|
if (!is_instanceobject(v)
|
||||||
|
|| !issubclass((object*)((instanceobject*)v)->in_class,
|
||||||
|
w))
|
||||||
|
err_setstr(TypeError,
|
||||||
|
"a class exception must have a value that is an instance of the class");
|
||||||
|
else
|
||||||
|
err_setval(w,v);
|
||||||
|
} else if (is_instanceobject(w)) {
|
||||||
|
if (v != None)
|
||||||
|
err_setstr(TypeError,
|
||||||
|
"an instance exception may not have a separate value");
|
||||||
|
else {
|
||||||
|
DECREF(v);
|
||||||
|
v = w;
|
||||||
|
w = (object*) ((instanceobject*)w)->in_class;
|
||||||
|
INCREF(w);
|
||||||
|
err_setval(w, v);
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
err_setstr(TypeError,
|
||||||
|
"exceptions must be strings, classes, or instances");
|
||||||
DECREF(v);
|
DECREF(v);
|
||||||
DECREF(w);
|
DECREF(w);
|
||||||
why = WHY_EXCEPTION;
|
why = WHY_EXCEPTION;
|
||||||
|
@ -2393,6 +2413,8 @@ cmp_exception(err, v)
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
if (is_classobject(v) && is_classobject(err))
|
||||||
|
return issubclass(err, v);
|
||||||
return err == v;
|
return err == v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue