@@ -243,25 +243,27 @@ def enum_types(mimedb):
243243 while True :
244244 try :
245245 ctype = _winreg .EnumKey (mimedb , i )
246- except OSError :
246+ except EnvironmentError :
247247 break
248248 else :
249249 yield ctype
250250 i += 1
251251
252- with _winreg .OpenKey (_winreg .HKEY_CLASSES_ROOT ,
253- r'MIME\Database\Content Type' ) as mimedb :
254- for ctype in enum_types (mimedb ):
252+ with _winreg .OpenKey (_winreg .HKEY_CLASSES_ROOT , '' ) as hkcr :
253+ for subkeyname in enum_types (hkcr ):
255254 try :
256- with _winreg .OpenKey (mimedb , ctype ) as key :
257- suffix , datatype = _winreg .QueryValueEx (key ,
258- 'Extension' )
259- except OSError :
255+ with _winreg .OpenKey (hkcr , subkeyname ) as subkey :
256+ # Only check file extensions
257+ if not subkeyname .startswith ("." ):
258+ continue
259+ # raises EnvironmentError if no 'Content Type' value
260+ mimetype , datatype = _winreg .QueryValueEx (
261+ subkey , 'Content Type' )
262+ if datatype != _winreg .REG_SZ :
263+ continue
264+ self .add_type (mimetype , subkeyname , strict )
265+ except EnvironmentError :
260266 continue
261- if datatype != _winreg .REG_SZ :
262- continue
263- self .add_type (ctype , suffix , strict )
264-
265267
266268def guess_type (url , strict = True ):
267269 """Guess the type of a file based on its URL.
0 commit comments