Skip to content

Commit 1436b56

Browse files
committed
Handle multipleOf overflow
1 parent ddcc2e2 commit 1436b56

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

jsonschema/_validators.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from fractions import Fraction
23

34
from jsonschema._utils import (
45
ensure_list,
@@ -166,7 +167,18 @@ def multipleOf(validator, dB, instance, schema):
166167

167168
if isinstance(dB, float):
168169
quotient = instance / dB
169-
failed = int(quotient) != quotient
170+
try:
171+
failed = int(quotient) != quotient
172+
except OverflowError:
173+
# When `instance` is large and `dB` is less than one, quotient can
174+
# overflow to infinity; and then casting to int raises an error.
175+
#
176+
# In this case we fall back to Fraction logic, which is exact and
177+
# cannot overflow. The performance is also acceptable: we try the
178+
# fast all-float option first, and we know that fraction(dB) can have
179+
# at most a few hundred digits in each part. The worst-case slowdown
180+
# is therefore for already-slow enormous integers or Decimals.
181+
failed = (Fraction(instance) / Fraction(dB)).denominator != 1
170182
else:
171183
failed = instance % dB
172184

0 commit comments

Comments
 (0)