From 528fe30c38365d57ca274bfe580f3b668592093a Mon Sep 17 00:00:00 2001 From: Min RK Date: Thu, 30 Sep 2021 13:02:56 +0200 Subject: [PATCH] json: call self.default for unsupported floats bpo-36841: allows `default` option to override unsupported float behavior, instead of unconditional ValueError. --- Lib/json/encoder.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py index c8c78b9c237652..207dbdac70d6f4 100644 --- a/Lib/json/encoder.py +++ b/Lib/json/encoder.py @@ -176,6 +176,11 @@ def default(self, o): return JSONEncoder.default(self, o) """ + if isinstance(o, float): + raise ValueError( + "Out of range float values are not JSON compliant: " + + repr(o) + ) raise TypeError(f'Object of type {o.__class__.__name__} ' f'is not JSON serializable') @@ -236,9 +241,7 @@ def floatstr(o, allow_nan=self.allow_nan, return _repr(o) if not allow_nan: - raise ValueError( - "Out of range float values are not JSON compliant: " + - repr(o)) + return self.default(o) return text