changeset: 86578:12bf7fc1ba76 parent: 86576:0917f6c62c62 parent: 86577:95b88273683c user: Tim Golden date: Tue Oct 22 20:03:47 2013 +0100 files: Lib/mimetypes.py Misc/ACKS Misc/NEWS description: Issue #15207: Fix mimetypes to read from correct area in Windows registry (Original patch by Dave Chambers) diff -r 0917f6c62c62 -r 12bf7fc1ba76 Doc/library/mimetypes.rst --- a/Doc/library/mimetypes.rst Tue Oct 22 10:49:20 2013 -0700 +++ b/Doc/library/mimetypes.rst Tue Oct 22 20:03:47 2013 +0100 @@ -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 -r 0917f6c62c62 -r 12bf7fc1ba76 Lib/mimetypes.py --- a/Lib/mimetypes.py Tue Oct 22 10:49:20 2013 -0700 +++ b/Lib/mimetypes.py Tue Oct 22 20:03:47 2013 +0100 @@ -243,25 +243,27 @@ while True: try: ctype = _winreg.EnumKey(mimedb, i) - except OSError: + except EnvironmentError: break else: 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') - except OSError: + 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 -r 0917f6c62c62 -r 12bf7fc1ba76 Lib/test/test_mimetypes.py --- a/Lib/test/test_mimetypes.py Tue Oct 22 10:49:20 2013 -0700 +++ b/Lib/test/test_mimetypes.py Tue Oct 22 20:03:47 2013 +0100 @@ -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 -r 0917f6c62c62 -r 12bf7fc1ba76 Misc/ACKS --- a/Misc/ACKS Tue Oct 22 10:49:20 2013 -0700 +++ b/Misc/ACKS Tue Oct 22 20:03:47 2013 +0100 @@ -209,6 +209,7 @@ Matej Cepl Carl Cerecke Octavian Cerna +Dave Chambers Pascal Chambon John Chandler Hye-Shik Chang diff -r 0917f6c62c62 -r 12bf7fc1ba76 Misc/NEWS --- a/Misc/NEWS Tue Oct 22 10:49:20 2013 -0700 +++ b/Misc/NEWS Tue Oct 22 20:03:47 2013 +0100 @@ -19,6 +19,9 @@ Library ------- +- Issue #15207: Fix mimetypes to read from correct part of Windows registry + Original patch by Dave Chambers + - Issue #16595: Add prlimit() to resource module. - Issue #19324: Expose Linux-specific constants in resource module.

AltStyle によって変換されたページ (->オリジナル) /