Skip to content

Commit 5550519

Browse files
[3.15] 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 5a6369f commit 5550519

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
@@ -271,6 +271,10 @@ def test_init_knownfiles(self):
271271
mimetypes.init()
272272
self.assertEqual(guess_file_type("file.test2")[0], "testing/test2")
273273

274+
@unittest.skipIf(sys.platform == "win32", "non-Windows only")
275+
def test_default_knownfiles(self):
276+
self.assertIn("/etc/mime.types", knownfiles)
277+
274278
def test_added_types_are_used(self):
275279
mimetypes.add_type('testing/default-type', '')
276280
mime_type, _ = mimetypes.guess_type('')
@@ -508,6 +512,9 @@ def test_registry_parsing(self):
508512
eq(self.db.guess_type("image.jpg"), ("image/jpeg", None))
509513
eq(self.db.guess_type("image.png"), ("image/png", None))
510514

515+
def test_default_knownfiles(self):
516+
self.assertEqual(knownfiles, [])
517+
511518
@unittest.skipIf(not hasattr(_winapi, "_mimetypes_read_windows_registry"),
512519
"read_windows_registry accelerator unavailable")
513520
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)