@@ -367,33 +367,27 @@ def load_info(info_path: str) -> Any:
367367 import pickle
368368
369369 try :
370- info_file = open (info_path , "rb" )
371- except OSError as exc :
370+ with open (info_path , "rb" ) as info_file :
371+ return pickle .load (info_file )
372+ except (OSError , EOFError ) as exc :
372373 raise _InvalidInfoFileError () from exc
373374
374- try :
375- return pickle .load (info_file )
376- except EOFError as exc :
377- raise _InvalidInfoFileError () from exc
378- finally :
379- info_file .close ()
380-
381375 def check_deps (deps : list [_Dependency ]) -> bool :
382- for name , date , md5sum in deps :
376+ for dep_name , date , md5sum in deps :
383377 try :
384- possibly_updated = os .stat (name ).st_mtime != date
378+ possibly_updated = os .stat (dep_name ).st_mtime != date
385379 except OSError as e :
386380 if debug_recompile :
387381 logger .info (
388382 "recompiling because dependency %s is "
389- "inaccessible (%s)." , name , e )
383+ "inaccessible (%s)." , dep_name , e )
390384 return False
391385 else :
392- if possibly_updated and md5sum != get_file_md5sum (name ):
386+ if possibly_updated and md5sum != get_file_md5sum (dep_name ):
393387 if debug_recompile :
394388 logger .info (
395389 "recompiling because dependency %s was "
396- "updated." , name )
390+ "updated." , dep_name )
397391 return False
398392
399393 return True
@@ -402,18 +396,17 @@ def check_source(source_path: list[str]) -> bool:
402396 valid = True
403397 for i , path in enumerate (source_path ):
404398 source = source_string [i ]
399+
405400 try :
406- src_f = open (path , "r" if not source_is_binary else "rb" )
401+ with open (path , "r" if not source_is_binary else "rb" ) as src_f :
402+ valid = valid and src_f .read () == source
407403 except OSError :
408404 if debug_recompile :
409405 logger .info (
410406 "recompiling because cache directory does "
411407 "not contain source file '%s'." , path )
412408 return False
413409
414- valid = valid and src_f .read () == source
415- src_f .close ()
416-
417410 if not valid :
418411 from warnings import warn
419412 warn ("hash collision in compiler cache" , stacklevel = 2 )
@@ -473,7 +466,7 @@ def check_source(source_path: list[str]) -> bool:
473466 dependencies = get_dep_structure (source_paths ),
474467 source_name = source_name ), info_file )
475468
476- return hex_checksum , mod_name , ext_file , True
469+ return hex_checksum , mod_name , ext_file , True # noqa: TRY300
477470 except Exception :
478471 cleanup_m .error_clean_up ()
479472 raise
0 commit comments