Skip to content

Commit

Permalink
Merge pull request #746 from Zac-HD/multipleOf-overflow
Browse files Browse the repository at this point in the history
Handle `multipleOf` overflow
  • Loading branch information
Julian authored Oct 5, 2020
2 parents 58259e7 + 46db62a commit e48d56c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 40 deletions.
14 changes: 13 additions & 1 deletion jsonschema/_validators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from fractions import Fraction
import re

from jsonschema._utils import (
Expand Down Expand Up @@ -166,7 +167,18 @@ def multipleOf(validator, dB, instance, schema):

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

Expand Down
39 changes: 0 additions & 39 deletions jsonschema/tests/test_jsonschema_test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ def missing_date_fromisoformat(test):
DRAFT4.tests(),
DRAFT4.format_tests(),
DRAFT4.optional_tests_of(name="bignum"),
DRAFT4.optional_tests_of(name="float-overflow"),
DRAFT4.optional_tests_of(name="non-bmp-regex"),
DRAFT4.optional_tests_of(name="zeroTerminatedFloats"),
Validator=Draft4Validator,
Expand Down Expand Up @@ -235,18 +234,6 @@ def missing_date_fromisoformat(test):
subject="uniqueItems",
description='{"a": true} and {"a": 1} are unique',
)(test)
or skip(
message=bug(743),
subject="float-overflow",
)(test)
or skip(
message=bug(743),
subject="multipleOf",
description=(
"always invalid, but naive implementations "
"may raise an overflow error"
),
)(test)
),
)

Expand All @@ -255,7 +242,6 @@ def missing_date_fromisoformat(test):
DRAFT6.tests(),
DRAFT6.format_tests(),
DRAFT6.optional_tests_of(name="bignum"),
DRAFT6.optional_tests_of(name="float-overflow"),
DRAFT6.optional_tests_of(name="non-bmp-regex"),
Validator=Draft6Validator,
format_checker=draft6_format_checker,
Expand Down Expand Up @@ -348,18 +334,6 @@ def missing_date_fromisoformat(test):
subject="const",
case_description='const with {"a": true} does not match {"a": 1}',
)(test)
or skip(
message=bug(743),
subject="float-overflow",
)(test)
or skip(
message=bug(743),
subject="multipleOf",
description=(
"always invalid, but naive implementations "
"may raise an overflow error"
),
)(test)
),
)

Expand All @@ -368,7 +342,6 @@ def missing_date_fromisoformat(test):
DRAFT7.tests(),
DRAFT7.format_tests(),
DRAFT7.optional_tests_of(name="bignum"),
DRAFT7.optional_tests_of(name="float-overflow"),
DRAFT7.optional_tests_of(name="content"),
DRAFT7.optional_tests_of(name="non-bmp-regex"),
Validator=Draft7Validator,
Expand Down Expand Up @@ -484,18 +457,6 @@ def missing_date_fromisoformat(test):
subject="const",
case_description='const with {"a": true} does not match {"a": 1}',
)(test)
or skip(
message=bug(743),
subject="float-overflow",
)(test)
or skip(
message=bug(743),
subject="multipleOf",
description=(
"always invalid, but naive implementations "
"may raise an overflow error"
),
)(test)
),
)

Expand Down

0 comments on commit e48d56c

Please sign in to comment.