Skip to content

Raise JMESPathTypeError for non-finite floor()/ceil() args - #391

Open
TrueFurina wants to merge 1 commit into
jmespath:developfrom
TrueFurina:fix/issue-381-floor-ceil-nonfinite
Open

TrueFurina wants to merge 1 commit into
jmespath:developfrom
TrueFurina:fix/issue-381-floor-ceil-nonfinite

Conversation

@TrueFurina

Copy link
Copy Markdown

Summary

floor() and ceil() pass their argument straight to math.floor / math.ceil, which raise uncatchable OverflowError / ValueError on Infinity and NaN. Python's json.loads accepts those literals by default, so JSON data loaded the usual way can contain them and reach these functions:

>>> import json, jmespath
>>> jmespath.search('floor(@)', json.loads('Infinity'))
OverflowError: cannot convert float infinity to integer
>>> jmespath.search('ceil(@)', json.loads('NaN'))
ValueError: cannot convert float NaN to integer

Every other bad function argument in jmespath raises JMESPathTypeError, so code that does try: search(...) except JMESPathError: around a query cannot catch these.

Fix

Guard the two functions with math.isfinite() and raise JMESPathTypeError for non-finite inputs, matching the project's existing convention for invalid function arguments. Finite values are unaffected.

Verification

  • Added tests/test_functions.py::test_floor_ceil_reject_non_finite (covers floor/ceil on Infinity/NaN) and test_floor_ceil_still_work_for_finite.
  • Full suite: 993 passed, 1 skipped (baseline 991 passed).

Closes #381.

math.floor/ceil raise uncatchable OverflowError/ValueError on
Infinity/NaN. json.loads accepts those by default, so data loaded
the usual way reaches these functions. Surface them as
JMESPathTypeError so callers catching JMESPathError handle them
like any other bad function argument (closes jmespath#381).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

floor() and ceil() raise OverflowError / ValueError on Infinity and NaN

1 participant