Skip to content

Commit a3c943c

Browse files
committed
Fix YAML configuration parsing with Unicode characters on non-UTF-8 locales
1 parent 366b8ec commit a3c943c

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix YAML configuration parsing to properly handle Unicode characters on Windows systems where the UTF-8 locale is not enabled by default.

datadog_checks_base/datadog_checks/base/checks/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,9 +1492,10 @@ def load_config(yaml_str: str) -> Any:
14921492
stdin=subprocess.PIPE,
14931493
stdout=subprocess.PIPE,
14941494
stderr=subprocess.PIPE,
1495+
encoding='utf-8',
14951496
)
1496-
stdout, stderr = process.communicate(yaml_str.encode())
1497+
stdout, stderr = process.communicate(yaml_str)
14971498
if process.returncode != 0:
1498-
raise ValueError(f'Failed to load config: {stderr.decode()}')
1499+
raise ValueError(f'Failed to load config: {stderr}')
14991500

1500-
return _parse_ast_config(stdout.strip().decode())
1501+
return _parse_ast_config(stdout.strip())

datadog_checks_base/tests/base/checks/test_load_config.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,25 @@ def test_load_config_nan():
7676
config = AgentCheck.load_config("number: .nan")
7777
assert "number" in config
7878
assert math.isnan(config["number"])
79+
80+
81+
@pytest.mark.parametrize(
82+
'yaml_str, expected_object',
83+
[
84+
pytest.param(
85+
"tag: テスト",
86+
{"tag": "テスト"},
87+
id="japanese_characters",
88+
),
89+
pytest.param(
90+
"chinese: 中文测试",
91+
{"chinese": "中文测试"},
92+
id="chinese_characters",
93+
),
94+
],
95+
)
96+
def test_load_config_unicode(yaml_str, expected_object):
97+
"""Test that load_config properly handles Unicode characters including Japanese, Chinese, Korean, and emoji.
98+
This is especially important on Windows where the system locale may not default to UTF-8."""
99+
config = AgentCheck.load_config(yaml_str)
100+
assert config == expected_object

0 commit comments

Comments
 (0)