From 0c6dbbd050866054b6758223871cf65497461364 Mon Sep 17 00:00:00 2001 From: Kusok <118438257+kus04e4ek@users.noreply.github.com> Date: Wed, 19 Jun 2024 17:20:48 +0800 Subject: [PATCH] Fix not using `encoding="utf-8"` when writing to files or reading from them Co-authored-by: ChristopheClaustre --- SConstruct | 2 +- editor/template_builders.py | 2 +- methods.py | 4 ++-- platform/macos/SCsub | 2 +- platform/windows/detect.py | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/SConstruct b/SConstruct index f4b8f03519b..3fabc4706f1 100644 --- a/SConstruct +++ b/SConstruct @@ -566,7 +566,7 @@ if env["build_profile"] != "": import json try: - ft = json.load(open(env["build_profile"])) + ft = json.load(open(env["build_profile"], "r", encoding="utf-8")) if "disabled_classes" in ft: env.disabled_classes = ft["disabled_classes"] if "disabled_build_options" in ft: diff --git a/editor/template_builders.py b/editor/template_builders.py index c02a2b0c651..e4fc41caf3a 100644 --- a/editor/template_builders.py +++ b/editor/template_builders.py @@ -16,7 +16,7 @@ def parse_template(inherits, source, delimiter): meta_prefix = delimiter + " meta-" meta = ["name", "description", "version", "space-indent"] - with open(source) as f: + with open(source, "r", encoding="utf-8") as f: lines = f.readlines() for line in lines: if line.startswith(meta_prefix): diff --git a/methods.py b/methods.py index f3798d121a8..99c47ca0770 100644 --- a/methods.py +++ b/methods.py @@ -347,7 +347,7 @@ def detect_modules(search_path, recursive=False): # Godot sources when using `custom_modules` build option. version_path = os.path.join(path, "version.py") if os.path.exists(version_path): - with open(version_path) as f: + with open(version_path, "r", encoding="utf-8") as f: if 'short_name = "godot"' in f.read(): return True return False @@ -989,7 +989,7 @@ def show_progress(env): pass try: - with open(node_count_fname) as f: + with open(node_count_fname, "r", encoding="utf-8") as f: node_count_max = int(f.readline()) except Exception: pass diff --git a/platform/macos/SCsub b/platform/macos/SCsub index eb8524826f3..c965e875c18 100644 --- a/platform/macos/SCsub +++ b/platform/macos/SCsub @@ -37,7 +37,7 @@ def generate_bundle(target, source, env): shutil.copy(target_bin, app_dir + "/Contents/MacOS/Godot") version = get_build_version(False) short_version = get_build_version(True) - with open(Dir("#misc/dist/macos").abspath + "/editor_info_plist.template", "rt") as fin: + with open(Dir("#misc/dist/macos").abspath + "/editor_info_plist.template", "rt", encoding="utf-8") as fin: with open(app_dir + "/Contents/Info.plist", "wt", encoding="utf-8", newline="\n") as fout: for line in fin: line = line.replace("$version", version) diff --git a/platform/windows/detect.py b/platform/windows/detect.py index 5092c6fadff..1e8ed0aee2d 100644 --- a/platform/windows/detect.py +++ b/platform/windows/detect.py @@ -393,7 +393,7 @@ def configure_msvc(env: "SConsEnvironment", vcvars_msvc_config): # Ensure we have a location to write captured output to, in case of false positives. capture_path = methods.base_folder_path + "platform/windows/msvc_capture.log" - with open(capture_path, "wt"): + with open(capture_path, "wt", encoding="utf-8"): pass old_spawn = env["SPAWN"] @@ -417,7 +417,7 @@ def configure_msvc(env: "SConsEnvironment", vcvars_msvc_config): ret = old_spawn(sh, escape, cmd, args, env) try: - with open(tmp_stdout_name, encoding="oem", errors="replace") as tmp_stdout: + with open(tmp_stdout_name, "r", encoding=sys.stdout.encoding, errors="replace") as tmp_stdout: lines = tmp_stdout.read().splitlines() os.remove(tmp_stdout_name) except OSError: @@ -436,7 +436,7 @@ def configure_msvc(env: "SConsEnvironment", vcvars_msvc_config): if not caught and (is_cl and re_cl_capture.match(line)) or (not is_cl and re_link_capture.match(line)): caught = True try: - with open(capture_path, "a") as log: + with open(capture_path, "a", encoding=sys.stdout.encoding) as log: log.write(line + "\n") except OSError: print_warning(f'Failed to log captured line: "{line}".')