Kaydet (Commit) a4df90ce authored tarafından Tim Golden's avatar Tim Golden

Issue #15207: Fix mimetypes to read from correct area in Windows registry…

Issue #15207: Fix mimetypes to read from correct area in Windows registry (Original patch by Dave Chambers)
...@@ -85,6 +85,9 @@ behavior of the module. ...@@ -85,6 +85,9 @@ behavior of the module.
:const:`knownfiles` takes precedence over those named before it. Calling :const:`knownfiles` takes precedence over those named before it. Calling
:func:`init` repeatedly is allowed. :func:`init` repeatedly is allowed.
Specifying an empty list for *files* will prevent the system defaults from
being applied: only the well-known values will be present from a built-in list.
.. versionchanged:: 3.2 .. versionchanged:: 3.2
Previously, Windows registry settings were ignored. Previously, Windows registry settings were ignored.
......
...@@ -243,25 +243,27 @@ class MimeTypes: ...@@ -243,25 +243,27 @@ class MimeTypes:
while True: while True:
try: try:
ctype = _winreg.EnumKey(mimedb, i) ctype = _winreg.EnumKey(mimedb, i)
except OSError: except EnvironmentError:
break break
else: else:
yield ctype yield ctype
i += 1 i += 1
with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr:
r'MIME\Database\Content Type') as mimedb: for subkeyname in enum_types(hkcr):
for ctype in enum_types(mimedb):
try: try:
with _winreg.OpenKey(mimedb, ctype) as key: with _winreg.OpenKey(hkcr, subkeyname) as subkey:
suffix, datatype = _winreg.QueryValueEx(key, # Only check file extensions
'Extension') if not subkeyname.startswith("."):
except OSError: continue
# raises EnvironmentError if no 'Content Type' value
mimetype, datatype = _winreg.QueryValueEx(
subkey, 'Content Type')
if datatype != _winreg.REG_SZ:
continue
self.add_type(mimetype, subkeyname, strict)
except EnvironmentError:
continue continue
if datatype != _winreg.REG_SZ:
continue
self.add_type(ctype, suffix, strict)
def guess_type(url, strict=True): def guess_type(url, strict=True):
"""Guess the type of a file based on its URL. """Guess the type of a file based on its URL.
......
...@@ -98,7 +98,8 @@ class Win32MimeTypesTestCase(unittest.TestCase): ...@@ -98,7 +98,8 @@ class Win32MimeTypesTestCase(unittest.TestCase):
# Use file types that should *always* exist: # Use file types that should *always* exist:
eq = self.assertEqual eq = self.assertEqual
eq(self.db.guess_type("foo.txt"), ("text/plain", None)) eq(self.db.guess_type("foo.txt"), ("text/plain", None))
eq(self.db.guess_type("image.jpg"), ("image/jpeg", None))
eq(self.db.guess_type("image.png"), ("image/png", None))
def test_main(): def test_main():
support.run_unittest(MimeTypesTestCase, support.run_unittest(MimeTypesTestCase,
......
...@@ -209,6 +209,7 @@ Per Cederqvist ...@@ -209,6 +209,7 @@ Per Cederqvist
Matej Cepl Matej Cepl
Carl Cerecke Carl Cerecke
Octavian Cerna Octavian Cerna
Dave Chambers
Pascal Chambon Pascal Chambon
John Chandler John Chandler
Hye-Shik Chang Hye-Shik Chang
......
...@@ -19,6 +19,9 @@ Core and Builtins ...@@ -19,6 +19,9 @@ Core and Builtins
Library Library
------- -------
- Issue #15207: Fix mimetypes to read from correct part of Windows registry
Original patch by Dave Chambers
- Issue #16595: Add prlimit() to resource module. - Issue #16595: Add prlimit() to resource module.
- Issue #19324: Expose Linux-specific constants in resource module. - Issue #19324: Expose Linux-specific constants in resource module.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment