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

Skip to content

Commit 5dd7ec5

Browse files
bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (pythonGH-30229)
1 parent 22403d3 commit 5dd7ec5

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
@@ -141,25 +141,23 @@ def guess_type(self, url, strict=True):
141141
type = 'text/plain'
142142
return type, None # never compressed, so encoding is None
143143
base, ext = posixpath.splitext(url)
144-
while ext in self.suffix_map:
145-
base, ext = posixpath.splitext(base + self.suffix_map[ext])
144+
while (ext_lower := ext.lower()) in self.suffix_map:
145+
base, ext = posixpath.splitext(base + self.suffix_map[ext_lower])
146+
# encodings_map is case sensitive
146147
if ext in self.encodings_map:
147148
encoding = self.encodings_map[ext]
148149
base, ext = posixpath.splitext(base)
149150
else:
150151
encoding = None
152+
ext = ext.lower()
151153
types_map = self.types_map[True]
152154
if ext in types_map:
153155
return types_map[ext], encoding
154-
elif ext.lower() in types_map:
155-
return types_map[ext.lower()], encoding
156156
elif strict:
157157
return None, encoding
158158
types_map = self.types_map[False]
159159
if ext in types_map:
160160
return types_map[ext], encoding
161-
elif ext.lower() in types_map:
162-
return types_map[ext.lower()], encoding
163161
else:
164162
return None, encoding
165163

Lib/test/test_mimetypes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ def tearDownModule():
3333
class MimeTypesTestCase(unittest.TestCase):
3434
def setUp(self):
3535
self.db = mimetypes.MimeTypes()
36+
37+
def test_case_sensitivity(self):
38+
eq = self.assertEqual
39+
eq(self.db.guess_type("foobar.HTML"), self.db.guess_type("foobar.html"))
40+
eq(self.db.guess_type("foobar.TGZ"), self.db.guess_type("foobar.tgz"))
41+
eq(self.db.guess_type("foobar.tar.Z"), ("application/x-tar", "compress"))
42+
eq(self.db.guess_type("foobar.tar.z"), (None, None))
3643

3744
def test_default_data(self):
3845
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)