Commit d1bf8a92 authored by jack's avatar jack

Handle exceptions in callback routines through longjmp, in stead of

silently ignoring them.
parent bc8351d0
...@@ -31,11 +31,12 @@ PERFORMANCE OF THIS SOFTWARE. ...@@ -31,11 +31,12 @@ PERFORMANCE OF THIS SOFTWARE.
#include "Python.h" #include "Python.h"
#include "xmlparse.h" #include "xmlparse.h"
#include <setjmp.h>
/* /*
** The version number should match the one in _checkversion ** The version number should match the one in _checkversion
*/ */
#define VERSION "1.2" #define VERSION "1.3"
static PyObject *ErrorObject; static PyObject *ErrorObject;
...@@ -50,6 +51,8 @@ typedef struct { ...@@ -50,6 +51,8 @@ typedef struct {
PyObject *EndElementHandler; PyObject *EndElementHandler;
PyObject *CharacterDataHandler; PyObject *CharacterDataHandler;
PyObject *ProcessingInstructionHandler; PyObject *ProcessingInstructionHandler;
int jmpbuf_valid;
jmp_buf jmpbuf;
} xmlparseobject; } xmlparseobject;
staticforward PyTypeObject Xmlparsetype; staticforward PyTypeObject Xmlparsetype;
...@@ -87,6 +90,12 @@ my_StartElementHandler(userdata, name, atts) ...@@ -87,6 +90,12 @@ my_StartElementHandler(userdata, name, atts)
if (!args) return; if (!args) return;
rv = PyEval_CallObject(self->StartElementHandler, args); rv = PyEval_CallObject(self->StartElementHandler, args);
Py_XDECREF(args); Py_XDECREF(args);
if (rv == NULL) {
if (self->jmpbuf_valid)
longjmp(self->jmpbuf, 1);
PySys_WriteStderr("Exception in StartElementHandler()\n");
PyErr_Clear();
}
Py_XDECREF(rv); Py_XDECREF(rv);
} }
} }
...@@ -106,6 +115,12 @@ my_EndElementHandler(userdata, name) ...@@ -106,6 +115,12 @@ my_EndElementHandler(userdata, name)
if (!args) return; if (!args) return;
rv = PyEval_CallObject(self->EndElementHandler, args); rv = PyEval_CallObject(self->EndElementHandler, args);
Py_XDECREF(args); Py_XDECREF(args);
if (rv == NULL) {
if (self->jmpbuf_valid)
longjmp(self->jmpbuf, 1);
PySys_WriteStderr("Exception in EndElementHandler()\n");
PyErr_Clear();
}
Py_XDECREF(rv); Py_XDECREF(rv);
} }
} }
...@@ -127,6 +142,12 @@ my_CharacterDataHandler(userdata, data, len) ...@@ -127,6 +142,12 @@ my_CharacterDataHandler(userdata, data, len)
if (!args) return; if (!args) return;
rv = PyEval_CallObject(self->CharacterDataHandler, args); rv = PyEval_CallObject(self->CharacterDataHandler, args);
Py_XDECREF(args); Py_XDECREF(args);
if (rv == NULL) {
if (self->jmpbuf_valid)
longjmp(self->jmpbuf, 1);
PySys_WriteStderr("Exception in CharacterDataHandler()\n");
PyErr_Clear();
}
Py_XDECREF(rv); Py_XDECREF(rv);
} }
} }
...@@ -148,6 +169,12 @@ my_ProcessingInstructionHandler(userdata, target, data) ...@@ -148,6 +169,12 @@ my_ProcessingInstructionHandler(userdata, target, data)
rv = PyEval_CallObject(self->ProcessingInstructionHandler, rv = PyEval_CallObject(self->ProcessingInstructionHandler,
args); args);
Py_XDECREF(args); Py_XDECREF(args);
if (rv == NULL) {
if (self->jmpbuf_valid)
longjmp(self->jmpbuf, 1);
PySys_WriteStderr("Exception in ProcessingInstructionHandler()\n");
PyErr_Clear();
}
Py_XDECREF(rv); Py_XDECREF(rv);
} }
} }
...@@ -171,7 +198,13 @@ xmlparse_Parse(self, args) ...@@ -171,7 +198,13 @@ xmlparse_Parse(self, args)
if (!PyArg_ParseTuple(args, "s#|i", &s, &slen, &isFinal)) if (!PyArg_ParseTuple(args, "s#|i", &s, &slen, &isFinal))
return NULL; return NULL;
if (setjmp(self->jmpbuf)) {
/* Error in callback routine */
return NULL;
}
self->jmpbuf_valid = 1;
rv = XML_Parse(self->itself, s, slen, isFinal); rv = XML_Parse(self->itself, s, slen, isFinal);
self->jmpbuf_valid = 0;
return Py_BuildValue("i", rv); return Py_BuildValue("i", rv);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment