From 29df4564e83cd54336d8f39bf6b36c5fa20ef620 Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Wed, 22 Nov 2023 11:28:26 -0500 Subject: [PATCH 1/6] CLN: Get rid of PyArray_GetCastFunc --- .../src/vendored/ujson/python/objToJSON.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pandas/_libs/src/vendored/ujson/python/objToJSON.c b/pandas/_libs/src/vendored/ujson/python/objToJSON.c index 22ffec51c82f1..89500ac80d12f 100644 --- a/pandas/_libs/src/vendored/ujson/python/objToJSON.c +++ b/pandas/_libs/src/vendored/ujson/python/objToJSON.c @@ -1280,13 +1280,10 @@ char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc, NPY_DATETIMEUNIT dateUnit = NPY_FR_ns; if (PyTypeNum_ISDATETIME(type_num)) { is_datetimelike = 1; - PyArray_VectorUnaryFunc *castfunc = - PyArray_GetCastFunc(PyArray_DescrFromType(type_num), NPY_INT64); - if (!castfunc) { - PyErr_Format(PyExc_ValueError, "Cannot cast numpy dtype %d to long", - enc->npyType); - } - castfunc(dataptr, &i8date, 1, NULL, NULL); + PyObject *scalarItem = PyArray_ToScalar(dataptr, labels); + PyArray_CastScalarToCtype(scalarItem, &i8date, + PyArray_DescrFromType(NPY_INT64)); + Py_DECREF(scalarItem); dateUnit = get_datetime_metadata_from_dtype(dtype).base; } else if (PyDate_Check(item) || PyDelta_Check(item)) { is_datetimelike = 1; @@ -1432,6 +1429,13 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { enc->npyType); } castfunc(enc->npyValue, &longVal, 1, NULL, NULL); + + PyObject *scalar = + PyArray_ToScalar(enc->npyValue, enc->npyCtxtPassthru->array); + PyArray_Scalar(enc->npyValue, PyArray_DescrFromType(enc->npyType), NULL); + PyArray_CastScalarToCtype(scalar, &longVal, + PyArray_DescrFromType(NPY_INT64)); + Py_DECREF(scalar); if (longVal == get_nat()) { tc->type = JT_NULL; } else { @@ -1532,7 +1536,6 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { PyArray_Descr *outcode = PyArray_DescrFromType(NPY_INT64); PyArray_CastScalarToCtype(obj, &longVal, outcode); - Py_DECREF(outcode); if (enc->datetimeIso) { GET_TC(tc)->longValue = longVal; From 5b38e0527bb85d20bfd0de3576a4781107e34290 Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Wed, 22 Nov 2023 11:43:27 -0500 Subject: [PATCH 2/6] remove old code --- pandas/_libs/src/vendored/ujson/python/objToJSON.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pandas/_libs/src/vendored/ujson/python/objToJSON.c b/pandas/_libs/src/vendored/ujson/python/objToJSON.c index 89500ac80d12f..3b371bbaedf86 100644 --- a/pandas/_libs/src/vendored/ujson/python/objToJSON.c +++ b/pandas/_libs/src/vendored/ujson/python/objToJSON.c @@ -1422,13 +1422,6 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { if (PyTypeNum_ISDATETIME(enc->npyType)) { int64_t longVal; - PyArray_VectorUnaryFunc *castfunc = - PyArray_GetCastFunc(PyArray_DescrFromType(enc->npyType), NPY_INT64); - if (!castfunc) { - PyErr_Format(PyExc_ValueError, "Cannot cast numpy dtype %d to long", - enc->npyType); - } - castfunc(enc->npyValue, &longVal, 1, NULL, NULL); PyObject *scalar = PyArray_ToScalar(enc->npyValue, enc->npyCtxtPassthru->array); From 553d6a81e3cc4871ca9ff2cb2f7744ba7aa4b371 Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Wed, 22 Nov 2023 11:49:14 -0500 Subject: [PATCH 3/6] simplify as per suggestion --- pandas/_libs/src/vendored/ujson/python/objToJSON.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/pandas/_libs/src/vendored/ujson/python/objToJSON.c b/pandas/_libs/src/vendored/ujson/python/objToJSON.c index 3b371bbaedf86..f1fbde22439ef 100644 --- a/pandas/_libs/src/vendored/ujson/python/objToJSON.c +++ b/pandas/_libs/src/vendored/ujson/python/objToJSON.c @@ -1281,9 +1281,7 @@ char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc, if (PyTypeNum_ISDATETIME(type_num)) { is_datetimelike = 1; PyObject *scalarItem = PyArray_ToScalar(dataptr, labels); - PyArray_CastScalarToCtype(scalarItem, &i8date, - PyArray_DescrFromType(NPY_INT64)); - Py_DECREF(scalarItem); + memcpy(&i8date, dataptr, sizeof(i8date)); dateUnit = get_datetime_metadata_from_dtype(dtype).base; } else if (PyDate_Check(item) || PyDelta_Check(item)) { is_datetimelike = 1; @@ -1425,10 +1423,7 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { PyObject *scalar = PyArray_ToScalar(enc->npyValue, enc->npyCtxtPassthru->array); - PyArray_Scalar(enc->npyValue, PyArray_DescrFromType(enc->npyType), NULL); - PyArray_CastScalarToCtype(scalar, &longVal, - PyArray_DescrFromType(NPY_INT64)); - Py_DECREF(scalar); + memcpy(&longVal, enc->npyValue, sizeof(longVal)); if (longVal == get_nat()) { tc->type = JT_NULL; } else { From 2f3b4fa857d6664f55cec61b3b3a7f6476644fe5 Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Wed, 22 Nov 2023 11:51:09 -0500 Subject: [PATCH 4/6] delete dead code --- pandas/_libs/src/vendored/ujson/python/objToJSON.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/_libs/src/vendored/ujson/python/objToJSON.c b/pandas/_libs/src/vendored/ujson/python/objToJSON.c index f1fbde22439ef..41d7617b6e2e0 100644 --- a/pandas/_libs/src/vendored/ujson/python/objToJSON.c +++ b/pandas/_libs/src/vendored/ujson/python/objToJSON.c @@ -1280,7 +1280,6 @@ char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc, NPY_DATETIMEUNIT dateUnit = NPY_FR_ns; if (PyTypeNum_ISDATETIME(type_num)) { is_datetimelike = 1; - PyObject *scalarItem = PyArray_ToScalar(dataptr, labels); memcpy(&i8date, dataptr, sizeof(i8date)); dateUnit = get_datetime_metadata_from_dtype(dtype).base; } else if (PyDate_Check(item) || PyDelta_Check(item)) { @@ -1421,8 +1420,6 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { if (PyTypeNum_ISDATETIME(enc->npyType)) { int64_t longVal; - PyObject *scalar = - PyArray_ToScalar(enc->npyValue, enc->npyCtxtPassthru->array); memcpy(&longVal, enc->npyValue, sizeof(longVal)); if (longVal == get_nat()) { tc->type = JT_NULL; From 740573d614b11130e78cb047a0f3294fadff1fec Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Wed, 22 Nov 2023 12:12:00 -0500 Subject: [PATCH 5/6] simplify again --- pandas/_libs/src/vendored/ujson/python/objToJSON.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/src/vendored/ujson/python/objToJSON.c b/pandas/_libs/src/vendored/ujson/python/objToJSON.c index 41d7617b6e2e0..da6dd333ab978 100644 --- a/pandas/_libs/src/vendored/ujson/python/objToJSON.c +++ b/pandas/_libs/src/vendored/ujson/python/objToJSON.c @@ -1280,7 +1280,7 @@ char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc, NPY_DATETIMEUNIT dateUnit = NPY_FR_ns; if (PyTypeNum_ISDATETIME(type_num)) { is_datetimelike = 1; - memcpy(&i8date, dataptr, sizeof(i8date)); + i8date = *(npy_int64 *)dataptr; dateUnit = get_datetime_metadata_from_dtype(dtype).base; } else if (PyDate_Check(item) || PyDelta_Check(item)) { is_datetimelike = 1; @@ -1420,7 +1420,7 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { if (PyTypeNum_ISDATETIME(enc->npyType)) { int64_t longVal; - memcpy(&longVal, enc->npyValue, sizeof(longVal)); + longVal = *(npy_int64 *)enc->npyValue; if (longVal == get_nat()) { tc->type = JT_NULL; } else { From 27c120d5cffa212fd91a6dd08846f2dd017a8ca8 Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Sun, 26 Nov 2023 08:43:38 -0500 Subject: [PATCH 6/6] Update objToJSON.c --- pandas/_libs/src/vendored/ujson/python/objToJSON.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/_libs/src/vendored/ujson/python/objToJSON.c b/pandas/_libs/src/vendored/ujson/python/objToJSON.c index da6dd333ab978..89f101964aff7 100644 --- a/pandas/_libs/src/vendored/ujson/python/objToJSON.c +++ b/pandas/_libs/src/vendored/ujson/python/objToJSON.c @@ -1521,6 +1521,7 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { PyArray_Descr *outcode = PyArray_DescrFromType(NPY_INT64); PyArray_CastScalarToCtype(obj, &longVal, outcode); + Py_DECREF(outcode); if (enc->datetimeIso) { GET_TC(tc)->longValue = longVal;