Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File-object given to PDF() and open() should not be closed #312

Closed
the-vindicar opened this issue Nov 12, 2020 · 1 comment
Closed

File-object given to PDF() and open() should not be closed #312

the-vindicar opened this issue Nov 12, 2020 · 1 comment
Labels

Comments

@the-vindicar
Copy link

the-vindicar commented Nov 12, 2020

Describe the bug

PDF class instance constructed with a file-like object closes that object upon deletion. open() does the same.
This behaviour is not documented, and is unexpected.

Code to reproduce the problem

import pdfplumber
with open('sample.pdf', 'rb') as src:
    with pdfplumber.PDF(src) as pdf:
        pass
    assert not src.closed, 'why is the file closed?'

PDF file

Any valid pdf file will do.

Actual behavior

File object given to PDF constructor is closed when close() method is called. See:

self.stream.close()

This disrupts using PDF class on temporary files and BytesIO objects, as those are destroyed upon being closed.

Expected behavior

File object given to PDF() constructor should remain in open state upon closing the resulting PDF class instance.
PDF class instance should only close those file objects it opened by itself.

Environment

  • pdfplumber version: 0.5.24
  • Python version: 3.8.2
  • OS: Windows 10
jsvine added a commit that referenced this issue Nov 13, 2020
Fixes #312. Now only closes pdfplumber-created streams, not file objects
passed to pdfplumber.open(...).
@jsvine
Copy link
Owner

jsvine commented Nov 13, 2020

Hi @the-vindicar, and thanks for flagging this! Good catch. Now fixed in develop, and a relevant assert added to the tests

@jsvine jsvine closed this as completed Nov 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants