Skip to content

Commit 01d0710

Browse files
committed
Fix handling of special yaml values in config.
1 parent 196f124 commit 01d0710

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

datadog_checks_base/datadog_checks/base/checks/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,8 @@ def load_config(yaml_str: str) -> Any:
14901490

14911491
decoded = stdout.strip().decode()
14921492
try:
1493-
return eval(decoded)
1493+
special_values = {"nan": float("nan"), "inf": float("inf"), "-inf": float("-inf")}
1494+
return eval(decoded, special_values)
14941495
# a single, literal unquoted string
14951496
except Exception:
14961497
return decoded

datadog_checks_base/tests/base/checks/test_agent_check.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Licensed under a 3-clause BSD style license (see LICENSE)
66
import json
77
import logging
8+
import math
89
import os
910
import re
1011
from typing import Any # noqa: F401
@@ -50,6 +51,39 @@ def test_load_config():
5051
AgentCheck.load_config(':')
5152

5253

54+
def test_parse_special_values_in_load_config():
55+
assert AgentCheck.load_config("boolean: true") == {"boolean": True}
56+
assert AgentCheck.load_config("boolean: false") == {"boolean": False}
57+
58+
assert AgentCheck.load_config("number: .inf") == {"number": float("inf")}
59+
assert AgentCheck.load_config("number: .INF") == {"number": float("inf")}
60+
61+
assert AgentCheck.load_config("number: -.inf") == {"number": float("-inf")}
62+
assert AgentCheck.load_config("number: -.INF") == {"number": float("-inf")}
63+
64+
assert AgentCheck.load_config("number: 0xF") == {"number": 15.0} # Hexadecimal
65+
assert AgentCheck.load_config("number: 0b1111") == {"number": 15.0} # Binary
66+
67+
assert AgentCheck.load_config("number: !!int 1 ") == {"number": 1}
68+
assert isinstance(AgentCheck.load_config("number: !!int 1 ")["number"], int)
69+
70+
assert AgentCheck.load_config("number: !!float 1 ") == {"number": 1}
71+
assert isinstance(AgentCheck.load_config("number: !!float 1 ")["number"], float)
72+
73+
# Check that "inf" is still a string
74+
assert AgentCheck.load_config("string: inf") == {"string": "inf"}
75+
assert isinstance(AgentCheck.load_config("string: inf")["string"], str)
76+
77+
# Check that quoted ".inf" is still a string
78+
assert AgentCheck.load_config("string: \".inf\"") == {"string": ".inf"}
79+
assert isinstance(AgentCheck.load_config("string: \".inf\"")["string"], str)
80+
81+
# nan values are not equal to themselves
82+
config = AgentCheck.load_config("number: .nan")
83+
assert "number" in config
84+
assert math.isnan(config["number"])
85+
86+
5387
def test_persistent_cache(datadog_agent):
5488
check = AgentCheck()
5589
check.check_id = 'test'

0 commit comments

Comments
 (0)