Use context manager for reading addon headers

This commit is contained in:
Campbell Barton 2015-06-08 21:21:54 +10:00
parent 07d51141ae
commit d63615272c

View File

@ -80,6 +80,7 @@ def modules_refresh(module_cache=addons_fake_modules):
print("Error opening file %r: %s" % (mod_path, e)) print("Error opening file %r: %s" % (mod_path, e))
return None return None
with file_mod:
if speedy: if speedy:
lines = [] lines = []
line_iter = iter(file_mod) line_iter = iter(file_mod)
@ -91,7 +92,6 @@ def modules_refresh(module_cache=addons_fake_modules):
if not error_encoding: if not error_encoding:
error_encoding = True error_encoding = True
print("Error reading file as UTF-8:", mod_path, e) print("Error reading file as UTF-8:", mod_path, e)
file_mod.close()
return None return None
if len(l) == 0: if len(l) == 0:
@ -104,15 +104,13 @@ def modules_refresh(module_cache=addons_fake_modules):
if not error_encoding: if not error_encoding:
error_encoding = True error_encoding = True
print("Error reading file as UTF-8:", mod_path, e) print("Error reading file as UTF-8:", mod_path, e)
file_mod.close()
return None return None
data = "".join(lines) data = "".join(lines)
else: else:
data = file_mod.read() data = file_mod.read()
del file_mod
file_mod.close()
try: try:
ast_data = ast.parse(data, filename=mod_path) ast_data = ast.parse(data, filename=mod_path)