@@ -555,6 +555,17 @@ def test_iterparse(self):
555555 ('end' , '{namespace}root' ),
556556 ])
557557
558+ with open (SIMPLE_XMLFILE , 'rb' ) as source :
559+ context = iterparse (source )
560+ action , elem = next (context )
561+ self .assertEqual ((action , elem .tag ), ('end' , 'element' ))
562+ self .assertEqual ([(action , elem .tag ) for action , elem in context ], [
563+ ('end' , 'element' ),
564+ ('end' , 'empty-element' ),
565+ ('end' , 'root' ),
566+ ])
567+ self .assertEqual (context .root .tag , 'root' )
568+
558569 events = ()
559570 context = iterparse (SIMPLE_XMLFILE , events )
560571 self .assertEqual ([(action , elem .tag ) for action , elem in context ], [])
@@ -646,12 +657,81 @@ def test_iterparse(self):
646657
647658 # Not exhausting the iterator still closes the resource (bpo-43292)
648659 with warnings_helper .check_no_resource_warning (self ):
649- it = iterparse (TESTFN )
660+ it = iterparse (SIMPLE_XMLFILE )
650661 del it
651662
663+ with warnings_helper .check_no_resource_warning (self ):
664+ it = iterparse (SIMPLE_XMLFILE )
665+ it .close ()
666+ del it
667+
668+ with warnings_helper .check_no_resource_warning (self ):
669+ it = iterparse (SIMPLE_XMLFILE )
670+ action , elem = next (it )
671+ self .assertEqual ((action , elem .tag ), ('end' , 'element' ))
672+ del it , elem
673+
674+ with warnings_helper .check_no_resource_warning (self ):
675+ it = iterparse (SIMPLE_XMLFILE )
676+ action , elem = next (it )
677+ it .close ()
678+ self .assertEqual ((action , elem .tag ), ('end' , 'element' ))
679+ del it , elem
680+
652681 with self .assertRaises (FileNotFoundError ):
653682 iterparse ("nonexistent" )
654683
684+ def test_iterparse_close (self ):
685+ iterparse = ET .iterparse
686+
687+ it = iterparse (SIMPLE_XMLFILE )
688+ it .close ()
689+ with self .assertRaises (StopIteration ):
690+ next (it )
691+ it .close () # idempotent
692+
693+ with open (SIMPLE_XMLFILE , 'rb' ) as source :
694+ it = iterparse (source )
695+ it .close ()
696+ self .assertFalse (source .closed )
697+ with self .assertRaises (StopIteration ):
698+ next (it )
699+ it .close () # idempotent
700+
701+ it = iterparse (SIMPLE_XMLFILE )
702+ action , elem = next (it )
703+ self .assertEqual ((action , elem .tag ), ('end' , 'element' ))
704+ it .close ()
705+ with self .assertRaises (StopIteration ):
706+ next (it )
707+ it .close () # idempotent
708+
709+ with open (SIMPLE_XMLFILE , 'rb' ) as source :
710+ it = iterparse (source )
711+ action , elem = next (it )
712+ self .assertEqual ((action , elem .tag ), ('end' , 'element' ))
713+ it .close ()
714+ self .assertFalse (source .closed )
715+ with self .assertRaises (StopIteration ):
716+ next (it )
717+ it .close () # idempotent
718+
719+ it = iterparse (SIMPLE_XMLFILE )
720+ list (it )
721+ it .close ()
722+ with self .assertRaises (StopIteration ):
723+ next (it )
724+ it .close () # idempotent
725+
726+ with open (SIMPLE_XMLFILE , 'rb' ) as source :
727+ it = iterparse (source )
728+ list (it )
729+ it .close ()
730+ self .assertFalse (source .closed )
731+ with self .assertRaises (StopIteration ):
732+ next (it )
733+ it .close () # idempotent
734+
655735 def test_writefile (self ):
656736 elem = ET .Element ("tag" )
657737 elem .text = "text"
@@ -3044,8 +3124,7 @@ def test_basic(self):
30443124 # With an explicit parser too (issue #9708)
30453125 sourcefile = serialize (doc , to_string = False )
30463126 parser = ET .XMLParser (target = ET .TreeBuilder ())
3047- self .assertEqual (next (ET .iterparse (sourcefile , parser = parser ))[0 ],
3048- 'end' )
3127+ self .assertEqual (next (ET .iterparse (sourcefile , parser = parser ))[0 ], 'end' )
30493128
30503129 tree = ET .ElementTree (None )
30513130 self .assertRaises (AttributeError , tree .iter )
0 commit comments