diff -r 51ac5f06dd04 Lib/mimetypes.py --- a/Lib/mimetypes.py Tue Jul 24 03:45:39 2012 -0700 +++ b/Lib/mimetypes.py Wed Jul 25 11:43:56 2012 +0900 @@ -238,30 +238,35 @@ if not _winreg: return - def enum_types(mimedb): + def enum_types(hkcr): i = 0 while True: try: - ctype = _winreg.EnumKey(mimedb, i) + subkeyname = _winreg.EnumKey(hkcr, i) except EnvironmentError: break else: - yield ctype + yield subkeyname i += 1 - with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, - r'MIME\Database\Content Type') as mimedb: - for ctype in enum_types(mimedb): - try: - with _winreg.OpenKey(mimedb, ctype) as key: - suffix, datatype = _winreg.QueryValueEx(key, - 'Extension') - except EnvironmentError: + with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr: + for subkeyname in enum_types(hkcr): + # Only check entries that begin with dots, + # i.e. file extensions + if not subkeyname[0] == ".": continue - if datatype != _winreg.REG_SZ: - continue - self.add_type(ctype, suffix, strict) - + # subkeyname is the file extension + with _winreg.OpenKey(hkcr, subkeyname) as subkey: + try: + mimetype, datatype = _winreg.QueryValueEx( + subkey, 'Content Type') + except EnvironmentError: + # throws EnvironmentError if no 'Content Type' value + continue + + if datatype != _winreg.REG_SZ: + continue + self.add_type(mimetype, subkeyname, strict) def guess_type(url, strict=True): """Guess the type of a file based on its URL.