Skip to content

Commit 8736c23

Browse files
committed
CLN: Dir iteration function in JSON serialiser (follows-up pandas-dev#42931)
1 parent 3011cca commit 8736c23

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

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

+13-30
Original file line numberDiff line numberDiff line change
@@ -881,38 +881,32 @@ void Dir_iterEnd(JSOBJ Py_UNUSED(obj), JSONTypeContext *tc) {
881881
}
882882

883883
int Dir_iterNext(JSOBJ _obj, JSONTypeContext *tc) {
884-
PyObject *obj = (PyObject *)_obj;
885-
PyObject *itemValue = GET_TC(tc)->itemValue;
886-
PyObject *itemName = GET_TC(tc)->itemName;
887-
PyObject *attr;
888-
PyObject *attrName;
889-
char *attrStr;
890-
891884
if (PyErr_Occurred() || ((JSONObjectEncoder *)tc->encoder)->errorMsg) {
892885
return 0;
893886
}
894887

895-
if (itemValue) {
888+
if (GET_TC(tc)->itemValue) {
896889
Py_DECREF(GET_TC(tc)->itemValue);
897-
GET_TC(tc)->itemValue = itemValue = NULL;
890+
GET_TC(tc)->itemValue = NULL;
898891
}
899892

900-
if (itemName) {
893+
if (GET_TC(tc)->itemName) {
901894
Py_DECREF(GET_TC(tc)->itemName);
902-
GET_TC(tc)->itemName = itemName = NULL;
895+
GET_TC(tc)->itemName = NULL;
903896
}
904897

905898
for (; GET_TC(tc)->index < GET_TC(tc)->size; GET_TC(tc)->index++) {
906-
attrName = PyList_GET_ITEM(GET_TC(tc)->attrList, GET_TC(tc)->index);
907-
attr = PyUnicode_AsUTF8String(attrName);
908-
attrStr = PyBytes_AS_STRING(attr);
899+
PyObject *attrName =
900+
PyList_GET_ITEM(GET_TC(tc)->attrList, GET_TC(tc)->index);
901+
PyObject *attr = PyUnicode_AsUTF8String(attrName);
902+
char *attrStr = PyBytes_AS_STRING(attr);
909903

910904
if (attrStr[0] == '_') {
911905
Py_DECREF(attr);
912906
continue;
913907
}
914908

915-
itemValue = PyObject_GetAttr(obj, attrName);
909+
PyObject *itemValue = PyObject_GetAttr((PyObject *)_obj, attrName);
916910
if (itemValue == NULL) {
917911
PyErr_Clear();
918912
Py_DECREF(attr);
@@ -925,24 +919,13 @@ int Dir_iterNext(JSOBJ _obj, JSONTypeContext *tc) {
925919
continue;
926920
}
927921

928-
GET_TC(tc)->itemName = itemName;
922+
GET_TC(tc)->itemName = attr;
929923
GET_TC(tc)->itemValue = itemValue;
930-
931-
itemName = attr;
932-
break;
924+
GET_TC(tc)->index++;
925+
return 1;
933926
}
934927

935-
if (itemName == NULL) {
936-
GET_TC(tc)->index = GET_TC(tc)->size;
937-
GET_TC(tc)->itemValue = NULL;
938-
return 0;
939-
}
940-
941-
GET_TC(tc)->itemName = itemName;
942-
GET_TC(tc)->itemValue = itemValue;
943-
GET_TC(tc)->index++;
944-
945-
return 1;
928+
return 0;
946929
}
947930

948931
JSOBJ Dir_iterGetValue(JSOBJ Py_UNUSED(obj), JSONTypeContext *tc) {

0 commit comments

Comments
 (0)