]> granicus.if.org Git - python/commitdiff
Make it so TypeError is raised if an instance of an object is put in an
authorBrett Cannon <bcannon@gmail.com>
Mon, 26 Feb 2007 22:01:14 +0000 (22:01 +0000)
committerBrett Cannon <bcannon@gmail.com>
Mon, 26 Feb 2007 22:01:14 +0000 (22:01 +0000)
'except' clause.  Also refactor some code to help keep Neal Norwitz happy.

Misc/NEWS
Python/ceval.c

index 4eb2f47b9053210decacde9078de4551a05a4f3c..8ffbe4500036be6405d863726fd0148f04a2eadf 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,7 +32,7 @@ Core and Builtins
   functionality formerly known as raw_input(); the name raw_input()
   is no longer defined.
 
-- Objects listed in an 'except' clause must inherit from BaseException.
+- Classes listed in an 'except' clause must inherit from BaseException.
 
 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
   and .keys(), .items(), .values() return dict views.
index fe5de0375aa6ac24abf2170fafa23ca300b2e211..5cad632c6d372d9babef3c1e2bd5f892c8734b7c 100644 (file)
@@ -3895,23 +3895,8 @@ assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
        }
 }
 
-/*
-   Return a true value if the exception is allowed to be in an 'except' clause,
-   otherwise return a false value.
-*/
-static int
-can_catch_exc(PyObject *exc)
-{
-       if (!(PyExceptionClass_Check(exc) || PyExceptionInstance_Check(exc))) {
-               PyErr_SetString(PyExc_TypeError,
-                               "catching an object must be a class or "
-                               "instance of BaseException");
-               return 0;
-       }
-       else {
-               return 1;
-       }
-}
+#define CANNOT_CATCH_MSG "catching classes that do not inherit from"\
+                        "BaseException is not allowed"
 
 static PyObject *
 cmp_outcome(int op, register PyObject *v, register PyObject *w)
@@ -3941,13 +3926,17 @@ cmp_outcome(int op, register PyObject *v, register PyObject *w)
                        length = PyTuple_Size(w);
                        for (i = 0; i < length; i += 1) {
                                PyObject *exc = PyTuple_GET_ITEM(w, i);
-                               if (!can_catch_exc(exc)) {
+                               if (!PyExceptionClass_Check(exc)) {
+                                       PyErr_SetString(PyExc_TypeError,
+                                                       CANNOT_CATCH_MSG);
                                        return NULL;
                                }
                        }
                }
                else {
-                       if (!can_catch_exc(w)) {
+                       if (!PyExceptionClass_Check(w)) {
+                               PyErr_SetString(PyExc_TypeError,
+                                               CANNOT_CATCH_MSG);
                                return NULL;
                        }
                }