Skip to content

Commit f015ed8

Browse files
Apply suggestions from code review
Co-authored-by: Chris Markiewicz <[email protected]>
1 parent 266feb2 commit f015ed8

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

nibabel/loadsave.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,21 @@ def _signature_matches_extension(filename, sniff):
4646
"""
4747
signatures = {
4848
".gz": {"signature": b"\x1f\x8b", "format_name": "gzip"},
49-
".bz2": {"signature": b"\x42\x5a\x68", "format_name": "bzip2"}
49+
".bz2": {"signature": b"BZh", "format_name": "bzip2"}
5050
}
5151
filename = _stringify_path(filename)
5252
*_, ext = splitext_addext(filename)
5353
ext = ext.lower()
5454
if ext not in signatures:
5555
return True, ""
5656
expected_signature = signatures[ext]["signature"]
57-
if sniff is not None and len(sniff) >= len(expected_signature):
58-
found_signature = sniff[:len(expected_signature)]
59-
else:
57+
if sniff is None or len(sniff) < len(expected_signature):
6058
try:
6159
with open(filename, "rb") as fh:
62-
found_signature = fh.read(len(expected_signature))
60+
sniff = fh.read(len(expected_signature))
6361
except OSError:
6462
return False, f"Could not read file: {filename}"
65-
if found_signature == expected_signature:
63+
if not sniff.startswith(expected_signature):
6664
return True, ""
6765
format_name = signatures[ext]["format_name"]
6866
return False, f"File {filename} is not a {format_name} file"

0 commit comments

Comments
 (0)