Skip to content

Commit d161b08

Browse files
committed
BUG, COMPAT: PyList_GET_SIZE works only by chance on ndarray
1 parent 496cc3a commit d161b08

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

pandas/_libs/src/ujson/python/JSONtoObj.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ JSOBJ Object_npyEndObject(void *prv, JSOBJ obj) {
409409
}
410410

411411
int Object_npyObjectAddKey(void *prv, JSOBJ obj, JSOBJ name, JSOBJ value) {
412-
PyObject *label;
412+
PyObject *label, *labels;
413413
npy_intp labelidx;
414414
// add key to label array, value to values array
415415
NpyArrContext *npyarr = (NpyArrContext *)obj;
@@ -424,11 +424,11 @@ int Object_npyObjectAddKey(void *prv, JSOBJ obj, JSOBJ name, JSOBJ value) {
424424
if (!npyarr->labels[labelidx]) {
425425
npyarr->labels[labelidx] = PyList_New(0);
426426
}
427-
427+
labels = npyarr->labels[labelidx];
428428
// only fill label array once, assumes all column labels are the same
429429
// for 2-dimensional arrays.
430-
if (PyList_GET_SIZE(npyarr->labels[labelidx]) <= npyarr->elcount) {
431-
PyList_Append(npyarr->labels[labelidx], label);
430+
if (PyList_Check(labels) && PyList_GET_SIZE(labels) <= npyarr->elcount) {
431+
PyList_Append(labels, label);
432432
}
433433

434434
if (((JSONObjectDecoder *)npyarr->dec)->arrayAddItem(prv, obj, value)) {
@@ -439,16 +439,16 @@ int Object_npyObjectAddKey(void *prv, JSOBJ obj, JSOBJ name, JSOBJ value) {
439439
}
440440

441441
int Object_objectAddKey(void *prv, JSOBJ obj, JSOBJ name, JSOBJ value) {
442-
PyDict_SetItem(obj, name, value);
442+
int ret = PyDict_SetItem(obj, name, value);
443443
Py_DECREF((PyObject *)name);
444444
Py_DECREF((PyObject *)value);
445-
return 1;
445+
return ret==0 ? 1 : 0;
446446
}
447447

448448
int Object_arrayAddItem(void *prv, JSOBJ obj, JSOBJ value) {
449-
PyList_Append(obj, value);
449+
int ret = PyList_Append(obj, value);
450450
Py_DECREF((PyObject *)value);
451-
return 1;
451+
return ret==0 ? 1 : 0;
452452
}
453453

454454
JSOBJ Object_newString(void *prv, wchar_t *start, wchar_t *end) {

0 commit comments

Comments
 (0)