mirror of
https://github.com/python/cpython.git
synced 2025-10-07 23:51:16 +00:00
Fix (and add test for) missing check for BaseException subclasses in the C
API.
This commit is contained in:
parent
4f564bd68a
commit
303de6a25b
2 changed files with 28 additions and 1 deletions
|
@ -171,10 +171,15 @@ except Exception, e: pass
|
||||||
# test that setting an exception at the C level works even if the
|
# test that setting an exception at the C level works even if the
|
||||||
# exception object can't be constructed.
|
# exception object can't be constructed.
|
||||||
|
|
||||||
class BadException:
|
class BadException(Exception):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
raise RuntimeError, "can't instantiate BadException"
|
raise RuntimeError, "can't instantiate BadException"
|
||||||
|
|
||||||
|
# Exceptions must inherit from BaseException, raising invalid exception
|
||||||
|
# should instead raise SystemError
|
||||||
|
class InvalidException:
|
||||||
|
pass
|
||||||
|
|
||||||
def test_capi1():
|
def test_capi1():
|
||||||
import _testcapi
|
import _testcapi
|
||||||
try:
|
try:
|
||||||
|
@ -201,8 +206,21 @@ def test_capi2():
|
||||||
else:
|
else:
|
||||||
print "Expected exception"
|
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'):
|
if not sys.platform.startswith('java'):
|
||||||
test_capi1()
|
test_capi1()
|
||||||
test_capi2()
|
test_capi2()
|
||||||
|
test_capi3()
|
||||||
|
|
||||||
unlink(TESTFN)
|
unlink(TESTFN)
|
||||||
|
|
|
@ -47,6 +47,15 @@ PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
|
||||||
void
|
void
|
||||||
PyErr_SetObject(PyObject *exception, PyObject *value)
|
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(exception);
|
||||||
Py_XINCREF(value);
|
Py_XINCREF(value);
|
||||||
PyErr_Restore(exception, value, (PyObject *)NULL);
|
PyErr_Restore(exception, value, (PyObject *)NULL);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue