Skip to content

Fix ResourceWarning from unclosed internal file handles (#1336)#1363

Open
ReinerBRO wants to merge 1 commit into
jsvine:developfrom
ReinerBRO:fix/resourcewarning-finalizer
Open

Fix ResourceWarning from unclosed internal file handles (#1336)#1363
ReinerBRO wants to merge 1 commit into
jsvine:developfrom
ReinerBRO:fix/resourcewarning-finalizer

Conversation

@ReinerBRO
Copy link
Copy Markdown

Summary: Fixes #1336 by ensuring internally-opened file streams are closed even when callers do not use a context manager or call close explicitly. This version uses weakref.finalize as a GC safety net and keeps external stream behavior unchanged.

Changes:

  • Add _close_stream_if_internal helper in pdfplumber/pdf.py.
  • Register a finalizer in PDF.init to close internal streams on GC.
  • Route PDF.close through the same finalizer for idempotent cleanup.
  • Add tests/test_resource_warning.py regression tests.
  • Update CHANGELOG under Unreleased.
  • Add contributor entry in README.

Validation:

  • .venv/bin/python -m pytest tests/test_resource_warning.py -q
  • .venv/bin/python -W error::ResourceWarning -m pytest tests/test_resource_warning.py tests/test_mcids.py -q
  • black/isort/flake8 checks on changed Python files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant