Skip to content

Commit c0ab755

Browse files
committed
FIX: Do not attempt to pass sniffed bytes to _signature_matches_extension
1 parent 3a5c1d0 commit c0ab755

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

nibabel/loadsave.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,14 @@
2222
_compressed_suffixes = ('.gz', '.bz2', '.zst')
2323

2424

25-
def _signature_matches_extension(filename, sniff):
25+
def _signature_matches_extension(filename):
2626
"""Check if signature aka magic number matches filename extension.
2727
2828
Parameters
2929
----------
3030
filename : str or os.PathLike
3131
Path to the file to check
3232
33-
sniff : bytes or None
34-
First bytes of the file. If not `None` and long enough to contain the
35-
signature, avoids having to read the start of the file.
36-
3733
Returns
3834
-------
3935
matches : bool
@@ -56,12 +52,11 @@ def _signature_matches_extension(filename, sniff):
5652
if ext not in signatures:
5753
return True, ""
5854
expected_signature = signatures[ext]["signature"]
59-
if sniff is None or len(sniff) < len(expected_signature):
60-
try:
61-
with open(filename, "rb") as fh:
62-
sniff = fh.read(len(expected_signature))
63-
except OSError:
64-
return False, f"Could not read file: {filename}"
55+
try:
56+
with open(filename, "rb") as fh:
57+
sniff = fh.read(len(expected_signature))
58+
except OSError:
59+
return False, f"Could not read file: {filename}"
6560
if sniff.startswith(expected_signature):
6661
return True, ""
6762
format_name = signatures[ext]["format_name"]
@@ -100,7 +95,7 @@ def load(filename, **kwargs):
10095
img = image_klass.from_filename(filename, **kwargs)
10196
return img
10297

103-
matches, msg = _signature_matches_extension(filename, sniff)
98+
matches, msg = _signature_matches_extension(filename)
10499
if not matches:
105100
raise ImageFileError(msg)
106101

0 commit comments

Comments
 (0)