Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-102594: test_fetch_exception_with_broken_init @ 3.12.0a3 #102606

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,20 @@ def test_capi3():
self.assertRaises(SystemError, _testcapi.raise_exception,
InvalidException, 1)

def test_capi4():
import _testcapi
class FlakyException(Exception):
def __init__(self):
raise ValueError("Broken __init__")

fetched_type_name = _testcapi.exc_set_object_fetch(FlakyException, ())
self.assertEqual(fetched_type_name, 'FlakyException')

if not sys.platform.startswith('java'):
test_capi1()
test_capi2()
test_capi3()
test_capi4()

def test_WindowsError(self):
try:
Expand Down
30 changes: 30 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,35 @@ pyobject_bytes_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
return PyObject_Bytes(NULL);
}

static PyObject *
exc_set_object_fetch(PyObject *self, PyObject *args)
{
PyObject *exc;
PyObject *obj;
PyObject *type;
PyObject *value;
PyObject *tb;

if (!PyArg_ParseTuple(args, "OO:exc_set_object", &exc, &obj)) {
return NULL;
}

PyErr_SetObject(exc, obj);
PyErr_Fetch(&type, &value, &tb);
Py_XDECREF(value);
Py_XDECREF(tb);
if (!PyType_Check(type)) {
Py_XDECREF(type);
PyErr_SetString(PyExc_RuntimeError,
"PyErr_Fetch() produced invalid type");
return NULL;
}
PyObject *fetched_type_name = PyUnicode_FromString(
((PyTypeObject *) type)->tp_name);
Py_XDECREF(type);
return fetched_type_name;
}

static PyObject *
raise_exception(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -3141,6 +3170,7 @@ function_set_kw_defaults(PyObject *self, PyObject *args)
static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);

static PyMethodDef TestMethods[] = {
{"exc_set_object_fetch", exc_set_object_fetch, METH_VARARGS},
{"raise_exception", raise_exception, METH_VARARGS},
{"raise_memoryerror", raise_memoryerror, METH_NOARGS},
{"set_errno", set_errno, METH_VARARGS},
Expand Down