summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1995-01-26 00:41:04 (GMT)
committerGuido van Rossum <guido@python.org>1995-01-26 00:41:04 (GMT)
commit8bf7c484c16edfc5084732fdd6d8b64a0f921930 (patch)
tree3ffb22998dc7480bc099fc3b7a2c33d33b5cb300
parent8ae87c0489e718f9bfa2c78d145628f79f9cdc81 (diff)
downloadcpython-8bf7c484c16edfc5084732fdd6d8b64a0f921930.zip
cpython-8bf7c484c16edfc5084732fdd6d8b64a0f921930.tar.gz
cpython-8bf7c484c16edfc5084732fdd6d8b64a0f921930.tar.bz2
allow classes as exceptions
-rw-r--r--Python/ceval.c30
1 files changed, 26 insertions, 4 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index c7fa697..fb1c0e7 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -783,13 +783,33 @@ eval_code(co, globals, locals, owner, arg)
u = w;
w = gettupleitem(u, 0);
INCREF(w);
+ INCREF(w);
DECREF(u);
}
- if (!is_stringobject(w))
- err_setstr(TypeError,
- "exceptions must be strings");
- else
+ if (is_stringobject(w)) {
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(w);
why = WHY_EXCEPTION;
@@ -2393,6 +2413,8 @@ cmp_exception(err, v)
}
return 0;
}
+ if (is_classobject(v) && is_classobject(err))
+ return issubclass(err, v);
return err == v;
}