Skip to content

Commit 908e81f

Browse files
GH-90699: Remove _Py_IDENTIFIER usage from _elementtree module (GH-99012)
1 parent f3007ac commit 908e81f

File tree

1 file changed

+59
-25
lines changed

1 file changed

+59
-25
lines changed

Modules/_elementtree.c

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313

1414
#define PY_SSIZE_T_CLEAN
15-
#define NEEDS_PY_IDENTIFIER
1615

1716
#include "Python.h"
1817
#include "structmember.h" // PyMemberDef
@@ -84,6 +83,15 @@ typedef struct {
8483
PyObject *elementpath_obj;
8584
PyObject *comment_factory;
8685
PyObject *pi_factory;
86+
/* Interned strings */
87+
PyObject *str_text;
88+
PyObject *str_tail;
89+
PyObject *str_append;
90+
PyObject *str_find;
91+
PyObject *str_findtext;
92+
PyObject *str_findall;
93+
PyObject *str_iterfind;
94+
PyObject *str_doctype;
8795
} elementtreestate;
8896

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

12211229
if (checkpath(path) || namespaces != Py_None) {
1222-
_Py_IDENTIFIER(find);
1223-
return _PyObject_CallMethodIdObjArgs(
1224-
st->elementpath_obj, &PyId_find, self, path, namespaces, NULL
1230+
return PyObject_CallMethodObjArgs(
1231+
st->elementpath_obj, st->str_find, self, path, namespaces, NULL
12251232
);
12261233
}
12271234

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

12661272
if (checkpath(path) || namespaces != Py_None)
1267-
return _PyObject_CallMethodIdObjArgs(
1268-
st->elementpath_obj, &PyId_findtext,
1273+
return PyObject_CallMethodObjArgs(
1274+
st->elementpath_obj, st->str_findtext,
12691275
self, path, default_value, namespaces, NULL
12701276
);
12711277

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

13191325
if (checkpath(path) || namespaces != Py_None) {
1320-
_Py_IDENTIFIER(findall);
1321-
return _PyObject_CallMethodIdObjArgs(
1322-
st->elementpath_obj, &PyId_findall, self, path, namespaces, NULL
1326+
return PyObject_CallMethodObjArgs(
1327+
st->elementpath_obj, st->str_findall, self, path, namespaces, NULL
13231328
);
13241329
}
13251330

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

1367-
return _PyObject_CallMethodIdObjArgs(
1368-
st->elementpath_obj, &PyId_iterfind, self, tag, namespaces, NULL);
1371+
return PyObject_CallMethodObjArgs(
1372+
st->elementpath_obj, st->str_iterfind, self, tag, namespaces, NULL);
13691373
}
13701374

13711375
/*[clinic input]
@@ -2545,7 +2549,7 @@ _elementtree__set_factories_impl(PyObject *module, PyObject *comment_factory,
25452549

25462550
static int
25472551
treebuilder_extend_element_text_or_tail(PyObject *element, PyObject **data,
2548-
PyObject **dest, _Py_Identifier *name)
2552+
PyObject **dest, PyObject *name)
25492553
{
25502554
/* Fast paths for the "almost always" cases. */
25512555
if (Element_CheckExact(element)) {
@@ -2569,7 +2573,7 @@ treebuilder_extend_element_text_or_tail(PyObject *element, PyObject **data,
25692573
{
25702574
int r;
25712575
PyObject* joined;
2572-
PyObject* previous = _PyObject_GetAttrId(element, name);
2576+
PyObject* previous = PyObject_GetAttr(element, name);
25732577
if (!previous)
25742578
return -1;
25752579
joined = list_join(*data);
@@ -2588,7 +2592,7 @@ treebuilder_extend_element_text_or_tail(PyObject *element, PyObject **data,
25882592
Py_DECREF(previous);
25892593
}
25902594

2591-
r = _PyObject_SetAttrId(element, name, joined);
2595+
r = PyObject_SetAttr(element, name, joined);
25922596
Py_DECREF(joined);
25932597
if (r < 0)
25942598
return -1;
@@ -2603,34 +2607,32 @@ treebuilder_flush_data(TreeBuilderObject* self)
26032607
if (!self->data) {
26042608
return 0;
26052609
}
2606-
2610+
elementtreestate *st = ET_STATE_GLOBAL;
26072611
if (!self->last_for_tail) {
26082612
PyObject *element = self->last;
2609-
_Py_IDENTIFIER(text);
26102613
return treebuilder_extend_element_text_or_tail(
26112614
element, &self->data,
2612-
&((ElementObject *) element)->text, &PyId_text);
2615+
&((ElementObject *) element)->text, st->str_text);
26132616
}
26142617
else {
26152618
PyObject *element = self->last_for_tail;
2616-
_Py_IDENTIFIER(tail);
26172619
return treebuilder_extend_element_text_or_tail(
26182620
element, &self->data,
2619-
&((ElementObject *) element)->tail, &PyId_tail);
2621+
&((ElementObject *) element)->tail, st->str_tail);
26202622
}
26212623
}
26222624

26232625
static int
26242626
treebuilder_add_subelement(PyObject *element, PyObject *child)
26252627
{
2626-
_Py_IDENTIFIER(append);
2628+
elementtreestate *st = ET_STATE_GLOBAL;
26272629
if (Element_CheckExact(element)) {
26282630
ElementObject *elem = (ElementObject *) element;
26292631
return element_add_subelement(elem, child);
26302632
}
26312633
else {
26322634
PyObject *res;
2633-
res = _PyObject_CallMethodIdOneArg(element, &PyId_append, child);
2635+
res = PyObject_CallMethodOneArg(element, st->str_append, child);
26342636
if (res == NULL)
26352637
return -1;
26362638
Py_DECREF(res);
@@ -3486,7 +3488,6 @@ expat_start_doctype_handler(XMLParserObject *self,
34863488
const XML_Char *pubid,
34873489
int has_internal_subset)
34883490
{
3489-
_Py_IDENTIFIER(doctype);
34903491
PyObject *doctype_name_obj, *sysid_obj, *pubid_obj;
34913492
PyObject *res;
34923493

@@ -3520,14 +3521,15 @@ expat_start_doctype_handler(XMLParserObject *self,
35203521
pubid_obj = Py_None;
35213522
}
35223523

3524+
elementtreestate *st = ET_STATE_GLOBAL;
35233525
/* If the target has a handler for doctype, call it. */
35243526
if (self->handle_doctype) {
35253527
res = PyObject_CallFunctionObjArgs(self->handle_doctype,
35263528
doctype_name_obj, pubid_obj,
35273529
sysid_obj, NULL);
35283530
Py_XDECREF(res);
35293531
}
3530-
else if (_PyObject_LookupAttrId((PyObject *)self, &PyId_doctype, &res) > 0) {
3532+
else if (_PyObject_LookupAttr((PyObject *)self, st->str_doctype, &res) > 0) {
35313533
(void)PyErr_WarnEx(PyExc_RuntimeWarning,
35323534
"The doctype() method of XMLParser is ignored. "
35333535
"Define doctype() method on the TreeBuilder target.",
@@ -4420,6 +4422,38 @@ PyInit__elementtree(void)
44204422
return NULL;
44214423
}
44224424

4425+
st->str_append = PyUnicode_InternFromString("append");
4426+
if (st->str_append == NULL) {
4427+
return NULL;
4428+
}
4429+
st->str_find = PyUnicode_InternFromString("find");
4430+
if (st->str_find == NULL) {
4431+
return NULL;
4432+
}
4433+
st->str_findall = PyUnicode_InternFromString("findall");
4434+
if (st->str_findall == NULL) {
4435+
return NULL;
4436+
}
4437+
st->str_findtext = PyUnicode_InternFromString("findtext");
4438+
if (st->str_findtext == NULL) {
4439+
return NULL;
4440+
}
4441+
st->str_iterfind = PyUnicode_InternFromString("iterfind");
4442+
if (st->str_iterfind == NULL) {
4443+
return NULL;
4444+
}
4445+
st->str_tail = PyUnicode_InternFromString("tail");
4446+
if (st->str_tail == NULL) {
4447+
return NULL;
4448+
}
4449+
st->str_text = PyUnicode_InternFromString("text");
4450+
if (st->str_text == NULL) {
4451+
return NULL;
4452+
}
4453+
st->str_doctype = PyUnicode_InternFromString("doctype");
4454+
if (st->str_doctype == NULL) {
4455+
return NULL;
4456+
}
44234457
st->parseerror_obj = PyErr_NewException(
44244458
"xml.etree.ElementTree.ParseError", PyExc_SyntaxError, NULL
44254459
);

0 commit comments

Comments
 (0)