Commit d53f2fec authored by Benjamin Peterson's avatar Benjamin Peterson

don't allow unicode into type_map on Windows (closes #21652)

Patch from Vladimir Iofik.
parent 496d4d3b
...@@ -247,23 +247,26 @@ class MimeTypes: ...@@ -247,23 +247,26 @@ class MimeTypes:
break break
i += 1 i += 1
default_encoding = sys.getdefaultencoding()
with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr: with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr:
for subkeyname in enum_types(hkcr): for subkeyname in enum_types(hkcr):
# Only check file extensions, not all possible classes try:
if not subkeyname.startswith("."): with _winreg.OpenKey(hkcr, subkeyname) as subkey:
continue # Only check file extensions
if not subkeyname.startswith("."):
with _winreg.OpenKey(hkcr, subkeyname) as subkey: continue
# If there is no "Content Type" value, or if it is not # raises EnvironmentError if no 'Content Type' value
# a simple string, simply skip
try:
mimetype, datatype = _winreg.QueryValueEx( mimetype, datatype = _winreg.QueryValueEx(
subkey, 'Content Type') subkey, 'Content Type')
except EnvironmentError: if datatype != _winreg.REG_SZ:
continue continue
if datatype != _winreg.REG_SZ: try:
continue mimetype = mimetype.encode(default_encoding)
self.add_type(mimetype, subkeyname, strict) except UnicodeEncodeError:
continue
self.add_type(mimetype, subkeyname, strict)
except EnvironmentError:
continue
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.
......
# -*- coding: utf-8 -*-
import mimetypes import mimetypes
import StringIO import StringIO
import unittest import unittest
...@@ -95,10 +97,10 @@ class Win32MimeTypesTestCase(unittest.TestCase): ...@@ -95,10 +97,10 @@ class Win32MimeTypesTestCase(unittest.TestCase):
def __getattr__(self, name): def __getattr__(self, name):
if name == 'EnumKey': if name == 'EnumKey':
return lambda key, i: _winreg.EnumKey(key, i) + "\xa3" 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")) return lambda key, name: _winreg.OpenKey(key, name.rstrip("\xa3"))
elif name == 'QueryValueEx': elif name == 'QueryValueEx':
return lambda subkey, label: (label + "\xa3", _winreg.REG_SZ) return lambda subkey, label: (u'текст/простой' , _winreg.REG_SZ)
return getattr(_winreg, name) return getattr(_winreg, name)
mimetypes._winreg = MockWinreg() mimetypes._winreg = MockWinreg()
...@@ -115,7 +117,7 @@ class Win32MimeTypesTestCase(unittest.TestCase): ...@@ -115,7 +117,7 @@ class Win32MimeTypesTestCase(unittest.TestCase):
class MockWinreg(object): class MockWinreg(object):
def __getattr__(self, name): def __getattr__(self, name):
if name == 'QueryValueEx': if name == 'QueryValueEx':
return lambda subkey, label: (label + "\xa3", _winreg.REG_SZ) return lambda subkey, label: (u'текст/простой', _winreg.REG_SZ)
return getattr(_winreg, name) return getattr(_winreg, name)
mimetypes._winreg = MockWinreg() mimetypes._winreg = MockWinreg()
...@@ -126,6 +128,22 @@ class Win32MimeTypesTestCase(unittest.TestCase): ...@@ -126,6 +128,22 @@ class Win32MimeTypesTestCase(unittest.TestCase):
finally: finally:
mimetypes._winreg = _winreg 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(): def test_main():
test_support.run_unittest(MimeTypesTestCase, test_support.run_unittest(MimeTypesTestCase,
Win32MimeTypesTestCase Win32MimeTypesTestCase
......
...@@ -35,6 +35,9 @@ Core and Builtins ...@@ -35,6 +35,9 @@ Core and Builtins
Library 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 - Issue #21729: Used the "with" statement in the dbm.dumb module to ensure
files closing. files closing.
......
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