@@ -411,14 +411,10 @@ element_init(PyObject *self, PyObject *args, PyObject *kwds)
411
411
Py_XDECREF (attrib );
412
412
413
413
/* Replace the objects already pointed to by tag, text and tail. */
414
- Py_INCREF (tag );
415
- Py_XSETREF (self_elem -> tag , tag );
414
+ Py_XSETREF (self_elem -> tag , Py_NewRef (tag ));
416
415
417
- Py_INCREF (Py_None );
418
- _set_joined_ptr (& self_elem -> text , Py_None );
419
-
420
- Py_INCREF (Py_None );
421
- _set_joined_ptr (& self_elem -> tail , Py_None );
416
+ _set_joined_ptr (& self_elem -> text , Py_NewRef (Py_None ));
417
+ _set_joined_ptr (& self_elem -> tail , Py_NewRef (Py_None ));
422
418
423
419
return 0 ;
424
420
}
@@ -690,11 +686,8 @@ _elementtree_Element_clear_impl(ElementObject *self)
690
686
{
691
687
clear_extra (self );
692
688
693
- Py_INCREF (Py_None );
694
- _set_joined_ptr (& self -> text , Py_None );
695
-
696
- Py_INCREF (Py_None );
697
- _set_joined_ptr (& self -> tail , Py_None );
689
+ _set_joined_ptr (& self -> text , Py_NewRef (Py_None ));
690
+ _set_joined_ptr (& self -> tail , Py_NewRef (Py_None ));
698
691
699
692
Py_RETURN_NONE ;
700
693
}
@@ -970,8 +963,7 @@ element_setstate_from_attributes(ElementObject *self,
970
963
return NULL ;
971
964
}
972
965
973
- Py_INCREF (tag );
974
- Py_XSETREF (self -> tag , tag );
966
+ Py_XSETREF (self -> tag , Py_NewRef (tag ));
975
967
976
968
text = text ? JOIN_SET (text , PyList_CheckExact (text )) : Py_None ;
977
969
Py_INCREF (JOIN_OBJ (text ));
@@ -1035,8 +1027,7 @@ element_setstate_from_attributes(ElementObject *self,
1035
1027
}
1036
1028
1037
1029
/* Stash attrib. */
1038
- Py_XINCREF (attrib );
1039
- Py_XSETREF (self -> extra -> attrib , attrib );
1030
+ Py_XSETREF (self -> extra -> attrib , Py_XNewRef (attrib ));
1040
1031
dealloc_extra (oldextra );
1041
1032
1042
1033
Py_RETURN_NONE ;
@@ -1173,8 +1164,7 @@ _elementtree_Element_extend(ElementObject *self, PyObject *elements)
1173
1164
}
1174
1165
1175
1166
for (i = 0 ; i < PySequence_Fast_GET_SIZE (seq ); i ++ ) {
1176
- PyObject * element = PySequence_Fast_GET_ITEM (seq , i );
1177
- Py_INCREF (element );
1167
+ PyObject * element = Py_NewRef (PySequence_Fast_GET_ITEM (seq , i ));
1178
1168
if (element_add_subelement (self , element ) < 0 ) {
1179
1169
Py_DECREF (seq );
1180
1170
Py_DECREF (element );
@@ -1363,8 +1353,7 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key,
1363
1353
{
1364
1354
if (self -> extra && self -> extra -> attrib ) {
1365
1355
PyObject * attrib = Py_NewRef (self -> extra -> attrib );
1366
- PyObject * value = PyDict_GetItemWithError (attrib , key );
1367
- Py_XINCREF (value );
1356
+ PyObject * value = Py_XNewRef (PyDict_GetItemWithError (attrib , key ));
1368
1357
Py_DECREF (attrib );
1369
1358
if (value != NULL || PyErr_Occurred ()) {
1370
1359
return value ;
@@ -1964,26 +1953,23 @@ static int
1964
1953
element_tag_setter (ElementObject * self , PyObject * value , void * closure )
1965
1954
{
1966
1955
_VALIDATE_ATTR_VALUE (value );
1967
- Py_INCREF (value );
1968
- Py_SETREF (self -> tag , value );
1956
+ Py_SETREF (self -> tag , Py_NewRef (value ));
1969
1957
return 0 ;
1970
1958
}
1971
1959
1972
1960
static int
1973
1961
element_text_setter (ElementObject * self , PyObject * value , void * closure )
1974
1962
{
1975
1963
_VALIDATE_ATTR_VALUE (value );
1976
- Py_INCREF (value );
1977
- _set_joined_ptr (& self -> text , value );
1964
+ _set_joined_ptr (& self -> text , Py_NewRef (value ));
1978
1965
return 0 ;
1979
1966
}
1980
1967
1981
1968
static int
1982
1969
element_tail_setter (ElementObject * self , PyObject * value , void * closure )
1983
1970
{
1984
1971
_VALIDATE_ATTR_VALUE (value );
1985
- Py_INCREF (value );
1986
- _set_joined_ptr (& self -> tail , value );
1972
+ _set_joined_ptr (& self -> tail , Py_NewRef (value ));
1987
1973
return 0 ;
1988
1974
}
1989
1975
@@ -2001,8 +1987,7 @@ element_attrib_setter(ElementObject *self, PyObject *value, void *closure)
2001
1987
if (create_extra (self , NULL ) < 0 )
2002
1988
return -1 ;
2003
1989
}
2004
- Py_INCREF (value );
2005
- Py_XSETREF (self -> extra -> attrib , value );
1990
+ Py_XSETREF (self -> extra -> attrib , Py_NewRef (value ));
2006
1991
return 0 ;
2007
1992
}
2008
1993
@@ -2149,9 +2134,8 @@ elementiter_next(ElementIterObject *it)
2149
2134
}
2150
2135
2151
2136
assert (Element_Check (extra -> children [child_index ]));
2152
- elem = (ElementObject * )extra -> children [child_index ];
2137
+ elem = (ElementObject * )Py_NewRef ( extra -> children [child_index ]) ;
2153
2138
item -> child_index ++ ;
2154
- Py_INCREF (elem );
2155
2139
}
2156
2140
2157
2141
if (parent_stack_push_new (it , elem ) < 0 ) {
@@ -2364,8 +2348,7 @@ _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self,
2364
2348
/*[clinic end generated code: output=8571d4dcadfdf952 input=ae98a94df20b5cc3]*/
2365
2349
{
2366
2350
if (element_factory != Py_None ) {
2367
- Py_INCREF (element_factory );
2368
- Py_XSETREF (self -> element_factory , element_factory );
2351
+ Py_XSETREF (self -> element_factory , Py_NewRef (element_factory ));
2369
2352
} else {
2370
2353
Py_CLEAR (self -> element_factory );
2371
2354
}
@@ -2375,8 +2358,7 @@ _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self,
2375
2358
comment_factory = st -> comment_factory ;
2376
2359
}
2377
2360
if (comment_factory ) {
2378
- Py_INCREF (comment_factory );
2379
- Py_XSETREF (self -> comment_factory , comment_factory );
2361
+ Py_XSETREF (self -> comment_factory , Py_NewRef (comment_factory ));
2380
2362
self -> insert_comments = insert_comments ;
2381
2363
} else {
2382
2364
Py_CLEAR (self -> comment_factory );
@@ -2388,8 +2370,7 @@ _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self,
2388
2370
pi_factory = st -> pi_factory ;
2389
2371
}
2390
2372
if (pi_factory ) {
2391
- Py_INCREF (pi_factory );
2392
- Py_XSETREF (self -> pi_factory , pi_factory );
2373
+ Py_XSETREF (self -> pi_factory , Py_NewRef (pi_factory ));
2393
2374
self -> insert_pis = insert_pis ;
2394
2375
} else {
2395
2376
Py_CLEAR (self -> pi_factory );
@@ -2492,14 +2473,12 @@ _elementtree__set_factories_impl(PyObject *module, PyObject *comment_factory,
2492
2473
if (comment_factory == Py_None ) {
2493
2474
Py_CLEAR (st -> comment_factory );
2494
2475
} else {
2495
- Py_INCREF (comment_factory );
2496
- Py_XSETREF (st -> comment_factory , comment_factory );
2476
+ Py_XSETREF (st -> comment_factory , Py_NewRef (comment_factory ));
2497
2477
}
2498
2478
if (pi_factory == Py_None ) {
2499
2479
Py_CLEAR (st -> pi_factory );
2500
2480
} else {
2501
- Py_INCREF (pi_factory );
2502
- Py_XSETREF (st -> pi_factory , pi_factory );
2481
+ Py_XSETREF (st -> pi_factory , Py_NewRef (pi_factory ));
2503
2482
}
2504
2483
2505
2484
return old ;
@@ -2676,10 +2655,8 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
2676
2655
}
2677
2656
self -> index ++ ;
2678
2657
2679
- Py_INCREF (node );
2680
- Py_SETREF (self -> this , node );
2681
- Py_INCREF (node );
2682
- Py_SETREF (self -> last , node );
2658
+ Py_SETREF (self -> this , Py_NewRef (node ));
2659
+ Py_SETREF (self -> last , Py_NewRef (node ));
2683
2660
2684
2661
if (treebuilder_append_event (self , self -> start_event_obj , node ) < 0 )
2685
2662
goto error ;
@@ -2719,9 +2696,9 @@ treebuilder_handle_data(TreeBuilderObject* self, PyObject* data)
2719
2696
PyObject * list = PyList_New (2 );
2720
2697
if (!list )
2721
2698
return NULL ;
2722
- PyList_SET_ITEM (list , 0 , self -> data );
2723
- Py_INCREF ( data ); PyList_SET_ITEM (list , 1 , data );
2724
- self -> data = list ;
2699
+ PyList_SET_ITEM (list , 0 , Py_NewRef ( self -> data ) );
2700
+ PyList_SET_ITEM (list , 1 , Py_NewRef ( data ) );
2701
+ Py_SETREF ( self -> data , list ) ;
2725
2702
}
2726
2703
}
2727
2704
@@ -2749,8 +2726,7 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
2749
2726
self -> last = Py_NewRef (self -> this );
2750
2727
Py_XSETREF (self -> last_for_tail , self -> last );
2751
2728
self -> index -- ;
2752
- self -> this = PyList_GET_ITEM (self -> stack , self -> index );
2753
- Py_INCREF (self -> this );
2729
+ self -> this = Py_NewRef (PyList_GET_ITEM (self -> stack , self -> index ));
2754
2730
Py_DECREF (item );
2755
2731
2756
2732
if (treebuilder_append_event (self , self -> end_event_obj , self -> last ) < 0 )
@@ -2778,8 +2754,7 @@ treebuilder_handle_comment(TreeBuilderObject* self, PyObject* text)
2778
2754
if (self -> insert_comments && this != Py_None ) {
2779
2755
if (treebuilder_add_subelement (this , comment ) < 0 )
2780
2756
goto error ;
2781
- Py_INCREF (comment );
2782
- Py_XSETREF (self -> last_for_tail , comment );
2757
+ Py_XSETREF (self -> last_for_tail , Py_NewRef (comment ));
2783
2758
}
2784
2759
} else {
2785
2760
comment = Py_NewRef (text );
@@ -2818,8 +2793,7 @@ treebuilder_handle_pi(TreeBuilderObject* self, PyObject* target, PyObject* text)
2818
2793
if (self -> insert_pis && this != Py_None ) {
2819
2794
if (treebuilder_add_subelement (this , pi ) < 0 )
2820
2795
goto error ;
2821
- Py_INCREF (pi );
2822
- Py_XSETREF (self -> last_for_tail , pi );
2796
+ Py_XSETREF (self -> last_for_tail , Py_NewRef (pi ));
2823
2797
}
2824
2798
} else {
2825
2799
pi = PyTuple_Pack (2 , target , text );
@@ -3038,12 +3012,9 @@ makeuniversal(XMLParserObject* self, const char* string)
3038
3012
if (!key )
3039
3013
return NULL ;
3040
3014
3041
- value = PyDict_GetItemWithError (self -> names , key );
3015
+ value = Py_XNewRef ( PyDict_GetItemWithError (self -> names , key ) );
3042
3016
3043
- if (value ) {
3044
- Py_INCREF (value );
3045
- }
3046
- else if (!PyErr_Occurred ()) {
3017
+ if (value == NULL && !PyErr_Occurred ()) {
3047
3018
/* new name. convert to universal name, and decode as
3048
3019
necessary */
3049
3020
@@ -4029,39 +4000,37 @@ _elementtree_XMLParser__setevents_impl(XMLParserObject *self,
4029
4000
return NULL ;
4030
4001
}
4031
4002
4032
- Py_INCREF (event_name_obj );
4033
4003
if (strcmp (event_name , "start" ) == 0 ) {
4034
- Py_XSETREF (target -> start_event_obj , event_name_obj );
4004
+ Py_XSETREF (target -> start_event_obj , Py_NewRef ( event_name_obj ) );
4035
4005
} else if (strcmp (event_name , "end" ) == 0 ) {
4036
- Py_XSETREF (target -> end_event_obj , event_name_obj );
4006
+ Py_XSETREF (target -> end_event_obj , Py_NewRef ( event_name_obj ) );
4037
4007
} else if (strcmp (event_name , "start-ns" ) == 0 ) {
4038
- Py_XSETREF (target -> start_ns_event_obj , event_name_obj );
4008
+ Py_XSETREF (target -> start_ns_event_obj , Py_NewRef ( event_name_obj ) );
4039
4009
EXPAT (SetNamespaceDeclHandler )(
4040
4010
self -> parser ,
4041
4011
(XML_StartNamespaceDeclHandler ) expat_start_ns_handler ,
4042
4012
(XML_EndNamespaceDeclHandler ) expat_end_ns_handler
4043
4013
);
4044
4014
} else if (strcmp (event_name , "end-ns" ) == 0 ) {
4045
- Py_XSETREF (target -> end_ns_event_obj , event_name_obj );
4015
+ Py_XSETREF (target -> end_ns_event_obj , Py_NewRef ( event_name_obj ) );
4046
4016
EXPAT (SetNamespaceDeclHandler )(
4047
4017
self -> parser ,
4048
4018
(XML_StartNamespaceDeclHandler ) expat_start_ns_handler ,
4049
4019
(XML_EndNamespaceDeclHandler ) expat_end_ns_handler
4050
4020
);
4051
4021
} else if (strcmp (event_name , "comment" ) == 0 ) {
4052
- Py_XSETREF (target -> comment_event_obj , event_name_obj );
4022
+ Py_XSETREF (target -> comment_event_obj , Py_NewRef ( event_name_obj ) );
4053
4023
EXPAT (SetCommentHandler )(
4054
4024
self -> parser ,
4055
4025
(XML_CommentHandler ) expat_comment_handler
4056
4026
);
4057
4027
} else if (strcmp (event_name , "pi" ) == 0 ) {
4058
- Py_XSETREF (target -> pi_event_obj , event_name_obj );
4028
+ Py_XSETREF (target -> pi_event_obj , Py_NewRef ( event_name_obj ) );
4059
4029
EXPAT (SetProcessingInstructionHandler )(
4060
4030
self -> parser ,
4061
4031
(XML_ProcessingInstructionHandler ) expat_pi_handler
4062
4032
);
4063
4033
} else {
4064
- Py_DECREF (event_name_obj );
4065
4034
Py_DECREF (events_seq );
4066
4035
PyErr_Format (PyExc_ValueError , "unknown event '%s'" , event_name );
4067
4036
return NULL ;
@@ -4406,9 +4375,7 @@ PyInit__elementtree(void)
4406
4375
st -> parseerror_obj = PyErr_NewException (
4407
4376
"xml.etree.ElementTree.ParseError" , PyExc_SyntaxError , NULL
4408
4377
);
4409
- Py_INCREF (st -> parseerror_obj );
4410
- if (PyModule_AddObject (m , "ParseError" , st -> parseerror_obj ) < 0 ) {
4411
- Py_DECREF (st -> parseerror_obj );
4378
+ if (PyModule_AddObjectRef (m , "ParseError" , st -> parseerror_obj ) < 0 ) {
4412
4379
return NULL ;
4413
4380
}
4414
4381
0 commit comments