FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-75223: Reject undotted extensions in `mimetypes.add_type()` by OddBloke · Pull Request #2895 · python/cpython · GitHub

/ cpython Public

gh-75223: Reject undotted extensions in mimetypes.add_type() - #2895

Closed
OddBloke wants to merge 16 commits into
python:mainfrom
OddBloke:fix-for-31040
Closed

gh-75223: Reject undotted extensions in mimetypes.add_type()#2895
OddBloke wants to merge 16 commits into
python:mainfrom
OddBloke:fix-for-31040

Conversation

OddBloke commented Jul 26, 2017
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Contributor

Extensions that don't start with a dot will never be found by
methods/functions that act on the registry, so we should stop users from
mistakenly adding them.

https://bugs.python.org/issue31040

Copy link
Copy Markdown
Contributor Author

I've opened this now to get feedback on whether this is an appropriate change to make. Does it need a warning first, before it becomes the default behaviour?

(At a minimum, I would expect to need to add to documentation.)

JulienPalard commented Sep 10, 2019
edited
Loading

Copy link
Copy Markdown
Member

As said in https://bugs.python.org/issue31040, please use startswith instead of find.

There is one case where it works without a dot:

>>> mimetypes.add_type('empty', '')
>>> mimetypes.guess_type('')
('empty', None)

And someone may have used it to distinguish between emptyness and type not found. What I propose:

Emit a warning in case the ext is empty, else ValueError in case the type does not start with a dot, to ensure we're not breaking any code.

Comment thread Lib/mimetypes.py Outdated

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

OddBloke requested a review from a team as a code owner September 10, 2019 12:45
Extensions that don't start with a dot will never be found by
methods/functions that act on the registry, so we should stop users from
mistakenly adding them.

The one exception is the empty string extension.  This could be in use
to detect absent extensions, so instead of raising a ValueError we emit
a warning.

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

Copy link
Copy Markdown

Thanks for making the requested changes!

@JulienPalard: please review the changes made to this pull request.

Comment thread Lib/mimetypes.py Outdated

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

This comment was marked as outdated.

github-actions Bot added the stale Stale PR or inactive for long period of time. label Feb 20, 2022
github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jul 29, 2022

This comment was marked as outdated.

github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 30, 2022
arhadthedev changed the title bpo-31040: Reject undotted extensions in mimetypes.add_type gh-75223: Reject undotted extensions in mimetypes.add_type Apr 15, 2023
arhadthedev added topic-email stdlib Standard Library Python modules in the Lib/ directory labels Apr 15, 2023

This comment was marked as outdated.

github-actions Bot added the stale Stale PR or inactive for long period of time. label Dec 21, 2024

encukou commented Jan 3, 2025

Copy link
Copy Markdown
Member

IMO, this is a backwards-incompatible change and should be deprecated with a warning, and documented. (For example, if add_type is fed from a data file, this PR can change a relatively harmless data-entry error into a hard failure. Or someone could use types_map directly.)

github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jan 3, 2025
hugovk marked this pull request as draft January 8, 2025 15:12

hugovk commented Jan 8, 2025

Copy link
Copy Markdown
Member

Thanks, please see #128638 to deprecate it first, and I've marked this as a draft until it's ready for merge.

This comment was marked as outdated.

github-actions Bot added the stale Stale PR or inactive for long period of time. label Feb 7, 2025
StanFromIreland removed the stale Stale PR or inactive for long period of time. label May 9, 2026

Copy link
Copy Markdown
Member

Thanks, please see #128638 to deprecate it first, and I've marked this as a draft until it's ready for merge.

It was deprecated and scheduled for 3.16, so it's time!

StanFromIreland self-assigned this May 9, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32618039 | 📁 Comparing c491311 against main (7241f27)

  🔍 Preview build  

3 files changed
± library/mimetypes.html
± whatsnew/3.16.html
± whatsnew/changelog.html

StanFromIreland marked this pull request as ready for review May 9, 2026 18:09
StanFromIreland requested a review from AA-Turner as a code owner May 9, 2026 18:09
StanFromIreland changed the title gh-75223: Reject undotted extensions in mimetypes.add_type gh-75223: Reject undotted extensions in mimetypes.add_type() May 9, 2026
StanFromIreland removed their assignment May 9, 2026
Comment thread Doc/library/mimetypes.rst

.. function:: add_type(type, ext, strict=True)

Add a mapping from the MIME type *type* to the extension *ext*. When the

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This should have the same "Valid extensions start with a '.' or are empty." note IMO, but it wasn't updated before?

Copy link
Copy Markdown
Member

Completed in #149721.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stdlib Standard Library Python modules in the Lib/ directory topic-email

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants


Back | FazBrowse Home | New Git URL