Skip to content

Commit fdf6f66

Browse files
committed
[3.13] 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 24d048d commit fdf6f66

3 files changed

Lines changed: 26 additions & 11 deletions

File tree

‎Lib/mimetypes.py‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,23 @@
4545
"suffix_map", "encodings_map", "types_map", "common_types"
4646
]
4747

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

6066
inited = False
6167
_db = None

‎Lib/test/test_mimetypes.py‎

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

198+
@unittest.skipIf(sys.platform == "win32", "non-Windows only")
199+
def test_default_knownfiles(self):
200+
self.assertIn("/etc/mime.types", knownfiles)
201+
198202
def test_added_types_are_used(self):
199203
mimetypes.add_type('testing/default-type', '')
200204
mime_type, _ = mimetypes.guess_type('')
@@ -431,6 +435,9 @@ def test_registry_parsing(self):
431435
eq(self.db.guess_type("image.jpg"), ("image/jpeg", None))
432436
eq(self.db.guess_type("image.png"), ("image/png", None))
433437

438+
def test_default_knownfiles(self):
439+
self.assertEqual(knownfiles, [])
440+
434441
@unittest.skipIf(not hasattr(_winapi, "_mimetypes_read_windows_registry"),
435442
"read_windows_registry accelerator unavailable")
436443
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)