Skip to content

gh-148428: Fix file handle leak in pulldom.parse()#148437

Open
WYSIATI wants to merge 5 commits intopython:mainfrom
WYSIATI:fix-pulldom-resource-leak-148428
Open

gh-148428: Fix file handle leak in pulldom.parse()#148437
WYSIATI wants to merge 5 commits intopython:mainfrom
WYSIATI:fix-pulldom-resource-leak-148428

Conversation

@WYSIATI
Copy link
Copy Markdown
Contributor

@WYSIATI WYSIATI commented Apr 12, 2026

When pulldom.parse() is called with a filename string, it opens a file but the resulting DOMEventStream never closes it — no close(), no context manager, and clear() just sets self.stream = None without closing.

This adds resource management to DOMEventStream:

The __del__ binds _warn=warnings.warn as a default argument to handle interpreter shutdown safely, matching the pattern in ElementTree.iterparse, subprocess.Popen, etc.

User-provided streams (passed as file objects to parse(), or via parseString()) are never closed by DOMEventStream.


📚 Documentation preview 📚: https://cpython-previews--148437.org.readthedocs.build/

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.
WYSIATI added 4 commits April 12, 2026 20:59
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__.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant