From d11c1102a0a13bb2f3057c9b4c52e268620a32c0 Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Sun, 26 Nov 2023 11:46:36 -0500 Subject: [PATCH 1/3] Backport PR #56114: CLN: Get rid of PyArray_GetCastFunc --- .../src/vendored/ujson/python/objToJSON.c | 42 ++++++++----------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/pandas/_libs/src/vendored/ujson/python/objToJSON.c b/pandas/_libs/src/vendored/ujson/python/objToJSON.c index 1fa82215179a8..67ac82fe349c3 100644 --- a/pandas/_libs/src/vendored/ujson/python/objToJSON.c +++ b/pandas/_libs/src/vendored/ujson/python/objToJSON.c @@ -1306,14 +1306,7 @@ 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); + i8date = *(npy_int64 *)dataptr; dateUnit = get_datetime_metadata_from_dtype(dtype).base; } else if (PyDate_Check(item) || PyDelta_Check(item)) { is_datetimelike = 1; @@ -1444,24 +1437,23 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { return; } - pc = createTypeContext(); - if (!pc) { - tc->type = JT_INVALID; - return; - } - tc->prv = pc; + pc = createTypeContext(); + if (!pc) { + tc->type = JT_INVALID; + return; + } + tc->prv = pc; - 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); - if (longVal == get_nat()) { - tc->type = JT_NULL; + if (PyTypeNum_ISDATETIME(enc->npyType)) { + int64_t longVal; + + longVal = *(npy_int64 *)enc->npyValue; + if (longVal == get_nat()) { + tc->type = JT_NULL; + } else { + if (enc->datetimeIso) { + if (enc->npyType == NPY_TIMEDELTA) { + pc->PyTypeToUTF8 = NpyTimeDeltaToIsoCallback; } else { if (enc->datetimeIso) { if (enc->npyType == NPY_TIMEDELTA) { From 1a24149e80521a9c5ccf43d83b49c6c98c7fcbad Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Sun, 3 Dec 2023 23:56:06 -0500 Subject: [PATCH 2/3] fix bad indent? --- .../src/vendored/ujson/python/objToJSON.c | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pandas/_libs/src/vendored/ujson/python/objToJSON.c b/pandas/_libs/src/vendored/ujson/python/objToJSON.c index 67ac82fe349c3..f54c117d3896e 100644 --- a/pandas/_libs/src/vendored/ujson/python/objToJSON.c +++ b/pandas/_libs/src/vendored/ujson/python/objToJSON.c @@ -1437,23 +1437,23 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { return; } - pc = createTypeContext(); - if (!pc) { - tc->type = JT_INVALID; - return; - } - tc->prv = pc; + pc = createTypeContext(); + if (!pc) { + tc->type = JT_INVALID; + return; + } + tc->prv = pc; - if (PyTypeNum_ISDATETIME(enc->npyType)) { - int64_t longVal; + if (PyTypeNum_ISDATETIME(enc->npyType)) { + int64_t longVal; - longVal = *(npy_int64 *)enc->npyValue; - if (longVal == get_nat()) { - tc->type = JT_NULL; - } else { - if (enc->datetimeIso) { - if (enc->npyType == NPY_TIMEDELTA) { - pc->PyTypeToUTF8 = NpyTimeDeltaToIsoCallback; + longVal = *(npy_int64 *)enc->npyValue; + if (longVal == get_nat()) { + tc->type = JT_NULL; + } else { + if (enc->datetimeIso) { + if (enc->npyType == NPY_TIMEDELTA) { + pc->PyTypeToUTF8 = NpyTimeDeltaToIsoCallback; } else { if (enc->datetimeIso) { if (enc->npyType == NPY_TIMEDELTA) { From 57f7b640d75cb0025a37e01c3b2a92b1612faebe Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Mon, 4 Dec 2023 00:03:14 -0500 Subject: [PATCH 3/3] remove more unintended changes --- pandas/_libs/src/vendored/ujson/python/objToJSON.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/_libs/src/vendored/ujson/python/objToJSON.c b/pandas/_libs/src/vendored/ujson/python/objToJSON.c index f54c117d3896e..912b0405d0800 100644 --- a/pandas/_libs/src/vendored/ujson/python/objToJSON.c +++ b/pandas/_libs/src/vendored/ujson/python/objToJSON.c @@ -1449,11 +1449,7 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { longVal = *(npy_int64 *)enc->npyValue; if (longVal == get_nat()) { - tc->type = JT_NULL; - } else { - if (enc->datetimeIso) { - if (enc->npyType == NPY_TIMEDELTA) { - pc->PyTypeToUTF8 = NpyTimeDeltaToIsoCallback; + tc->type = JT_NULL; } else { if (enc->datetimeIso) { if (enc->npyType == NPY_TIMEDELTA) {