Skip to content

Fix numpy-dev CI warnings #57379

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 1 commit into from
Feb 12, 2024
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
44 changes: 36 additions & 8 deletions pandas/_libs/src/vendored/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,15 @@ static void NpyArrPassThru_iterEnd(JSOBJ obj, JSONTypeContext *tc) {
npyarr->curdim--;
npyarr->dataptr -= npyarr->stride * npyarr->index[npyarr->stridedim];
npyarr->stridedim -= npyarr->inc;
npyarr->dim = PyArray_DIM(npyarr->array, npyarr->stridedim);
npyarr->stride = PyArray_STRIDE(npyarr->array, npyarr->stridedim);

if (!PyArray_Check(npyarr->array)) {
PyErr_SetString(PyExc_TypeError,
"NpyArrayPassThru_iterEnd received a non-array object");
return;
}
const PyArrayObject *arrayobj = (const PyArrayObject *)npyarr->array;
npyarr->dim = PyArray_DIM(arrayobj, npyarr->stridedim);
npyarr->stride = PyArray_STRIDE(arrayobj, npyarr->stridedim);
npyarr->dataptr += npyarr->stride;

NpyArr_freeItemValue(obj, tc);
Expand All @@ -467,12 +474,19 @@ static int NpyArr_iterNextItem(JSOBJ obj, JSONTypeContext *tc) {

NpyArr_freeItemValue(obj, tc);

if (PyArray_ISDATETIME(npyarr->array)) {
if (!PyArray_Check(npyarr->array)) {
PyErr_SetString(PyExc_TypeError,
"NpyArr_iterNextItem received a non-array object");
return 0;
}
PyArrayObject *arrayobj = (PyArrayObject *)npyarr->array;
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the only instance in our JSON code where we could not use a const qualifier for the PyArrayObject type because PyArray_DESCR accepts a non-const type. I think this is something that will eventually be addressed in numpy/numpy#24855 (comment) but @seberg probably knows more

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I noticed that problem as well today. I think we should change it, might annoy some C++ folks, but should be easy enough to work around.


if (PyArray_ISDATETIME(arrayobj)) {
GET_TC(tc)->itemValue = obj;
Py_INCREF(obj);
((PyObjectEncoder *)tc->encoder)->npyType = PyArray_TYPE(npyarr->array);
((PyObjectEncoder *)tc->encoder)->npyType = PyArray_TYPE(arrayobj);
// Also write the resolution (unit) of the ndarray
PyArray_Descr *dtype = PyArray_DESCR(npyarr->array);
PyArray_Descr *dtype = PyArray_DESCR(arrayobj);
((PyObjectEncoder *)tc->encoder)->valueUnit =
get_datetime_metadata_from_dtype(dtype).base;
((PyObjectEncoder *)tc->encoder)->npyValue = npyarr->dataptr;
Expand Down Expand Up @@ -505,8 +519,15 @@ static int NpyArr_iterNext(JSOBJ _obj, JSONTypeContext *tc) {

npyarr->curdim++;
npyarr->stridedim += npyarr->inc;
npyarr->dim = PyArray_DIM(npyarr->array, npyarr->stridedim);
npyarr->stride = PyArray_STRIDE(npyarr->array, npyarr->stridedim);
if (!PyArray_Check(npyarr->array)) {
PyErr_SetString(PyExc_TypeError,
"NpyArr_iterNext received a non-array object");
return 0;
}
const PyArrayObject *arrayobj = (const PyArrayObject *)npyarr->array;

npyarr->dim = PyArray_DIM(arrayobj, npyarr->stridedim);
npyarr->stride = PyArray_STRIDE(arrayobj, npyarr->stridedim);
npyarr->index[npyarr->stridedim] = 0;

((PyObjectEncoder *)tc->encoder)->npyCtxtPassthru = npyarr;
Expand Down Expand Up @@ -1610,7 +1631,14 @@ static void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
if (!values) {
goto INVALID;
}
pc->columnLabelsLen = PyArray_DIM(pc->newObj, 0);

if (!PyArray_Check(pc->newObj)) {
PyErr_SetString(PyExc_TypeError,
"Object_beginTypeContext received a non-array object");
goto INVALID;
}
const PyArrayObject *arrayobj = (const PyArrayObject *)pc->newObj;
pc->columnLabelsLen = PyArray_DIM(arrayobj, 0);
pc->columnLabels = NpyArr_encodeLabels((PyArrayObject *)values, enc,
pc->columnLabelsLen);
if (!pc->columnLabels) {
Expand Down