Skip to content

GH-90699: Remove _Py_IDENTIFIER usage from _elementtree module #99012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 59 additions & 25 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/

#define PY_SSIZE_T_CLEAN
#define NEEDS_PY_IDENTIFIER

#include "Python.h"
#include "structmember.h" // PyMemberDef
Expand Down Expand Up @@ -84,6 +83,15 @@ typedef struct {
PyObject *elementpath_obj;
PyObject *comment_factory;
PyObject *pi_factory;
/* Interned strings */
PyObject *str_text;
PyObject *str_tail;
PyObject *str_append;
PyObject *str_find;
PyObject *str_findtext;
PyObject *str_findall;
PyObject *str_iterfind;
PyObject *str_doctype;
} elementtreestate;

static struct PyModuleDef elementtreemodule;
Expand Down Expand Up @@ -1219,9 +1227,8 @@ _elementtree_Element_find_impl(ElementObject *self, PyObject *path,
elementtreestate *st = ET_STATE_GLOBAL;

if (checkpath(path) || namespaces != Py_None) {
_Py_IDENTIFIER(find);
return _PyObject_CallMethodIdObjArgs(
st->elementpath_obj, &PyId_find, self, path, namespaces, NULL
return PyObject_CallMethodObjArgs(
st->elementpath_obj, st->str_find, self, path, namespaces, NULL
);
}

Expand Down Expand Up @@ -1260,12 +1267,11 @@ _elementtree_Element_findtext_impl(ElementObject *self, PyObject *path,
/*[clinic end generated code: output=83b3ba4535d308d2 input=b53a85aa5aa2a916]*/
{
Py_ssize_t i;
_Py_IDENTIFIER(findtext);
elementtreestate *st = ET_STATE_GLOBAL;

if (checkpath(path) || namespaces != Py_None)
return _PyObject_CallMethodIdObjArgs(
st->elementpath_obj, &PyId_findtext,
return PyObject_CallMethodObjArgs(
st->elementpath_obj, st->str_findtext,
self, path, default_value, namespaces, NULL
);

Expand Down Expand Up @@ -1317,9 +1323,8 @@ _elementtree_Element_findall_impl(ElementObject *self, PyObject *path,
elementtreestate *st = ET_STATE_GLOBAL;

if (checkpath(path) || namespaces != Py_None) {
_Py_IDENTIFIER(findall);
return _PyObject_CallMethodIdObjArgs(
st->elementpath_obj, &PyId_findall, self, path, namespaces, NULL
return PyObject_CallMethodObjArgs(
st->elementpath_obj, st->str_findall, self, path, namespaces, NULL
);
}

Expand Down Expand Up @@ -1361,11 +1366,10 @@ _elementtree_Element_iterfind_impl(ElementObject *self, PyObject *path,
/*[clinic end generated code: output=ecdd56d63b19d40f input=abb974e350fb65c7]*/
{
PyObject* tag = path;
_Py_IDENTIFIER(iterfind);
elementtreestate *st = ET_STATE_GLOBAL;

return _PyObject_CallMethodIdObjArgs(
st->elementpath_obj, &PyId_iterfind, self, tag, namespaces, NULL);
return PyObject_CallMethodObjArgs(
st->elementpath_obj, st->str_iterfind, self, tag, namespaces, NULL);
}

/*[clinic input]
Expand Down Expand Up @@ -2545,7 +2549,7 @@ _elementtree__set_factories_impl(PyObject *module, PyObject *comment_factory,

static int
treebuilder_extend_element_text_or_tail(PyObject *element, PyObject **data,
PyObject **dest, _Py_Identifier *name)
PyObject **dest, PyObject *name)
{
/* Fast paths for the "almost always" cases. */
if (Element_CheckExact(element)) {
Expand All @@ -2569,7 +2573,7 @@ treebuilder_extend_element_text_or_tail(PyObject *element, PyObject **data,
{
int r;
PyObject* joined;
PyObject* previous = _PyObject_GetAttrId(element, name);
PyObject* previous = PyObject_GetAttr(element, name);
if (!previous)
return -1;
joined = list_join(*data);
Expand All @@ -2588,7 +2592,7 @@ treebuilder_extend_element_text_or_tail(PyObject *element, PyObject **data,
Py_DECREF(previous);
}

r = _PyObject_SetAttrId(element, name, joined);
r = PyObject_SetAttr(element, name, joined);
Py_DECREF(joined);
if (r < 0)
return -1;
Expand All @@ -2603,34 +2607,32 @@ treebuilder_flush_data(TreeBuilderObject* self)
if (!self->data) {
return 0;
}

elementtreestate *st = ET_STATE_GLOBAL;
if (!self->last_for_tail) {
PyObject *element = self->last;
_Py_IDENTIFIER(text);
return treebuilder_extend_element_text_or_tail(
element, &self->data,
&((ElementObject *) element)->text, &PyId_text);
&((ElementObject *) element)->text, st->str_text);
}
else {
PyObject *element = self->last_for_tail;
_Py_IDENTIFIER(tail);
return treebuilder_extend_element_text_or_tail(
element, &self->data,
&((ElementObject *) element)->tail, &PyId_tail);
&((ElementObject *) element)->tail, st->str_tail);
}
}

static int
treebuilder_add_subelement(PyObject *element, PyObject *child)
{
_Py_IDENTIFIER(append);
elementtreestate *st = ET_STATE_GLOBAL;
if (Element_CheckExact(element)) {
ElementObject *elem = (ElementObject *) element;
return element_add_subelement(elem, child);
}
else {
PyObject *res;
res = _PyObject_CallMethodIdOneArg(element, &PyId_append, child);
res = PyObject_CallMethodOneArg(element, st->str_append, child);
if (res == NULL)
return -1;
Py_DECREF(res);
Expand Down Expand Up @@ -3486,7 +3488,6 @@ expat_start_doctype_handler(XMLParserObject *self,
const XML_Char *pubid,
int has_internal_subset)
{
_Py_IDENTIFIER(doctype);
PyObject *doctype_name_obj, *sysid_obj, *pubid_obj;
PyObject *res;

Expand Down Expand Up @@ -3520,14 +3521,15 @@ expat_start_doctype_handler(XMLParserObject *self,
pubid_obj = Py_None;
}

elementtreestate *st = ET_STATE_GLOBAL;
/* If the target has a handler for doctype, call it. */
if (self->handle_doctype) {
res = PyObject_CallFunctionObjArgs(self->handle_doctype,
doctype_name_obj, pubid_obj,
sysid_obj, NULL);
Py_XDECREF(res);
}
else if (_PyObject_LookupAttrId((PyObject *)self, &PyId_doctype, &res) > 0) {
else if (_PyObject_LookupAttr((PyObject *)self, st->str_doctype, &res) > 0) {
(void)PyErr_WarnEx(PyExc_RuntimeWarning,
"The doctype() method of XMLParser is ignored. "
"Define doctype() method on the TreeBuilder target.",
Expand Down Expand Up @@ -4420,6 +4422,38 @@ PyInit__elementtree(void)
return NULL;
}

st->str_append = PyUnicode_InternFromString("append");
if (st->str_append == NULL) {
return NULL;
}
st->str_find = PyUnicode_InternFromString("find");
if (st->str_find == NULL) {
return NULL;
}
st->str_findall = PyUnicode_InternFromString("findall");
if (st->str_findall == NULL) {
return NULL;
}
st->str_findtext = PyUnicode_InternFromString("findtext");
if (st->str_findtext == NULL) {
return NULL;
}
st->str_iterfind = PyUnicode_InternFromString("iterfind");
if (st->str_iterfind == NULL) {
return NULL;
}
st->str_tail = PyUnicode_InternFromString("tail");
if (st->str_tail == NULL) {
return NULL;
}
st->str_text = PyUnicode_InternFromString("text");
if (st->str_text == NULL) {
return NULL;
}
st->str_doctype = PyUnicode_InternFromString("doctype");
if (st->str_doctype == NULL) {
return NULL;
}
st->parseerror_obj = PyErr_NewException(
"xml.etree.ElementTree.ParseError", PyExc_SyntaxError, NULL
);
Expand Down