Skip to content

Commit dceb268

Browse files
committed
[3.12] 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 58ed60b commit dceb268

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

53+
@unittest.skipIf(sys.platform == "win32", "non-Windows only")
54+
def test_default_knownfiles(self):
55+
self.assertIn("/etc/mime.types", knownfiles)
56+
5357
def test_data_urls(self):
5458
eq = self.assertEqual
5559
guess_type = self.db.guess_type
@@ -296,6 +300,9 @@ def test_registry_parsing(self):
296300
eq(self.db.guess_type("image.jpg"), ("image/jpeg", None))
297301
eq(self.db.guess_type("image.png"), ("image/png", None))
298302

303+
def test_default_knownfiles(self):
304+
self.assertEqual(knownfiles, [])
305+
299306
@unittest.skipIf(not hasattr(_winapi, "_mimetypes_read_windows_registry"),
300307
"read_windows_registry accelerator unavailable")
301308
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)