Skip to content

Commit

Permalink
fix error with very large ints in to_jsonable
Browse files Browse the repository at this point in the history
  • Loading branch information
tybug committed Feb 8, 2024
1 parent 5ec646c commit 4d71563
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

This patch fixes an error when generating :doc:`observability <observability>` reports involving large (``n > 1e308``) integers.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from hypothesis.internal.cache import LRUReusedCache
from hypothesis.internal.compat import dataclass_asdict
from hypothesis.internal.conjecture.junkdrawer import clamp
from hypothesis.internal.floats import float_to_int
from hypothesis.internal.reflection import proxies
from hypothesis.vendor.pretty import pretty
Expand Down Expand Up @@ -160,6 +161,9 @@ def to_jsonable(obj: object) -> object:
"""
if isinstance(obj, (str, int, float, bool, type(None))):
if isinstance(obj, int) and abs(obj) >= 2**63:
# Silently clamp very large ints to max_float, to avoid
# OverflowError when casting to float.
obj = clamp(-sys.float_info.max, obj, sys.float_info.max)
return float(obj)
return obj
if isinstance(obj, (list, tuple, set, frozenset)):
Expand Down
7 changes: 7 additions & 0 deletions hypothesis-python/tests/cover/test_searchstrategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import dataclasses
import functools
import random
import sys
from collections import defaultdict, namedtuple

import attr
Expand Down Expand Up @@ -153,3 +154,9 @@ def test_jsonable_large_ints_are_floats():
n = 2**63
assert isinstance(to_jsonable(n), float)
assert to_jsonable(n) == float(n)


def test_jsonable_very_large_ints():
# previously caused OverflowError when casting to float.
n = 2**1024
assert to_jsonable(n) == sys.float_info.max

0 comments on commit 4d71563

Please sign in to comment.