[Python-checkins] cpython (3.3): Issue #15207: Fix mimetypes to read from correct area in Windows registry
tim.golden
python-checkins at python.org
Tue Oct 22 21:05:11 CEST 2013
http://hg.python.org/cpython/rev/95b88273683c
changeset: 86577:95b88273683c
branch: 3.3
parent: 86561:4c4f31a1b706
user: Tim Golden <mail at timgolden.me.uk>
date: Tue Oct 22 19:27:34 2013 +0100
summary:
Issue #15207: Fix mimetypes to read from correct area in Windows registry (Original patch by Dave Chambers)
files:
Doc/library/mimetypes.rst | 3 +++
Lib/mimetypes.py | 22 ++++++++++++----------
Lib/test/test_mimetypes.py | 3 ++-
Misc/ACKS | 1 +
Misc/NEWS | 3 +++
5 files changed, 21 insertions(+), 11 deletions(-)
diff --git a/Doc/library/mimetypes.rst b/Doc/library/mimetypes.rst
--- a/Doc/library/mimetypes.rst
+++ b/Doc/library/mimetypes.rst
@@ -85,6 +85,9 @@
: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:: 3.2
Previously, Windows registry settings were ignored.
diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -249,19 +249,21 @@
yield ctype
i += 1
- 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
+ self.add_type(mimetype, subkeyname, strict)
except EnvironmentError:
continue
- if datatype != _winreg.REG_SZ:
- 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
--- a/Lib/test/test_mimetypes.py
+++ b/Lib/test/test_mimetypes.py
@@ -98,7 +98,8 @@
# 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():
support.run_unittest(MimeTypesTestCase,
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -201,6 +201,7 @@
Matej Cepl
Carl Cerecke
Octavian Cerna
+Dave Chambers
Pascal Chambon
John Chandler
Hye-Shik Chang
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,9 @@
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.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list