Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2720dbd
gh-92455: Respect case-sensitive mimetype suffixes
yuanx749 Apr 19, 2026
547a64e
Add NEWS entry
yuanx749 Apr 19, 2026
992a6e3
Merge remote-tracking branch 'upstream/main' into fix-mimetypes-case-…
yuanx749 Apr 19, 2026
561a0aa
Merge remote-tracking branch 'upstream/main' into fix-mimetypes-case-…
yuanx749 May 16, 2026
90b7360
Merge remote-tracking branch 'upstream/main' into fix-mimetypes-case-…
yuanx749 May 20, 2026
268d57c
Merge remote-tracking branch 'upstream/main' into fix-mimetypes-case-…
yuanx749 Jun 3, 2026
47b2b6b
Add non-strict mimetype case test
yuanx749 Jun 3, 2026
fe1096a
Merge branch 'main' into fix-mimetypes-case-sensitive-add-type
yuanx749 Jun 4, 2026
e554892
Test strict lookup excludes non-strict types
yuanx749 Jun 6, 2026
3085bb9
Merge remote-tracking branch 'upstream/main' into fix-mimetypes-case-…
yuanx749 Jun 6, 2026
206be36
Test suffix map case lookup
yuanx749 Jun 8, 2026
6f639f2
Respect suffix map case lookup
yuanx749 Jun 8, 2026
4886f13
Document mimetypes case lookup
yuanx749 Jun 8, 2026
046dc95
Merge remote-tracking branch 'upstream/main' into fix-mimetypes-case-…
yuanx749 Jun 8, 2026
430a329
Merge remote-tracking branch 'upstream/main' into fix-mimetypes-case-…
yuanx749 Jun 8, 2026
2ed828a
Merge branch 'main' into fix-mimetypes-case-sensitive-add-type
yuanx749 Jun 9, 2026
50552fa
Merge remote-tracking branch 'upstream/main' into fix-mimetypes-case-…
yuanx749 Jun 9, 2026
0874c07
Update documentation
yuanx749 Jun 14, 2026
5c56f96
Merge remote-tracking branch 'upstream/main' into fix-mimetypes-case-…
yuanx749 Jun 14, 2026
44a5da7
Merge remote-tracking branch 'upstream/main' into fix-mimetypes-case-…
yuanx749 Jun 14, 2026
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
6 changes: 5 additions & 1 deletion Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,19 @@ def _guess_file_type(self, path, strict, splitext):
base, ext = splitext(base)
else:
encoding = None
ext = ext.lower()
ext_lower = ext.lower()
types_map = self.types_map[True]
if ext in types_map:
return types_map[ext], encoding
if 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
if ext_lower in types_map:
return types_map[ext_lower], encoding
else:
return None, encoding

Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ def test_case_sensitivity(self):
eq(self.db.guess_file_type("foobar.tar.z"), (None, None))
eq(self.db.guess_type("scheme:foobar.tar.z"), (None, None))

def test_added_types_case_sensitive_preferred(self):
self.db.add_type("text/x-r-script", ".R")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"text/x-r-script" could be added to the database in future. It is better to use something less likely to be added.

Please add also tests for strict=False.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. I changed the test type and added tests for strict=False.

self.db.add_type("text/x-test-lowercase-r", ".r")
self.assertEqual(
self.db.guess_file_type("example.R"),
("text/x-r-script", None),
)
self.assertEqual(
self.db.guess_file_type("example.r"),
("text/x-test-lowercase-r", None),
)

def test_default_data(self):
eq = self.assertEqual
eq(self.db.guess_file_type("foo.html"), ("text/html", None))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :mod:`mimetypes` to prefer case-sensitive matches for MIME type suffixes
registered with :func:`mimetypes.add_type` before falling back to
case-insensitive matches. Contributed by Xiao Yuan.
Loading