Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multipleOf overflow #746

Merged
merged 1 commit into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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