This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Inconsistency with uppercase file extensions in MimeTypes.guess_type
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, iritkatriel, kumaraditya, miss-islington, r.david.murray, rodrigo.parra, tim.golden
Priority: normal Keywords: easy, patch

Created on 2014-01-25 17:17 by rodrigo.parra, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
case_guess_type.patch rodrigo.parra, 2014-01-25 17:17 review
Pull Requests
URL Status Linked Edit
PR 30229 merged kumaraditya, 2021-12-22 12:00
PR 31903 merged miss-islington, 2022-03-15 14:50
PR 31904 merged miss-islington, 2022-03-15 14:50
Messages (6)
msg209218 - (view) Author: Rodrigo Parra (rodrigo.parra) Date: 2014-01-25 17:17
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.
msg209332 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-01-26 15:55
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.
msg408460 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-13 16:08
Reproduced on 3.11.
msg415243 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2022-03-15 13:25
New changeset 5dd7ec52b83e7f239774cf7478106fcc7b0a36f3 by Kumar Aditya in branch 'main':
bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229)
https://github.com/python/cpython/commit/5dd7ec52b83e7f239774cf7478106fcc7b0a36f3
msg415252 - (view) Author: miss-islington (miss-islington) Date: 2022-03-15 15:14
New changeset 3c4f24face4cca9256ae79bf6968663a04daf655 by Miss Islington (bot) in branch '3.10':
bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229)
https://github.com/python/cpython/commit/3c4f24face4cca9256ae79bf6968663a04daf655
msg415258 - (view) Author: miss-islington (miss-islington) Date: 2022-03-15 15:50
New changeset 32ae9ab55f2cd97b5a28d5398c4820d8bc96f30c by Miss Islington (bot) in branch '3.9':
bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229)
https://github.com/python/cpython/commit/32ae9ab55f2cd97b5a28d5398c4820d8bc96f30c
History
Date User Action Args
2022-04-11 14:57:57adminsetgithub: 64591
2022-03-15 15:50:53asvetlovsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2022-03-15 15:50:33miss-islingtonsetmessages: + msg415258
2022-03-15 15:14:27miss-islingtonsetmessages: + msg415252
2022-03-15 14:50:28miss-islingtonsetpull_requests: + pull_request29998
2022-03-15 14:50:23miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request29997
2022-03-15 13:25:46asvetlovsetnosy: + asvetlov
messages: + msg415243
2021-12-22 12:00:39kumaradityasetkeywords: + patch
nosy: + kumaraditya

pull_requests: + pull_request28451
stage: patch review
2021-12-13 16:08:59iritkatrielsetversions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.5
nosy: + iritkatriel

messages: + msg408460

keywords: + easy, - patch
2014-01-26 15:55:33r.david.murraysetnosy: + r.david.murray

messages: + msg209332
versions: + Python 3.5, - Python 3.4
2014-01-25 17:17:48rodrigo.parracreate