From 636c7fa2aee7cc592deb467739106e26b5fe6db8 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 20 Jan 2023 13:04:25 +0100 Subject: [PATCH 01/11] Refactor: replace query with parameter Pass state as arg to create_new_element() --- Modules/_elementtree.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 8c94cf0161061c..20fa36d4fa2be1 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -297,11 +297,10 @@ clear_extra(ElementObject* self) * tag and attributes. */ LOCAL(PyObject*) -create_new_element(PyObject* tag, PyObject* attrib) +create_new_element(elementtreestate *st, PyObject *tag, PyObject *attrib) { ElementObject* self; - elementtreestate *st = ET_STATE_GLOBAL; self = PyObject_GC_New(ElementObject, st->Element_Type); if (self == NULL) return NULL; @@ -614,7 +613,7 @@ subelement(PyObject *self, PyObject *args, PyObject *kwds) /* no attrib arg, no kwds, so no attribute */ } - elem = create_new_element(tag, attrib); + elem = create_new_element(st, tag, attrib); Py_XDECREF(attrib); if (elem == NULL) return NULL; @@ -728,9 +727,10 @@ _elementtree_Element___copy___impl(ElementObject *self) { Py_ssize_t i; ElementObject* element; + elementtreestate *st = ET_STATE_GLOBAL; element = (ElementObject*) create_new_element( - self->tag, self->extra ? self->extra->attrib : NULL); + st, self->tag, self->extra ? self->extra->attrib : NULL); if (!element) return NULL; @@ -795,7 +795,8 @@ _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo) attrib = NULL; } - element = (ElementObject*) create_new_element(tag, attrib); + elementtreestate *st = ET_STATE_GLOBAL; + element = (ElementObject*) create_new_element(st, tag, attrib); Py_DECREF(tag); Py_XDECREF(attrib); @@ -818,7 +819,6 @@ _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo) if (element_resize(element, self->extra->length) < 0) goto error; - elementtreestate *st = ET_STATE_GLOBAL; for (i = 0; i < self->extra->length; i++) { PyObject* child = deepcopy(self->extra->children[i], memo); if (!child || !Element_Check(st, child)) { @@ -1567,7 +1567,8 @@ _elementtree_Element_makeelement_impl(ElementObject *self, PyObject *tag, if (!attrib) return NULL; - elem = create_new_element(tag, attrib); + elementtreestate *st = ET_STATE_GLOBAL; + elem = create_new_element(st, tag, attrib); Py_DECREF(attrib); @@ -2633,7 +2634,7 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag, } if (!self->element_factory) { - node = create_new_element(tag, attrib); + node = create_new_element(st, tag, attrib); } else if (attrib == NULL) { attrib = PyDict_New(); if (!attrib) From 065bcb67c26130b826375ce1c25084800ce7f740 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 20 Jan 2023 13:36:29 +0100 Subject: [PATCH 02/11] Refactor: replace query with parameter Pass state as arg to deepcopy() --- Modules/_elementtree.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 20fa36d4fa2be1..cddeb75ea7b934 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -759,7 +759,7 @@ _elementtree_Element___copy___impl(ElementObject *self) } /* Helper for a deep copy. */ -LOCAL(PyObject *) deepcopy(PyObject *, PyObject *); +LOCAL(PyObject *) deepcopy(elementtreestate *, PyObject *, PyObject *); /*[clinic input] _elementtree.Element.__deepcopy__ @@ -781,12 +781,13 @@ _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo) PyObject* tail; PyObject* id; - tag = deepcopy(self->tag, memo); + elementtreestate *st = ET_STATE_GLOBAL; + tag = deepcopy(st, self->tag, memo); if (!tag) return NULL; if (self->extra && self->extra->attrib) { - attrib = deepcopy(self->extra->attrib, memo); + attrib = deepcopy(st, self->extra->attrib, memo); if (!attrib) { Py_DECREF(tag); return NULL; @@ -795,7 +796,6 @@ _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo) attrib = NULL; } - elementtreestate *st = ET_STATE_GLOBAL; element = (ElementObject*) create_new_element(st, tag, attrib); Py_DECREF(tag); @@ -804,12 +804,12 @@ _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo) if (!element) return NULL; - text = deepcopy(JOIN_OBJ(self->text), memo); + text = deepcopy(st, JOIN_OBJ(self->text), memo); if (!text) goto error; _set_joined_ptr(&element->text, JOIN_SET(text, JOIN_GET(self->text))); - tail = deepcopy(JOIN_OBJ(self->tail), memo); + tail = deepcopy(st, JOIN_OBJ(self->tail), memo); if (!tail) goto error; _set_joined_ptr(&element->tail, JOIN_SET(tail, JOIN_GET(self->tail))); @@ -820,7 +820,7 @@ _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo) goto error; for (i = 0; i < self->extra->length; i++) { - PyObject* child = deepcopy(self->extra->children[i], memo); + PyObject* child = deepcopy(st, self->extra->children[i], memo); if (!child || !Element_Check(st, child)) { if (child) { raise_type_error(child); @@ -856,10 +856,9 @@ _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo) } LOCAL(PyObject *) -deepcopy(PyObject *object, PyObject *memo) +deepcopy(elementtreestate *st, PyObject *object, PyObject *memo) { /* do a deep copy of the given object */ - elementtreestate *st = ET_STATE_GLOBAL; PyObject *stack[2]; /* Fast paths */ From de344e1bac807c130c122cbb302c58565392d742 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 20 Jan 2023 13:48:38 +0100 Subject: [PATCH 03/11] Refactor: replace query with parameter Pass state as arg to expat_set_error() --- Modules/_elementtree.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index cddeb75ea7b934..48ef07b6539686 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -3091,11 +3091,10 @@ makeuniversal(XMLParserObject* self, const char* string) * message string is the default for the given error_code. */ static void -expat_set_error(enum XML_Error error_code, Py_ssize_t line, Py_ssize_t column, - const char *message) +expat_set_error(elementtreestate *st, enum XML_Error error_code, + Py_ssize_t line, Py_ssize_t column, const char *message) { PyObject *errmsg, *error, *position, *code; - elementtreestate *st = ET_STATE_GLOBAL; errmsg = PyUnicode_FromFormat("%s: line %zd, column %zd", message ? message : EXPAT(ErrorString)(error_code), @@ -3160,8 +3159,8 @@ expat_default_handler(XMLParserObject* self, const XML_Char* data_in, value = PyDict_GetItemWithError(self->entity, key); + elementtreestate *st = self->state; if (value) { - elementtreestate *st = self->state; if (TreeBuilder_CheckExact(st, self->target)) res = treebuilder_handle_data( (TreeBuilderObject*) self->target, value @@ -3176,6 +3175,7 @@ expat_default_handler(XMLParserObject* self, const XML_Char* data_in, char message[128] = "undefined entity "; strncat(message, data_in, data_len < 100?data_len:100); expat_set_error( + st, XML_ERROR_UNDEFINED_ENTITY, EXPAT(GetErrorLineNumber)(self->parser), EXPAT(GetErrorColumnNumber)(self->parser), @@ -3785,7 +3785,9 @@ expat_parse(XMLParserObject* self, const char* data, int data_len, int final) return NULL; if (!ok) { + elementtreestate *st = ET_STATE_GLOBAL; expat_set_error( + st, EXPAT(GetErrorCode)(self->parser), EXPAT(GetErrorLineNumber)(self->parser), EXPAT(GetErrorColumnNumber)(self->parser), From a85276e9bcf31f025bb920b73192b5ef8fce1631 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 20 Jan 2023 13:38:44 +0100 Subject: [PATCH 04/11] Refactor: replace query with parameter Pass state as arg to element_setstate_from_attributes() --- Modules/_elementtree.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 48ef07b6539686..ee840dbf48e982 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -973,7 +973,8 @@ _elementtree_Element___getstate___impl(ElementObject *self) } static PyObject * -element_setstate_from_attributes(ElementObject *self, +element_setstate_from_attributes(elementtreestate *st, + ElementObject *self, PyObject *tag, PyObject *attrib, PyObject *text, @@ -1031,7 +1032,6 @@ element_setstate_from_attributes(ElementObject *self, } /* Copy children */ - elementtreestate *st = ET_STATE_GLOBAL; for (i = 0; i < nchildren; i++) { PyObject *child = PyList_GET_ITEM(children, i); if (!Element_Check(st, child)) { @@ -1077,9 +1077,10 @@ element_setstate_from_Python(ElementObject *self, PyObject *state) if (!args) return NULL; + elementtreestate *st = ET_STATE_GLOBAL; if (PyArg_ParseTupleAndKeywords(args, state, "|$OOOOO", kwlist, &tag, &attrib, &text, &tail, &children)) - retval = element_setstate_from_attributes(self, tag, attrib, text, + retval = element_setstate_from_attributes(st, self, tag, attrib, text, tail, children); else retval = NULL; From ac623de963ff45fb580764a534b8aae3c1a0d511 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 20 Jan 2023 13:40:59 +0100 Subject: [PATCH 05/11] Refactor: replace query with parameter Pass state as arg to create_elementiter() --- Modules/_elementtree.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index ee840dbf48e982..6d4f0ad0b15c16 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -1391,7 +1391,8 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key, } static PyObject * -create_elementiter(ElementObject *self, PyObject *tag, int gettext); +create_elementiter(elementtreestate *st, ElementObject *self, PyObject *tag, + int gettext); /*[clinic input] @@ -1416,7 +1417,8 @@ _elementtree_Element_iter_impl(ElementObject *self, PyObject *tag) tag = Py_None; } - return create_elementiter(self, tag, 0); + elementtreestate *st = ET_STATE_GLOBAL; + return create_elementiter(st, self, tag, 0); } @@ -1429,7 +1431,8 @@ static PyObject * _elementtree_Element_itertext_impl(ElementObject *self) /*[clinic end generated code: output=5fa34b2fbcb65df6 input=af8f0e42cb239c89]*/ { - return create_elementiter(self, Py_None, 1); + elementtreestate *st = ET_STATE_GLOBAL; + return create_elementiter(st, self, Py_None, 1); } @@ -2247,11 +2250,11 @@ static PyType_Spec elementiter_spec = { #define INIT_PARENT_STACK_SIZE 8 static PyObject * -create_elementiter(ElementObject *self, PyObject *tag, int gettext) +create_elementiter(elementtreestate *st, ElementObject *self, PyObject *tag, + int gettext) { ElementIterObject *it; - elementtreestate *st = ET_STATE_GLOBAL; it = PyObject_GC_New(ElementIterObject, st->ElementIter_Type); if (!it) return NULL; From b7827db801c7f3406d36ec135bb801edac8ef31e Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 20 Jan 2023 13:42:11 +0100 Subject: [PATCH 06/11] Refactor: replace query with parameter Pass state as arg to treebuilder_extend_element_text_or_tail() --- Modules/_elementtree.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 6d4f0ad0b15c16..8e62bca925ad7a 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -2510,11 +2510,11 @@ _elementtree__set_factories_impl(PyObject *module, PyObject *comment_factory, } static int -treebuilder_extend_element_text_or_tail(PyObject *element, PyObject **data, - PyObject **dest, PyObject *name) +treebuilder_extend_element_text_or_tail(elementtreestate *st, PyObject *element, + PyObject **data, PyObject **dest, + PyObject *name) { /* Fast paths for the "almost always" cases. */ - elementtreestate *st = ET_STATE_GLOBAL; if (Element_CheckExact(st, element)) { PyObject *dest_obj = JOIN_OBJ(*dest); if (dest_obj == Py_None) { @@ -2574,13 +2574,13 @@ treebuilder_flush_data(TreeBuilderObject* self) if (!self->last_for_tail) { PyObject *element = self->last; return treebuilder_extend_element_text_or_tail( - element, &self->data, + st, element, &self->data, &((ElementObject *) element)->text, st->str_text); } else { PyObject *element = self->last_for_tail; return treebuilder_extend_element_text_or_tail( - element, &self->data, + st, element, &self->data, &((ElementObject *) element)->tail, st->str_tail); } } From 80431124e4f3610556ed6604fe2ab82f527da466 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 20 Jan 2023 13:52:51 +0100 Subject: [PATCH 07/11] Refactor: replace query with parameter Pass state as arg to treebuilder_add_subelement() --- Modules/_elementtree.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 8e62bca925ad7a..cdcbad2f89ab9d 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -2586,9 +2586,9 @@ treebuilder_flush_data(TreeBuilderObject* self) } static int -treebuilder_add_subelement(PyObject *element, PyObject *child) +treebuilder_add_subelement(elementtreestate *st, PyObject *element, + PyObject *child) { - elementtreestate *st = ET_STATE_GLOBAL; if (Element_CheckExact(st, element)) { ElementObject *elem = (ElementObject *) element; return element_add_subelement(elem, child); @@ -2658,7 +2658,7 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag, Py_CLEAR(self->last_for_tail); if (this != Py_None) { - if (treebuilder_add_subelement(this, node) < 0) + if (treebuilder_add_subelement(st, this, node) < 0) goto error; } else { if (self->root) { @@ -2777,8 +2777,9 @@ treebuilder_handle_comment(TreeBuilderObject* self, PyObject* text) return NULL; this = self->this; + elementtreestate *st = ET_STATE_GLOBAL; if (self->insert_comments && this != Py_None) { - if (treebuilder_add_subelement(this, comment) < 0) + if (treebuilder_add_subelement(st, this, comment) < 0) goto error; Py_XSETREF(self->last_for_tail, Py_NewRef(comment)); } @@ -2816,8 +2817,9 @@ treebuilder_handle_pi(TreeBuilderObject* self, PyObject* target, PyObject* text) } this = self->this; + elementtreestate *st = ET_STATE_GLOBAL; if (self->insert_pis && this != Py_None) { - if (treebuilder_add_subelement(this, pi) < 0) + if (treebuilder_add_subelement(st, this, pi) < 0) goto error; Py_XSETREF(self->last_for_tail, Py_NewRef(pi)); } From 53bd559b359f9a76645597d68111861fe742b720 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 22 Jan 2023 13:21:23 +0100 Subject: [PATCH 08/11] Refactor: replace query with parameter Pass state as arg to element_add_subelement() --- Modules/_elementtree.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index cdcbad2f89ab9d..1f022a8de2d05d 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -503,10 +503,10 @@ raise_type_error(PyObject *element) } LOCAL(int) -element_add_subelement(ElementObject* self, PyObject* element) +element_add_subelement(elementtreestate *st, ElementObject *self, + PyObject *element) { /* add a child element to a parent */ - elementtreestate *st = ET_STATE_GLOBAL; if (!Element_Check(st, element)) { raise_type_error(element); return -1; @@ -618,7 +618,7 @@ subelement(PyObject *self, PyObject *args, PyObject *kwds) if (elem == NULL) return NULL; - if (element_add_subelement(parent, elem) < 0) { + if (element_add_subelement(st, parent, elem) < 0) { Py_DECREF(elem); return NULL; } @@ -693,7 +693,8 @@ static PyObject * _elementtree_Element_append_impl(ElementObject *self, PyObject *subelement) /*[clinic end generated code: output=54a884b7cf2295f4 input=439f2bd777288fb6]*/ { - if (element_add_subelement(self, subelement) < 0) + elementtreestate *st = ET_STATE_GLOBAL; + if (element_add_subelement(st, self, subelement) < 0) return NULL; Py_RETURN_NONE; @@ -1190,9 +1191,10 @@ _elementtree_Element_extend(ElementObject *self, PyObject *elements) return NULL; } + elementtreestate *st = ET_STATE_GLOBAL; for (i = 0; i < PySequence_Fast_GET_SIZE(seq); i++) { PyObject* element = Py_NewRef(PySequence_Fast_GET_ITEM(seq, i)); - if (element_add_subelement(self, element) < 0) { + if (element_add_subelement(st, self, element) < 0) { Py_DECREF(seq); Py_DECREF(element); return NULL; @@ -2591,7 +2593,7 @@ treebuilder_add_subelement(elementtreestate *st, PyObject *element, { if (Element_CheckExact(st, element)) { ElementObject *elem = (ElementObject *) element; - return element_add_subelement(elem, child); + return element_add_subelement(st, elem, child); } else { PyObject *res; From 706385a24c8864a5f50692364cad0bb9385d738a Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 20 Jan 2023 13:51:06 +0100 Subject: [PATCH 09/11] Refactor: replace query with parameter Pass state as arg to element_setstate_from_Python() --- Modules/_elementtree.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 1f022a8de2d05d..3f4d3edf001788 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -1065,7 +1065,8 @@ element_setstate_from_attributes(elementtreestate *st, */ static PyObject * -element_setstate_from_Python(ElementObject *self, PyObject *state) +element_setstate_from_Python(elementtreestate *st, ElementObject *self, + PyObject *state) { static char *kwlist[] = {PICKLED_TAG, PICKLED_ATTRIB, PICKLED_TEXT, PICKLED_TAIL, PICKLED_CHILDREN, 0}; @@ -1078,7 +1079,6 @@ element_setstate_from_Python(ElementObject *self, PyObject *state) if (!args) return NULL; - elementtreestate *st = ET_STATE_GLOBAL; if (PyArg_ParseTupleAndKeywords(args, state, "|$OOOOO", kwlist, &tag, &attrib, &text, &tail, &children)) retval = element_setstate_from_attributes(st, self, tag, attrib, text, @@ -1108,8 +1108,10 @@ _elementtree_Element___setstate__(ElementObject *self, PyObject *state) state); return NULL; } - else - return element_setstate_from_Python(self, state); + else { + elementtreestate *st = ET_STATE_GLOBAL; + return element_setstate_from_Python(st, self, state); + } } LOCAL(int) From 0195c35ce12da7e5aa75fd259f267ea2c6c2acf4 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 20 Jan 2023 13:56:07 +0100 Subject: [PATCH 10/11] Fix cherry-picks --- Modules/_elementtree.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 3f4d3edf001788..431ee2340a0f96 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -2781,10 +2781,10 @@ treebuilder_handle_comment(TreeBuilderObject* self, PyObject* text) return NULL; this = self->this; - elementtreestate *st = ET_STATE_GLOBAL; if (self->insert_comments && this != Py_None) { - if (treebuilder_add_subelement(st, this, comment) < 0) + if (treebuilder_add_subelement(self->state, this, comment) < 0) { goto error; + } Py_XSETREF(self->last_for_tail, Py_NewRef(comment)); } } else { @@ -2821,9 +2821,8 @@ treebuilder_handle_pi(TreeBuilderObject* self, PyObject* target, PyObject* text) } this = self->this; - elementtreestate *st = ET_STATE_GLOBAL; if (self->insert_pis && this != Py_None) { - if (treebuilder_add_subelement(st, this, pi) < 0) + if (treebuilder_add_subelement(self->state, this, pi) < 0) goto error; Py_XSETREF(self->last_for_tail, Py_NewRef(pi)); } @@ -3784,7 +3783,8 @@ _check_xmlparser(XMLParserObject* self) } LOCAL(PyObject*) -expat_parse(XMLParserObject* self, const char* data, int data_len, int final) +expat_parse(elementtreestate *st, XMLParserObject *self, const char *data, + int data_len, int final) { int ok; @@ -3795,7 +3795,6 @@ expat_parse(XMLParserObject* self, const char* data, int data_len, int final) return NULL; if (!ok) { - elementtreestate *st = ET_STATE_GLOBAL; expat_set_error( st, EXPAT(GetErrorCode)(self->parser), @@ -3825,11 +3824,11 @@ _elementtree_XMLParser_close_impl(XMLParserObject *self) if (!_check_xmlparser(self)) { return NULL; } - res = expat_parse(self, "", 0, 1); + elementtreestate *st = self->state; + res = expat_parse(st, self, "", 0, 1); if (!res) return NULL; - elementtreestate *st = self->state; if (TreeBuilder_CheckExact(st, self->target)) { Py_DECREF(res); return treebuilder_done((TreeBuilderObject*) self->target); @@ -3860,6 +3859,7 @@ _elementtree_XMLParser_feed(XMLParserObject *self, PyObject *data) if (!_check_xmlparser(self)) { return NULL; } + elementtreestate *st = self->state; if (PyUnicode_Check(data)) { Py_ssize_t data_len; const char *data_ptr = PyUnicode_AsUTF8AndSize(data, &data_len); @@ -3871,7 +3871,8 @@ _elementtree_XMLParser_feed(XMLParserObject *self, PyObject *data) } /* Explicitly set UTF-8 encoding. Return code ignored. */ (void)EXPAT(SetEncoding)(self->parser, "utf-8"); - return expat_parse(self, data_ptr, (int)data_len, 0); + + return expat_parse(st, self, data_ptr, (int)data_len, 0); } else { Py_buffer view; @@ -3883,7 +3884,7 @@ _elementtree_XMLParser_feed(XMLParserObject *self, PyObject *data) PyErr_SetString(PyExc_OverflowError, "size does not fit in an int"); return NULL; } - res = expat_parse(self, view.buf, (int)view.len, 0); + res = expat_parse(st, self, view.buf, (int)view.len, 0); PyBuffer_Release(&view); return res; } @@ -3915,6 +3916,7 @@ _elementtree_XMLParser__parse_whole(XMLParserObject *self, PyObject *file) return NULL; /* read from open file object */ + elementtreestate *st = self->state; for (;;) { buffer = PyObject_CallFunction(reader, "i", 64*1024); @@ -3952,8 +3954,8 @@ _elementtree_XMLParser__parse_whole(XMLParserObject *self, PyObject *file) return NULL; } res = expat_parse( - self, PyBytes_AS_STRING(buffer), (int)PyBytes_GET_SIZE(buffer), 0 - ); + st, self, PyBytes_AS_STRING(buffer), (int)PyBytes_GET_SIZE(buffer), + 0); Py_DECREF(buffer); @@ -3967,9 +3969,8 @@ _elementtree_XMLParser__parse_whole(XMLParserObject *self, PyObject *file) Py_DECREF(reader); - res = expat_parse(self, "", 0, 1); + res = expat_parse(st, self, "", 0, 1); - elementtreestate *st = self->state; if (res && TreeBuilder_CheckExact(st, self->target)) { Py_DECREF(res); return treebuilder_done((TreeBuilderObject*) self->target); From 8e1e07016cb11e972d096bd366e87268115cb795 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 23 Jan 2023 13:54:20 +0100 Subject: [PATCH 11/11] Apply PEP 7 to affected lines --- Modules/_elementtree.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 431ee2340a0f96..f7ab8881723fdb 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -2642,7 +2642,8 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag, if (!self->element_factory) { node = create_new_element(st, tag, attrib); - } else if (attrib == NULL) { + } + else if (attrib == NULL) { attrib = PyDict_New(); if (!attrib) return NULL; @@ -2662,8 +2663,9 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag, Py_CLEAR(self->last_for_tail); if (this != Py_None) { - if (treebuilder_add_subelement(st, this, node) < 0) + if (treebuilder_add_subelement(st, this, node) < 0) { goto error; + } } else { if (self->root) { PyErr_SetString( @@ -2822,8 +2824,9 @@ treebuilder_handle_pi(TreeBuilderObject* self, PyObject* target, PyObject* text) this = self->this; if (self->insert_pis && this != Py_None) { - if (treebuilder_add_subelement(self->state, this, pi) < 0) + if (treebuilder_add_subelement(self->state, this, pi) < 0) { goto error; + } Py_XSETREF(self->last_for_tail, Py_NewRef(pi)); } } else {