summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-01-06 22:30:21 +0000
committerMichael Williamson <mike@zwobble.org>2023-01-06 22:33:26 +0000
commit073ea85a376fc8f0253a6d0160ad3bd7b1c3c83d (patch)
tree169c4c84a157df83b5409247ddc00e9a0420b2a1
parentcccaea76bfc67624c6ccb5d8d8990629b79e0bf8 (diff)
Update handling of non-finite numbers to match jq 1.6
-rw-r--r--CHANGELOG.rst7
-rw-r--r--jq.pyx11
-rw-r--r--tests/jq_tests.py21
3 files changed, 37 insertions, 2 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 2bba698..bfb2984 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,6 +1,13 @@
Changelog
=========
+1.4.0
+-----
+
+* Update handling of non-finite numbers to match the behaviour jq 1.6.
+ Specifically, NaN is outputted as None, Inf is outputted as DBL_MAX,
+ and -Inf is outputted as DBL_MIN.
+
1.3.0
-----
diff --git a/jq.pyx b/jq.pyx
index 00609a8..691b783 100644
--- a/jq.pyx
+++ b/jq.pyx
@@ -2,7 +2,8 @@ import json
import threading
from cpython.bytes cimport PyBytes_AsString
-from libc.math cimport modf
+from libc.float cimport DBL_MAX
+from libc.math cimport INFINITY, modf
cdef extern from "jv.h":
@@ -83,7 +84,13 @@ cdef object _jv_to_python(jv value):
python_value = True
elif kind == JV_KIND_NUMBER:
number_value = jv_number_value(value)
- if _is_integer(number_value):
+ if number_value == INFINITY:
+ python_value = DBL_MAX
+ elif number_value == -INFINITY:
+ python_value = -DBL_MAX
+ elif number_value != number_value:
+ python_value = None
+ elif _is_integer(number_value):
python_value = int(number_value)
else:
python_value = number_value
diff --git a/tests/jq_tests.py b/tests/jq_tests.py
index 4eab9f7..bcacdca 100644
--- a/tests/jq_tests.py
+++ b/tests/jq_tests.py
@@ -20,6 +20,27 @@ def test_can_add_one_to_each_element_of_an_array():
)
+def test_nan_is_outputted_as_null():
+ assert_equal(
+ None,
+ jq.compile("0/0").input(0).first(),
+ )
+
+
+def test_positive_infinity_is_outputted_as_positive_max_double():
+ assert_equal(
+ 1.7976931348623157e+308,
+ jq.compile(". * 2").input(1.7976931348623157e+308).first(),
+ )
+
+
+def test_negative_infinity_is_outputted_as_negative_max_double():
+ assert_equal(
+ -1.7976931348623157e+308,
+ jq.compile(". * 2").input(-1.7976931348623157e+308).first(),
+ )
+
+
def test_can_use_regexes():
assert_equal(
True,