12
12
*/
13
13
14
14
#define PY_SSIZE_T_CLEAN
15
- #define NEEDS_PY_IDENTIFIER
16
15
17
16
#include "Python.h"
18
17
#include "structmember.h" // PyMemberDef
@@ -84,6 +83,15 @@ typedef struct {
84
83
PyObject * elementpath_obj ;
85
84
PyObject * comment_factory ;
86
85
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 ;
87
95
} elementtreestate ;
88
96
89
97
static struct PyModuleDef elementtreemodule ;
@@ -1219,9 +1227,8 @@ _elementtree_Element_find_impl(ElementObject *self, PyObject *path,
1219
1227
elementtreestate * st = ET_STATE_GLOBAL ;
1220
1228
1221
1229
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
1225
1232
);
1226
1233
}
1227
1234
@@ -1260,12 +1267,11 @@ _elementtree_Element_findtext_impl(ElementObject *self, PyObject *path,
1260
1267
/*[clinic end generated code: output=83b3ba4535d308d2 input=b53a85aa5aa2a916]*/
1261
1268
{
1262
1269
Py_ssize_t i ;
1263
- _Py_IDENTIFIER (findtext );
1264
1270
elementtreestate * st = ET_STATE_GLOBAL ;
1265
1271
1266
1272
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 ,
1269
1275
self , path , default_value , namespaces , NULL
1270
1276
);
1271
1277
@@ -1317,9 +1323,8 @@ _elementtree_Element_findall_impl(ElementObject *self, PyObject *path,
1317
1323
elementtreestate * st = ET_STATE_GLOBAL ;
1318
1324
1319
1325
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
1323
1328
);
1324
1329
}
1325
1330
@@ -1361,11 +1366,10 @@ _elementtree_Element_iterfind_impl(ElementObject *self, PyObject *path,
1361
1366
/*[clinic end generated code: output=ecdd56d63b19d40f input=abb974e350fb65c7]*/
1362
1367
{
1363
1368
PyObject * tag = path ;
1364
- _Py_IDENTIFIER (iterfind );
1365
1369
elementtreestate * st = ET_STATE_GLOBAL ;
1366
1370
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 );
1369
1373
}
1370
1374
1371
1375
/*[clinic input]
@@ -2545,7 +2549,7 @@ _elementtree__set_factories_impl(PyObject *module, PyObject *comment_factory,
2545
2549
2546
2550
static int
2547
2551
treebuilder_extend_element_text_or_tail (PyObject * element , PyObject * * data ,
2548
- PyObject * * dest , _Py_Identifier * name )
2552
+ PyObject * * dest , PyObject * name )
2549
2553
{
2550
2554
/* Fast paths for the "almost always" cases. */
2551
2555
if (Element_CheckExact (element )) {
@@ -2569,7 +2573,7 @@ treebuilder_extend_element_text_or_tail(PyObject *element, PyObject **data,
2569
2573
{
2570
2574
int r ;
2571
2575
PyObject * joined ;
2572
- PyObject * previous = _PyObject_GetAttrId (element , name );
2576
+ PyObject * previous = PyObject_GetAttr (element , name );
2573
2577
if (!previous )
2574
2578
return -1 ;
2575
2579
joined = list_join (* data );
@@ -2588,7 +2592,7 @@ treebuilder_extend_element_text_or_tail(PyObject *element, PyObject **data,
2588
2592
Py_DECREF (previous );
2589
2593
}
2590
2594
2591
- r = _PyObject_SetAttrId (element , name , joined );
2595
+ r = PyObject_SetAttr (element , name , joined );
2592
2596
Py_DECREF (joined );
2593
2597
if (r < 0 )
2594
2598
return -1 ;
@@ -2603,34 +2607,32 @@ treebuilder_flush_data(TreeBuilderObject* self)
2603
2607
if (!self -> data ) {
2604
2608
return 0 ;
2605
2609
}
2606
-
2610
+ elementtreestate * st = ET_STATE_GLOBAL ;
2607
2611
if (!self -> last_for_tail ) {
2608
2612
PyObject * element = self -> last ;
2609
- _Py_IDENTIFIER (text );
2610
2613
return treebuilder_extend_element_text_or_tail (
2611
2614
element , & self -> data ,
2612
- & ((ElementObject * ) element )-> text , & PyId_text );
2615
+ & ((ElementObject * ) element )-> text , st -> str_text );
2613
2616
}
2614
2617
else {
2615
2618
PyObject * element = self -> last_for_tail ;
2616
- _Py_IDENTIFIER (tail );
2617
2619
return treebuilder_extend_element_text_or_tail (
2618
2620
element , & self -> data ,
2619
- & ((ElementObject * ) element )-> tail , & PyId_tail );
2621
+ & ((ElementObject * ) element )-> tail , st -> str_tail );
2620
2622
}
2621
2623
}
2622
2624
2623
2625
static int
2624
2626
treebuilder_add_subelement (PyObject * element , PyObject * child )
2625
2627
{
2626
- _Py_IDENTIFIER ( append ) ;
2628
+ elementtreestate * st = ET_STATE_GLOBAL ;
2627
2629
if (Element_CheckExact (element )) {
2628
2630
ElementObject * elem = (ElementObject * ) element ;
2629
2631
return element_add_subelement (elem , child );
2630
2632
}
2631
2633
else {
2632
2634
PyObject * res ;
2633
- res = _PyObject_CallMethodIdOneArg (element , & PyId_append , child );
2635
+ res = PyObject_CallMethodOneArg (element , st -> str_append , child );
2634
2636
if (res == NULL )
2635
2637
return -1 ;
2636
2638
Py_DECREF (res );
@@ -3486,7 +3488,6 @@ expat_start_doctype_handler(XMLParserObject *self,
3486
3488
const XML_Char * pubid ,
3487
3489
int has_internal_subset )
3488
3490
{
3489
- _Py_IDENTIFIER (doctype );
3490
3491
PyObject * doctype_name_obj , * sysid_obj , * pubid_obj ;
3491
3492
PyObject * res ;
3492
3493
@@ -3520,14 +3521,15 @@ expat_start_doctype_handler(XMLParserObject *self,
3520
3521
pubid_obj = Py_None ;
3521
3522
}
3522
3523
3524
+ elementtreestate * st = ET_STATE_GLOBAL ;
3523
3525
/* If the target has a handler for doctype, call it. */
3524
3526
if (self -> handle_doctype ) {
3525
3527
res = PyObject_CallFunctionObjArgs (self -> handle_doctype ,
3526
3528
doctype_name_obj , pubid_obj ,
3527
3529
sysid_obj , NULL );
3528
3530
Py_XDECREF (res );
3529
3531
}
3530
- else if (_PyObject_LookupAttrId ((PyObject * )self , & PyId_doctype , & res ) > 0 ) {
3532
+ else if (_PyObject_LookupAttr ((PyObject * )self , st -> str_doctype , & res ) > 0 ) {
3531
3533
(void )PyErr_WarnEx (PyExc_RuntimeWarning ,
3532
3534
"The doctype() method of XMLParser is ignored. "
3533
3535
"Define doctype() method on the TreeBuilder target." ,
@@ -4420,6 +4422,38 @@ PyInit__elementtree(void)
4420
4422
return NULL ;
4421
4423
}
4422
4424
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
+ }
4423
4457
st -> parseerror_obj = PyErr_NewException (
4424
4458
"xml.etree.ElementTree.ParseError" , PyExc_SyntaxError , NULL
4425
4459
);
0 commit comments