Skip to content

Commit 9e4e9be

Browse files
committed
[3.11] 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 59d1bd5 commit 9e4e9be

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
@@ -51,6 +51,10 @@ def test_default_data(self):
5151
eq(self.db.guess_type("foo.tar.bz2"), ("application/x-tar", "bzip2"))
5252
eq(self.db.guess_type("foo.tar.xz"), ("application/x-tar", "xz"))
5353

54+
@unittest.skipIf(sys.platform == "win32", "non-Windows only")
55+
def test_default_knownfiles(self):
56+
self.assertIn("/etc/mime.types", knownfiles)
57+
5458
def test_data_urls(self):
5559
eq = self.assertEqual
5660
guess_type = self.db.guess_type
@@ -288,6 +292,9 @@ def test_registry_parsing(self):
288292
eq(self.db.guess_type("image.jpg"), ("image/jpeg", None))
289293
eq(self.db.guess_type("image.png"), ("image/png", None))
290294

295+
def test_default_knownfiles(self):
296+
self.assertEqual(knownfiles, [])
297+
291298
@unittest.skipIf(not hasattr(_winapi, "_mimetypes_read_windows_registry"),
292299
"read_windows_registry accelerator unavailable")
293300
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)