gh-148428: Fix file handle leak in pulldom.parse()#148437
Open
WYSIATI wants to merge 4 commits intopython:mainfrom
Open
gh-148428: Fix file handle leak in pulldom.parse()#148437WYSIATI wants to merge 4 commits intopython:mainfrom
WYSIATI wants to merge 4 commits intopython:mainfrom
Conversation
When pulldom.parse() is called with a filename, the opened file handle is never closed. Add close(), context manager support, and __del__ with ResourceWarning to DOMEventStream. Update clear() to close owned streams.
Keep the existing test_parse unchanged; context manager behavior is covered by the new dedicated tests.
Remove test_context_manager_closes_file (covered by test_close_is_idempotent) and revert the unrelated modification to test_parse.
Use handler.close() instead of handler.stream.close() in addCleanup so the DOMEventStream is properly closed before GC triggers __del__.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
pulldom.parse()is called with a filename string, it opens a file but the resultingDOMEventStreamnever closes it — noclose(), no context manager, andclear()just setsself.stream = Nonewithout closing.This adds resource management to
DOMEventStream:parse()opened the file via_owns_streamclose()that closes owned streams only__enter__/__exit__forwithstatement support__del__withResourceWarning(follows theiterparsepattern from xml.etree.ElementTree.iterparse() should emit ResourceWarning when not explicitly closed #140601)clear()to callclose()before releasing referencesThe
__del__binds_warn=warnings.warnas a default argument to handle interpreter shutdown safely, matching the pattern inElementTree.iterparse,subprocess.Popen, etc.User-provided streams (passed as file objects to
parse(), or viaparseString()) are never closed byDOMEventStream.📚 Documentation preview 📚: https://cpython-previews--148437.org.readthedocs.build/