@@ -155,7 +155,7 @@ deepcopy(PyObject* object, PyObject* memo)
155
155
LOCAL (PyObject * )
156
156
list_join (PyObject * list )
157
157
{
158
- /* join list elements (destroying the list in the process) */
158
+ /* join list elements */
159
159
PyObject * joiner ;
160
160
PyObject * result ;
161
161
@@ -164,8 +164,6 @@ list_join(PyObject* list)
164
164
return NULL ;
165
165
result = PyUnicode_Join (joiner , list );
166
166
Py_DECREF (joiner );
167
- if (result )
168
- Py_DECREF (list );
169
167
return result ;
170
168
}
171
169
@@ -534,15 +532,17 @@ element_get_text(ElementObject* self)
534
532
{
535
533
/* return borrowed reference to text attribute */
536
534
537
- PyObject * res = self -> text ;
535
+ PyObject * res = self -> text ;
538
536
539
537
if (JOIN_GET (res )) {
540
538
res = JOIN_OBJ (res );
541
539
if (PyList_CheckExact (res )) {
542
- res = list_join (res );
543
- if (!res )
540
+ PyObject * tmp = list_join (res );
541
+ if (!tmp )
544
542
return NULL ;
545
- self -> text = res ;
543
+ self -> text = tmp ;
544
+ Py_DECREF (res );
545
+ res = tmp ;
546
546
}
547
547
}
548
548
@@ -554,15 +554,17 @@ element_get_tail(ElementObject* self)
554
554
{
555
555
/* return borrowed reference to text attribute */
556
556
557
- PyObject * res = self -> tail ;
557
+ PyObject * res = self -> tail ;
558
558
559
559
if (JOIN_GET (res )) {
560
560
res = JOIN_OBJ (res );
561
561
if (PyList_CheckExact (res )) {
562
- res = list_join (res );
563
- if (!res )
562
+ PyObject * tmp = list_join (res );
563
+ if (!tmp )
564
564
return NULL ;
565
- self -> tail = res ;
565
+ self -> tail = tmp ;
566
+ Py_DECREF (res );
567
+ res = tmp ;
566
568
}
567
569
}
568
570
@@ -2147,6 +2149,12 @@ elementiter_next(ElementIterObject *it)
2147
2149
cur_parent = it -> parent_stack -> parent ;
2148
2150
child_index = it -> parent_stack -> child_index ;
2149
2151
if (cur_parent -> extra && child_index < cur_parent -> extra -> length ) {
2152
+ if (!PyObject_TypeCheck (cur_parent -> extra -> children [child_index ], & Element_Type )) {
2153
+ PyErr_Format (PyExc_AttributeError ,
2154
+ "'%.100s' object has no attribute 'iter'" ,
2155
+ Py_TYPE (cur_parent -> extra -> children [child_index ])-> tp_name );
2156
+ return NULL ;
2157
+ }
2150
2158
elem = (ElementObject * )cur_parent -> extra -> children [child_index ];
2151
2159
it -> parent_stack -> child_index ++ ;
2152
2160
it -> parent_stack = parent_stack_push_new (it -> parent_stack ,
@@ -2435,40 +2443,51 @@ treebuilder_dealloc(TreeBuilderObject *self)
2435
2443
/* helpers for handling of arbitrary element-like objects */
2436
2444
2437
2445
static int
2438
- treebuilder_set_element_text_or_tail (PyObject * element , PyObject * data ,
2446
+ treebuilder_set_element_text_or_tail (PyObject * element , PyObject * * data ,
2439
2447
PyObject * * dest , _Py_Identifier * name )
2440
2448
{
2441
2449
if (Element_CheckExact (element )) {
2442
- Py_DECREF (JOIN_OBJ (* dest ));
2443
- * dest = JOIN_SET (data , PyList_CheckExact (data ));
2450
+ PyObject * tmp = JOIN_OBJ (* dest );
2451
+ * dest = JOIN_SET (* data , PyList_CheckExact (* data ));
2452
+ * data = NULL ;
2453
+ Py_DECREF (tmp );
2444
2454
return 0 ;
2445
2455
}
2446
2456
else {
2447
- PyObject * joined = list_join (data );
2457
+ PyObject * joined = list_join (* data );
2448
2458
int r ;
2449
2459
if (joined == NULL )
2450
2460
return -1 ;
2451
2461
r = _PyObject_SetAttrId (element , name , joined );
2452
2462
Py_DECREF (joined );
2453
- return r ;
2463
+ if (r < 0 )
2464
+ return -1 ;
2465
+ Py_CLEAR (* data );
2466
+ return 0 ;
2454
2467
}
2455
2468
}
2456
2469
2457
- /* These two functions steal a reference to data */
2458
- static int
2459
- treebuilder_set_element_text (PyObject * element , PyObject * data )
2470
+ LOCAL (int )
2471
+ treebuilder_flush_data (TreeBuilderObject * self )
2460
2472
{
2461
- _Py_IDENTIFIER (text );
2462
- return treebuilder_set_element_text_or_tail (
2463
- element , data , & ((ElementObject * ) element )-> text , & PyId_text );
2464
- }
2473
+ PyObject * element = self -> last ;
2465
2474
2466
- static int
2467
- treebuilder_set_element_tail (PyObject * element , PyObject * data )
2468
- {
2469
- _Py_IDENTIFIER (tail );
2470
- return treebuilder_set_element_text_or_tail (
2471
- element , data , & ((ElementObject * ) element )-> tail , & PyId_tail );
2475
+ if (!self -> data ) {
2476
+ return 0 ;
2477
+ }
2478
+
2479
+ if (self -> this == element ) {
2480
+ _Py_IDENTIFIER (text );
2481
+ return treebuilder_set_element_text_or_tail (
2482
+ element , & self -> data ,
2483
+ & ((ElementObject * ) element )-> text , & PyId_text );
2484
+ }
2485
+ else {
2486
+ _Py_IDENTIFIER (tail );
2487
+ return treebuilder_set_element_text_or_tail (
2488
+ element , & self -> data ,
2489
+ & ((ElementObject * ) element )-> tail , & PyId_tail );
2490
+ }
2472
2491
}
2473
2492
2474
2493
static int
@@ -2517,16 +2536,8 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
2517
2536
PyObject * this ;
2518
2537
elementtreestate * st = ET_STATE_GLOBAL ;
2519
2538
2520
- if (self -> data ) {
2521
- if (self -> this == self -> last ) {
2522
- if (treebuilder_set_element_text (self -> last , self -> data ))
2523
- return NULL ;
2524
- }
2525
- else {
2526
- if (treebuilder_set_element_tail (self -> last , self -> data ))
2527
- return NULL ;
2528
- }
2529
- self -> data = NULL ;
2539
+ if (treebuilder_flush_data (self ) < 0 ) {
2540
+ return NULL ;
2530
2541
}
2531
2542
2532
2543
if (self -> element_factory && self -> element_factory != Py_None ) {
@@ -2622,15 +2633,8 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
2622
2633
{
2623
2634
PyObject * item ;
2624
2635
2625
- if (self -> data ) {
2626
- if (self -> this == self -> last ) {
2627
- if (treebuilder_set_element_text (self -> last , self -> data ))
2628
- return NULL ;
2629
- } else {
2630
- if (treebuilder_set_element_tail (self -> last , self -> data ))
2631
- return NULL ;
2632
- }
2633
- self -> data = NULL ;
2636
+ if (treebuilder_flush_data (self ) < 0 ) {
2637
+ return NULL ;
2634
2638
}
2635
2639
2636
2640
if (self -> index == 0 ) {
0 commit comments