GH-90699: Remove `_Py_IDENTIFIER` usage from `_elementtree` module (GH-99012)

This commit is contained in:
Kumar Aditya 2022-11-03 01:59:41 +05:30 committed by GitHub
parent f3007ac370
commit 908e81f6c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 59 additions and 25 deletions

View File

@ -12,7 +12,6 @@
*/
#define PY_SSIZE_T_CLEAN
#define NEEDS_PY_IDENTIFIER
#include "Python.h"
#include "structmember.h" // PyMemberDef
@ -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;
@ -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
);
}
@ -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
);
@ -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
);
}
@ -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]
@ -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)) {
@ -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);
@ -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;
@ -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);
@ -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;
@ -3520,6 +3521,7 @@ 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,
@ -3527,7 +3529,7 @@ expat_start_doctype_handler(XMLParserObject *self,
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.",
@ -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
);