Skip to content
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
2 changes: 1 addition & 1 deletion python/src/magika/magika.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def identify_stream(self, stream: BinaryIO) -> MagikaResult:
"Input stream must be opened in bytes mode, not in text mode."
)

if not isinstance(stream, io.BufferedIOBase):
if not isinstance(stream, (io.BufferedIOBase, io.RawIOBase)):
raise TypeError("Input stream must be a readable BinaryIO object.")

if (
Expand Down
24 changes: 24 additions & 0 deletions python/tests/test_magika_python_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import io
import os
import signal
import tempfile
from pathlib import Path
Expand Down Expand Up @@ -242,6 +243,29 @@ def test_magika_module_identify_stream_does_not_alter_position() -> None:
assert stream.tell() == pos


def test_magika_module_identify_stream_accepts_raw_fileio() -> None:
m = Magika()

fd, tmp_path = tempfile.mkstemp()
os.close(fd)
try:
with open(tmp_path, "wb") as f:
f.write(b"print('hello')\n")

stream = io.FileIO(tmp_path, "rb")
try:
# Verify raw binary streams are accepted and position is restored.
expected_pos = 2
stream.seek(expected_pos)
res = m.identify_stream(stream)
assert res.ok
assert stream.tell() == expected_pos
finally:
stream.close()
finally:
os.unlink(tmp_path)


def test_magika_module_with_whitespaces() -> None:
m = Magika()

Expand Down