Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8acbb93

Browse files
bpo-43292: Fix file leak inET.iterparse() when not exhausted (GH-31696)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>(cherry picked from commit496c428)Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
1 parentee18df4 commit8acbb93

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

‎Lib/test/test_xml_etree.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,14 @@ def test_iterparse(self):
658658
'junk after document element: line 1, column 12')
659659
delcm,it
660660

661+
# Not exhausting the iterator still closes the resource (bpo-43292)
662+
withwarnings_helper.check_no_resource_warning(self):
663+
it=iterparse(TESTFN)
664+
delit
665+
666+
withself.assertRaises(FileNotFoundError):
667+
iterparse("nonexistent")
668+
661669
deftest_writefile(self):
662670
elem=ET.Element("tag")
663671
elem.text="text"

‎Lib/xml/etree/ElementTree.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,8 +1248,14 @@ def iterparse(source, events=None, parser=None):
12481248
# Use the internal, undocumented _parser argument for now; When the
12491249
# parser argument of iterparse is removed, this can be killed.
12501250
pullparser=XMLPullParser(events=events,_parser=parser)
1251-
defiterator():
1251+
1252+
defiterator(source):
1253+
close_source=False
12521254
try:
1255+
ifnothasattr(source,"read"):
1256+
source=open(source,"rb")
1257+
close_source=True
1258+
yieldNone
12531259
whileTrue:
12541260
yieldfrompullparser.read_events()
12551261
# load event buffer
@@ -1265,16 +1271,12 @@ def iterator():
12651271
source.close()
12661272

12671273
classIterParseIterator(collections.abc.Iterator):
1268-
__next__=iterator().__next__
1274+
__next__=iterator(source).__next__
12691275
it=IterParseIterator()
12701276
it.root=None
12711277
deliterator,IterParseIterator
12721278

1273-
close_source=False
1274-
ifnothasattr(source,"read"):
1275-
source=open(source,"rb")
1276-
close_source=True
1277-
1279+
next(it)
12781280
returnit
12791281

12801282

‎Misc/ACKS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,7 @@ Wojtek Walczak
18701870
Charles Waldman
18711871
Richard Walker
18721872
Larry Wall
1873+
Jacob Walls
18731874
Kevin Walzer
18741875
Rodrigo Steinmuller Wanderley
18751876
Dingyuan Wang
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a file leak in:func:`xml.etree.ElementTree.iterparse` when the
2+
iterator is not exhausted. Patch by Jacob Walls.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp