Skip to content

Commit 76f4555

Browse files
[3.14] gh-159428: Don't read mimetypes.knownfiles by default on Windows (GH-159445)
Make knownfiles empty on Windows so that only the registry is used by default. Files passed to init() or assigned to knownfiles explicitly are still read. (cherry picked from commit 04a29bf) Co-authored-by: Cary Hawkins <hawkinscary23@gmail.com>
1 parent 65feab8 commit 76f4555

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

‎Lib/mimetypes.py‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
read_mime_types(file) -- parse one file, return a dictionary or None
2424
"""
2525

26+
import sys
27+
2628
try:
2729
from _winapi import _mimetypes_read_windows_registry
2830
except ImportError:
@@ -40,17 +42,23 @@
4042
"suffix_map", "encodings_map", "types_map", "common_types"
4143
]
4244

43-
knownfiles = [
44-
"/etc/mime.types",
45-
"/etc/httpd/mime.types", # Mac OS X
46-
"/etc/httpd/conf/mime.types", # Apache
47-
"/etc/apache/mime.types", # Apache 1
48-
"/etc/apache2/mime.types", # Apache 2
49-
"/usr/local/etc/httpd/conf/mime.types",
50-
"/usr/local/lib/netscape/mime.types",
51-
"/usr/local/etc/httpd/conf/mime.types", # Apache 1.2
52-
"/usr/local/etc/mime.types", # Apache 1.3
53-
]
45+
if sys.platform == "win32":
46+
# The default paths would resolve to the root of the current drive on Windows
47+
# (e.g. C:\etc\mime.types), which unprivileged users can usually create.
48+
# Only the registry is used by default.
49+
knownfiles = []
50+
else:
51+
knownfiles = [
52+
"/etc/mime.types",
53+
"/etc/httpd/mime.types", # Mac OS X
54+
"/etc/httpd/conf/mime.types", # Apache
55+
"/etc/apache/mime.types", # Apache 1
56+
"/etc/apache2/mime.types", # Apache 2
57+
"/usr/local/etc/httpd/conf/mime.types",
58+
"/usr/local/lib/netscape/mime.types",
59+
"/usr/local/etc/httpd/conf/mime.types", # Apache 1.2
60+
"/usr/local/etc/mime.types", # Apache 1.3
61+
]
5462

5563
inited = False
5664
_db = None

‎Lib/test/test_mimetypes.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ def test_init_knownfiles(self):
265265
mimetypes.init()
266266
self.assertEqual(guess_file_type("file.test2")[0], "testing/test2")
267267

268+
@unittest.skipIf(sys.platform == "win32", "non-Windows only")
269+
def test_default_knownfiles(self):
270+
self.assertIn("/etc/mime.types", knownfiles)
271+
268272
def test_added_types_are_used(self):
269273
mimetypes.add_type('testing/default-type', '')
270274
mime_type, _ = mimetypes.guess_type('')
@@ -505,6 +509,9 @@ def test_registry_parsing(self):
505509
eq(self.db.guess_type("image.jpg"), ("image/jpeg", None))
506510
eq(self.db.guess_type("image.png"), ("image/png", None))
507511

512+
def test_default_knownfiles(self):
513+
self.assertEqual(knownfiles, [])
514+
508515
@unittest.skipIf(not hasattr(_winapi, "_mimetypes_read_windows_registry"),
509516
"read_windows_registry accelerator unavailable")
510517
def test_registry_accelerator(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`mimetypes` no longer reads the files listed in :data:`mimetypes.knownfiles`
2+
by default on Windows, and uses only the registry.

0 commit comments

Comments
 (0)