Skip to content

Commit

Permalink
src/__init__.py tests/test_general.py: improve exception if fz_open_d…
Browse files Browse the repository at this point in the history
…ocument_with_stream() fails.

We wrap fz_open_document_with_stream(), converting exception to FileDataError;
this makes things behave similarly to when we call fz_open_document().

Addresses #3905.
  • Loading branch information
julian-smith-artifex-com committed Sep 30, 2024
1 parent 311cd87 commit 53f8a06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2812,7 +2812,11 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0
# handler = fz_recognize_document(gctx, filetype);
# if (!handler) raise ValueError( MSG_BAD_FILETYPE)
# but prefer to leave fz_open_document_with_stream() to raise.
doc = mupdf.fz_open_document_with_stream(magic, data)
try:
doc = mupdf.fz_open_document_with_stream(magic, data)
except Exception as e:
if g_exceptions_verbose > 1: exception_info()
raise FileDataError(f'Failed to open stream') from e
else:
if filename:
if not filetype:
Expand Down Expand Up @@ -13168,7 +13172,7 @@ def width(self):
pass
else:
#assert not inspect.isroutine(value)
#log(f'importing {name}')
#log(f'importing {_name=} {_value=}.')
setattr(_self, _name, _value)
#log(f'{getattr( self, name, None)=}')
else:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -1353,3 +1353,14 @@ def test_3859():
assert type(v)==pymupdf.mupdf.PdfObj, f'`v` is not a pymupdf.mupdf.PdfObj.'
else:
assert not hasattr(pymupdf.mupdf, 'PDF_TRUE')

def test_3905():
data = b'A,B,C,D\r\n1,2,1,2\r\n2,2,1,2\r\n'
try:
document = pymupdf.open(stream=data)
except pymupdf.FileDataError as e:
pass
else:
assert 0
wt = pymupdf.TOOLS.mupdf_warnings()
assert wt == 'format error: cannot recognize version marker\ntrying to repair broken xref\nrepairing PDF document'

0 comments on commit 53f8a06

Please sign in to comment.