mirror of https://github.com/python/cpython
bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229)
This commit is contained in:
parent
22403d3a81
commit
5dd7ec52b8
|
@ -141,25 +141,23 @@ class MimeTypes:
|
||||||
type = 'text/plain'
|
type = 'text/plain'
|
||||||
return type, None # never compressed, so encoding is None
|
return type, None # never compressed, so encoding is None
|
||||||
base, ext = posixpath.splitext(url)
|
base, ext = posixpath.splitext(url)
|
||||||
while ext in self.suffix_map:
|
while (ext_lower := ext.lower()) in self.suffix_map:
|
||||||
base, ext = posixpath.splitext(base + self.suffix_map[ext])
|
base, ext = posixpath.splitext(base + self.suffix_map[ext_lower])
|
||||||
|
# encodings_map is case sensitive
|
||||||
if ext in self.encodings_map:
|
if ext in self.encodings_map:
|
||||||
encoding = self.encodings_map[ext]
|
encoding = self.encodings_map[ext]
|
||||||
base, ext = posixpath.splitext(base)
|
base, ext = posixpath.splitext(base)
|
||||||
else:
|
else:
|
||||||
encoding = None
|
encoding = None
|
||||||
|
ext = ext.lower()
|
||||||
types_map = self.types_map[True]
|
types_map = self.types_map[True]
|
||||||
if ext in types_map:
|
if ext in types_map:
|
||||||
return types_map[ext], encoding
|
return types_map[ext], encoding
|
||||||
elif ext.lower() in types_map:
|
|
||||||
return types_map[ext.lower()], encoding
|
|
||||||
elif strict:
|
elif strict:
|
||||||
return None, encoding
|
return None, encoding
|
||||||
types_map = self.types_map[False]
|
types_map = self.types_map[False]
|
||||||
if ext in types_map:
|
if ext in types_map:
|
||||||
return types_map[ext], encoding
|
return types_map[ext], encoding
|
||||||
elif ext.lower() in types_map:
|
|
||||||
return types_map[ext.lower()], encoding
|
|
||||||
else:
|
else:
|
||||||
return None, encoding
|
return None, encoding
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,13 @@ def tearDownModule():
|
||||||
class MimeTypesTestCase(unittest.TestCase):
|
class MimeTypesTestCase(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.db = mimetypes.MimeTypes()
|
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):
|
def test_default_data(self):
|
||||||
eq = self.assertEqual
|
eq = self.assertEqual
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix inconsistency with uppercase file extensions in :meth:`MimeTypes.guess_type`. Patch by Kumar Aditya.
|
Loading…
Reference in New Issue