INNER CODE UNIT · Python
_reject_constant
simonlin1212/Vibe-Research · calc/cli.py:294
def _reject_constant(name: str):
raise ValueError(f"JSON 中不允许非标准常量 {name}(NaN / Infinity),请传 null 或有限数")
def _load_json(text: str):
"""严格 JSON 解析:拒绝 NaN / Infinity / -Infinity 字面量(Python 默认会接受)。"""
return json.loads(text, parse_constant=_reject_constant)
def _dump(obj) -> str:
try:
return json.dumps(obj, ensure_ascii=False, indent=2, allow_nan=False)
except ValueError: # 输出树含 NaN/inf(理论上被 _res 守卫挡住;此处是最后一道闸):输出全新的干净信封,不复用原对象
safe = {"calculation_id": None, "function": str((obj or {}).get("function")), "calc_version": CALC_VERSION,
"inputs": None, "inputs_resolved": {}, "inputs_refs": [],
"output": _error_out("输出含非有限数,已拦截;原始输入未回显", "nonfinite_output")}
return json.dumps(safe, ensure_ascii=False, indent=2, allow_nan=False)