20
20
from .deprecated import deprecate_with_version
21
21
22
22
23
+ def _signature_matches_extension (filename ):
24
+ """Check if signature aka magic number matches filename extension.
25
+
26
+ Parameters
27
+ ----------
28
+ filename : str or os.PathLike
29
+ Path to the file to check
30
+
31
+ Returns
32
+ -------
33
+ matches : bool
34
+ - `True` if the filename extension is not recognized (not .gz nor .bz2)
35
+ - `True` if the magic number was successfully read and corresponds to
36
+ the format indicated by the extension.
37
+ - `False` otherwise.
38
+ error_message : str
39
+ An error message if opening the file failed or a mismatch is detected;
40
+ the empty string otherwise.
41
+
42
+ """
43
+ signatures = {
44
+ ".gz" : {"signature" : b"\x1f \x8b " , "format_name" : "gzip" },
45
+ ".bz2" : {"signature" : b"\x42 \x5a \x68 " , "format_name" : "bzip2" }
46
+ }
47
+ filename = _stringify_path (filename )
48
+ * _ , ext = splitext_addext (filename )
49
+ ext = ext .lower ()
50
+ if ext not in signatures :
51
+ return True , ""
52
+ expected_signature = signatures [ext ]["signature" ]
53
+ try :
54
+ with open (filename , "rb" ) as fh :
55
+ found_signature = fh .read (len (expected_signature ))
56
+ except Exception :
57
+ return False , f"Could not read file: { filename } "
58
+ if found_signature == expected_signature :
59
+ return True , ""
60
+ format_name = signatures [ext ]["format_name" ]
61
+ return False , f"File { filename } is not a { format_name } file"
62
+
63
+
23
64
def load (filename , ** kwargs ):
24
65
r""" Load file given filename, guessing at file type
25
66
@@ -44,6 +85,9 @@ def load(filename, **kwargs):
44
85
raise FileNotFoundError (f"No such file or no access: '{ filename } '" )
45
86
if stat_result .st_size <= 0 :
46
87
raise ImageFileError (f"Empty file: '{ filename } '" )
88
+ matches , msg = _signature_matches_extension (filename )
89
+ if not matches :
90
+ raise ImageFileError (msg )
47
91
48
92
sniff = None
49
93
for image_klass in all_image_classes :
0 commit comments