summaryrefslogtreecommitdiffstats
path: root/Lib/mimetypes.py
diff options
context:
space:
mode:
authorTim Golden <mail@timgolden.me.uk>2013-10-22 19:45:13 (GMT)
committerTim Golden <mail@timgolden.me.uk>2013-10-22 19:45:13 (GMT)
commit17934856dc76b8cf1667a293445df6aeca225768 (patch)
treee77c8c8ac228a214eb56e78db1133042feb7d4d9 /Lib/mimetypes.py
parent51c4d72d436769dab2edbbc7b4685912546e8d47 (diff)
downloadcpython-17934856dc76b8cf1667a293445df6aeca225768.zip
cpython-17934856dc76b8cf1667a293445df6aeca225768.tar.gz
cpython-17934856dc76b8cf1667a293445df6aeca225768.tar.bz2
Issue #15207: Fix mimetypes to read from correct area in Windows registry (Original patch by Dave Chambers)
Diffstat (limited to 'Lib/mimetypes.py')
-rw-r--r--Lib/mimetypes.py31
1 files changed, 17 insertions, 14 deletions
diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
index 18ade73..3dde4cd 100644
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -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.