Fix ResourceWarning from unclosed file handles#1362
Open
rdcrampton wants to merge 2 commits into
Open
Conversation
Add __del__ to PDF class to close internally-opened file handles when the object is garbage collected without an explicit close() call. This prevents ResourceWarning in Python 3.11+ when users don't use the context manager pattern. Also fix tests that were not properly closing PDF handles. Closes jsvine#1336
Update __del__ to emit ResourceWarning before closing, matching Python stdlib behavior (io.FileIO, zipfile.ZipFile). This actively nudges users toward context managers rather than silently cleaning up. Also adds docstring to open() promoting the context manager pattern, and updates tests to verify the warning is emitted when close() is forgotten but not when using 'with' or explicit close(). Addresses feedback from jsvine#1336.
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.
Summary
When
pdfplumber.open()is called with a file path (not a stream), it opens an internal file handle. If the user doesn't use awithstatement or callclose(), that handle leaks until garbage collection, causing aResourceWarningin Python 3.11+.This adds a
__del__method to thePDFclass that closes internally-opened file handles as a safety net. External streams (passed by the caller) are never closed by the destructor, matching the existing behavior inclose().Changes
pdfplumber/pdf.py: Added__del__method that defensively closes internal file handles on garbage collection. Usesgetattr(self, "stream_is_external", True)to safely handle partially-initialized objects.tests/test_resource_warning.py: New test file verifying noResourceWarningis emitted with context manager usage, destructor cleanup, and that external streams are left open.tests/test_mcids.py: Wrappedpdfplumber.open()inwithto fix leaked file handle.tests/test_utils.py: Added missingself.pdf_scotus.close()inteardown_class.CHANGELOG.md: Added entry under Unreleased.README.md: Added to contributors list.Test plan
python -W all -m pytestpasses with noResourceWarningfrom pdfplumbermake lintpasses (black, isort, flake8, mypy --strict)Closes #1336