@@ -411,14 +411,10 @@ element_init(PyObject *self, PyObject *args, PyObject *kwds)
411411 Py_XDECREF (attrib );
412412
413413 /* 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 ));
416415
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 ));
422418
423419 return 0 ;
424420}
@@ -690,11 +686,8 @@ _elementtree_Element_clear_impl(ElementObject *self)
690686{
691687 clear_extra (self );
692688
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 ));
698691
699692 Py_RETURN_NONE ;
700693}
@@ -970,8 +963,7 @@ element_setstate_from_attributes(ElementObject *self,
970963 return NULL ;
971964 }
972965
973- Py_INCREF (tag );
974- Py_XSETREF (self -> tag , tag );
966+ Py_XSETREF (self -> tag , Py_NewRef (tag ));
975967
976968 text = text ? JOIN_SET (text , PyList_CheckExact (text )) : Py_None ;
977969 Py_INCREF (JOIN_OBJ (text ));
@@ -1035,8 +1027,7 @@ element_setstate_from_attributes(ElementObject *self,
10351027 }
10361028
10371029 /* Stash attrib. */
1038- Py_XINCREF (attrib );
1039- Py_XSETREF (self -> extra -> attrib , attrib );
1030+ Py_XSETREF (self -> extra -> attrib , Py_XNewRef (attrib ));
10401031 dealloc_extra (oldextra );
10411032
10421033 Py_RETURN_NONE ;
@@ -1173,8 +1164,7 @@ _elementtree_Element_extend(ElementObject *self, PyObject *elements)
11731164 }
11741165
11751166 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 ));
11781168 if (element_add_subelement (self , element ) < 0 ) {
11791169 Py_DECREF (seq );
11801170 Py_DECREF (element );
@@ -1363,8 +1353,7 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key,
13631353{
13641354 if (self -> extra && self -> extra -> attrib ) {
13651355 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 ));
13681357 Py_DECREF (attrib );
13691358 if (value != NULL || PyErr_Occurred ()) {
13701359 return value ;
@@ -1964,26 +1953,23 @@ static int
19641953element_tag_setter (ElementObject * self , PyObject * value , void * closure )
19651954{
19661955 _VALIDATE_ATTR_VALUE (value );
1967- Py_INCREF (value );
1968- Py_SETREF (self -> tag , value );
1956+ Py_SETREF (self -> tag , Py_NewRef (value ));
19691957 return 0 ;
19701958}
19711959
19721960static int
19731961element_text_setter (ElementObject * self , PyObject * value , void * closure )
19741962{
19751963 _VALIDATE_ATTR_VALUE (value );
1976- Py_INCREF (value );
1977- _set_joined_ptr (& self -> text , value );
1964+ _set_joined_ptr (& self -> text , Py_NewRef (value ));
19781965 return 0 ;
19791966}
19801967
19811968static int
19821969element_tail_setter (ElementObject * self , PyObject * value , void * closure )
19831970{
19841971 _VALIDATE_ATTR_VALUE (value );
1985- Py_INCREF (value );
1986- _set_joined_ptr (& self -> tail , value );
1972+ _set_joined_ptr (& self -> tail , Py_NewRef (value ));
19871973 return 0 ;
19881974}
19891975
@@ -2001,8 +1987,7 @@ element_attrib_setter(ElementObject *self, PyObject *value, void *closure)
20011987 if (create_extra (self , NULL ) < 0 )
20021988 return -1 ;
20031989 }
2004- Py_INCREF (value );
2005- Py_XSETREF (self -> extra -> attrib , value );
1990+ Py_XSETREF (self -> extra -> attrib , Py_NewRef (value ));
20061991 return 0 ;
20071992}
20081993
@@ -2149,9 +2134,8 @@ elementiter_next(ElementIterObject *it)
21492134 }
21502135
21512136 assert (Element_Check (extra -> children [child_index ]));
2152- elem = (ElementObject * )extra -> children [child_index ];
2137+ elem = (ElementObject * )Py_NewRef ( extra -> children [child_index ]) ;
21532138 item -> child_index ++ ;
2154- Py_INCREF (elem );
21552139 }
21562140
21572141 if (parent_stack_push_new (it , elem ) < 0 ) {
@@ -2364,8 +2348,7 @@ _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self,
23642348/*[clinic end generated code: output=8571d4dcadfdf952 input=ae98a94df20b5cc3]*/
23652349{
23662350 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 ));
23692352 } else {
23702353 Py_CLEAR (self -> element_factory );
23712354 }
@@ -2375,8 +2358,7 @@ _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self,
23752358 comment_factory = st -> comment_factory ;
23762359 }
23772360 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 ));
23802362 self -> insert_comments = insert_comments ;
23812363 } else {
23822364 Py_CLEAR (self -> comment_factory );
@@ -2388,8 +2370,7 @@ _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self,
23882370 pi_factory = st -> pi_factory ;
23892371 }
23902372 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 ));
23932374 self -> insert_pis = insert_pis ;
23942375 } else {
23952376 Py_CLEAR (self -> pi_factory );
@@ -2492,14 +2473,12 @@ _elementtree__set_factories_impl(PyObject *module, PyObject *comment_factory,
24922473 if (comment_factory == Py_None ) {
24932474 Py_CLEAR (st -> comment_factory );
24942475 } else {
2495- Py_INCREF (comment_factory );
2496- Py_XSETREF (st -> comment_factory , comment_factory );
2476+ Py_XSETREF (st -> comment_factory , Py_NewRef (comment_factory ));
24972477 }
24982478 if (pi_factory == Py_None ) {
24992479 Py_CLEAR (st -> pi_factory );
25002480 } else {
2501- Py_INCREF (pi_factory );
2502- Py_XSETREF (st -> pi_factory , pi_factory );
2481+ Py_XSETREF (st -> pi_factory , Py_NewRef (pi_factory ));
25032482 }
25042483
25052484 return old ;
@@ -2676,10 +2655,8 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
26762655 }
26772656 self -> index ++ ;
26782657
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 ));
26832660
26842661 if (treebuilder_append_event (self , self -> start_event_obj , node ) < 0 )
26852662 goto error ;
@@ -2719,9 +2696,9 @@ treebuilder_handle_data(TreeBuilderObject* self, PyObject* data)
27192696 PyObject * list = PyList_New (2 );
27202697 if (!list )
27212698 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 ) ;
27252702 }
27262703 }
27272704
@@ -2749,8 +2726,7 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
27492726 self -> last = Py_NewRef (self -> this );
27502727 Py_XSETREF (self -> last_for_tail , self -> last );
27512728 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 ));
27542730 Py_DECREF (item );
27552731
27562732 if (treebuilder_append_event (self , self -> end_event_obj , self -> last ) < 0 )
@@ -2778,8 +2754,7 @@ treebuilder_handle_comment(TreeBuilderObject* self, PyObject* text)
27782754 if (self -> insert_comments && this != Py_None ) {
27792755 if (treebuilder_add_subelement (this , comment ) < 0 )
27802756 goto error ;
2781- Py_INCREF (comment );
2782- Py_XSETREF (self -> last_for_tail , comment );
2757+ Py_XSETREF (self -> last_for_tail , Py_NewRef (comment ));
27832758 }
27842759 } else {
27852760 comment = Py_NewRef (text );
@@ -2818,8 +2793,7 @@ treebuilder_handle_pi(TreeBuilderObject* self, PyObject* target, PyObject* text)
28182793 if (self -> insert_pis && this != Py_None ) {
28192794 if (treebuilder_add_subelement (this , pi ) < 0 )
28202795 goto error ;
2821- Py_INCREF (pi );
2822- Py_XSETREF (self -> last_for_tail , pi );
2796+ Py_XSETREF (self -> last_for_tail , Py_NewRef (pi ));
28232797 }
28242798 } else {
28252799 pi = PyTuple_Pack (2 , target , text );
@@ -3038,12 +3012,9 @@ makeuniversal(XMLParserObject* self, const char* string)
30383012 if (!key )
30393013 return NULL ;
30403014
3041- value = PyDict_GetItemWithError (self -> names , key );
3015+ value = Py_XNewRef ( PyDict_GetItemWithError (self -> names , key ) );
30423016
3043- if (value ) {
3044- Py_INCREF (value );
3045- }
3046- else if (!PyErr_Occurred ()) {
3017+ if (value == NULL && !PyErr_Occurred ()) {
30473018 /* new name. convert to universal name, and decode as
30483019 necessary */
30493020
@@ -4029,39 +4000,37 @@ _elementtree_XMLParser__setevents_impl(XMLParserObject *self,
40294000 return NULL ;
40304001 }
40314002
4032- Py_INCREF (event_name_obj );
40334003 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 ) );
40354005 } 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 ) );
40374007 } 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 ) );
40394009 EXPAT (SetNamespaceDeclHandler )(
40404010 self -> parser ,
40414011 (XML_StartNamespaceDeclHandler ) expat_start_ns_handler ,
40424012 (XML_EndNamespaceDeclHandler ) expat_end_ns_handler
40434013 );
40444014 } 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 ) );
40464016 EXPAT (SetNamespaceDeclHandler )(
40474017 self -> parser ,
40484018 (XML_StartNamespaceDeclHandler ) expat_start_ns_handler ,
40494019 (XML_EndNamespaceDeclHandler ) expat_end_ns_handler
40504020 );
40514021 } 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 ) );
40534023 EXPAT (SetCommentHandler )(
40544024 self -> parser ,
40554025 (XML_CommentHandler ) expat_comment_handler
40564026 );
40574027 } 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 ) );
40594029 EXPAT (SetProcessingInstructionHandler )(
40604030 self -> parser ,
40614031 (XML_ProcessingInstructionHandler ) expat_pi_handler
40624032 );
40634033 } else {
4064- Py_DECREF (event_name_obj );
40654034 Py_DECREF (events_seq );
40664035 PyErr_Format (PyExc_ValueError , "unknown event '%s'" , event_name );
40674036 return NULL ;
@@ -4406,9 +4375,7 @@ PyInit__elementtree(void)
44064375 st -> parseerror_obj = PyErr_NewException (
44074376 "xml.etree.ElementTree.ParseError" , PyExc_SyntaxError , NULL
44084377 );
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 ) {
44124379 return NULL ;
44134380 }
44144381
0 commit comments