[Python-3000-checkins] r45589 - in python/branches/p3yk: Lib/test/test_exceptions.py Python/errors.c

thomas.wouters python-3000-checkins at python.org
Fri Apr 21 00:42:38 CEST 2006


Author: thomas.wouters
Date: Fri Apr 21 00:42:37 2006
New Revision: 45589

Modified:
   python/branches/p3yk/Lib/test/test_exceptions.py
   python/branches/p3yk/Python/errors.c
Log:

Fix (and add test for) missing check for BaseException subclasses in the C
API.



Modified: python/branches/p3yk/Lib/test/test_exceptions.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_exceptions.py	(original)
+++ python/branches/p3yk/Lib/test/test_exceptions.py	Fri Apr 21 00:42:37 2006
@@ -171,10 +171,15 @@
 # test that setting an exception at the C level works even if the
 # exception object can't be constructed.
 
-class BadException:
+class BadException(Exception):
     def __init__(self):
         raise RuntimeError, "can't instantiate BadException"
 
+# Exceptions must inherit from BaseException, raising invalid exception
+# should instead raise SystemError
+class InvalidException:
+    pass
+
 def test_capi1():
     import _testcapi
     try:
@@ -201,8 +206,21 @@
     else:
         print "Expected exception"
 
+def test_capi3():
+    import _testcapi
+    try:
+        _testcapi.raise_exception(InvalidException, 1)
+    except SystemError:
+        pass
+    except InvalidException:
+        raise AssertionError("Managed to raise InvalidException");
+    else:
+        print "Expected SystemError exception"
+    
+
 if not sys.platform.startswith('java'):
     test_capi1()
     test_capi2()
+    test_capi3()
 
 unlink(TESTFN)

Modified: python/branches/p3yk/Python/errors.c
==============================================================================
--- python/branches/p3yk/Python/errors.c	(original)
+++ python/branches/p3yk/Python/errors.c	Fri Apr 21 00:42:37 2006
@@ -47,6 +47,15 @@
 void
 PyErr_SetObject(PyObject *exception, PyObject *value)
 {
+	if (exception != NULL &&
+	    !PyExceptionClass_Check(exception)) {
+		PyObject *excstr = PyObject_Repr(exception);
+		PyErr_Format(PyExc_SystemError,
+			     "exception %s not a BaseException subclass",
+			     PyString_AS_STRING(excstr));
+		Py_DECREF(excstr);
+		return;
+	}
 	Py_XINCREF(exception);
 	Py_XINCREF(value);
 	PyErr_Restore(exception, value, (PyObject *)NULL);


More information about the Python-3000-checkins mailing list