Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 32ae9ab

Browse files
bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229)
(cherry picked from commit 5dd7ec5) Co-authored-by: Kumar Aditya <[email protected]>
1 parent 64a68c3 commit 32ae9ab

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

Lib/mimetypes.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,25 +135,23 @@ def guess_type(self, url, strict=True):
135135
type = 'text/plain'
136136
return type, None # never compressed, so encoding is None
137137
base, ext = posixpath.splitext(url)
138-
while ext in self.suffix_map:
139-
base, ext = posixpath.splitext(base + self.suffix_map[ext])
138+
while (ext_lower := ext.lower()) in self.suffix_map:
139+
base, ext = posixpath.splitext(base + self.suffix_map[ext_lower])
140+
# encodings_map is case sensitive
140141
if ext in self.encodings_map:
141142
encoding = self.encodings_map[ext]
142143
base, ext = posixpath.splitext(base)
143144
else:
144145
encoding = None
146+
ext = ext.lower()
145147
types_map = self.types_map[True]
146148
if ext in types_map:
147149
return types_map[ext], encoding
148-
elif ext.lower() in types_map:
149-
return types_map[ext.lower()], encoding
150150
elif strict:
151151
return None, encoding
152152
types_map = self.types_map[False]
153153
if ext in types_map:
154154
return types_map[ext], encoding
155-
elif ext.lower() in types_map:
156-
return types_map[ext.lower()], encoding
157155
else:
158156
return None, encoding
159157

Lib/test/test_mimetypes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ def tearDownModule():
2727
class MimeTypesTestCase(unittest.TestCase):
2828
def setUp(self):
2929
self.db = mimetypes.MimeTypes()
30+
31+
def test_case_sensitivity(self):
32+
eq = self.assertEqual
33+
eq(self.db.guess_type("foobar.HTML"), self.db.guess_type("foobar.html"))
34+
eq(self.db.guess_type("foobar.TGZ"), self.db.guess_type("foobar.tgz"))
35+
eq(self.db.guess_type("foobar.tar.Z"), ("application/x-tar", "compress"))
36+
eq(self.db.guess_type("foobar.tar.z"), (None, None))
3037

3138
def test_default_data(self):
3239
eq = self.assertEqual
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix inconsistency with uppercase file extensions in :meth:`MimeTypes.guess_type`. Patch by Kumar Aditya.

0 commit comments

Comments
 (0)