summaryrefslogtreecommitdiffstats
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
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)
-rw-r--r--Doc/library/mimetypes.rst3
-rw-r--r--Lib/mimetypes.py31
-rw-r--r--Lib/test/test_mimetypes.py2
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
5 files changed, 26 insertions, 14 deletions
diff --git a/Doc/library/mimetypes.rst b/Doc/library/mimetypes.rst
index ccda1e9..8891e7a 100644
--- a/Doc/library/mimetypes.rst
+++ b/Doc/library/mimetypes.rst
@@ -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.
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.
diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py
index 788d7a8..f8e1e63 100644
--- a/Lib/test/test_mimetypes.py
+++ b/Lib/test/test_mimetypes.py
@@ -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,
diff --git a/Misc/ACKS b/Misc/ACKS
index ffa3154..e276960 100644
--- 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
diff --git a/Misc/NEWS b/Misc/NEWS
index 80a1a23..3ee2643 100644
--- 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.