Skip to content

Conversation

@bebound
Copy link
Contributor

@bebound bebound commented Nov 11, 2025

This PR is a supplement of #3062. Change the doc to match the current behavior.

After #3062, the init(files) function still loads the knownfiles when files=[]:

cpython/Lib/mimetypes.py

Lines 405 to 433 in 9b0179f

def init(files=None):
global suffix_map, types_map, encodings_map, common_types
global inited, _db
inited = True # so that MimeTypes.__init__() doesn't call us again
if files is None or _db is None:
db = MimeTypes()
# Quick return if not supported
db.read_windows_registry()
if files is None:
files = knownfiles
else:
files = knownfiles + list(files)
else:
db = _db
# Lazy import to improve module import time
import os
for file in files:
if os.path.isfile(file):
db.read(file)
encodings_map = db.encodings_map
suffix_map = db.suffix_map
types_map = db.types_map[True]
common_types = db.types_map[False]
# Make the DB a global variable now that it is fully initialized
_db = db


Specifying an empty list for files will prevent the system defaults from being applied: only the well-known values will be present from a built-in list. --https://docs.python.org/3/library/mimetypes.html#mimetypes.init

The above doc reflects on the old code logic:

cpython/Lib/mimetypes.py

Lines 341 to 358 in 27a8564

def init(files=None):
global suffix_map, types_map, encodings_map, common_types
global inited, _db
inited = True # so that MimeTypes.__init__() doesn't call us again
db = MimeTypes()
if files is None:
if _winreg:
db.read_windows_registry()
files = knownfiles
for file in files:
if os.path.isfile(file):
db.read(file)
encodings_map = db.encodings_map
suffix_map = db.suffix_map
types_map = db.types_map[True]
common_types = db.types_map[False]
# Make the DB a global variable now that it is fully initialized
_db = db

PS: I did not walk through the https://bugs.python.org/issue4963 to see whether the init(files=[]) behavior change is necessary. (Well, reverting to Python 3.7's behavior is also unlikely?) For now, we should at least update the documentation.


📚 Documentation preview 📚: https://cpython-previews--141393.org.readthedocs.build/

@bebound bebound requested a review from a team as a code owner November 11, 2025 08:58
@bedevere-app bedevere-app bot added awaiting review docs Documentation in the Doc dir skip news labels Nov 11, 2025
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Nov 11, 2025
@bebound bebound changed the title gh-93417: Update mimetypes doc to match the code gh-93417: Update mimetypes.init() doc to match the code Nov 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant