]> 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 19:45:13 +0000 (20:45 +0100)
committerTim Golden <mail@timgolden.me.uk>
Tue, 22 Oct 2013 19:45:13 +0000 (20:45 +0100)
Doc/library/mimetypes.rst
Lib/mimetypes.py
Lib/test/test_mimetypes.py
Misc/ACKS
Misc/NEWS

index ccda1e9fd203ca3575b41bd19242fc030b664192..8891e7a76ac43cce60d0574b47a960c537df5403 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:: 2.7
       Previously, Windows registry settings were ignored.
 
index 18ade7381adabdc149ab6c748722ae529ff2822e..3dde4cd9380678097be22050e21440d1309e24b1 100644 (file)
@@ -254,23 +254,26 @@ class MimeTypes:
                 i += 1
 
         default_encoding = sys.getdefaultencoding()
-        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
+                        try:
+                            mimetype = mimetype.encode(default_encoding)
+                            subkeyname = subkeyname.encode(default_encoding)
+                        except UnicodeEncodeError:
+                            continue
+                        self.add_type(mimetype, subkeyname, strict)
                 except EnvironmentError:
                     continue
-                if datatype != _winreg.REG_SZ:
-                    continue
-                try:
-                    suffix = suffix.encode(default_encoding) # omit in 3.x!
-                except UnicodeEncodeError:
-                    continue
-                self.add_type(ctype, suffix, strict)
-
 
 def guess_type(url, strict=True):
     """Guess the type of a file based on its URL.
index 788d7a8360a5c9ae2ccc8dbd5789e9c114037b3f..f8e1e632821cb3135475edd1b98d6859e808f4f5 100644 (file)
@@ -85,6 +85,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():
     test_support.run_unittest(MimeTypesTestCase,
index ffa3154f98cb75f650cba6f805900f1a2861eeaf..e276960db78e75b7610c8a082b581780433b08f0 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -166,6 +166,7 @@ Jesús Cea Avión
 Per Cederqvist
 Carl Cerecke
 Octavian Cerna
+Dave Chambers
 Pascal Chambon
 John Chandler
 Hye-Shik Chang
index 80a1a231f36841a7bbb77694c2fdac67153d2774..3ee26437a849d429f7f1bd5cf4c9f94de7c10ca2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -40,6 +40,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.