You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
xml.etree.ElementTree.iterparse() should emit ResourceWarning when not explicitly closed #140601
The iterparse() function in xml.etree.ElementTree has a TODO comment (line 1270) requesting that a ResourceWarning be emitted when the iterator is garbage collected without being explicitly closed:
def__del__(self):
# TODO: Emit a ResourceWarning if it was not explicitly closed.# (When the close() method will be supported in all maintained Python versions.)ifclose_source:
source.close()
The close() method has been available for many years now, so this TODO can be implemented.
Problem
When iterparse() opens a file by filename (rather than accepting a pre-opened file object), it manages the file internally. If the iterator is not explicitly closed, the file handle remains open until garbage collection, which can lead to:
File descriptor leaks
Files remaining locked on Windows
Resource exhaustion in long-running programs
Currently, no warning is issued to alert developers of this problem.
Proposed Solution
Implement the TODO by:
Adding a _closed flag to track whether close() has been called
Emitting a ResourceWarning in __del__() if the iterator was not closed and had opened a file internally (close_source == True)
The warning should only apply when iterparse() opened the file itself (filename passed), not when a file object was provided
Example
Current behavior (no warning):
importxml.etree.ElementTreeasET# This leaks a file handle but gives no warningcontext=ET.iterparse('data.xml')
next(context)
# Iterator goes out of scope without close()
Expected behavior (with fix):
importxml.etree.ElementTreeasET# This should emit ResourceWarningcontext=ET.iterparse('data.xml')
next(context)
# ResourceWarning: unclosed file <_io.BufferedReader name='data.xml'># Proper usage (no warning):context=ET.iterparse('data.xml')
next(context)
context.close() # Explicit close
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Activity
pythongh-140601: Add ResourceWarning to iterparse when not closed
pythongh-140601: Add ResourceWarning to iterparse when not closed
pythongh-140601: Add ResourceWarning to iterparse when not closed
pythongh-140601: Add ResourceWarning to iterparse when not closed
pythongh-140601: Add ResourceWarning to iterparse when not closed
pythongh-140601: Add ResourceWarning to iterparse when not closed
pythongh-140601: Add ResourceWarning to iterparse when not closed
pythongh-140601: Refactor ElementTree.iterparse() tests
gh-140601: Refactor ElementTree.iterparse() tests (GH-141499)
pythongh-140601: Refactor ElementTree.iterparse() tests (pythonGH-141499
pythongh-140601: Refactor ElementTree.iterparse() tests (pythonGH-141499
[3.13] gh-140601: Refactor ElementTree.iterparse() tests (GH-141499) (G…
[3.14] gh-140601: Refactor ElementTree.iterparse() tests (GH-141499) (G…
gh-140601: Add ResourceWarning to iterparse when not closed (GH-140603)
serhiy-storchaka commentedon Nov 13, 2025
Thank you for your contribution @osamakader.
osamakader commentedon Nov 13, 2025
Many thanks @serhiy-storchaka for your support.
Merge branch 'main' into fix-issue21914
Merge branch 'main' of github.com:python/cpython into fix-Issue18387
Merge branch 'main' of github.com:python/cpython into fix-issue24255
pythongh-140601: Refactor ElementTree.iterparse() tests (pythonGH-141499
pythongh-140601: Add ResourceWarning to iterparse when not closed (py…
Fix unit test with Python 3.15.0a3
Fix unit test with Python 3.15.0a3
Fix unit test with Python 3.15.0a3