Skip to content

gh-92455: Respect case-sensitive mimetype suffixes#148782

Open
yuanx749 wants to merge 8 commits into
python:mainfrom
yuanx749:fix-mimetypes-case-sensitive-add-type
Open

gh-92455: Respect case-sensitive mimetype suffixes#148782
yuanx749 wants to merge 8 commits into
python:mainfrom
yuanx749:fix-mimetypes-case-sensitive-add-type

Conversation

@yuanx749
Copy link
Copy Markdown
Contributor

Fixes gh-92455.

As described in the issue, the current implementation cannot find uppercase
extensions added via mimetypes.add_type(). This appears to be a regression from gh-30229.

This restores the lookup order documented for mimetypes.guess_type():
type suffixes are tried case-sensitively first, then case-insensitively.

Copy link
Copy Markdown

@ifrh ifrh left a comment

Choose a reason for hiding this comment

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

I cannot approve the changes formaly, but they look good for me.

Comment thread Lib/test/test_mimetypes.py Outdated
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.

@serhiy-storchaka
Copy link
Copy Markdown
Member

#30229 changed also the code related to suffix_map. I wonder if it needs correction too:

        while True:
            if ext in self.suffix_map:
                base, ext = posixpath.splitext(base + self.suffix_map[ext])
                continue
            ext_lower = ext.lower():
            if ext_lower in self.suffix_map:
                base, ext = posixpath.splitext(base + self.suffix_map[ext_lower])
                continue
            break

@kumaraditya303, what are your thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants