Skip to content

Commit

Permalink
Store state in XMLParserObject context
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed Jan 21, 2023
1 parent b6962cd commit b2698e2
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2371,7 +2371,7 @@ _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self,
}

if (pi_factory == Py_None) {
elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
pi_factory = st->pi_factory;
}
if (pi_factory) {
Expand Down Expand Up @@ -3002,6 +3002,7 @@ typedef struct {

PyObject *handle_close;

elementtreestate *state;
} XMLParserObject;

/* helpers */
Expand Down Expand Up @@ -3145,7 +3146,7 @@ expat_default_handler(XMLParserObject* self, const XML_Char* data_in,

value = PyDict_GetItemWithError(self->entity, key);

elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
if (value) {
if (TreeBuilder_CheckExact(st, self->target))
res = treebuilder_handle_data(
Expand Down Expand Up @@ -3220,7 +3221,7 @@ expat_start_handler(XMLParserObject* self, const XML_Char* tag_in,
attrib = NULL;
}

elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
if (TreeBuilder_CheckExact(st, self->target)) {
/* shortcut */
res = treebuilder_handle_start((TreeBuilderObject*) self->target,
Expand Down Expand Up @@ -3259,7 +3260,7 @@ expat_data_handler(XMLParserObject* self, const XML_Char* data_in,
if (!data)
return; /* parser will look for errors */

elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
if (TreeBuilder_CheckExact(st, self->target))
/* shortcut */
res = treebuilder_handle_data((TreeBuilderObject*) self->target, data);
Expand All @@ -3282,7 +3283,7 @@ expat_end_handler(XMLParserObject* self, const XML_Char* tag_in)
if (PyErr_Occurred())
return;

elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
if (TreeBuilder_CheckExact(st, self->target))
/* shortcut */
/* the standard tree builder doesn't look at the end tag */
Expand Down Expand Up @@ -3317,7 +3318,7 @@ expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix_in,
if (!prefix_in)
prefix_in = "";

elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
if (TreeBuilder_CheckExact(st, self->target)) {
/* shortcut - TreeBuilder does not actually implement .start_ns() */
TreeBuilderObject *target = (TreeBuilderObject*) self->target;
Expand Down Expand Up @@ -3368,7 +3369,7 @@ expat_end_ns_handler(XMLParserObject* self, const XML_Char* prefix_in)
if (!prefix_in)
prefix_in = "";

elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
if (TreeBuilder_CheckExact(st, self->target)) {
/* shortcut - TreeBuilder does not actually implement .end_ns() */
TreeBuilderObject *target = (TreeBuilderObject*) self->target;
Expand Down Expand Up @@ -3397,7 +3398,7 @@ expat_comment_handler(XMLParserObject* self, const XML_Char* comment_in)
if (PyErr_Occurred())
return;

elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
if (TreeBuilder_CheckExact(st, self->target)) {
/* shortcut */
TreeBuilderObject *target = (TreeBuilderObject*) self->target;
Expand Down Expand Up @@ -3458,7 +3459,7 @@ expat_start_doctype_handler(XMLParserObject *self,
pubid_obj = Py_NewRef(Py_None);
}

elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
/* If the target has a handler for doctype, call it. */
if (self->handle_doctype) {
res = PyObject_CallFunctionObjArgs(self->handle_doctype,
Expand Down Expand Up @@ -3491,7 +3492,7 @@ expat_pi_handler(XMLParserObject* self, const XML_Char* target_in,
if (PyErr_Occurred())
return;

elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
if (TreeBuilder_CheckExact(st, self->target)) {
/* shortcut */
TreeBuilderObject *target = (TreeBuilderObject*) self->target;
Expand Down Expand Up @@ -3544,6 +3545,7 @@ xmlparser_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self->handle_start = self->handle_data = self->handle_end = NULL;
self->handle_comment = self->handle_pi = self->handle_close = NULL;
self->handle_doctype = NULL;
self->state = ET_STATE_GLOBAL;
}
return (PyObject *)self;
}
Expand Down Expand Up @@ -3600,7 +3602,7 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target,
if (target != Py_None) {
Py_INCREF(target);
} else {
elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
target = treebuilder_new(st->TreeBuilder_Type, NULL, NULL);
if (!target) {
Py_CLEAR(self->entity);
Expand Down Expand Up @@ -3770,7 +3772,7 @@ expat_parse(XMLParserObject* self, const char* data, int data_len, int final)
return NULL;

if (!ok) {
elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
expat_set_error(
st,
EXPAT(GetErrorCode)(self->parser),
Expand Down Expand Up @@ -3804,7 +3806,7 @@ _elementtree_XMLParser_close_impl(XMLParserObject *self)
if (!res)
return NULL;

elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
if (TreeBuilder_CheckExact(st, self->target)) {
Py_DECREF(res);
return treebuilder_done((TreeBuilderObject*) self->target);
Expand Down Expand Up @@ -3944,7 +3946,7 @@ _elementtree_XMLParser__parse_whole(XMLParserObject *self, PyObject *file)

res = expat_parse(self, "", 0, 1);

elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
if (res && TreeBuilder_CheckExact(st, self->target)) {
Py_DECREF(res);
return treebuilder_done((TreeBuilderObject*) self->target);
Expand Down Expand Up @@ -3976,7 +3978,7 @@ _elementtree_XMLParser__setevents_impl(XMLParserObject *self,
if (!_check_xmlparser(self)) {
return NULL;
}
elementtreestate *st = ET_STATE_GLOBAL;
elementtreestate *st = self->state;
if (!TreeBuilder_CheckExact(st, self->target)) {
PyErr_SetString(
PyExc_TypeError,
Expand Down

0 comments on commit b2698e2

Please sign in to comment.