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

bpo-43292: Fix file leak inET.iterparse() when not exhausted#31696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletionsLib/test/test_xml_etree.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -658,6 +658,14 @@ def test_iterparse(self):
'junk after document element: line 1, column 12')
del cm, it

# Not exhausting the iterator still closes the resource (bpo-43292)
with warnings_helper.check_no_resource_warning(self):
it = iterparse(TESTFN)
del it

with self.assertRaises(FileNotFoundError):
iterparse("nonexistent")

def test_writefile(self):
elem = ET.Element("tag")
elem.text = "text"
Expand Down
16 changes: 9 additions & 7 deletionsLib/xml/etree/ElementTree.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1244,8 +1244,14 @@ def iterparse(source, events=None, parser=None):
# Use the internal, undocumented _parser argument for now; When the
# parser argument of iterparse is removed, this can be killed.
pullparser = XMLPullParser(events=events, _parser=parser)
def iterator():

def iterator(source):
close_source = False
try:
if not hasattr(source, "read"):
source = open(source, "rb")
close_source = True
yield None
while True:
yield from pullparser.read_events()
# load event buffer
Expand All@@ -1261,16 +1267,12 @@ def iterator():
source.close()

class IterParseIterator(collections.abc.Iterator):
__next__ = iterator().__next__
__next__ = iterator(source).__next__
it = IterParseIterator()
it.root = None
del iterator, IterParseIterator

close_source = False
if not hasattr(source, "read"):
source = open(source, "rb")
close_source = True

next(it)
return it


Expand Down
1 change: 1 addition & 0 deletionsMisc/ACKS
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1879,6 +1879,7 @@ Wojtek Walczak
Charles Waldman
Richard Walker
Larry Wall
Jacob Walls
Kevin Walzer
Rodrigo Steinmuller Wanderley
Dingyuan Wang
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Fixed a file leak in :func:`xml.etree.ElementTree.iterparse` when the
iterator is not exhausted. Patch by Jacob Walls.

[8]ページ先頭

©2009-2025 Movatter.jp