Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-43292: Fix file leak in ET.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update ElementTree.py
  • Loading branch information
serhiy-storchaka authored Mar 6, 2022
commit b29dba75e02c47a91dc94b4ce199070491c49db5
13 changes: 7 additions & 6 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,13 +1245,13 @@ def iterparse(source, events=None, parser=None):
# parser argument of iterparse is removed, this can be killed.
pullparser = XMLPullParser(events=events, _parser=parser)

def iterator():
nonlocal source
def iterator(source):
close_source = False
if not hasattr(source, "read"):
source = open(source, "rb")
close_source = True
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 @@ -1267,11 +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

next(it)
return it


Expand Down