@@ -480,11 +480,24 @@ element_resize(ElementObject* self, Py_ssize_t extra)
480
480
return -1 ;
481
481
}
482
482
483
+ LOCAL (void )
484
+ raise_type_error (PyObject * element )
485
+ {
486
+ PyErr_Format (PyExc_TypeError ,
487
+ "expected an Element, not \"%.200s\"" ,
488
+ Py_TYPE (element )-> tp_name );
489
+ }
490
+
483
491
LOCAL (int )
484
492
element_add_subelement (ElementObject * self , PyObject * element )
485
493
{
486
494
/* add a child element to a parent */
487
495
496
+ if (!Element_Check (element )) {
497
+ raise_type_error (element );
498
+ return -1 ;
499
+ }
500
+
488
501
if (element_resize (self , 1 ) < 0 )
489
502
return -1 ;
490
503
@@ -803,7 +816,11 @@ _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo)
803
816
804
817
for (i = 0 ; i < self -> extra -> length ; i ++ ) {
805
818
PyObject * child = deepcopy (self -> extra -> children [i ], memo );
806
- if (!child ) {
819
+ if (!child || !Element_Check (child )) {
820
+ if (child ) {
821
+ raise_type_error (child );
822
+ Py_DECREF (child );
823
+ }
807
824
element -> extra -> length = i ;
808
825
goto error ;
809
826
}
@@ -1024,8 +1041,15 @@ element_setstate_from_attributes(ElementObject *self,
1024
1041
1025
1042
/* Copy children */
1026
1043
for (i = 0 ; i < nchildren ; i ++ ) {
1027
- self -> extra -> children [i ] = PyList_GET_ITEM (children , i );
1028
- Py_INCREF (self -> extra -> children [i ]);
1044
+ PyObject * child = PyList_GET_ITEM (children , i );
1045
+ if (!Element_Check (child )) {
1046
+ raise_type_error (child );
1047
+ self -> extra -> length = i ;
1048
+ dealloc_extra (oldextra );
1049
+ return NULL ;
1050
+ }
1051
+ Py_INCREF (child );
1052
+ self -> extra -> children [i ] = child ;
1029
1053
}
1030
1054
1031
1055
assert (!self -> extra -> length );
@@ -1167,16 +1191,6 @@ _elementtree_Element_extend(ElementObject *self, PyObject *elements)
1167
1191
for (i = 0 ; i < PySequence_Fast_GET_SIZE (seq ); i ++ ) {
1168
1192
PyObject * element = PySequence_Fast_GET_ITEM (seq , i );
1169
1193
Py_INCREF (element );
1170
- if (!Element_Check (element )) {
1171
- PyErr_Format (
1172
- PyExc_TypeError ,
1173
- "expected an Element, not \"%.200s\"" ,
1174
- Py_TYPE (element )-> tp_name );
1175
- Py_DECREF (seq );
1176
- Py_DECREF (element );
1177
- return NULL ;
1178
- }
1179
-
1180
1194
if (element_add_subelement (self , element ) < 0 ) {
1181
1195
Py_DECREF (seq );
1182
1196
Py_DECREF (element );
@@ -1219,8 +1233,7 @@ _elementtree_Element_find_impl(ElementObject *self, PyObject *path,
1219
1233
for (i = 0 ; i < self -> extra -> length ; i ++ ) {
1220
1234
PyObject * item = self -> extra -> children [i ];
1221
1235
int rc ;
1222
- if (!Element_Check (item ))
1223
- continue ;
1236
+ assert (Element_Check (item ));
1224
1237
Py_INCREF (item );
1225
1238
rc = PyObject_RichCompareBool (((ElementObject * )item )-> tag , path , Py_EQ );
1226
1239
if (rc > 0 )
@@ -1266,8 +1279,7 @@ _elementtree_Element_findtext_impl(ElementObject *self, PyObject *path,
1266
1279
for (i = 0 ; i < self -> extra -> length ; i ++ ) {
1267
1280
PyObject * item = self -> extra -> children [i ];
1268
1281
int rc ;
1269
- if (!Element_Check (item ))
1270
- continue ;
1282
+ assert (Element_Check (item ));
1271
1283
Py_INCREF (item );
1272
1284
rc = PyObject_RichCompareBool (((ElementObject * )item )-> tag , path , Py_EQ );
1273
1285
if (rc > 0 ) {
@@ -1323,8 +1335,7 @@ _elementtree_Element_findall_impl(ElementObject *self, PyObject *path,
1323
1335
for (i = 0 ; i < self -> extra -> length ; i ++ ) {
1324
1336
PyObject * item = self -> extra -> children [i ];
1325
1337
int rc ;
1326
- if (!Element_Check (item ))
1327
- continue ;
1338
+ assert (Element_Check (item ));
1328
1339
Py_INCREF (item );
1329
1340
rc = PyObject_RichCompareBool (((ElementObject * )item )-> tag , path , Py_EQ );
1330
1341
if (rc != 0 && (rc < 0 || PyList_Append (out , item ) < 0 )) {
@@ -1736,6 +1747,10 @@ element_setitem(PyObject* self_, Py_ssize_t index, PyObject* item)
1736
1747
old = self -> extra -> children [index ];
1737
1748
1738
1749
if (item ) {
1750
+ if (!Element_Check (item )) {
1751
+ raise_type_error (item );
1752
+ return -1 ;
1753
+ }
1739
1754
Py_INCREF (item );
1740
1755
self -> extra -> children [index ] = item ;
1741
1756
} else {
@@ -1930,6 +1945,15 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
1930
1945
}
1931
1946
}
1932
1947
1948
+ for (i = 0 ; i < newlen ; i ++ ) {
1949
+ PyObject * element = PySequence_Fast_GET_ITEM (seq , i );
1950
+ if (!Element_Check (element )) {
1951
+ raise_type_error (element );
1952
+ Py_DECREF (seq );
1953
+ return -1 ;
1954
+ }
1955
+ }
1956
+
1933
1957
if (slicelen > 0 ) {
1934
1958
/* to avoid recursive calls to this method (via decref), move
1935
1959
old items to the recycle bin here, and get rid of them when
@@ -2207,12 +2231,7 @@ elementiter_next(ElementIterObject *it)
2207
2231
continue ;
2208
2232
}
2209
2233
2210
- if (!Element_Check (extra -> children [child_index ])) {
2211
- PyErr_Format (PyExc_AttributeError ,
2212
- "'%.100s' object has no attribute 'iter'" ,
2213
- Py_TYPE (extra -> children [child_index ])-> tp_name );
2214
- return NULL ;
2215
- }
2234
+ assert (Element_Check (extra -> children [child_index ]));
2216
2235
elem = (ElementObject * )extra -> children [child_index ];
2217
2236
item -> child_index ++ ;
2218
2237
Py_INCREF (elem );
0 commit comments