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

Skip to content

[3.10] bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229) #31904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,23 @@ def guess_type(self, url, strict=True):
type = 'text/plain'
return type, None # never compressed, so encoding is None
base, ext = posixpath.splitext(url)
while ext in self.suffix_map:
base, ext = posixpath.splitext(base + self.suffix_map[ext])
while (ext_lower := ext.lower()) in self.suffix_map:
base, ext = posixpath.splitext(base + self.suffix_map[ext_lower])
# encodings_map is case sensitive
if ext in self.encodings_map:
encoding = self.encodings_map[ext]
base, ext = posixpath.splitext(base)
else:
encoding = None
ext = ext.lower()
types_map = self.types_map[True]
if ext in types_map:
return types_map[ext], encoding
elif ext.lower() in types_map:
return types_map[ext.lower()], encoding
elif strict:
return None, encoding
types_map = self.types_map[False]
if ext in types_map:
return types_map[ext], encoding
elif ext.lower() in types_map:
return types_map[ext.lower()], encoding
else:
return None, encoding

Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def tearDownModule():
class MimeTypesTestCase(unittest.TestCase):
def setUp(self):
self.db = mimetypes.MimeTypes()

def test_case_sensitivity(self):
eq = self.assertEqual
eq(self.db.guess_type("foobar.HTML"), self.db.guess_type("foobar.html"))
eq(self.db.guess_type("foobar.TGZ"), self.db.guess_type("foobar.tgz"))
eq(self.db.guess_type("foobar.tar.Z"), ("application/x-tar", "compress"))
eq(self.db.guess_type("foobar.tar.z"), (None, None))

def test_default_data(self):
eq = self.assertEqual
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix inconsistency with uppercase file extensions in :meth:`MimeTypes.guess_type`. Patch by Kumar Aditya.