From 53f8a06d49d7347a3e7f030ac1288b3fa548d92d Mon Sep 17 00:00:00 2001 From: Julian Smith Date: Sat, 28 Sep 2024 22:57:31 +0100 Subject: [PATCH] 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. --- src/__init__.py | 8 ++++++-- tests/test_general.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/__init__.py b/src/__init__.py index 4502f91e3..39350d1c8 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -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: @@ -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: diff --git a/tests/test_general.py b/tests/test_general.py index 5d6a2f56a..0b5ce3bc9 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -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'