From b27352be0cf430a6b4a854d5d3b8366979fcc777 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Fri, 26 Apr 2019 14:25:41 -0700 Subject: [PATCH 1/3] Converted static PyObject to PyTypeObject --- pandas/_libs/src/ujson/python/objToJSON.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/src/ujson/python/objToJSON.c b/pandas/_libs/src/ujson/python/objToJSON.c index ef6ff61a29630..1758e555a90a8 100644 --- a/pandas/_libs/src/ujson/python/objToJSON.c +++ b/pandas/_libs/src/ujson/python/objToJSON.c @@ -48,10 +48,9 @@ Numeric decoder derived from from TCL library #include <../../../tslibs/src/datetime/np_datetime_strings.h> #include "datetime.h" -static PyObject *type_decimal; - #define NPY_JSON_BUFSIZE 32768 +static PyTypeObject *type_decimal; static PyTypeObject *cls_dataframe; static PyTypeObject *cls_series; static PyTypeObject *cls_index; @@ -154,8 +153,8 @@ void *initObjToJSON(void) PyObject *mod_pandas; PyObject *mod_nattype; PyObject *mod_decimal = PyImport_ImportModule("decimal"); - type_decimal = PyObject_GetAttrString(mod_decimal, "Decimal"); - Py_INCREF(type_decimal); + type_decimal = + (PyTypeObject *)PyObject_GetAttrString(mod_decimal, "Decimal"); Py_DECREF(mod_decimal); PyDateTime_IMPORT; @@ -1782,7 +1781,7 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { pc->PyTypeToJSON = PyUnicodeToUTF8; tc->type = JT_UTF8; return; - } else if (PyObject_IsInstance(obj, type_decimal)) { + } else if (PyObject_TypeCheck(obj, type_decimal)) { PRINTMARK(); pc->PyTypeToJSON = PyFloatToDOUBLE; tc->type = JT_DOUBLE; From 08e58b0ed3416b7b1402f98160ccb580ce8bb309 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Fri, 26 Apr 2019 14:29:45 -0700 Subject: [PATCH 2/3] Removed unreachable PyLong check (was PyInt in python2) --- pandas/_libs/src/ujson/python/objToJSON.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/pandas/_libs/src/ujson/python/objToJSON.c b/pandas/_libs/src/ujson/python/objToJSON.c index 1758e555a90a8..d54c77bc6d900 100644 --- a/pandas/_libs/src/ujson/python/objToJSON.c +++ b/pandas/_libs/src/ujson/python/objToJSON.c @@ -1749,17 +1749,6 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { goto INVALID; } - return; - } else if (PyLong_Check(obj)) { - PRINTMARK(); - -#ifdef _LP64 - pc->PyTypeToJSON = PyIntToINT64; - tc->type = JT_LONG; -#else - pc->PyTypeToJSON = PyIntToINT32; - tc->type = JT_INT; -#endif return; } else if (PyFloat_Check(obj)) { PRINTMARK(); From 2f478f9faea59be468e618bcf3f44f5fe6cd3fea Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Fri, 26 Apr 2019 14:43:57 -0700 Subject: [PATCH 3/3] Removed unreachable condition for ndarray ndim < 0 --- pandas/_libs/src/ujson/python/objToJSON.c | 67 +++++++++++------------ 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/pandas/_libs/src/ujson/python/objToJSON.c b/pandas/_libs/src/ujson/python/objToJSON.c index d54c77bc6d900..55e508eab816e 100644 --- a/pandas/_libs/src/ujson/python/objToJSON.c +++ b/pandas/_libs/src/ujson/python/objToJSON.c @@ -627,44 +627,39 @@ void NpyArr_iterBegin(JSOBJ _obj, JSONTypeContext *tc) { obj = (PyArrayObject *)_obj; } - if (PyArray_SIZE(obj) < 0) { - PRINTMARK(); - GET_TC(tc)->iterNext = NpyArr_iterNextNone; + PRINTMARK(); + npyarr = PyObject_Malloc(sizeof(NpyArrContext)); + GET_TC(tc)->npyarr = npyarr; + + if (!npyarr) { + PyErr_NoMemory(); + GET_TC(tc)->iterNext = NpyArr_iterNextNone; + return; + } + + npyarr->array = (PyObject *)obj; + npyarr->getitem = (PyArray_GetItemFunc *)PyArray_DESCR(obj)->f->getitem; + npyarr->dataptr = PyArray_DATA(obj); + npyarr->ndim = PyArray_NDIM(obj) - 1; + npyarr->curdim = 0; + npyarr->type_num = PyArray_DESCR(obj)->type_num; + + if (GET_TC(tc)->transpose) { + npyarr->dim = PyArray_DIM(obj, npyarr->ndim); + npyarr->stride = PyArray_STRIDE(obj, npyarr->ndim); + npyarr->stridedim = npyarr->ndim; + npyarr->index[npyarr->ndim] = 0; + npyarr->inc = -1; } else { - PRINTMARK(); - npyarr = PyObject_Malloc(sizeof(NpyArrContext)); - GET_TC(tc)->npyarr = npyarr; - - if (!npyarr) { - PyErr_NoMemory(); - GET_TC(tc)->iterNext = NpyArr_iterNextNone; - return; - } - - npyarr->array = (PyObject *)obj; - npyarr->getitem = (PyArray_GetItemFunc *)PyArray_DESCR(obj)->f->getitem; - npyarr->dataptr = PyArray_DATA(obj); - npyarr->ndim = PyArray_NDIM(obj) - 1; - npyarr->curdim = 0; - npyarr->type_num = PyArray_DESCR(obj)->type_num; - - if (GET_TC(tc)->transpose) { - npyarr->dim = PyArray_DIM(obj, npyarr->ndim); - npyarr->stride = PyArray_STRIDE(obj, npyarr->ndim); - npyarr->stridedim = npyarr->ndim; - npyarr->index[npyarr->ndim] = 0; - npyarr->inc = -1; - } else { - npyarr->dim = PyArray_DIM(obj, 0); - npyarr->stride = PyArray_STRIDE(obj, 0); - npyarr->stridedim = 0; - npyarr->index[0] = 0; - npyarr->inc = 1; - } - - npyarr->columnLabels = GET_TC(tc)->columnLabels; - npyarr->rowLabels = GET_TC(tc)->rowLabels; + npyarr->dim = PyArray_DIM(obj, 0); + npyarr->stride = PyArray_STRIDE(obj, 0); + npyarr->stridedim = 0; + npyarr->index[0] = 0; + npyarr->inc = 1; } + + npyarr->columnLabels = GET_TC(tc)->columnLabels; + npyarr->rowLabels = GET_TC(tc)->rowLabels; } void NpyArr_iterEnd(JSOBJ obj, JSONTypeContext *tc) {