Skip to content

Commit f451a23

Browse files
authored
Assorted ujson Cleanups (#35064)
1 parent bf12604 commit f451a23

File tree

1 file changed

+11
-36
lines changed

1 file changed

+11
-36
lines changed

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

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,6 @@ static npy_float64 total_seconds(PyObject *td) {
324324
return double_val;
325325
}
326326

327-
static PyObject *get_item(PyObject *obj, Py_ssize_t i) {
328-
PyObject *tmp = PyLong_FromSsize_t(i);
329-
PyObject *ret;
330-
331-
if (tmp == 0) {
332-
return 0;
333-
}
334-
ret = PyObject_GetItem(obj, tmp);
335-
Py_DECREF(tmp);
336-
337-
return ret;
338-
}
339-
340327
static char *PyBytesToUTF8(JSOBJ _obj, JSONTypeContext *Py_UNUSED(tc),
341328
size_t *_outLen) {
342329
PyObject *obj = (PyObject *)_obj;
@@ -704,18 +691,13 @@ void PdBlock_iterBegin(JSOBJ _obj, JSONTypeContext *tc) {
704691
PdBlockContext *blkCtxt;
705692
NpyArrContext *npyarr;
706693
Py_ssize_t i;
707-
PyArray_Descr *dtype;
708694
NpyIter *iter;
709695
NpyIter_IterNextFunc *iternext;
710696
npy_int64 **dataptr;
711697
npy_int64 colIdx;
712698
npy_intp idx;
713699

714700
PRINTMARK();
715-
716-
i = 0;
717-
blocks = NULL;
718-
dtype = PyArray_DescrFromType(NPY_INT64);
719701
obj = (PyObject *)_obj;
720702

721703
GET_TC(tc)->iterGetName = GET_TC(tc)->transpose
@@ -726,7 +708,7 @@ void PdBlock_iterBegin(JSOBJ _obj, JSONTypeContext *tc) {
726708
if (!blkCtxt) {
727709
PyErr_NoMemory();
728710
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
729-
goto BLKRET;
711+
return;
730712
}
731713
GET_TC(tc)->pdblock = blkCtxt;
732714

@@ -739,38 +721,38 @@ void PdBlock_iterBegin(JSOBJ _obj, JSONTypeContext *tc) {
739721
blkCtxt->cindices = NULL;
740722

741723
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
742-
goto BLKRET;
724+
return;
743725
}
744726

745727
blkCtxt->npyCtxts =
746728
PyObject_Malloc(sizeof(NpyArrContext *) * blkCtxt->ncols);
747729
if (!blkCtxt->npyCtxts) {
748730
PyErr_NoMemory();
749731
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
750-
goto BLKRET;
751-
}
752-
for (i = 0; i < blkCtxt->ncols; i++) {
753-
blkCtxt->npyCtxts[i] = NULL;
732+
return;
754733
}
755734

756735
blkCtxt->cindices = PyObject_Malloc(sizeof(int) * blkCtxt->ncols);
757736
if (!blkCtxt->cindices) {
758737
PyErr_NoMemory();
759738
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
760-
goto BLKRET;
739+
return;
761740
}
762741

763742
blocks = get_sub_attr(obj, "_mgr", "blocks");
764743
if (!blocks) {
765744
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
766-
goto BLKRET;
745+
return;
746+
} else if (!PyTuple_Check(blocks)) {
747+
PyErr_SetString(PyExc_TypeError, "blocks must be a tuple!");
748+
goto BLKRET;
767749
}
768750

769751
// force transpose so each NpyArrContext strides down its column
770752
GET_TC(tc)->transpose = 1;
771753

772754
for (i = 0; i < PyObject_Length(blocks); i++) {
773-
block = get_item(blocks, i);
755+
block = PyTuple_GET_ITEM(blocks, i);
774756
if (!block) {
775757
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
776758
goto BLKRET;
@@ -779,31 +761,27 @@ void PdBlock_iterBegin(JSOBJ _obj, JSONTypeContext *tc) {
779761
tmp = PyObject_CallMethod(block, "get_block_values_for_json", NULL);
780762
if (!tmp) {
781763
((JSONObjectEncoder *)tc->encoder)->errorMsg = "";
782-
Py_DECREF(block);
783764
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
784765
goto BLKRET;
785766
}
786767

787768
values = PyArray_Transpose((PyArrayObject *)tmp, NULL);
788769
Py_DECREF(tmp);
789770
if (!values) {
790-
Py_DECREF(block);
791771
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
792772
goto BLKRET;
793773
}
794774

795775
locs = (PyArrayObject *)get_sub_attr(block, "mgr_locs", "as_array");
796776
if (!locs) {
797-
Py_DECREF(block);
798777
Py_DECREF(values);
799778
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
800779
goto BLKRET;
801780
}
802781

803782
iter = NpyIter_New(locs, NPY_ITER_READONLY, NPY_KEEPORDER,
804-
NPY_NO_CASTING, dtype);
783+
NPY_NO_CASTING, NULL);
805784
if (!iter) {
806-
Py_DECREF(block);
807785
Py_DECREF(values);
808786
Py_DECREF(locs);
809787
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
@@ -812,7 +790,6 @@ void PdBlock_iterBegin(JSOBJ _obj, JSONTypeContext *tc) {
812790
iternext = NpyIter_GetIterNext(iter, NULL);
813791
if (!iternext) {
814792
NpyIter_Deallocate(iter);
815-
Py_DECREF(block);
816793
Py_DECREF(values);
817794
Py_DECREF(locs);
818795
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
@@ -846,15 +823,13 @@ void PdBlock_iterBegin(JSOBJ _obj, JSONTypeContext *tc) {
846823
} while (iternext(iter));
847824

848825
NpyIter_Deallocate(iter);
849-
Py_DECREF(block);
850826
Py_DECREF(values);
851827
Py_DECREF(locs);
852828
}
853829
GET_TC(tc)->npyarr = blkCtxt->npyCtxts[0];
854830

855831
BLKRET:
856-
Py_XDECREF(dtype);
857-
Py_XDECREF(blocks);
832+
Py_DECREF(blocks);
858833
}
859834

860835
void PdBlock_iterEnd(JSOBJ obj, JSONTypeContext *tc) {

0 commit comments

Comments
 (0)