1
0
mirror of https://github.com/ultrajson/ultrajson.git synced 2024-12-04 06:38:23 +01:00

Fix nan bug in pandas port

This commit is contained in:
joncrall 2022-04-03 16:16:26 -04:00
parent f090103b31
commit 13aa30e152
No known key found for this signature in database
GPG Key ID: BE04D092BDD81C0D
4 changed files with 16 additions and 6 deletions

@ -166,6 +166,7 @@ enum JSTYPES
JT_ARRAY, // Array structure
JT_OBJECT, // Key/Value structure
JT_INVALID, // Internal, do not return nor expect
JT_NAN, // Not A Number
JT_POS_INF, // Positive infinity
JT_NEG_INF, // Negative infinity
};
@ -323,6 +324,7 @@ typedef struct __JSONObjectDecoder
JSOBJ (*newTrue)(void *prv);
JSOBJ (*newFalse)(void *prv);
JSOBJ (*newNull)(void *prv);
JSOBJ (*newNaN)(void *prv);
JSOBJ (*newPosInf)(void *prv);
JSOBJ (*newNegInf)(void *prv);
JSOBJ (*newObject)(void *prv);

@ -201,9 +201,9 @@ DECODE_NAN:
if (*(offset++) != 'a') goto SET_NAN_ERROR;
if (*(offset++) != 'N') goto SET_NAN_ERROR;
ds->lastType = JT_NULL;
ds->lastType = JT_NAN;
ds->start = offset;
return ds->dec->newNull(ds->prv);
return ds->dec->newNaN(ds->prv);
SET_NAN_ERROR:
return SetError(ds, -1, "Unexpected character found when decoding 'NaN'");

@ -79,12 +79,17 @@ static JSOBJ Object_newNull(void *prv)
Py_RETURN_NONE;
}
JSOBJ Object_newPosInf(void *prv)
static JSOBJ Object_newNaN(void *prv)
{
return PyFloat_FromDouble(Py_NAN);
}
static JSOBJ Object_newPosInf(void *prv)
{
return PyFloat_FromDouble(Py_HUGE_VAL);
}
JSOBJ Object_newNegInf(void *prv)
static JSOBJ Object_newNegInf(void *prv)
{
return PyFloat_FromDouble(-Py_HUGE_VAL);
}
@ -139,6 +144,7 @@ PyObject* JSONToObj(PyObject* self, PyObject *args, PyObject *kwargs)
Object_newTrue,
Object_newFalse,
Object_newNull,
Object_newNaN,
Object_newPosInf,
Object_newNegInf,
Object_newObject,

@ -671,11 +671,13 @@ def test_encode_raises_allow_nan(test_input, expected_exception):
def test_nan_inf_support():
import math
text = '["a", NaN, "NaN", Infinity, "Infinity", -Infinity, "-Infinity"]'
data = ujson.loads(text)
expected = ["a", float('nan'), "NaN", float('inf'), "Infinity", -float('inf'), "-Infinity"]
expected = ["a", float('nan'), "NaN", float('inf'), "Infinity",
-float('inf'), "-Infinity"]
for a, b in zip(data, expected):
assert a == b or a is b
assert a == b or math.isnan(a) and math.isnan(b)
@pytest.mark.parametrize(