Skip to content

CLN: More numpy 2 stuff #57668

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 7 commits into from
Mar 2, 2024
Merged
Changes from 4 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
5 changes: 2 additions & 3 deletions pandas/_libs/src/vendored/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ typedef struct __NpyArrContext {
npy_intp ndim;
npy_intp index[NPY_MAXDIMS];
int type_num;
PyArray_GetItemFunc *getitem;

char **rowLabels;
char **columnLabels;
Expand Down Expand Up @@ -405,7 +404,6 @@ static void NpyArr_iterBegin(JSOBJ _obj, JSONTypeContext *tc) {
}

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;
Expand Down Expand Up @@ -492,7 +490,8 @@ static int NpyArr_iterNextItem(JSOBJ obj, JSONTypeContext *tc) {
((PyObjectEncoder *)tc->encoder)->npyValue = npyarr->dataptr;
((PyObjectEncoder *)tc->encoder)->npyCtxtPassthru = npyarr;
} else {
GET_TC(tc)->itemValue = npyarr->getitem(npyarr->dataptr, npyarr->array);
GET_TC(tc)->itemValue =
PyArray_GETITEM((PyArrayObject *)npyarr->array, npyarr->dataptr);
Copy link
Member

Choose a reason for hiding this comment

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

Would still prefer a type check before the cast - prevents strange bugs in case this ever gets refactored.

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like it already happens a couple lines above.

if (!PyArray_Check(npyarr->array)) {
PyErr_SetString(PyExc_TypeError,
"NpyArr_iterNextItem received a non-array object");
return 0;

Copy link
Member

Choose a reason for hiding this comment

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

In that case we should assign const PyArrayObject *arrayobj = (const PyArrayObject *)npyarr->array right after the type check and reference that here.

Generally want to minimize casting and keep it very localized to where the type check happens (historically we have not done a good job of this) Otherwise things are very liable to break when refactoring

}

npyarr->dataptr += npyarr->stride;
Expand Down