-
Notifications
You must be signed in to change notification settings - Fork 702
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catch exceptions from pdfminer and malformed PDFs
... thanks to OSS-Fuzz and @ennamarie19 Cf.: google/oss-fuzz#12949
- Loading branch information
Showing
22 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class MalformedPDFException(Exception): | ||
pass | ||
|
||
|
||
class PdfminerException(Exception): | ||
pass |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python | ||
import logging | ||
import os | ||
import unittest | ||
from pathlib import Path | ||
|
||
import pdfplumber | ||
from pdfplumber.utils.exceptions import MalformedPDFException, PdfminerException | ||
|
||
logging.disable(logging.ERROR) | ||
|
||
HERE = Path(os.path.abspath(os.path.dirname(__file__))) | ||
|
||
ACCEPTABLE_EXCEPTIONS = (MalformedPDFException, PdfminerException) | ||
|
||
|
||
class Test(unittest.TestCase): | ||
def test_load(self): | ||
def test_conversions(pdf): | ||
methods = [pdf.to_dict, pdf.to_json, pdf.to_csv, pdf.pages[0].to_image] | ||
for method in methods: | ||
try: | ||
method() | ||
except ACCEPTABLE_EXCEPTIONS: | ||
continue | ||
except Exception as e: | ||
print(f"Failed on: {path.name}") | ||
raise e | ||
|
||
paths = sorted((HERE / "pdfs/from-oss-fuzz/load/").glob("*.pdf")) | ||
for path in paths: | ||
try: | ||
with pdfplumber.open(path) as pdf: | ||
assert pdf.pages | ||
test_conversions(pdf) | ||
except ACCEPTABLE_EXCEPTIONS: | ||
continue | ||
except Exception as e: | ||
print(f"Failed on: {path.name}") | ||
raise e |