From e8d493f4d635cee6127fc6d1c9a7c3a48f9ca385 Mon Sep 17 00:00:00 2001 From: Michael Panchenko Date: Mon, 19 May 2025 17:18:51 +0200 Subject: [PATCH] Use utf8 in all file read operations --- scripts/gen_prompt_factory.py | 4 ++-- src/multilspy/language_server.py | 2 +- .../language_servers/eclipse_jdtls/eclipse_jdtls.py | 4 ++-- src/multilspy/language_servers/gopls/gopls.py | 2 +- src/multilspy/language_servers/intelephense/intelephense.py | 4 ++-- .../language_servers/jedi_language_server/jedi_server.py | 2 +- .../kotlin_language_server/kotlin_language_server.py | 4 ++-- src/multilspy/language_servers/omnisharp/omnisharp.py | 6 +++--- .../language_servers/rust_analyzer/rust_analyzer.py | 4 ++-- src/multilspy/language_servers/solargraph/solargraph.py | 4 ++-- src/serena/llm/multilang_prompt.py | 2 +- test/resources/repos/python/test_repo/scripts/run_app.py | 2 +- 12 files changed, 20 insertions(+), 20 deletions(-) diff --git a/scripts/gen_prompt_factory.py b/scripts/gen_prompt_factory.py index a1f477bf..bc7a2e59 100644 --- a/scripts/gen_prompt_factory.py +++ b/scripts/gen_prompt_factory.py @@ -31,13 +31,13 @@ def main(): ) # write prompt factory with added methods - with open("code_templates/prompt_factory_template.py") as f: + with open("code_templates/prompt_factory_template.py", encoding="utf-8") as f: code = f.read() methods_str = "".join(methods) code = code.replace("# methods", methods_str) prompt_factory_module = f"{package}/llm/prompt_factory.py" - with open(prompt_factory_module, "w") as f: + with open(prompt_factory_module, "w", encoding="utf-8") as f: f.write(code) log.info(f"Prompt factory generated successfully in {prompt_factory_module}") diff --git a/src/multilspy/language_server.py b/src/multilspy/language_server.py index 98ab6dbc..b556b943 100644 --- a/src/multilspy/language_server.py +++ b/src/multilspy/language_server.py @@ -123,7 +123,7 @@ class LanguageServer: raise ValueError( f"Asked to add gitignore content to the config for {repository_root_path=} but there already is a non-empty entry" ) - with open(gitignore_path) as f: + with open(gitignore_path, "r", encoding="utf-8") as f: gitignore_file_content = f.read() config.gitignore_file_content = gitignore_file_content diff --git a/src/multilspy/language_servers/eclipse_jdtls/eclipse_jdtls.py b/src/multilspy/language_servers/eclipse_jdtls/eclipse_jdtls.py index 1f952283..6753bf12 100644 --- a/src/multilspy/language_servers/eclipse_jdtls/eclipse_jdtls.py +++ b/src/multilspy/language_servers/eclipse_jdtls/eclipse_jdtls.py @@ -165,7 +165,7 @@ class EclipseJDTLS(LanguageServer): """ platformId = PlatformUtils.get_platform_id() - with open(str(PurePath(os.path.dirname(__file__), "runtime_dependencies.json")), "r") as f: + with open(str(PurePath(os.path.dirname(__file__), "runtime_dependencies.json")), "r", encoding="utf-8") as f: runtimeDependencies = json.load(f) del runtimeDependencies["_description"] @@ -264,7 +264,7 @@ class EclipseJDTLS(LanguageServer): Returns the initialize parameters for the EclipseJDTLS server. """ # Look into https://github.com/eclipse/eclipse.jdt.ls/blob/master/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java to understand all the options available - with open(str(PurePath(os.path.dirname(__file__), "initialize_params.json")), "r") as f: + with open(str(PurePath(os.path.dirname(__file__), "initialize_params.json")), "r", encoding="utf-8") as f: d: InitializeParams = json.load(f) del d["_description"] diff --git a/src/multilspy/language_servers/gopls/gopls.py b/src/multilspy/language_servers/gopls/gopls.py index 6269db82..ac474451 100644 --- a/src/multilspy/language_servers/gopls/gopls.py +++ b/src/multilspy/language_servers/gopls/gopls.py @@ -90,7 +90,7 @@ class Gopls(LanguageServer): """ Returns the initialize params for the TypeScript Language Server. """ - with open(os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r") as f: + with open(os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r", encoding="utf-8") as f: d = json.load(f) del d["_description"] diff --git a/src/multilspy/language_servers/intelephense/intelephense.py b/src/multilspy/language_servers/intelephense/intelephense.py index 1e7da5a2..33382cfc 100644 --- a/src/multilspy/language_servers/intelephense/intelephense.py +++ b/src/multilspy/language_servers/intelephense/intelephense.py @@ -52,7 +52,7 @@ class Intelephense(LanguageServer): ] assert platform_id in valid_platforms, f"Platform {platform_id} is not supported for multilspy PHP at the moment" - with open(os.path.join(os.path.dirname(__file__), "runtime_dependencies.json"), "r") as f: + with open(os.path.join(os.path.dirname(__file__), "runtime_dependencies.json"), "r", encoding="utf-8") as f: d = json.load(f) del d["_description"] @@ -116,7 +116,7 @@ class Intelephense(LanguageServer): """ Returns the initialize params for the TypeScript Language Server. """ - with open(os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r") as f: + with open(os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r", encoding="utf-8") as f: d = json.load(f) del d["_description"] diff --git a/src/multilspy/language_servers/jedi_language_server/jedi_server.py b/src/multilspy/language_servers/jedi_language_server/jedi_server.py index 9dec2c7d..90d4376e 100644 --- a/src/multilspy/language_servers/jedi_language_server/jedi_server.py +++ b/src/multilspy/language_servers/jedi_language_server/jedi_server.py @@ -43,7 +43,7 @@ class JediServer(LanguageServer): """ Returns the initialize params for the Jedi Language Server. """ - with open(os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r") as f: + with open(os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r", encoding="utf-8") as f: d = json.load(f) del d["_description"] diff --git a/src/multilspy/language_servers/kotlin_language_server/kotlin_language_server.py b/src/multilspy/language_servers/kotlin_language_server/kotlin_language_server.py index 76bc1e86..5edc0e76 100644 --- a/src/multilspy/language_servers/kotlin_language_server/kotlin_language_server.py +++ b/src/multilspy/language_servers/kotlin_language_server/kotlin_language_server.py @@ -67,7 +67,7 @@ class KotlinLanguageServer(LanguageServer): assert platform_id.value.startswith("win-") or platform_id.value.startswith("linux-") or platform_id.value.startswith("osx-"), "Only Windows, Linux and macOS platforms are supported for Kotlin in multilspy at the moment" # Load dependency information - with open(os.path.join(os.path.dirname(__file__), "runtime_dependencies.json"), "r") as f: + with open(os.path.join(os.path.dirname(__file__), "runtime_dependencies.json"), "r", encoding="utf-8") as f: d = json.load(f) del d["_description"] @@ -134,7 +134,7 @@ class KotlinLanguageServer(LanguageServer): """ Returns the initialize params for the Kotlin Language Server. """ - with open(str(pathlib.PurePath(os.path.dirname(__file__), "initialize_params.json")), "r") as f: + with open(str(pathlib.PurePath(os.path.dirname(__file__), "initialize_params.json")), "r", encoding="utf-8") as f: d: InitializeParams = json.load(f) del d["_description"] diff --git a/src/multilspy/language_servers/omnisharp/omnisharp.py b/src/multilspy/language_servers/omnisharp/omnisharp.py index 27ad76a5..08295a91 100644 --- a/src/multilspy/language_servers/omnisharp/omnisharp.py +++ b/src/multilspy/language_servers/omnisharp/omnisharp.py @@ -118,7 +118,7 @@ class OmniSharp(LanguageServer): """ Returns the initialize params for the Omnisharp Language Server. """ - with open(os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r") as f: + with open(os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r", encoding="utf-8") as f: d = json.load(f) del d["_description"] @@ -145,7 +145,7 @@ class OmniSharp(LanguageServer): platform_id = PlatformUtils.get_platform_id() dotnet_version = PlatformUtils.get_dotnet_version() - with open(os.path.join(os.path.dirname(__file__), "runtime_dependencies.json"), "r") as f: + with open(os.path.join(os.path.dirname(__file__), "runtime_dependencies.json"), "r", encoding="utf-8") as f: d = json.load(f) del d["_description"] @@ -384,7 +384,7 @@ class OmniSharp(LanguageServer): ) init_response = await self.server.send.initialize(initialize_params) self.server.notify.initialized({}) - with open(os.path.join(os.path.dirname(__file__), "workspace_did_change_configuration.json"), "r") as f: + with open(os.path.join(os.path.dirname(__file__), "workspace_did_change_configuration.json"), "r", encoding="utf-8") as f: self.server.notify.workspace_did_change_configuration({ "settings": json.load(f) }) diff --git a/src/multilspy/language_servers/rust_analyzer/rust_analyzer.py b/src/multilspy/language_servers/rust_analyzer/rust_analyzer.py index f36a9d18..6a2dcee5 100644 --- a/src/multilspy/language_servers/rust_analyzer/rust_analyzer.py +++ b/src/multilspy/language_servers/rust_analyzer/rust_analyzer.py @@ -51,7 +51,7 @@ class RustAnalyzer(LanguageServer): """ platform_id = PlatformUtils.get_platform_id() - with open(os.path.join(os.path.dirname(__file__), "runtime_dependencies.json"), "r") as f: + with open(os.path.join(os.path.dirname(__file__), "runtime_dependencies.json"), "r", encoding="utf-8") as f: d = json.load(f) del d["_description"] @@ -88,7 +88,7 @@ class RustAnalyzer(LanguageServer): """ Returns the initialize params for the Rust Analyzer Language Server. """ - with open(os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r") as f: + with open(os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r", encoding="utf-8") as f: d = json.load(f) del d["_description"] diff --git a/src/multilspy/language_servers/solargraph/solargraph.py b/src/multilspy/language_servers/solargraph/solargraph.py index 5017748c..057c5b8f 100644 --- a/src/multilspy/language_servers/solargraph/solargraph.py +++ b/src/multilspy/language_servers/solargraph/solargraph.py @@ -56,7 +56,7 @@ class Solargraph(LanguageServer): if platform_id in [PlatformId.WIN_x64, PlatformId.WIN_arm64, PlatformId.WIN_x86]: which_cmd = "where" - with open(os.path.join(os.path.dirname(__file__), "runtime_dependencies.json"), "r") as f: + with open(os.path.join(os.path.dirname(__file__), "runtime_dependencies.json"), "r", encoding="utf-8") as f: d = json.load(f) del d["_description"] @@ -100,7 +100,7 @@ class Solargraph(LanguageServer): """ Returns the initialize params for the Solargraph Language Server. """ - with open(os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r") as f: + with open(os.path.join(os.path.dirname(__file__), "initialize_params.json"), "r", encoding="utf-8") as f: d = json.load(f) del d["_description"] diff --git a/src/serena/llm/multilang_prompt.py b/src/serena/llm/multilang_prompt.py index 4092e735..1c8f8f5e 100644 --- a/src/serena/llm/multilang_prompt.py +++ b/src/serena/llm/multilang_prompt.py @@ -180,7 +180,7 @@ class MultiLangPromptTemplateCollection: for fn in os.listdir(prompts_dir): path = os.path.join(prompts_dir, fn) if fn.endswith(".txt"): - with open(path) as file: + with open(path, encoding="utf-8") as file: prompt_template_string = file.read() self._add_prompt_template(fn[:-4], prompt_template_string) elif fn.endswith(".yml"): diff --git a/test/resources/repos/python/test_repo/scripts/run_app.py b/test/resources/repos/python/test_repo/scripts/run_app.py index 40c76c6d..96254510 100644 --- a/test/resources/repos/python/test_repo/scripts/run_app.py +++ b/test/resources/repos/python/test_repo/scripts/run_app.py @@ -44,7 +44,7 @@ def load_config(config_path: str) -> dict[str, Any]: return {} try: - with open(config_path) as f: + with open(config_path, encoding="utf-8") as f: return json.load(f) except json.JSONDecodeError: logger.error(f"Invalid JSON in configuration file: {config_path}")