You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistency with uppercase file extensions in MimeTypes.guess_type #64591
The functions looks up for the file extension in three maps: types_map, suffix_map and encodings_map.
Lookup in types_map is case insensitive (by calling lower() first).
Lookup in both suffix_map and encodings_map is case sensitive.
These can lead to some seemingly counterintuitive results, like:
a)
guess_type("foo.tar") == ("application/x-tar", None)
guess_type("foo.TAR") == ("application/x-tar", None)
b)
guess_type("foo.tgz") == ("application/x-tar", "gzip")
guess_type("foo.TGZ") == (None, None)
c)
guess_type("foo.tar.gz") == ("application/x-tar", "gzip")
guess_type("foo.TAR.GZ") == (None, None)
Lookup should be case insensitive at least for the suffix_map, in which case (b) would be solved. The submitted patch implements this change.
As for the encodings_map, I am not so sure, in particular because of the tar.Z extension. I found that the compress command expects the uppercase 'Z'. If someone is relying in the results of guess_type to call compress, errors could occur.
New changeset 5dd7ec5 by Kumar Aditya in branch 'main': bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229) 5dd7ec5
New changeset 3c4f24f by Miss Islington (bot) in branch '3.10': bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229) 3c4f24f
New changeset 32ae9ab by Miss Islington (bot) in branch '3.9': bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229) 32ae9ab
Activity
rodrigoparra commentedon Jan 25, 2014
The functions looks up for the file extension in three maps: types_map, suffix_map and encodings_map.
Lookup in types_map is case insensitive (by calling lower() first).
Lookup in both suffix_map and encodings_map is case sensitive.
These can lead to some seemingly counterintuitive results, like:
a)
guess_type("foo.tar") == ("application/x-tar", None)
guess_type("foo.TAR") == ("application/x-tar", None)
b)
guess_type("foo.tgz") == ("application/x-tar", "gzip")
guess_type("foo.TGZ") == (None, None)
c)
guess_type("foo.tar.gz") == ("application/x-tar", "gzip")
guess_type("foo.TAR.GZ") == (None, None)
Lookup should be case insensitive at least for the suffix_map, in which case (b) would be solved. The submitted patch implements this change.
As for the encodings_map, I am not so sure, in particular because of the tar.Z extension. I found that the compress command expects the uppercase 'Z'. If someone is relying in the results of guess_type to call compress, errors could occur.
bitdancer commentedon Jan 26, 2014
I'm tagging this for 3.5 instead, since there are backward compatibility concerns and the 3.4 RC will probably be a couple weeks from now.
iritkatriel commentedon Dec 13, 2021
Reproduced on 3.11.
asvetlov commentedon Mar 15, 2022
New changeset 5dd7ec5 by Kumar Aditya in branch 'main':
bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229)
5dd7ec5
miss-islington commentedon Mar 15, 2022
New changeset 3c4f24f by Miss Islington (bot) in branch '3.10':
bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229)
3c4f24f
miss-islington commentedon Mar 15, 2022
New changeset 32ae9ab by Miss Islington (bot) in branch '3.9':
bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229)
32ae9ab
ifrh commentedon May 8, 2022
This implementation cannot find upercase extensions added via
mimetype.add_typesmore information see#92455