Skip to content

JSON Code Cleanup #30671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 17 additions & 43 deletions pandas/_libs/src/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,65 +241,39 @@ static int scaleNanosecToUnit(npy_int64 *value, NPY_DATETIMEUNIT unit) {
static PyObject *get_values(PyObject *obj) {
PyObject *values = NULL;

values = PyObject_GetAttrString(obj, "values");
PRINTMARK();

if (values && !PyArray_CheckExact(values)) {

if (PyObject_HasAttrString(values, "to_numpy")) {
values = PyObject_CallMethod(values, "to_numpy", NULL);
}

if (PyObject_HasAttrString(values, "values")) {
PyObject *subvals = get_values(values);
PyErr_Clear();
PRINTMARK();
// subvals are sometimes missing a dimension
if (subvals) {
PyArrayObject *reshape = (PyArrayObject *)subvals;
PyObject *shape = PyObject_GetAttrString(obj, "shape");
PyArray_Dims dims;
PRINTMARK();

if (!shape || !PyArray_IntpConverter(shape, &dims)) {
subvals = NULL;
} else {
subvals = PyArray_Newshape(reshape, &dims, NPY_ANYORDER);
PyDimMem_FREE(dims.ptr);
}
Py_DECREF(reshape);
Py_XDECREF(shape);
}
Py_DECREF(values);
values = subvals;
} else {
PRINTMARK();
Py_DECREF(values);
values = NULL;
}
}

if (!values && PyObject_HasAttrString(obj, "_internal_get_values")) {
if (PyObject_HasAttrString(obj, "_internal_get_values")) {
PRINTMARK();
values = PyObject_CallMethod(obj, "_internal_get_values", NULL);
if (values && !PyArray_CheckExact(values)) {

if (values == NULL) {
// Clear so we can subsequently try another method
PyErr_Clear();
} else if (!PyArray_CheckExact(values)) {
// Didn't get a numpy array, so keep trying
PRINTMARK();
Py_DECREF(values);
values = NULL;
}
}

if (!values && PyObject_HasAttrString(obj, "get_block_values")) {
if ((values == NULL) && PyObject_HasAttrString(obj, "get_block_values")) {
PRINTMARK();
values = PyObject_CallMethod(obj, "get_block_values", NULL);
if (values && !PyArray_CheckExact(values)) {

if (values == NULL) {
// Clear so we can subsequently try another method
PyErr_Clear();
} else if (!PyArray_CheckExact(values)) {
// Didn't get a numpy array, so keep trying
PRINTMARK();
Py_DECREF(values);
values = NULL;
}
}

if (!values) {
if (values == NULL) {
PyObject *typeRepr = PyObject_Repr((PyObject *)Py_TYPE(obj));
PyObject *repr;
PRINTMARK();
Expand Down Expand Up @@ -435,8 +409,8 @@ static char *int64ToIso(int64_t value, NPY_DATETIMEUNIT base, size_t *len) {
}

/* JSON callback. returns a char* and mutates the pointer to *len */
static char *NpyDateTimeToIsoCallback(JSOBJ Py_UNUSED(unused), JSONTypeContext *tc,
size_t *len) {
static char *NpyDateTimeToIsoCallback(JSOBJ Py_UNUSED(unused),
JSONTypeContext *tc, size_t *len) {
NPY_DATETIMEUNIT base = ((PyObjectEncoder *)tc->encoder)->datetimeUnit;
return int64ToIso(GET_TC(tc)->longValue, base, len);
}
Expand Down