[Python-checkins] cpython (2.7): don't allow unicode into type_map on Windows (closes #21652)

benjamin.peterson python-checkins at python.org
Sun Jun 29 22:04:04 CEST 2014


http://hg.python.org/cpython/rev/1aafbdfba25a
changeset:   91465:1aafbdfba25a
branch:      2.7
parent:      91458:4ef517041573
user:        Benjamin Peterson <benjamin at python.org>
date:        Sun Jun 29 12:58:16 2014 -0700
summary:
  don't allow unicode into type_map on Windows (closes #21652)

Patch from Vladimir Iofik.

files:
  Lib/mimetypes.py           |  29 ++++++++++++++-----------
  Lib/test/test_mimetypes.py |  24 ++++++++++++++++++--
  Misc/NEWS                  |   3 ++
  3 files changed, 40 insertions(+), 16 deletions(-)


diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -247,23 +247,26 @@
                     break
                 i += 1
 
+        default_encoding = sys.getdefaultencoding()
         with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr:
             for subkeyname in enum_types(hkcr):
-                # Only check file extensions, not all possible classes
-                if not subkeyname.startswith("."):
-                    continue
-
-                with _winreg.OpenKey(hkcr, subkeyname) as subkey:
-                    # If there is no "Content Type" value, or if it is not
-                    # a simple string, simply skip
-                    try:
+                try:
+                    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')
-                    except EnvironmentError:
-                        continue
-                    if datatype != _winreg.REG_SZ:
-                        continue
-                    self.add_type(mimetype, subkeyname, strict)
+                        if datatype != _winreg.REG_SZ:
+                            continue
+                        try:
+                            mimetype = mimetype.encode(default_encoding)
+                        except UnicodeEncodeError:
+                            continue
+                        self.add_type(mimetype, subkeyname, strict)
+                except EnvironmentError:
+                    continue
 
 def guess_type(url, strict=True):
     """Guess the type of a file based on its URL.
diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py
--- a/Lib/test/test_mimetypes.py
+++ b/Lib/test/test_mimetypes.py
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
 import mimetypes
 import StringIO
 import unittest
@@ -95,10 +97,10 @@
             def __getattr__(self, name):
                 if name == 'EnumKey':
                     return lambda key, i: _winreg.EnumKey(key, i) + "\xa3"
-                elif name == "OpenKey":
+                elif name == 'OpenKey':
                     return lambda key, name: _winreg.OpenKey(key, name.rstrip("\xa3"))
                 elif name == 'QueryValueEx':
-                    return lambda subkey, label: (label + "\xa3", _winreg.REG_SZ)
+                    return lambda subkey, label: (u'текст/простой' , _winreg.REG_SZ)
                 return getattr(_winreg, name)
 
         mimetypes._winreg = MockWinreg()
@@ -115,7 +117,7 @@
         class MockWinreg(object):
             def __getattr__(self, name):
                 if name == 'QueryValueEx':
-                    return lambda subkey, label: (label + "\xa3", _winreg.REG_SZ)
+                    return lambda subkey, label: (u'текст/простой', _winreg.REG_SZ)
                 return getattr(_winreg, name)
 
         mimetypes._winreg = MockWinreg()
@@ -126,6 +128,22 @@
         finally:
             mimetypes._winreg = _winreg
 
+    def test_type_map_values(self):
+        import _winreg
+
+        class MockWinreg(object):
+            def __getattr__(self, name):
+                if name == 'QueryValueEx':
+                    return lambda subkey, label: (u'text/plain', _winreg.REG_SZ)
+                return getattr(_winreg, name)
+
+        mimetypes._winreg = MockWinreg()
+        try:
+            mimetypes.init()
+            self.assertTrue(isinstance(mimetypes.types_map.values()[0], str))
+        finally:
+            mimetypes._winreg = _winreg
+
 def test_main():
     test_support.run_unittest(MimeTypesTestCase,
         Win32MimeTypesTestCase
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -35,6 +35,9 @@
 Library
 -------
 
+- Issue #21652: Prevent mimetypes.type_map from containing unicode keys on
+  Windows.
+
 - Issue #21729: Used the "with" statement in the dbm.dumb module to ensure
   files closing.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list