Skip to content

Commit 5b97142

Browse files
committed
open memoryview object as-is they were bytes - it supports the same protocol and avoids an unecessary copy
1 parent 25f14d3 commit 5b97142

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,7 +2891,7 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0
28912891
raise TypeError(f"bad filename: {type(filename)=} {filename=}.")
28922892

28932893
if stream is not None:
2894-
if type(stream) is bytes:
2894+
if type(stream) in (bytes, memoryview):
28952895
self.stream = stream
28962896
elif type(stream) is bytearray:
28972897
self.stream = bytes(stream)
@@ -2922,7 +2922,7 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0
29222922

29232923
if from_file and os.path.getsize(filename) == 0:
29242924
raise EmptyFileError(f'Cannot open empty file: {filename=}.')
2925-
if type(self.stream) is bytes and len(self.stream) == 0:
2925+
if type(stream) in (bytes, memoryview) and len(self.stream) == 0:
29262926
raise EmptyFileError(f'Cannot open empty stream.')
29272927
w = width
29282928
h = height
@@ -2932,7 +2932,7 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0
29322932
h = r.y1 - r.y0
29332933

29342934
if stream: # stream given, **MUST** be bytes!
2935-
assert isinstance(stream, bytes)
2935+
assert isinstance(stream, (bytes, memoryview))
29362936
c = stream
29372937
#len = (size_t) PyBytes_Size(stream);
29382938

0 commit comments

Comments
 (0)