Tim Golden added the comment:
Thanks for the review, Ben. Updated patches attached.
1 & 3) default_encoding -- Your two points appear to contradict each
other slightly. What's in the updated patches is: 3.x has no encoding
(because everything's unicode end-to-end); 2.7 attempts to apply the
default encoding -- which is probably ascii -- to the extension and the
mimetype and continues on error. I'm not 100% sure about this because it
seems possible if unlikely to have a non-ascii extension / mimetype, but
this seems like the best compromise (and is no worse than what was there
before). Does that seem to fit the bill?
2) subkeyname[0] -- done
4) "throws EnvironmentError" -- done
5) test for .png -- done
----------
Added file: http://bugs.python.org/file31259/issue15207.27.2.patch
Added file: http://bugs.python.org/file31260/issue15207.33.2.patch
_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue15207>
_______________________________________
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:: 2.7
Previously, Windows registry settings were ignored.
diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -254,23 +254,26 @@
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
--- a/Lib/test/test_mimetypes.py
+++ b/Lib/test/test_mimetypes.py
@@ -85,6 +85,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():
test_support.run_unittest(MimeTypesTestCase,
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,
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com