Issue25814: Propagate all errors from custom XML parser handlers

in ElementTree.iterparse().
This commit is contained in:
Serhiy Storchaka 2015-12-06 23:51:53 +02:00
parent 4902591cae
commit 45cf0b7f3e
1 changed files with 45 additions and 73 deletions

View File

@ -1720,6 +1720,23 @@ treebuilder_dealloc(TreeBuilderObject* self)
PyObject_Del(self);
}
LOCAL(int)
treebuilder_append_event(TreeBuilderObject *self, PyObject *action,
PyObject *node)
{
if (action != NULL) {
PyObject *res = PyTuple_Pack(2, action, node);
if (res == NULL)
return -1;
if (PyList_Append(self->events, res) < 0) {
Py_DECREF(res);
return -1;
}
Py_DECREF(res);
}
return 0;
}
/* -------------------------------------------------------------------- */
/* handlers */
@ -1791,18 +1808,8 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
Py_INCREF(node);
self->last = (ElementObject*) node;
if (self->start_event_obj) {
PyObject* res;
PyObject* action = self->start_event_obj;
res = PyTuple_New(2);
if (res) {
Py_INCREF(action); PyTuple_SET_ITEM(res, 0, (PyObject*) action);
Py_INCREF(node); PyTuple_SET_ITEM(res, 1, (PyObject*) node);
PyList_Append(self->events, res);
Py_DECREF(res);
} else
PyErr_Clear(); /* FIXME: propagate error */
}
if (treebuilder_append_event(self, self->start_event_obj, node) < 0)
goto error;
return node;
@ -1885,63 +1892,13 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
self->last = (ElementObject*) self->this;
self->this = (ElementObject*) item;
if (self->end_event_obj) {
PyObject* res;
PyObject* action = self->end_event_obj;
PyObject* node = (PyObject*) self->last;
res = PyTuple_New(2);
if (res) {
Py_INCREF(action); PyTuple_SET_ITEM(res, 0, (PyObject*) action);
Py_INCREF(node); PyTuple_SET_ITEM(res, 1, (PyObject*) node);
PyList_Append(self->events, res);
Py_DECREF(res);
} else
PyErr_Clear(); /* FIXME: propagate error */
}
if (treebuilder_append_event(self, self->end_event_obj, (PyObject*)self->last) < 0)
return NULL;
Py_INCREF(self->last);
return (PyObject*) self->last;
}
LOCAL(void)
treebuilder_handle_namespace(TreeBuilderObject* self, int start,
PyObject *prefix, PyObject *uri)
{
PyObject* res;
PyObject* action;
PyObject* parcel;
if (!self->events)
return;
if (start) {
if (!self->start_ns_event_obj)
return;
action = self->start_ns_event_obj;
parcel = Py_BuildValue("OO", prefix, uri);
if (!parcel)
return;
Py_INCREF(action);
} else {
if (!self->end_ns_event_obj)
return;
action = self->end_ns_event_obj;
Py_INCREF(action);
parcel = Py_None;
Py_INCREF(parcel);
}
res = PyTuple_New(2);
if (res) {
PyTuple_SET_ITEM(res, 0, action);
PyTuple_SET_ITEM(res, 1, parcel);
PyList_Append(self->events, res);
Py_DECREF(res);
} else
PyErr_Clear(); /* FIXME: propagate error */
}
/* -------------------------------------------------------------------- */
/* methods (in alphabetical order) */
@ -2367,8 +2324,16 @@ static void
expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix,
const XML_Char *uri)
{
PyObject* sprefix = NULL;
PyObject* suri = NULL;
TreeBuilderObject *target = (TreeBuilderObject*) self->target;
PyObject *parcel;
PyObject *sprefix = NULL;
PyObject *suri = NULL;
if (PyErr_Occurred())
return;
if (!target->events || !target->start_ns_event_obj)
return;
if (uri)
suri = makestring(uri, strlen(uri));
@ -2386,20 +2351,27 @@ expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix,
return;
}
treebuilder_handle_namespace(
(TreeBuilderObject*) self->target, 1, sprefix, suri
);
parcel = PyTuple_Pack(2, sprefix, suri);
Py_DECREF(sprefix);
Py_DECREF(suri);
if (!parcel)
return;
treebuilder_append_event(target, target->start_ns_event_obj, parcel);
Py_DECREF(parcel);
}
static void
expat_end_ns_handler(XMLParserObject* self, const XML_Char* prefix_in)
{
treebuilder_handle_namespace(
(TreeBuilderObject*) self->target, 0, NULL, NULL
);
TreeBuilderObject *target = (TreeBuilderObject*) self->target;
if (PyErr_Occurred())
return;
if (!target->events)
return;
treebuilder_append_event(target, target->end_ns_event_obj, Py_None);
}
static void