diff --git a/laya/confidence.py b/laya/confidence.py index 8e8e51149..4c790a02d 100644 --- a/laya/confidence.py +++ b/laya/confidence.py @@ -64,7 +64,10 @@ def jev_confidence(answer: Dict[str, Any]) -> Optional[float]: p = [float(probabilities[key]) for key in sorted(probabilities, key=int)] else: p = [float(value) for value in probabilities.values()] - except (TypeError, ValueError): + except (TypeError, ValueError, OverflowError): + # `OverflowError` is the same "does not read as a number": a JSON integer literal too wide + # for a double (a 400-digit probability) overflows `float()`, and an infinite score level + # overflows `int()`. `calibrate.py:780` already names it for the same reason. return None if (any(isinstance(v, bool) for v in probabilities.values()) or not all(math.isfinite(v) and v >= 0.0 for v in p)): diff --git a/tests/test_confidence.py b/tests/test_confidence.py index d11dabf91..a08821c53 100644 --- a/tests/test_confidence.py +++ b/tests/test_confidence.py @@ -2476,6 +2476,14 @@ def _score(*p): ("all-negative-probabilities", _choice(-0.9, -0.1)), ("negative-total-near-zero", _choice(-0.61, 0.6)), ("negative-score-level", _score(1.5, -0.5, 0.0)), + # `float()` and `int()` do not only refuse a value that is not a number, they also refuse one + # that is too wide to become one: JSON's grammar has no double limit, so a 400-digit integer + # probability arrives as a Python `int` and `float()` raises `OverflowError` out of a function + # whose docstring promises no number rather than a made-up one. + ("overflowing-integer-probability", _choice(10 ** 400, 1)), + ("json-integer-too-wide-for-a-double", + _json.loads('{"type":"choice","probabilities":{"a":' + "9" * 400 + ',"b":0.1}}')), + ("overflowing-score-level", {"type": "score", "probabilities": {float("inf"): 0.5, 2: 0.5}}), ): check("jev_confidence/none-for-%s" % _name, jev_confidence(_answer), None)