]> granicus.if.org Git - python/commitdiff
Issue #15207: Fix mimetypes to read from correct area in Windows registry (Original...
authorTim Golden <mail@timgolden.me.uk>
Tue, 22 Oct 2013 18:27:34 +0000 (19:27 +0100)
committerTim Golden <mail@timgolden.me.uk>
Tue, 22 Oct 2013 18:27:34 +0000 (19:27 +0100)
Doc/library/mimetypes.rst
Lib/mimetypes.py
Lib/test/test_mimetypes.py
Misc/ACKS
Misc/NEWS

index be11c0dce77411869870a7a1be5cf0ae9424da4f..12c9eca6e8534843bc3d1ff818eb839b2c144890 100644 (file)
@@ -85,6 +85,9 @@ behavior of the module.
    :const:`knownfiles` takes precedence over those named before it.  Calling
    :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
       Previously, Windows registry settings were ignored.
 
index 2872ee4245cdb4fe17835b0867ca19f6b9b3848b..3e742a755b1ae355e1da23275152375f5aa67a5c 100644 (file)
@@ -249,19 +249,21 @@ class MimeTypes:
                     yield ctype
                 i += 1
 
-        with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,
-                             r'MIME\Database\Content Type') as mimedb:
-            for ctype in enum_types(mimedb):
+        with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr:
+            for subkeyname in enum_types(hkcr):
                 try:
-                    with _winreg.OpenKey(mimedb, ctype) as key:
-                        suffix, datatype = _winreg.QueryValueEx(key,
-                                                                'Extension')
+                    with _winreg.OpenKey(hkcr, subkeyname) as subkey:
+                        # Only check file extensions
+                        if not subkeyname.startswith("."):
+                            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
-                if datatype != _winreg.REG_SZ:
-                    continue
-                self.add_type(ctype, suffix, strict)
-
 
 def guess_type(url, strict=True):
     """Guess the type of a file based on its URL.
index 593fdb0a424fad0b069b65c373d39db292d83818..0b53032d39325b476279dfae0e0bd5c049cbe426 100644 (file)
@@ -98,7 +98,8 @@ class Win32MimeTypesTestCase(unittest.TestCase):
         # Use file types that should *always* exist:
         eq = self.assertEqual
         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():
     support.run_unittest(MimeTypesTestCase,
index 13e708c81a2bb78e1e068b475b5c83b59cec9be5..7ac726e5cbf9769993e86c363a81266c8606c671 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -201,6 +201,7 @@ Per Cederqvist
 Matej Cepl
 Carl Cerecke
 Octavian Cerna
+Dave Chambers
 Pascal Chambon
 John Chandler
 Hye-Shik Chang
index c2cb8504d9eb388b16aea589c152cedd9f160d04..233affa808121f8ab5d8cdacb9432b6356740704 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #15207: Fix mimetypes to read from correct part of Windows registry
+  Original patch by Dave Chambers
+
 - Issue #8964: fix platform._sys_version to handle IronPython 2.6+.
   Patch by Martin Matusiak.