# HG changeset patch # User Vladimir Iofik # Date 1403959468 -14400 # Sat Jun 28 16:44:28 2014 +0400 # Branch 2.7 # Node ID 695c45d508dfa2200888490ab4fbed2d113e2dba # Parent 94f7cdab9f71d5913e2fd10326488373cc0eddc9 fix issue 21652 diff -r 94f7cdab9f71 -r 695c45d508df Lib/mimetypes.py --- a/Lib/mimetypes.py Thu Jun 26 23:38:14 2014 -0700 +++ b/Lib/mimetypes.py Sat Jun 28 16:44:28 2014 +0400 @@ -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 -r 94f7cdab9f71 -r 695c45d508df Lib/test/test_mimetypes.py --- a/Lib/test/test_mimetypes.py Thu Jun 26 23:38:14 2014 -0700 +++ b/Lib/test/test_mimetypes.py Sat Jun 28 16:44:28 2014 +0400 @@ -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