Skip to content

Commit 27a8564

Browse files
committed
Issue #15207: Fix mimetypes to read from correct area in Windows registry (Original patch by Dave Chambers)
1 parent 49e6180 commit 27a8564

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

Doc/library/mimetypes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ behavior of the module.
8585
:const:`knownfiles` takes precedence over those named before it. Calling
8686
:func:`init` repeatedly is allowed.
8787

88+
Specifying an empty list for *files* will prevent the system defaults from
89+
being applied: only the well-known values will be present from a built-in list.
90+
8891
.. versionchanged:: 3.2
8992
Previously, Windows registry settings were ignored.
9093

Lib/mimetypes.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,19 +249,21 @@ def enum_types(mimedb):
249249
yield ctype
250250
i += 1
251251

252-
with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,
253-
r'MIME\Database\Content Type') as mimedb:
254-
for ctype in enum_types(mimedb):
252+
with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr:
253+
for subkeyname in enum_types(hkcr):
255254
try:
256-
with _winreg.OpenKey(mimedb, ctype) as key:
257-
suffix, datatype = _winreg.QueryValueEx(key,
258-
'Extension')
255+
with _winreg.OpenKey(hkcr, subkeyname) as subkey:
256+
# Only check file extensions
257+
if not subkeyname.startswith("."):
258+
continue
259+
# raises EnvironmentError if no 'Content Type' value
260+
mimetype, datatype = _winreg.QueryValueEx(
261+
subkey, 'Content Type')
262+
if datatype != _winreg.REG_SZ:
263+
continue
264+
self.add_type(mimetype, subkeyname, strict)
259265
except EnvironmentError:
260266
continue
261-
if datatype != _winreg.REG_SZ:
262-
continue
263-
self.add_type(ctype, suffix, strict)
264-
265267

266268
def guess_type(url, strict=True):
267269
"""Guess the type of a file based on its URL.

Lib/test/test_mimetypes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ def test_registry_parsing(self):
9898
# Use file types that should *always* exist:
9999
eq = self.assertEqual
100100
eq(self.db.guess_type("foo.txt"), ("text/plain", None))
101-
101+
eq(self.db.guess_type("image.jpg"), ("image/jpeg", None))
102+
eq(self.db.guess_type("image.png"), ("image/png", None))
102103

103104
def test_main():
104105
support.run_unittest(MimeTypesTestCase,

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Per Cederqvist
201201
Matej Cepl
202202
Carl Cerecke
203203
Octavian Cerna
204+
Dave Chambers
204205
Pascal Chambon
205206
John Chandler
206207
Hye-Shik Chang

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ Core and Builtins
8181
Library
8282
-------
8383

84+
- Issue #15207: Fix mimetypes to read from correct part of Windows registry
85+
Original patch by Dave Chambers
86+
8487
- Issue #8964: fix platform._sys_version to handle IronPython 2.6+.
8588
Patch by Martin Matusiak.
8689

0 commit comments

Comments
 (0)