FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-102594: test_fetch_exception_with_broken_init @ main HEAD by rwgk · Pull Request #102607 · python/cpython · GitHub

Repository navigation

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

Filter by extension

Filter by extension .c  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 9 additions & 0 deletions Lib/test/test_capi/test_exceptions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,14 @@ class Broken(Exception, metaclass=Meta):
with self.assertRaises(ZeroDivisionError) as e:
_testcapi.exc_set_object(Broken, Broken())

def test_fetch_exception_with_broken_init(self):

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 __name__ == "__main__":
unittest.main()
30 changes: 30 additions & 0 deletions Modules/_testcapi/exceptions.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,35 @@ exc_set_object(PyObject *self, PyObject *args)
return 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 @@ -262,6 +291,7 @@ static PyMethodDef test_methods[] = {
{"make_exception_with_doc", _PyCFunction_CAST(make_exception_with_doc),
METH_VARARGS | METH_KEYWORDS},
{"exc_set_object", exc_set_object, METH_VARARGS},
{"exc_set_object_fetch", exc_set_object_fetch, METH_VARARGS},
{"raise_exception", raise_exception, METH_VARARGS},
{"raise_memoryerror", raise_memoryerror, METH_NOARGS},
{"set_exc_info", test_set_exc_info, METH_VARARGS},
Expand Down

Back | FazBrowse Home | New Git URL