@@ -229,8 +229,7 @@ create_extra(ElementObject* self, PyObject* attrib)
229
229
return -1 ;
230
230
}
231
231
232
- Py_XINCREF (attrib );
233
- self -> extra -> attrib = attrib ;
232
+ self -> extra -> attrib = Py_XNewRef (attrib );
234
233
235
234
self -> extra -> length = 0 ;
236
235
self -> extra -> allocated = STATIC_CHILDREN ;
@@ -286,16 +285,9 @@ create_new_element(PyObject* tag, PyObject* attrib)
286
285
if (self == NULL )
287
286
return NULL ;
288
287
self -> extra = NULL ;
289
-
290
- Py_INCREF (tag );
291
- self -> tag = tag ;
292
-
293
- Py_INCREF (Py_None );
294
- self -> text = Py_None ;
295
-
296
- Py_INCREF (Py_None );
297
- self -> tail = Py_None ;
298
-
288
+ self -> tag = Py_NewRef (tag );
289
+ self -> text = Py_NewRef (Py_None );
290
+ self -> tail = Py_NewRef (Py_None );
299
291
self -> weakreflist = NULL ;
300
292
301
293
PyObject_GC_Track (self );
@@ -315,15 +307,9 @@ element_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
315
307
{
316
308
ElementObject * e = (ElementObject * )type -> tp_alloc (type , 0 );
317
309
if (e != NULL ) {
318
- Py_INCREF (Py_None );
319
- e -> tag = Py_None ;
320
-
321
- Py_INCREF (Py_None );
322
- e -> text = Py_None ;
323
-
324
- Py_INCREF (Py_None );
325
- e -> tail = Py_None ;
326
-
310
+ e -> tag = Py_NewRef (Py_None );
311
+ e -> text = Py_NewRef (Py_None );
312
+ e -> tail = Py_NewRef (Py_None );
327
313
e -> extra = NULL ;
328
314
e -> weakreflist = NULL ;
329
315
}
@@ -514,8 +500,7 @@ element_add_subelement(ElementObject* self, PyObject* element)
514
500
if (element_resize (self , 1 ) < 0 )
515
501
return -1 ;
516
502
517
- Py_INCREF (element );
518
- self -> extra -> children [self -> extra -> length ] = element ;
503
+ self -> extra -> children [self -> extra -> length ] = Py_NewRef (element );
519
504
520
505
self -> extra -> length ++ ;
521
506
@@ -747,8 +732,7 @@ _elementtree_Element___copy___impl(ElementObject *self)
747
732
}
748
733
749
734
for (i = 0 ; i < self -> extra -> length ; i ++ ) {
750
- Py_INCREF (self -> extra -> children [i ]);
751
- element -> extra -> children [i ] = self -> extra -> children [i ];
735
+ element -> extra -> children [i ] = Py_NewRef (self -> extra -> children [i ]);
752
736
}
753
737
754
738
assert (!element -> extra -> length );
@@ -863,8 +847,7 @@ deepcopy(PyObject *object, PyObject *memo)
863
847
864
848
/* Fast paths */
865
849
if (object == Py_None || PyUnicode_CheckExact (object )) {
866
- Py_INCREF (object );
867
- return object ;
850
+ return Py_NewRef (object );
868
851
}
869
852
870
853
if (Py_REFCNT (object ) == 1 ) {
@@ -956,8 +939,7 @@ _elementtree_Element___getstate___impl(ElementObject *self)
956
939
}
957
940
958
941
if (self -> extra && self -> extra -> attrib ) {
959
- attrib = self -> extra -> attrib ;
960
- Py_INCREF (attrib );
942
+ attrib = Py_NewRef (self -> extra -> attrib );
961
943
}
962
944
else {
963
945
attrib = PyDict_New ();
@@ -1043,8 +1025,7 @@ element_setstate_from_attributes(ElementObject *self,
1043
1025
dealloc_extra (oldextra );
1044
1026
return NULL ;
1045
1027
}
1046
- Py_INCREF (child );
1047
- self -> extra -> children [i ] = child ;
1028
+ self -> extra -> children [i ] = Py_NewRef (child );
1048
1029
}
1049
1030
1050
1031
assert (!self -> extra -> length );
@@ -1276,8 +1257,7 @@ _elementtree_Element_findtext_impl(ElementObject *self, PyObject *path,
1276
1257
);
1277
1258
1278
1259
if (!self -> extra ) {
1279
- Py_INCREF (default_value );
1280
- return default_value ;
1260
+ return Py_NewRef (default_value );
1281
1261
}
1282
1262
1283
1263
for (i = 0 ; i < self -> extra -> length ; i ++ ) {
@@ -1301,8 +1281,7 @@ _elementtree_Element_findtext_impl(ElementObject *self, PyObject *path,
1301
1281
return NULL ;
1302
1282
}
1303
1283
1304
- Py_INCREF (default_value );
1305
- return default_value ;
1284
+ return Py_NewRef (default_value );
1306
1285
}
1307
1286
1308
1287
/*[clinic input]
@@ -1396,8 +1375,7 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key,
1396
1375
}
1397
1376
}
1398
1377
1399
- Py_INCREF (default_value );
1400
- return default_value ;
1378
+ return Py_NewRef (default_value );
1401
1379
}
1402
1380
1403
1381
static PyObject *
@@ -1456,8 +1434,7 @@ element_getitem(PyObject* self_, Py_ssize_t index)
1456
1434
return NULL ;
1457
1435
}
1458
1436
1459
- Py_INCREF (self -> extra -> children [index ]);
1460
- return self -> extra -> children [index ];
1437
+ return Py_NewRef (self -> extra -> children [index ]);
1461
1438
}
1462
1439
1463
1440
/*[clinic input]
@@ -1495,8 +1472,7 @@ _elementtree_Element_insert_impl(ElementObject *self, Py_ssize_t index,
1495
1472
for (i = self -> extra -> length ; i > index ; i -- )
1496
1473
self -> extra -> children [i ] = self -> extra -> children [i - 1 ];
1497
1474
1498
- Py_INCREF (subelement );
1499
- self -> extra -> children [index ] = subelement ;
1475
+ self -> extra -> children [index ] = Py_NewRef (subelement );
1500
1476
1501
1477
self -> extra -> length ++ ;
1502
1478
@@ -1697,8 +1673,7 @@ element_setitem(PyObject* self_, Py_ssize_t index, PyObject* item)
1697
1673
raise_type_error (item );
1698
1674
return -1 ;
1699
1675
}
1700
- Py_INCREF (item );
1701
- self -> extra -> children [index ] = item ;
1676
+ self -> extra -> children [index ] = Py_NewRef (item );
1702
1677
} else {
1703
1678
self -> extra -> length -- ;
1704
1679
for (i = index ; i < self -> extra -> length ; i ++ )
@@ -1929,8 +1904,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
1929
1904
for (cur = start , i = 0 ; i < newlen ;
1930
1905
cur += step , i ++ ) {
1931
1906
PyObject * element = PySequence_Fast_GET_ITEM (seq , i );
1932
- Py_INCREF (element );
1933
- self -> extra -> children [cur ] = element ;
1907
+ self -> extra -> children [cur ] = Py_NewRef (element );
1934
1908
}
1935
1909
1936
1910
self -> extra -> length += newlen - slicelen ;
@@ -1953,24 +1927,21 @@ static PyObject*
1953
1927
element_tag_getter (ElementObject * self , void * closure )
1954
1928
{
1955
1929
PyObject * res = self -> tag ;
1956
- Py_INCREF (res );
1957
- return res ;
1930
+ return Py_NewRef (res );
1958
1931
}
1959
1932
1960
1933
static PyObject *
1961
1934
element_text_getter (ElementObject * self , void * closure )
1962
1935
{
1963
1936
PyObject * res = element_get_text (self );
1964
- Py_XINCREF (res );
1965
- return res ;
1937
+ return Py_XNewRef (res );
1966
1938
}
1967
1939
1968
1940
static PyObject *
1969
1941
element_tail_getter (ElementObject * self , void * closure )
1970
1942
{
1971
1943
PyObject * res = element_get_tail (self );
1972
- Py_XINCREF (res );
1973
- return res ;
1944
+ return Py_XNewRef (res );
1974
1945
}
1975
1946
1976
1947
static PyObject *
@@ -1982,8 +1953,7 @@ element_attrib_getter(ElementObject *self, void *closure)
1982
1953
return NULL ;
1983
1954
}
1984
1955
res = element_get_attrib (self );
1985
- Py_XINCREF (res );
1986
- return res ;
1956
+ return Py_XNewRef (res );
1987
1957
}
1988
1958
1989
1959
/* macro for setter validation */
@@ -2123,8 +2093,7 @@ parent_stack_push_new(ElementIterObject *it, ElementObject *parent)
2123
2093
it -> parent_stack_size = new_size ;
2124
2094
}
2125
2095
item = it -> parent_stack + it -> parent_stack_used ++ ;
2126
- Py_INCREF (parent );
2127
- item -> parent = parent ;
2096
+ item -> parent = (ElementObject * )Py_NewRef (parent );
2128
2097
item -> child_index = 0 ;
2129
2098
return 0 ;
2130
2099
}
@@ -2291,11 +2260,9 @@ create_elementiter(ElementObject *self, PyObject *tag, int gettext)
2291
2260
if (!it )
2292
2261
return NULL ;
2293
2262
2294
- Py_INCREF (tag );
2295
- it -> sought_tag = tag ;
2263
+ it -> sought_tag = Py_NewRef (tag );
2296
2264
it -> gettext = gettext ;
2297
- Py_INCREF (self );
2298
- it -> root_element = self ;
2265
+ it -> root_element = (ElementObject * )Py_NewRef (self );
2299
2266
2300
2267
it -> parent_stack = PyMem_New (ParentLocator , INIT_PARENT_STACK_SIZE );
2301
2268
if (it -> parent_stack == NULL ) {
@@ -2357,12 +2324,8 @@ treebuilder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2357
2324
TreeBuilderObject * t = (TreeBuilderObject * )type -> tp_alloc (type , 0 );
2358
2325
if (t != NULL ) {
2359
2326
t -> root = NULL ;
2360
-
2361
- Py_INCREF (Py_None );
2362
- t -> this = Py_None ;
2363
- Py_INCREF (Py_None );
2364
- t -> last = Py_None ;
2365
-
2327
+ t -> this = Py_NewRef (Py_None );
2328
+ t -> last = Py_NewRef (Py_None );
2366
2329
t -> data = NULL ;
2367
2330
t -> element_factory = NULL ;
2368
2331
t -> comment_factory = NULL ;
@@ -2705,8 +2668,7 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
2705
2668
);
2706
2669
goto error ;
2707
2670
}
2708
- Py_INCREF (node );
2709
- self -> root = node ;
2671
+ self -> root = Py_NewRef (node );
2710
2672
}
2711
2673
2712
2674
if (self -> index < PyList_GET_SIZE (self -> stack )) {
@@ -2743,7 +2705,7 @@ treebuilder_handle_data(TreeBuilderObject* self, PyObject* data)
2743
2705
Py_RETURN_NONE ;
2744
2706
}
2745
2707
/* store the first item as is */
2746
- Py_INCREF ( data ); self -> data = data ;
2708
+ self -> data = Py_NewRef ( data ) ;
2747
2709
} else {
2748
2710
/* more than one item; use a list to collect items */
2749
2711
if (PyBytes_CheckExact (self -> data ) && Py_REFCNT (self -> data ) == 1 &&
@@ -2789,8 +2751,7 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
2789
2751
}
2790
2752
2791
2753
item = self -> last ;
2792
- self -> last = self -> this ;
2793
- Py_INCREF (self -> last );
2754
+ self -> last = Py_NewRef (self -> this );
2794
2755
Py_XSETREF (self -> last_for_tail , self -> last );
2795
2756
self -> index -- ;
2796
2757
self -> this = PyList_GET_ITEM (self -> stack , self -> index );
@@ -2827,8 +2788,7 @@ treebuilder_handle_comment(TreeBuilderObject* self, PyObject* text)
2827
2788
Py_XSETREF (self -> last_for_tail , comment );
2828
2789
}
2829
2790
} else {
2830
- Py_INCREF (text );
2831
- comment = text ;
2791
+ comment = Py_NewRef (text );
2832
2792
}
2833
2793
2834
2794
if (self -> events_append && self -> comment_event_obj ) {
@@ -2996,8 +2956,7 @@ treebuilder_done(TreeBuilderObject* self)
2996
2956
else
2997
2957
res = Py_None ;
2998
2958
2999
- Py_INCREF (res );
3000
- return res ;
2959
+ return Py_NewRef (res );
3001
2960
}
3002
2961
3003
2962
/*[clinic input]
@@ -3115,8 +3074,7 @@ makeuniversal(XMLParserObject* self, const char* string)
3115
3074
size ++ ;
3116
3075
} else {
3117
3076
/* plain name; use key as tag */
3118
- Py_INCREF (key );
3119
- tag = key ;
3077
+ tag = Py_NewRef (key );
3120
3078
}
3121
3079
3122
3080
/* decode universal name */
@@ -3505,8 +3463,7 @@ expat_start_doctype_handler(XMLParserObject *self,
3505
3463
return ;
3506
3464
}
3507
3465
} else {
3508
- Py_INCREF (Py_None );
3509
- sysid_obj = Py_None ;
3466
+ sysid_obj = Py_NewRef (Py_None );
3510
3467
}
3511
3468
3512
3469
if (pubid ) {
@@ -3517,8 +3474,7 @@ expat_start_doctype_handler(XMLParserObject *self,
3517
3474
return ;
3518
3475
}
3519
3476
} else {
3520
- Py_INCREF (Py_None );
3521
- pubid_obj = Py_None ;
3477
+ pubid_obj = Py_NewRef (Py_None );
3522
3478
}
3523
3479
3524
3480
elementtreestate * st = ET_STATE_GLOBAL ;
@@ -4377,8 +4333,7 @@ PyInit__elementtree(void)
4377
4333
4378
4334
m = PyState_FindModule (& elementtreemodule );
4379
4335
if (m ) {
4380
- Py_INCREF (m );
4381
- return m ;
4336
+ return Py_NewRef (m );
4382
4337
}
4383
4338
4384
4339
/* Initialize object types */
0 commit comments