summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-03-15 15:14:19 (GMT)
committerGitHub <noreply@github.com>2022-03-15 15:14:19 (GMT)
commit3c4f24face4cca9256ae79bf6968663a04daf655 (patch)
treef13cdebaa4fa3ecccffa372d74b75a918da626f1
parent0e4bebad0a099e5f1440edcdd0ce3eae9952bf74 (diff)
downloadcpython-3c4f24face4cca9256ae79bf6968663a04daf655.zip
cpython-3c4f24face4cca9256ae79bf6968663a04daf655.tar.gz
cpython-3c4f24face4cca9256ae79bf6968663a04daf655.tar.bz2
bpo-20392: Fix inconsistency with uppercase file extensions in mimetypes.guess_type (GH-30229)
(cherry picked from commit 5dd7ec52b83e7f239774cf7478106fcc7b0a36f3) Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
-rw-r--r--Lib/mimetypes.py10
-rw-r--r--Lib/test/test_mimetypes.py7
-rw-r--r--Misc/NEWS.d/next/Library/2021-12-22-12-02-27.bpo-20392.CLAFIp.rst1
3 files changed, 12 insertions, 6 deletions
diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
index c389685..b72ce08 100644
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -141,25 +141,23 @@ class MimeTypes:
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
diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py
index 4098a22..f31ea46 100644
--- a/Lib/test/test_mimetypes.py
+++ b/Lib/test/test_mimetypes.py
@@ -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
diff --git a/Misc/NEWS.d/next/Library/2021-12-22-12-02-27.bpo-20392.CLAFIp.rst b/Misc/NEWS.d/next/Library/2021-12-22-12-02-27.bpo-20392.CLAFIp.rst
new file mode 100644
index 0000000..8973c4d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-12-22-12-02-27.bpo-20392.CLAFIp.rst
@@ -0,0 +1 @@
+Fix inconsistency with uppercase file extensions in :meth:`MimeTypes.guess_type`. Patch by Kumar Aditya.