Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open memoryview object as-is they were bytes #4272

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2889,13 +2889,13 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0
filename = filename.name
else:
raise TypeError(f"bad filename: {type(filename)=} {filename=}.")

if stream is not None:
if type(stream) is bytes:
if isinstance(stream, (bytes, memoryview)):
self.stream = stream
elif type(stream) is bytearray:
elif isinstance(stream, bytearray):
self.stream = bytes(stream)
elif type(stream) is io.BytesIO:
elif isinstance(stream, io.BytesIO):
self.stream = stream.getvalue()
else:
raise TypeError(f"bad stream: {type(stream)=}.")
Expand All @@ -2919,26 +2919,26 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0
elif not os.path.isfile(filename):
msg = f"'{filename}' is no file"
raise FileDataError(msg)

if from_file and os.path.getsize(filename) == 0:
raise EmptyFileError(f'Cannot open empty file: {filename=}.')
if type(self.stream) is bytes and len(self.stream) == 0:
raise EmptyFileError(f'Cannot open empty stream.')
elif os.path.getsize(filename) == 0:
raise EmptyFileError(f'Cannot open empty file: {filename=}.')

w = width
h = height
r = JM_rect_from_py(rect)
if not mupdf.fz_is_infinite_rect(r):
w = r.x1 - r.x0
h = r.y1 - r.y0

if stream: # stream given, **MUST** be bytes!
assert isinstance(stream, bytes)
if stream is not None:
assert isinstance(stream, (bytes, memoryview))
if len(stream) == 0:
raise EmptyFileError('Cannot open empty stream.')
c = stream
#len = (size_t) PyBytes_Size(stream);

if mupdf_cppyy:
buffer_ = mupdf.fz_new_buffer_from_copied_data( c)
data = mupdf.fz_open_buffer( buffer_)
buffer_ = mupdf.fz_new_buffer_from_copied_data(c)
data = mupdf.fz_open_buffer(buffer_)
else:
# Pass raw bytes data to mupdf.fz_open_memory(). This assumes
# that the bytes string will not be modified; i think the
Expand All @@ -2957,7 +2957,7 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0
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
raise FileDataError('Failed to open stream') from e
else:
if filename:
if not filetype:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,22 @@ def test_pdfstring():

def test_open_exceptions():
try:
doc = pymupdf.open(filename, filetype="xps")
pymupdf.open(filename, filetype="xps")
except RuntimeError as e:
assert repr(e).startswith("FileDataError")

try:
doc = pymupdf.open(filename, filetype="xxx")
pymupdf.open(filename, filetype="xxx")
except Exception as e:
assert repr(e).startswith("ValueError")

try:
doc = pymupdf.open("x.y")
pymupdf.open("x.y")
except Exception as e:
assert repr(e).startswith("FileNotFoundError")

try:
doc = pymupdf.open("pdf", b"")
pymupdf.open(stream=b"", filetype="pdf")
except RuntimeError as e:
assert repr(e).startswith("EmptyFileError")

Expand Down