Skip to content

Commit 53f8a06

Browse files
src/__init__.py tests/test_general.py: improve exception if fz_open_document_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.
1 parent 311cd87 commit 53f8a06

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,7 +2812,11 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0
28122812
# handler = fz_recognize_document(gctx, filetype);
28132813
# if (!handler) raise ValueError( MSG_BAD_FILETYPE)
28142814
# but prefer to leave fz_open_document_with_stream() to raise.
2815-
doc = mupdf.fz_open_document_with_stream(magic, data)
2815+
try:
2816+
doc = mupdf.fz_open_document_with_stream(magic, data)
2817+
except Exception as e:
2818+
if g_exceptions_verbose > 1: exception_info()
2819+
raise FileDataError(f'Failed to open stream') from e
28162820
else:
28172821
if filename:
28182822
if not filetype:
@@ -13168,7 +13172,7 @@ def width(self):
1316813172
pass
1316913173
else:
1317013174
#assert not inspect.isroutine(value)
13171-
#log(f'importing {name}')
13175+
#log(f'importing {_name=} {_value=}.')
1317213176
setattr(_self, _name, _value)
1317313177
#log(f'{getattr( self, name, None)=}')
1317413178
else:

tests/test_general.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,3 +1353,14 @@ def test_3859():
13531353
assert type(v)==pymupdf.mupdf.PdfObj, f'`v` is not a pymupdf.mupdf.PdfObj.'
13541354
else:
13551355
assert not hasattr(pymupdf.mupdf, 'PDF_TRUE')
1356+
1357+
def test_3905():
1358+
data = b'A,B,C,D\r\n1,2,1,2\r\n2,2,1,2\r\n'
1359+
try:
1360+
document = pymupdf.open(stream=data)
1361+
except pymupdf.FileDataError as e:
1362+
pass
1363+
else:
1364+
assert 0
1365+
wt = pymupdf.TOOLS.mupdf_warnings()
1366+
assert wt == 'format error: cannot recognize version marker\ntrying to repair broken xref\nrepairing PDF document'

0 commit comments

Comments
 (0)