Skip to content

Simplified objToJSON signatures #30271

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

Closed
wants to merge 3 commits into from
Closed
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
38 changes: 15 additions & 23 deletions pandas/_libs/src/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ PyObject *cls_timedelta;

npy_int64 get_nat(void) { return NPY_MIN_INT64; }

typedef void *(*PFN_PyTypeToJSON)(JSOBJ obj, JSONTypeContext *ti,
void *outValue, size_t *_outLen);
typedef void *(*PFN_PyTypeToJSON)(JSOBJ obj, JSONTypeContext *ti, size_t *_outLen);

typedef struct __NpyArrContext {
PyObject *array;
Expand Down Expand Up @@ -396,20 +395,18 @@ static PyObject *get_item(PyObject *obj, Py_ssize_t i) {
return ret;
}

static void *PyBytesToUTF8(JSOBJ _obj, JSONTypeContext *tc, void *outValue,
size_t *_outLen) {
static void *PyBytesToUTF8(JSOBJ _obj, JSONTypeContext *tc, size_t *_outLen) {
PyObject *obj = (PyObject *)_obj;
*_outLen = PyBytes_GET_SIZE(obj);
return PyBytes_AS_STRING(obj);
}

static void *PyUnicodeToUTF8(JSOBJ _obj, JSONTypeContext *tc, void *outValue,
size_t *_outLen) {
static void *PyUnicodeToUTF8(JSOBJ _obj, JSONTypeContext *tc, size_t *_outLen) {
return PyUnicode_AsUTF8AndSize(_obj, _outLen);
}

static void *PandasDateTimeStructToJSON(npy_datetimestruct *dts,
JSONTypeContext *tc, void *outValue,
JSONTypeContext *tc,
size_t *_outLen) {
NPY_DATETIMEUNIT base = ((PyObjectEncoder *)tc->encoder)->datetimeUnit;

Expand Down Expand Up @@ -437,33 +434,30 @@ static void *PandasDateTimeStructToJSON(npy_datetimestruct *dts,
}
} else {
PRINTMARK();
*((JSINT64 *)outValue) = npy_datetimestruct_to_datetime(base, dts);
return NULL;
return (JSINT64)npy_datetimestruct_to_datetime(base, dts);
}
}

static void *NpyDateTimeScalarToJSON(JSOBJ _obj, JSONTypeContext *tc,
void *outValue, size_t *_outLen) {
static void *NpyDateTimeScalarToJSON(JSOBJ _obj, JSONTypeContext *tc, size_t *_outLen) {
npy_datetimestruct dts;
PyDatetimeScalarObject *obj = (PyDatetimeScalarObject *)_obj;
PRINTMARK();
// TODO(anyone): Does not appear to be reached in tests.

pandas_datetime_to_datetimestruct(obj->obval,
(NPY_DATETIMEUNIT)obj->obmeta.base, &dts);
return PandasDateTimeStructToJSON(&dts, tc, outValue, _outLen);
return PandasDateTimeStructToJSON(&dts, tc, _outLen);
}

static void *PyDateTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, void *outValue,
size_t *_outLen) {
static void *PyDateTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, size_t *_outLen) {
npy_datetimestruct dts;
PyDateTime_Date *obj = (PyDateTime_Date *)_obj;

PRINTMARK();

if (!convert_pydatetime_to_datetimestruct(obj, &dts)) {
PRINTMARK();
return PandasDateTimeStructToJSON(&dts, tc, outValue, _outLen);
return PandasDateTimeStructToJSON(&dts, tc, _outLen);
} else {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError,
Expand All @@ -474,18 +468,16 @@ static void *PyDateTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, void *outValue,
}
}

static void *NpyDatetime64ToJSON(JSOBJ _obj, JSONTypeContext *tc,
void *outValue, size_t *_outLen) {
static void *NpyDatetime64ToJSON(JSOBJ _obj, JSONTypeContext *tc, size_t *_outLen) {
npy_datetimestruct dts;
PRINTMARK();

pandas_datetime_to_datetimestruct((npy_datetime)GET_TC(tc)->longValue,
NPY_FR_ns, &dts);
return PandasDateTimeStructToJSON(&dts, tc, outValue, _outLen);
return PandasDateTimeStructToJSON(&dts, tc, _outLen);
}

static void *PyTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, void *outValue,
size_t *outLen) {
static void *PyTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, size_t *outLen) {
PyObject *obj = (PyObject *)_obj;
PyObject *str;
PyObject *tmp;
Expand All @@ -509,7 +501,7 @@ static void *PyTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, void *outValue,
GET_TC(tc)->newObj = str;

*outLen = PyBytes_GET_SIZE(str);
outValue = (void *)PyBytes_AS_STRING(str);
char *outValue = PyBytes_AS_STRING(str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like it should have implications for the return type of this function. is the returned outValue ever used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the scope of this function the current assignment to outValue goes nowhere. Particularly for strings it has no use, as Object_getString ignores it altogether. Definitely confusing

Goal in the next PR(s) is to split out all of these functions that return void pointers into functions that return either char * or int to streamline things and make readable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Pending green, LGTM.

return outValue;
}

Expand Down Expand Up @@ -1805,7 +1797,7 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
PRINTMARK();
// TODO: last argument here is unused; should decouple string
// from long datetimelike conversion routines
PyDateTimeToJSON(obj, tc, &(GET_TC(tc)->longValue), 0);
GET_TC(tc)->longValue = (JSINT64)PyDateTimeToJSON(obj, tc, 0);
tc->type = JT_LONG;
}
return;
Expand Down Expand Up @@ -2226,7 +2218,7 @@ void Object_endTypeContext(JSOBJ obj, JSONTypeContext *tc) {

const char *Object_getStringValue(JSOBJ obj, JSONTypeContext *tc,
size_t *_outLen) {
return GET_TC(tc)->PyTypeToJSON(obj, tc, NULL, _outLen);
return GET_TC(tc)->PyTypeToJSON(obj, tc, _outLen);
}

JSINT64 Object_getLongValue(JSOBJ obj, JSONTypeContext *tc) {
Expand Down