Skip to content

Commit c064308

Browse files
authored
Fix numpy-dev CI warnings (#57379)
1 parent 2393a13 commit c064308

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

pandas/_libs/src/vendored/ujson/python/objToJSON.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,15 @@ static void NpyArrPassThru_iterEnd(JSOBJ obj, JSONTypeContext *tc) {
447447
npyarr->curdim--;
448448
npyarr->dataptr -= npyarr->stride * npyarr->index[npyarr->stridedim];
449449
npyarr->stridedim -= npyarr->inc;
450-
npyarr->dim = PyArray_DIM(npyarr->array, npyarr->stridedim);
451-
npyarr->stride = PyArray_STRIDE(npyarr->array, npyarr->stridedim);
450+
451+
if (!PyArray_Check(npyarr->array)) {
452+
PyErr_SetString(PyExc_TypeError,
453+
"NpyArrayPassThru_iterEnd received a non-array object");
454+
return;
455+
}
456+
const PyArrayObject *arrayobj = (const PyArrayObject *)npyarr->array;
457+
npyarr->dim = PyArray_DIM(arrayobj, npyarr->stridedim);
458+
npyarr->stride = PyArray_STRIDE(arrayobj, npyarr->stridedim);
452459
npyarr->dataptr += npyarr->stride;
453460

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

468475
NpyArr_freeItemValue(obj, tc);
469476

470-
if (PyArray_ISDATETIME(npyarr->array)) {
477+
if (!PyArray_Check(npyarr->array)) {
478+
PyErr_SetString(PyExc_TypeError,
479+
"NpyArr_iterNextItem received a non-array object");
480+
return 0;
481+
}
482+
PyArrayObject *arrayobj = (PyArrayObject *)npyarr->array;
483+
484+
if (PyArray_ISDATETIME(arrayobj)) {
471485
GET_TC(tc)->itemValue = obj;
472486
Py_INCREF(obj);
473-
((PyObjectEncoder *)tc->encoder)->npyType = PyArray_TYPE(npyarr->array);
487+
((PyObjectEncoder *)tc->encoder)->npyType = PyArray_TYPE(arrayobj);
474488
// Also write the resolution (unit) of the ndarray
475-
PyArray_Descr *dtype = PyArray_DESCR(npyarr->array);
489+
PyArray_Descr *dtype = PyArray_DESCR(arrayobj);
476490
((PyObjectEncoder *)tc->encoder)->valueUnit =
477491
get_datetime_metadata_from_dtype(dtype).base;
478492
((PyObjectEncoder *)tc->encoder)->npyValue = npyarr->dataptr;
@@ -505,8 +519,15 @@ static int NpyArr_iterNext(JSOBJ _obj, JSONTypeContext *tc) {
505519

506520
npyarr->curdim++;
507521
npyarr->stridedim += npyarr->inc;
508-
npyarr->dim = PyArray_DIM(npyarr->array, npyarr->stridedim);
509-
npyarr->stride = PyArray_STRIDE(npyarr->array, npyarr->stridedim);
522+
if (!PyArray_Check(npyarr->array)) {
523+
PyErr_SetString(PyExc_TypeError,
524+
"NpyArr_iterNext received a non-array object");
525+
return 0;
526+
}
527+
const PyArrayObject *arrayobj = (const PyArrayObject *)npyarr->array;
528+
529+
npyarr->dim = PyArray_DIM(arrayobj, npyarr->stridedim);
530+
npyarr->stride = PyArray_STRIDE(arrayobj, npyarr->stridedim);
510531
npyarr->index[npyarr->stridedim] = 0;
511532

512533
((PyObjectEncoder *)tc->encoder)->npyCtxtPassthru = npyarr;
@@ -1610,7 +1631,14 @@ static void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
16101631
if (!values) {
16111632
goto INVALID;
16121633
}
1613-
pc->columnLabelsLen = PyArray_DIM(pc->newObj, 0);
1634+
1635+
if (!PyArray_Check(pc->newObj)) {
1636+
PyErr_SetString(PyExc_TypeError,
1637+
"Object_beginTypeContext received a non-array object");
1638+
goto INVALID;
1639+
}
1640+
const PyArrayObject *arrayobj = (const PyArrayObject *)pc->newObj;
1641+
pc->columnLabelsLen = PyArray_DIM(arrayobj, 0);
16141642
pc->columnLabels = NpyArr_encodeLabels((PyArrayObject *)values, enc,
16151643
pc->columnLabelsLen);
16161644
if (!pc->columnLabels) {

0 commit comments

Comments
 (0)