Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a4df90c

Browse files
committed
Issue #15207: Fix mimetypes to read from correct area in Windows registry (Original patch by Dave Chambers)
2 parents d0a7e67 + 27a8564 commit a4df90c

5 files changed

Lines changed: 23 additions & 13 deletions

File tree

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: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,25 +243,27 @@ def enum_types(mimedb):
243243
while True:
244244
try:
245245
ctype = _winreg.EnumKey(mimedb, i)
246-
except OSError:
246+
except EnvironmentError:
247247
break
248248
else:
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')
259-
except OSError:
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)
265+
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
@@ -209,6 +209,7 @@ Per Cederqvist
209209
Matej Cepl
210210
Carl Cerecke
211211
Octavian Cerna
212+
Dave Chambers
212213
Pascal Chambon
213214
John Chandler
214215
Hye-Shik Chang

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Core and Builtins
1919
Library
2020
-------
2121

22+
- Issue #15207: Fix mimetypes to read from correct part of Windows registry
23+
Original patch by Dave Chambers
24+
2225
- Issue #16595: Add prlimit() to resource module.
2326

2427
- Issue #19324: Expose Linux-specific constants in resource module.

0 commit comments

Comments
 (0)