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 20, 2023
1 parent 44c5447 commit b1127be
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 @@ -2369,7 +2369,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 @@ -3000,6 +3000,7 @@ typedef struct {

PyObject *handle_close;

elementtreestate *state;
} XMLParserObject;

/* helpers */
Expand Down Expand Up @@ -3143,7 +3144,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 @@ -3218,7 +3219,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 @@ -3257,7 +3258,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 @@ -3280,7 +3281,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 @@ -3315,7 +3316,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 @@ -3366,7 +3367,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 @@ -3395,7 +3396,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 @@ -3456,7 +3457,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 @@ -3489,7 +3490,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 @@ -3542,6 +3543,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 @@ -3598,7 +3600,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 @@ -3768,7 +3770,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 @@ -3802,7 +3804,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 @@ -3942,7 +3944,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 @@ -3974,7 +3976,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 b1127be

Please sign in to comment.