Cleanup: use context-manager for opening files

This commit is contained in:
Campbell Barton 2025-01-04 22:26:18 +11:00
parent 0f1e1bcdae
commit c5203ef7fd
4 changed files with 19 additions and 24 deletions

View file

@ -190,23 +190,21 @@ class AddPresetBase:
file_preset.write("{:s} = {!r}\n".format(rna_path_step, value))
file_preset = open(filepath, "w", encoding="utf-8")
file_preset.write("import bpy\n")
with open(filepath, "w", encoding="utf-8") as file_preset:
file_preset.write("import bpy\n")
namespace_globals = {"bpy": bpy}
namespace_locals = {}
namespace_globals = {"bpy": bpy}
namespace_locals = {}
if hasattr(self, "preset_defines"):
for rna_path in self.preset_defines:
exec(rna_path, namespace_globals, namespace_locals)
file_preset.write("{:s}\n".format(rna_path))
file_preset.write("\n")
if hasattr(self, "preset_defines"):
for rna_path in self.preset_defines:
exec(rna_path, namespace_globals, namespace_locals)
file_preset.write("{:s}\n".format(rna_path))
file_preset.write("\n")
for rna_path in self.preset_values:
value = eval(rna_path, namespace_globals, namespace_locals)
rna_recursive_attr_expand(value, rna_path, 1)
file_preset.close()
for rna_path in self.preset_values:
value = eval(rna_path, namespace_globals, namespace_locals)
rna_recursive_attr_expand(value, rna_path, 1)
preset_menu_class.bl_label = bpy.path.display_name(filename)

View file

@ -149,9 +149,8 @@ def main():
md5_update = md5_instance.update
for f in md5_source:
filehandle = open(f, "rb")
md5_update(filehandle.read())
filehandle.close()
with open(f, "rb") as fh:
md5_update(fh.read())
md5_new = md5_instance.hexdigest()

View file

@ -15,12 +15,11 @@ import os
def main():
xpm_ls = [f for f in sys.argv[1:] if f.lower().endswith(".xpm")]
print("Converting: " + " ".join(xpm_ls))
print("Converting:", " ".join(xpm_ls))
for xpm in xpm_ls:
f = open(xpm, "r")
data = f.read()
f.close()
with open(xpm, "r", encoding="utf-8") as fh:
data = fh.read()
# all after first {
data = data.split("{", 1)[1]

View file

@ -234,9 +234,8 @@ def cmake_compiler_defines() -> list[str] | None:
os.system("%s -dM -E %s > %s" % (compiler, temp_c, temp_def))
temp_def_file = open(temp_def)
lines = [l.strip() for l in temp_def_file if l.strip()]
temp_def_file.close()
with open(temp_def) as temp_def_fh:
lines = [l.strip() for l in temp_def_fh if l.strip()]
os.remove(temp_c)
os.remove(temp_def)