@@ -170,7 +170,7 @@ is_empty_dict(PyObject *obj)
170
170
171
171
typedef struct {
172
172
173
- /* attributes (a dictionary object), or None if no attributes */
173
+ /* attributes (a dictionary object), or NULL if no attributes */
174
174
PyObject * attrib ;
175
175
176
176
/* child elements */
@@ -225,10 +225,7 @@ create_extra(ElementObject* self, PyObject* attrib)
225
225
return -1 ;
226
226
}
227
227
228
- if (!attrib )
229
- attrib = Py_None ;
230
-
231
- Py_INCREF (attrib );
228
+ Py_XINCREF (attrib );
232
229
self -> extra -> attrib = attrib ;
233
230
234
231
self -> extra -> length = 0 ;
@@ -246,7 +243,7 @@ dealloc_extra(ElementObjectExtra *extra)
246
243
if (!extra )
247
244
return ;
248
245
249
- Py_DECREF (extra -> attrib );
246
+ Py_XDECREF (extra -> attrib );
250
247
251
248
for (i = 0 ; i < extra -> length ; i ++ )
252
249
Py_DECREF (extra -> children [i ]);
@@ -300,7 +297,7 @@ create_new_element(PyObject* tag, PyObject* attrib)
300
297
ALLOC (sizeof (ElementObject ), "create element" );
301
298
PyObject_GC_Track (self );
302
299
303
- if (attrib != Py_None && !is_empty_dict (attrib )) {
300
+ if (attrib != NULL && !is_empty_dict (attrib )) {
304
301
if (create_extra (self , attrib ) < 0 ) {
305
302
Py_DECREF (self );
306
303
return NULL ;
@@ -530,13 +527,9 @@ element_get_attrib(ElementObject* self)
530
527
531
528
PyObject * res = self -> extra -> attrib ;
532
529
533
- if (res == Py_None ) {
530
+ if (! res ) {
534
531
/* create missing dictionary */
535
- res = PyDict_New ();
536
- if (!res )
537
- return NULL ;
538
- Py_DECREF (Py_None );
539
- self -> extra -> attrib = res ;
532
+ res = self -> extra -> attrib = PyDict_New ();
540
533
}
541
534
542
535
return res ;
@@ -616,12 +609,10 @@ subelement(PyObject *self, PyObject *args, PyObject *kwds)
616
609
return NULL ;
617
610
} else {
618
611
/* no attrib arg, no kwds, so no attribute */
619
- Py_INCREF (Py_None );
620
- attrib = Py_None ;
621
612
}
622
613
623
614
elem = create_new_element (tag , attrib );
624
- Py_DECREF (attrib );
615
+ Py_XDECREF (attrib );
625
616
if (elem == NULL )
626
617
return NULL ;
627
618
@@ -736,7 +727,7 @@ _elementtree_Element___copy___impl(ElementObject *self)
736
727
ElementObject * element ;
737
728
738
729
element = (ElementObject * ) create_new_element (
739
- self -> tag , ( self -> extra ) ? self -> extra -> attrib : Py_None );
730
+ self -> tag , self -> extra ? self -> extra -> attrib : NULL );
740
731
if (!element )
741
732
return NULL ;
742
733
@@ -792,21 +783,20 @@ _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo)
792
783
if (!tag )
793
784
return NULL ;
794
785
795
- if (self -> extra ) {
786
+ if (self -> extra && self -> extra -> attrib ) {
796
787
attrib = deepcopy (self -> extra -> attrib , memo );
797
788
if (!attrib ) {
798
789
Py_DECREF (tag );
799
790
return NULL ;
800
791
}
801
792
} else {
802
- Py_INCREF (Py_None );
803
- attrib = Py_None ;
793
+ attrib = NULL ;
804
794
}
805
795
806
796
element = (ElementObject * ) create_new_element (tag , attrib );
807
797
808
798
Py_DECREF (tag );
809
- Py_DECREF (attrib );
799
+ Py_XDECREF (attrib );
810
800
811
801
if (!element )
812
802
return NULL ;
@@ -963,7 +953,7 @@ _elementtree_Element___getstate___impl(ElementObject *self)
963
953
PyList_SET_ITEM (children , i , child );
964
954
}
965
955
966
- if (self -> extra && self -> extra -> attrib != Py_None ) {
956
+ if (self -> extra && self -> extra -> attrib ) {
967
957
attrib = self -> extra -> attrib ;
968
958
Py_INCREF (attrib );
969
959
}
@@ -1037,9 +1027,9 @@ element_setstate_from_attributes(ElementObject *self,
1037
1027
assert (self -> extra );
1038
1028
assert (self -> extra -> allocated >= nchildren );
1039
1029
if (oldextra ) {
1040
- assert (self -> extra -> attrib == Py_None );
1030
+ assert (self -> extra -> attrib == NULL );
1041
1031
self -> extra -> attrib = oldextra -> attrib ;
1042
- oldextra -> attrib = Py_None ;
1032
+ oldextra -> attrib = NULL ;
1043
1033
}
1044
1034
1045
1035
/* Copy children */
@@ -1065,10 +1055,8 @@ element_setstate_from_attributes(ElementObject *self,
1065
1055
}
1066
1056
1067
1057
/* Stash attrib. */
1068
- if (attrib ) {
1069
- Py_INCREF (attrib );
1070
- Py_XSETREF (self -> extra -> attrib , attrib );
1071
- }
1058
+ Py_XINCREF (attrib );
1059
+ Py_XSETREF (self -> extra -> attrib , attrib );
1072
1060
dealloc_extra (oldextra );
1073
1061
1074
1062
Py_RETURN_NONE ;
@@ -1401,7 +1389,7 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key,
1401
1389
{
1402
1390
PyObject * value ;
1403
1391
1404
- if (!self -> extra || self -> extra -> attrib == Py_None )
1392
+ if (!self -> extra || ! self -> extra -> attrib )
1405
1393
value = default_value ;
1406
1394
else {
1407
1395
value = PyDict_GetItemWithError (self -> extra -> attrib , key );
@@ -1529,7 +1517,7 @@ static PyObject *
1529
1517
_elementtree_Element_items_impl (ElementObject * self )
1530
1518
/*[clinic end generated code: output=6db2c778ce3f5a4d input=adbe09aaea474447]*/
1531
1519
{
1532
- if (!self -> extra || self -> extra -> attrib == Py_None )
1520
+ if (!self -> extra || ! self -> extra -> attrib )
1533
1521
return PyList_New (0 );
1534
1522
1535
1523
return PyDict_Items (self -> extra -> attrib );
@@ -1544,7 +1532,7 @@ static PyObject *
1544
1532
_elementtree_Element_keys_impl (ElementObject * self )
1545
1533
/*[clinic end generated code: output=bc5bfabbf20eeb3c input=f02caf5b496b5b0b]*/
1546
1534
{
1547
- if (!self -> extra || self -> extra -> attrib == Py_None )
1535
+ if (!self -> extra || ! self -> extra -> attrib )
1548
1536
return PyList_New (0 );
1549
1537
1550
1538
return PyDict_Keys (self -> extra -> attrib );
@@ -1563,15 +1551,15 @@ element_length(ElementObject* self)
1563
1551
_elementtree.Element.makeelement
1564
1552
1565
1553
tag: object
1566
- attrib: object
1554
+ attrib: object(subclass_of='&PyDict_Type')
1567
1555
/
1568
1556
1569
1557
[clinic start generated code]*/
1570
1558
1571
1559
static PyObject *
1572
1560
_elementtree_Element_makeelement_impl (ElementObject * self , PyObject * tag ,
1573
1561
PyObject * attrib )
1574
- /*[clinic end generated code: output=4109832d5bb789ef input=9480d1d2e3e68235 ]*/
1562
+ /*[clinic end generated code: output=4109832d5bb789ef input=2279d974529c3861 ]*/
1575
1563
{
1576
1564
PyObject * elem ;
1577
1565
@@ -2043,12 +2031,18 @@ static int
2043
2031
element_attrib_setter (ElementObject * self , PyObject * value , void * closure )
2044
2032
{
2045
2033
_VALIDATE_ATTR_VALUE (value );
2034
+ if (!PyDict_Check (value )) {
2035
+ PyErr_Format (PyExc_TypeError ,
2036
+ "attrib must be dict, not %.200s" ,
2037
+ value -> ob_type -> tp_name );
2038
+ return -1 ;
2039
+ }
2046
2040
if (!self -> extra ) {
2047
2041
if (create_extra (self , NULL ) < 0 )
2048
2042
return -1 ;
2049
2043
}
2050
2044
Py_INCREF (value );
2051
- Py_SETREF (self -> extra -> attrib , value );
2045
+ Py_XSETREF (self -> extra -> attrib , value );
2052
2046
return 0 ;
2053
2047
}
2054
2048
@@ -2688,7 +2682,7 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
2688
2682
2689
2683
if (!self -> element_factory ) {
2690
2684
node = create_new_element (tag , attrib );
2691
- } else if (attrib == Py_None ) {
2685
+ } else if (attrib == NULL ) {
2692
2686
attrib = PyDict_New ();
2693
2687
if (!attrib )
2694
2688
return NULL ;
@@ -3297,8 +3291,7 @@ expat_start_handler(XMLParserObject* self, const XML_Char* tag_in,
3297
3291
attrib_in += 2 ;
3298
3292
}
3299
3293
} else {
3300
- Py_INCREF (Py_None );
3301
- attrib = Py_None ;
3294
+ attrib = NULL ;
3302
3295
}
3303
3296
3304
3297
if (TreeBuilder_CheckExact (self -> target )) {
@@ -3307,8 +3300,7 @@ expat_start_handler(XMLParserObject* self, const XML_Char* tag_in,
3307
3300
tag , attrib );
3308
3301
}
3309
3302
else if (self -> handle_start ) {
3310
- if (attrib == Py_None ) {
3311
- Py_DECREF (attrib );
3303
+ if (attrib == NULL ) {
3312
3304
attrib = PyDict_New ();
3313
3305
if (!attrib ) {
3314
3306
Py_DECREF (tag );
@@ -3321,7 +3313,7 @@ expat_start_handler(XMLParserObject* self, const XML_Char* tag_in,
3321
3313
res = NULL ;
3322
3314
3323
3315
Py_DECREF (tag );
3324
- Py_DECREF (attrib );
3316
+ Py_XDECREF (attrib );
3325
3317
3326
3318
Py_XDECREF (res );
3327
3319
}
0 commit comments