From 87aa2e3c42d96790852436f8f108fc70bd87de9c Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Thu, 14 Nov 2019 19:08:53 +0000 Subject: [PATCH] catacomb-python.h, *.c: Fix how Python `pgen' handlers handle exceptions. Oh, this was a mess. The old code would convert an exception from a Python handler into `PGEN_ABORT', and hope that the exception state was still available when the overall operation ended. This doesn't work. In particular, steppers and testers are finalized by calling them with `PGEN_DONE', and the interpreter doesn't like re-entering Python with an exception set. (In debug builds, this is an assertion failure.) Overhaul all of this nonsense. * Add a collection of utilities for saving and restoring the exception state. * Add a hook, in the `catacomb' module, for reporting `lost' exceptions, for the case where further exceptions are raised while responding to a first exception. * Use a larger `pypgev' structure to track the state of a Python event handler through the framework. This structure holds a reference to the Python object itself, and a slot for recording an exception. * When a Python handler fails, stash the exception state in the slot provided by the `pypgev' structure if there isn't one already, and clear the pending exception. If there is already an exception in the slot, then report the new exception through the hook described above. * Once a `pgen' operation completes, if it raised any exceptions at all, then the first of these is left in the exception slot. If it fails otherwise, then we supply a generic exception. --- catacomb-python.h | 43 ++++++++++++++++++-- util.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+), 4 deletions(-) diff --git a/catacomb-python.h b/catacomb-python.h index 490862b..517ec0b 100644 --- a/catacomb-python.h +++ b/catacomb-python.h @@ -145,7 +145,7 @@ PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); \ goto end; \ } while (0) -#define PGENERR do { pgenerr(); goto end; } while (0) +#define PGENERR(exc) do { pgenerr(exc); goto end; } while (0) #define CONVFUNC(ty, cty, ext) \ int conv##ty(PyObject *o, void *p) \ @@ -171,7 +171,9 @@ } while (0) #define INITTYPE(ty, base) INITTYPE_META(ty, base, type) -#define INSERT(name, ob) do { \ +extern PyObject *home_module; + +#define INSERT(name, ob) do { \ PyObject *_o = (PyObject *)(ob); \ Py_INCREF(_o); \ PyModule_AddObject(mod, name, _o); \ @@ -255,7 +257,34 @@ extern PyObject *getulong(unsigned long); extern PyObject *getk64(kludge64); extern void *newtype(PyTypeObject *, const PyTypeObject *, const char *); +struct excinfo { PyObject *ty, *val, *tb; }; +#define EXCINFO_INIT { 0, 0, 0 } + extern PyObject *mkexc(PyObject *, PyObject *, const char *, PyMethodDef *); +#define INIT_EXCINFO(exc) do { \ + struct excinfo *_exc = (exc); _exc->ty = _exc->val = _exc->tb = 0; \ +} while (0) +#define RELEASE_EXCINFO(exc) do { \ + struct excinfo *_exc = (exc); \ + Py_XDECREF(_exc->ty); _exc->ty = 0; \ + Py_XDECREF(_exc->val); _exc->val = 0; \ + Py_XDECREF(_exc->tb); _exc->tb = 0; \ +} while (0) +#define STASH_EXCINFO(exc) do { \ + struct excinfo *_exc = (exc); \ + PyErr_Fetch(&_exc->ty, &_exc->val, &_exc->tb); \ + PyErr_NormalizeException(&_exc->ty, &_exc->val, &_exc->tb); \ +} while (0) +#define RESTORE_EXCINFO(exc) do { \ + struct excinfo *_exc = (exc); \ + PyErr_Restore(_exc->ty, _exc->val, _exc->tb); \ + _exc->ty = _exc->val = _exc->tb = 0; \ +} while (0) +extern void report_lost_exception(struct excinfo *, const char *, ...); +extern void report_lost_exception_v(struct excinfo *, const char *, va_list); +extern void stash_exception(struct excinfo *, const char *, ...); +extern void restore_exception(struct excinfo *, const char *, ...); + extern void typeready(PyTypeObject *); extern PyTypeObject *inittype(PyTypeObject *, PyTypeObject *); extern void addmethods(const PyMethodDef *); @@ -595,9 +624,15 @@ extern PyTypeObject *pgev_pytype; #define PGEV_PYCHECK(o) PyObject_TypeCheck(o, pgev_pytype) #define PGEV_PG(o) (&((pgev_pyobj *)(o))->pg) +typedef struct pypgev { + pgev ev; + PyObject *obj; + struct excinfo *exc; +} pypgev; + extern int convpgev(PyObject *, void *); -extern void droppgev(pgev *); -extern void pgenerr(void); +extern void droppgev(pypgev *); +extern void pgenerr(struct excinfo *exc); /*----- That's all, folks -------------------------------------------------*/ diff --git a/util.c b/util.c index 8a4b87e..5abd7c9 100644 --- a/util.c +++ b/util.c @@ -33,6 +33,7 @@ /*----- External values ---------------------------------------------------*/ static PyObject *modname = 0; +PyObject *home_module = 0; /*----- Conversions -------------------------------------------------------*/ @@ -356,6 +357,101 @@ fail: goto done; } +void report_lost_exception_v(struct excinfo *exc, + const char *why, va_list ap) +{ + PyObject *hookfn = 0; + PyObject *whyobj = 0; + PyObject *obj = 0; + + /* Make sure we start out without a pending exception, or this will get + * really confusing. + */ + assert(!PyErr_Occurred()); + + /* Format the explanation. */ + if (why) whyobj = PyString_FromFormatV(why, ap); + else { whyobj = Py_None; Py_INCREF(whyobj); } + + /* Find our home module's `lostexchook' function. This won't work if + * there's no module, or the function isn't defined, or it's `None'. + */ + if (!home_module) goto sys; + hookfn = PyObject_GetAttrString(home_module, "lostexchook"); + if (hookfn == Py_None) goto sys; + else if (hookfn) ; + else if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto ouch; + else { PyErr_Clear(); goto sys; } + + /* Call the hook function. */ + obj = PyObject_CallFunction(hookfn, "(OOOO)", + whyobj, exc->ty, exc->val, exc->tb); + if (!obj) goto ouch; + goto end; + + /* Something went wrong reporting the problem. */ +ouch: + PySys_WriteStderr("\n!!! FAILURE REPORTING LOST EXCEPTION\n"); + PyErr_Print(); + /* drop through... */ + + /* There was no hook, so try to do something sensible using + * `sys.excepthook'. + */ +sys: + PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n", + PyString_AS_STRING(whyobj)); + RESTORE_EXCINFO(exc); + PyErr_Print(); + /* drop through... */ + + /* Clean up afterwards. */ +end: + Py_XDECREF(hookfn); + Py_XDECREF(whyobj); + Py_XDECREF(obj); +} + +void report_lost_exception(struct excinfo *exc, const char *why, ...) +{ + va_list ap; + + va_start(ap, why); + report_lost_exception_v(exc, why, ap); + va_end(ap); +} + +void stash_exception(struct excinfo *exc, const char *why, ...) +{ + va_list ap; + struct excinfo stash; + + if (!exc->ty) + STASH_EXCINFO(exc); + else { + va_start(ap, why); + STASH_EXCINFO(&stash); + report_lost_exception_v(&stash, why, ap); + va_end(ap); + } +} + +void restore_exception(struct excinfo *exc, const char *why, ...) +{ + va_list ap; + struct excinfo stash; + + if (!PyErr_Occurred()) + RESTORE_EXCINFO(exc); + else { + va_start(ap, why); + STASH_EXCINFO(&stash); + report_lost_exception_v(exc, why, ap); + RESTORE_EXCINFO(&stash); + va_end(ap); + } +} + /*----- Generic dictionary methods ----------------------------------------*/ static PyTypeObject *itemiter_pytype, *valiter_pytype; @@ -729,11 +825,29 @@ PyMethodDef gmap_pymethods[] = { /*----- Initialization ----------------------------------------------------*/ +static PyObject *meth__set_home_module(PyObject *me, PyObject *arg) +{ + PyObject *mod; + + if (!PyArg_ParseTuple(arg, "O!:_set_home_module", &PyModule_Type, &mod)) + return (0); + Py_XDECREF(home_module); home_module = mod; Py_INCREF(home_module); + RETURN_NONE; +} + +static const PyMethodDef methods[] = { +#define METHNAME(func) meth_##func + METH (_set_home_module, "_set_home_module(MOD)") +#undef METHNAME + { 0 } +}; + void util_pyinit(void) { modname = PyString_FromString("catacomb"); INITTYPE(itemiter, root); INITTYPE(valiter, root); + addmethods(methods); } void util_pyinsert(PyObject *mod) -- 2.11.0