Merge pull request #12506 from dev-starlight/develop

fix, Add UTF-8 encoding to read_text method
This commit is contained in:
Matthias
2026-01-02 13:56:25 +01:00
committed by GitHub
7 changed files with 29 additions and 9 deletions

View File

@@ -30,7 +30,7 @@ def log_config_error_range(path: str, errmsg: str) -> str:
offsetlist = re.findall(r"(?<=Parse\serror\sat\soffset\s)\d+", errmsg)
if offsetlist:
offset = int(offsetlist[0])
text = Path(path).read_text()
text = Path(path).read_text(encoding="utf-8")
# Fetch an offset of 80 characters around the error line
subtext = text[offset - min(80, offset) : offset + 80]
segments = subtext.split("\n")

View File

@@ -148,7 +148,7 @@ class IResolver:
logger.debug("Ignoring broken symlink %s", entry)
continue
module_path = entry.resolve()
if entry.read_text().find(f"class {object_name}(") == -1:
if entry.read_text(encoding="utf-8").find(f"class {object_name}(") == -1:
logger.debug(f"Skipping {module_path} as it does not contain class {object_name}.")
continue

View File

@@ -66,8 +66,7 @@ class StrategyUpdater:
target_file = Path.joinpath(strategies_backup_folder, strategy_obj["location_rel"])
# read the file
with Path(source_file).open("r") as f:
old_code = f.read()
old_code = Path(source_file).read_text(encoding="utf-8")
if not strategies_backup_folder.is_dir():
Path(strategies_backup_folder).mkdir(parents=True, exist_ok=True)
@@ -80,8 +79,7 @@ class StrategyUpdater:
# update the code
new_code = self.update_code(old_code)
# write the modified code to the destination folder
with Path(source_file).open("w") as f:
f.write(new_code)
Path(source_file).write_text(new_code, encoding="utf-8")
# define the function to update the code
def update_code(self, code):

View File

@@ -2521,7 +2521,9 @@ def test_api_strategy(botclient, tmp_path, mocker):
assert_response(rc)
assert rc.json()["strategy"] == CURRENT_TEST_STRATEGY
data = (Path(__file__).parents[1] / "strategy/strats/strategy_test_v3.py").read_text()
data = (Path(__file__).parents[1] / "strategy/strats/strategy_test_v3.py").read_text(
encoding="utf-8"
)
assert rc.json()["code"] == data
rc = client_get(client, f"{BASE_URI}/strategy/NoStrat")

View File

@@ -14,6 +14,16 @@ class StrategyTestV2(IStrategy):
Please look at the SampleStrategy in the user_data/strategy directory
or strategy repository https://github.com/freqtrade/freqtrade-strategies
for samples and inspiration.
---
Some test asian characters.
Ensures that unicode characters are handled correctly when reading strategy files.
Otherwise this may break on windows systems.
All roughly translate to "hello world".
chinese string: "你好世界"
korean string: "안녕하세요,세계"
japanese string: "こんにちは、世界"
"""
INTERFACE_VERSION = 2

View File

@@ -23,6 +23,16 @@ class StrategyTestV3(IStrategy):
Please look at the SampleStrategy in the user_data/strategy directory
or strategy repository https://github.com/freqtrade/freqtrade-strategies
for samples and inspiration.
---
Some test asian characters.
Ensures that unicode characters are handled correctly when reading strategy files.
Otherwise this may break on windows systems.
All roughly translate to "hello world".
chinese string: "你好世界"
korean string: "안녕하세요,세계"
japanese string: "こんにちは、世界"
"""
INTERFACE_VERSION = 3

View File

@@ -15,7 +15,7 @@ def test_strategy_updater_start(user_dir, capsys) -> None:
tmpdirp = Path(user_dir) / "strategies"
tmpdirp.mkdir(parents=True, exist_ok=True)
shutil.copy(teststrats / "strategy_test_v2.py", tmpdirp)
old_code = (teststrats / "strategy_test_v2.py").read_text()
old_code = (teststrats / "strategy_test_v2.py").read_text(encoding="utf-8")
args = ["strategy-updater", "--userdir", str(user_dir), "--strategy-list", "StrategyTestV2"]
pargs = get_args(args)
@@ -29,7 +29,7 @@ def test_strategy_updater_start(user_dir, capsys) -> None:
# updated file exists
new_file = tmpdirp / "strategy_test_v2.py"
assert new_file.exists()
new_code = new_file.read_text()
new_code = new_file.read_text(encoding="utf-8")
assert "INTERFACE_VERSION = 3" in new_code
assert "INTERFACE_VERSION = 2" in old_code
captured = capsys.readouterr()