Skip to content

Commit

Permalink
Read model stream from start (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpmorgan authored Mar 29, 2024
1 parent 0d9f680 commit e21035e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions modelscan/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ def get_stream(self) -> IO[bytes]:
if not self._stream:
raise ModelDataEmpty("Model data is empty.")

self._stream.seek(0)
return self._stream
9 changes: 5 additions & 4 deletions modelscan/tools/picklescanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,11 @@ def scan_numpy(model: Model, settings: Dict[str, Any]) -> ScanResults:
_ZIP_PREFIX = b"PK\x03\x04"
_ZIP_SUFFIX = b"PK\x05\x06" # empty zip files start with this
N = len(np.lib.format.MAGIC_PREFIX)
magic = model.get_stream().read(N)
stream = model.get_stream()
magic = stream.read(N)
# If the file size is less than N, we need to make sure not
# to seek past the beginning of the file
model.get_stream().seek(-min(N, len(magic)), 1) # back-up
stream.seek(-min(N, len(magic)), 1) # back-up
if magic.startswith(_ZIP_PREFIX) or magic.startswith(_ZIP_SUFFIX):
# .npz file
return ScanResults(
Expand All @@ -208,9 +209,9 @@ def scan_numpy(model: Model, settings: Dict[str, Any]) -> ScanResults:

elif magic == np.lib.format.MAGIC_PREFIX:
# .npy file
version = np.lib.format.read_magic(model.get_stream()) # type: ignore[no-untyped-call]
version = np.lib.format.read_magic(stream) # type: ignore[no-untyped-call]
np.lib.format._check_version(version) # type: ignore[attr-defined]
_, _, dtype = np.lib.format._read_array_header(model.get_stream(), version) # type: ignore[attr-defined]
_, _, dtype = np.lib.format._read_array_header(stream, version) # type: ignore[attr-defined]

if dtype.hasobject:
return scan_pickle_bytes(model, settings, scan_name)
Expand Down

0 comments on commit e21035e

Please sign in to comment.