From 77d851889dbe65fc0c4429a6f4b0696e56401e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pred=C3=A4?= <46051820+PredaaA@users.noreply.github.com> Date: Wed, 29 Oct 2025 12:44:55 +0100 Subject: [PATCH] refactor: Replace codecs.open with built-in open for UTF-8 file handling --- fluent.runtime/fluent/runtime/fallback.py | 3 +-- fluent.runtime/tests/test_utils.py | 3 +-- fluent.runtime/tests/utils.py | 7 ++++++- fluent.syntax/tests/syntax/test_reference.py | 3 +-- fluent.syntax/tests/syntax/test_structure.py | 3 +-- tools/fluentfmt.py | 3 +-- tools/parse.py | 3 +-- tools/serialize.py | 3 +-- 8 files changed, 13 insertions(+), 15 deletions(-) diff --git a/fluent.runtime/fluent/runtime/fallback.py b/fluent.runtime/fluent/runtime/fallback.py index f39bbf82..0433c625 100644 --- a/fluent.runtime/fluent/runtime/fallback.py +++ b/fluent.runtime/fluent/runtime/fallback.py @@ -1,4 +1,3 @@ -import codecs import os from typing import ( TYPE_CHECKING, @@ -134,7 +133,7 @@ def resources( path = self.localize_path(os.path.join(root, resource_id), locale) if not os.path.isfile(path): continue - content = codecs.open(path, "r", "utf-8").read() + content = open(path, "r", encoding="utf-8").read() resources.append(FluentParser().parse(content)) if resources: yield resources diff --git a/fluent.runtime/tests/test_utils.py b/fluent.runtime/tests/test_utils.py index 32c0ab0e..976e4775 100644 --- a/fluent.runtime/tests/test_utils.py +++ b/fluent.runtime/tests/test_utils.py @@ -1,7 +1,6 @@ import unittest from .utils import patch_files import os -import codecs class TestFileSimulate(unittest.TestCase): @@ -38,5 +37,5 @@ def assertFileIs(self, filename, expect_contents): else: self.assertTrue(os.path.isfile(filename), f"Expected {filename} to exist.") - with codecs.open(filename, "r", "utf-8") as f: + with open(filename, "r", encoding="utf-8") as f: self.assertEqual(f.read(), expect_contents) diff --git a/fluent.runtime/tests/utils.py b/fluent.runtime/tests/utils.py index fec04180..ff3d5226 100644 --- a/fluent.runtime/tests/utils.py +++ b/fluent.runtime/tests/utils.py @@ -42,9 +42,14 @@ def patch_files(files: dict): def then(func): @mock.patch("os.path.isfile", side_effect=lambda p: _normalize_file_path(p) in files) - @mock.patch("codecs.open", side_effect=lambda p, _, __: StringIO(files[_normalize_file_path(p)])) + @mock.patch( + "builtins.open", + side_effect=lambda p, *a, **kw: StringIO(files[_normalize_file_path(p)]), + ) @functools.wraps(func) # Make ret look like func to later decorators def ret(*args, **kwargs): func(*args[:-2], **kwargs) + return ret + return then diff --git a/fluent.syntax/tests/syntax/test_reference.py b/fluent.syntax/tests/syntax/test_reference.py index 0de996e0..b62925ad 100644 --- a/fluent.syntax/tests/syntax/test_reference.py +++ b/fluent.syntax/tests/syntax/test_reference.py @@ -1,4 +1,3 @@ -import codecs import json import os import unittest @@ -7,7 +6,7 @@ def read_file(path): - with codecs.open(path, "r", encoding="utf-8") as file: + with open(path, "r", encoding="utf-8") as file: text = file.read() return text diff --git a/fluent.syntax/tests/syntax/test_structure.py b/fluent.syntax/tests/syntax/test_structure.py index 067187fb..cfdd54fe 100644 --- a/fluent.syntax/tests/syntax/test_structure.py +++ b/fluent.syntax/tests/syntax/test_structure.py @@ -1,4 +1,3 @@ -import codecs import json import os import unittest @@ -7,7 +6,7 @@ def read_file(path): - with codecs.open(path, "r", encoding="utf-8") as file: + with open(path, "r", encoding="utf-8") as file: text = file.read() return text diff --git a/tools/fluentfmt.py b/tools/fluentfmt.py index 9d6b75a2..ffec39f2 100755 --- a/tools/fluentfmt.py +++ b/tools/fluentfmt.py @@ -1,6 +1,5 @@ #!/usr/bin/python -import codecs import sys from fluent.syntax import parse, serialize @@ -9,7 +8,7 @@ def read_file(path): - with codecs.open(path, "r", encoding="utf-8") as file: + with open(path, "r", encoding="utf-8") as file: text = file.read() return text diff --git a/tools/parse.py b/tools/parse.py index f3fece98..ad2f46ab 100755 --- a/tools/parse.py +++ b/tools/parse.py @@ -1,6 +1,5 @@ #!/usr/bin/python -import codecs import json import sys @@ -10,7 +9,7 @@ def read_file(path): - with codecs.open(path, "r", encoding="utf-8") as file: + with open(path, "r", encoding="utf-8") as file: text = file.read() return text diff --git a/tools/serialize.py b/tools/serialize.py index dccaa8d6..6edb5937 100755 --- a/tools/serialize.py +++ b/tools/serialize.py @@ -1,6 +1,5 @@ #!/usr/bin/python -import codecs import json import sys @@ -10,7 +9,7 @@ def read_json(path): - with codecs.open(path, "r", encoding="utf-8") as file: + with open(path, "r", encoding="utf-8") as file: return json.load(file)