diff --git a/scripts/startup/bl_operators/presets.py b/scripts/startup/bl_operators/presets.py index 15f5a656a3a..802328bb478 100644 --- a/scripts/startup/bl_operators/presets.py +++ b/scripts/startup/bl_operators/presets.py @@ -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) diff --git a/tests/python/bl_test.py b/tests/python/bl_test.py index 9001aee28e8..3fe089c41bb 100644 --- a/tests/python/bl_test.py +++ b/tests/python/bl_test.py @@ -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() diff --git a/tools/utils/make_gl_stipple_from_xpm.py b/tools/utils/make_gl_stipple_from_xpm.py index 1f80705abd1..5174d516757 100644 --- a/tools/utils/make_gl_stipple_from_xpm.py +++ b/tools/utils/make_gl_stipple_from_xpm.py @@ -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] diff --git a/tools/utils_ide/project_info.py b/tools/utils_ide/project_info.py index ae095442292..aa66ffd64b3 100755 --- a/tools/utils_ide/project_info.py +++ b/tools/utils_ide/project_info.py @@ -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)