]> granicus.if.org Git - python/commitdiff
- Issue #2371: Add a Py3k warning when catching an exception that
authorGuido van Rossum <guido@python.org>
Tue, 18 Mar 2008 02:49:46 +0000 (02:49 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 18 Mar 2008 02:49:46 +0000 (02:49 +0000)
  doesn't derive from BaseException.

Misc/ACKS
Misc/NEWS
Python/ceval.c

index cce61522cf17030828df668dee23b829e7e6b3f8..86ff1ea1a3d9f694c504f163f602901567c479c9 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -360,6 +360,7 @@ Magnus Kessler
 Lawrence Kesteloot
 Vivek Khera
 Mads Kiilerich
+Taek Joo Kim
 Steve Kirsch
 Ron Klatchko
 Bastian Kleineidam
index 03597a5bb1f98ace75d1868208a1ebd3153ea32e..cb8449175d36450b60819eb7e92de2b7aa8c089c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 2?
 Core and builtins
 -----------------
 
+- Issue #2371: Add a Py3k warning when catching an exception that
+  doesn't derive from BaseException.
+
 - Issue #2321: use pymalloc for unicode object string data to reduce
   memory usage in some circumstances.
 
index 4fc1709d110fc3167bf7478e6f13fd9ed372419a..72da26399207e491c39ca58e483e3b97f07b6c2a 100644 (file)
@@ -4042,6 +4042,13 @@ assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
        }
 }
 
+#define Py3kExceptionClass_Check(x)     \
+    (PyType_Check((x)) &&               \
+     PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
+
+#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
+                        "BaseException is not allowed in 3.x."
+
 static PyObject *
 cmp_outcome(int op, register PyObject *v, register PyObject *w)
 {
@@ -4079,6 +4086,16 @@ cmp_outcome(int op, register PyObject *v, register PyObject *w)
                                        if (ret_val == -1)
                                                return NULL;
                                }
+                               if (Py_Py3kWarningFlag  &&
+                                   !Py3kExceptionClass_Check(exc))
+                               {
+                                       int ret_val;
+                                       ret_val = PyErr_WarnEx(
+                                               PyExc_DeprecationWarning,
+                                               CANNOT_CATCH_MSG, 1);
+                                       if (ret_val == -1)
+                                               return NULL;
+                               }
                        }
                }
                else {
@@ -4091,6 +4108,16 @@ cmp_outcome(int op, register PyObject *v, register PyObject *w)
                                if (ret_val == -1)
                                        return NULL;
                        }
+                       if (Py_Py3kWarningFlag  &&
+                           !Py3kExceptionClass_Check(w))
+                       {
+                               int ret_val;
+                               ret_val = PyErr_WarnEx(
+                                       PyExc_DeprecationWarning,
+                                       CANNOT_CATCH_MSG, 1);
+                               if (ret_val == -1)
+                                       return NULL;
+                       }
                }
                res = PyErr_GivenExceptionMatches(v, w);
                break;