Skip to content

Commit

Permalink
Address issue #578 (PR #581)
Browse files Browse the repository at this point in the history
Add try-except block in open() (#578), by @augeos-grosso / @johnhuge
  • Loading branch information
augeos-grosso authored Jan 14, 2022
1 parent b904dd6 commit f7417bc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ Many thanks to the following users who've contributed ideas, features, and fixes
- [Daniel Peña](https://github.com/trifling)
- [bobluda](https://github.com/bobluda)
- [@ramcdona](https://github.com/ramcdona)
- [johnhuge](https://github.com/johnhuge)

## Contributing

Expand Down
7 changes: 6 additions & 1 deletion pdfplumber/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser
from pdfminer.psparser import PSException

from .container import Container
from .page import Page
Expand Down Expand Up @@ -52,7 +53,11 @@ def __init__(
def open(cls, path_or_fp, **kwargs):
if isinstance(path_or_fp, (str, pathlib.Path)):
fp = open(path_or_fp, "rb")
inst = cls(fp, **kwargs)
try:
inst = cls(fp, **kwargs)
except PSException:
fp.close()
raise
inst.close_file = fp.close
return inst
else:
Expand Down

0 comments on commit f7417bc

Please sign in to comment.