@@ -51,6 +51,8 @@ Numeric decoder derived from TCL library
51
51
#include <numpy/ndarraytypes.h>
52
52
#include <numpy/npy_math.h>
53
53
54
+ static const int CSTR_SIZE = 20 ;
55
+
54
56
npy_int64 get_nat (void ) { return NPY_MIN_INT64 ; }
55
57
56
58
typedef const char * (* PFN_PyTypeToUTF8 )(JSOBJ obj , JSONTypeContext * ti ,
@@ -106,7 +108,7 @@ typedef struct __TypeContext {
106
108
double doubleValue ;
107
109
JSINT64 longValue ;
108
110
109
- const char * cStr ;
111
+ char * cStr ;
110
112
NpyArrContext * npyarr ;
111
113
PdBlockContext * pdblock ;
112
114
int transpose ;
@@ -347,7 +349,8 @@ static const char *PyDateTimeToIsoCallback(JSOBJ obj, JSONTypeContext *tc,
347
349
}
348
350
349
351
NPY_DATETIMEUNIT base = ((PyObjectEncoder * )tc -> encoder )-> datetimeUnit ;
350
- return PyDateTimeToIso (obj , base , len );
352
+ GET_TC (tc )-> cStr = PyDateTimeToIso (obj , base , len );
353
+ return GET_TC (tc )-> cStr ;
351
354
}
352
355
353
356
static const char * PyTimeToJSON (JSOBJ _obj , JSONTypeContext * tc ,
@@ -1007,16 +1010,24 @@ static const char *List_iterGetName(JSOBJ Py_UNUSED(obj),
1007
1010
//=============================================================================
1008
1011
static void Index_iterBegin (JSOBJ Py_UNUSED (obj ), JSONTypeContext * tc ) {
1009
1012
GET_TC (tc )-> index = 0 ;
1013
+ GET_TC (tc )-> cStr = PyObject_Malloc (CSTR_SIZE );
1014
+ if (!GET_TC (tc )-> cStr ) {
1015
+ PyErr_NoMemory ();
1016
+ }
1010
1017
}
1011
1018
1012
1019
static int Index_iterNext (JSOBJ obj , JSONTypeContext * tc ) {
1013
1020
const Py_ssize_t index = GET_TC (tc )-> index ;
1014
1021
Py_XDECREF (GET_TC (tc )-> itemValue );
1022
+ if (!GET_TC (tc )-> cStr ) {
1023
+ return 0 ;
1024
+ }
1025
+
1015
1026
if (index == 0 ) {
1016
- GET_TC (tc )-> cStr = "name" ;
1027
+ strcpy ( GET_TC (tc )-> cStr , "name" ) ;
1017
1028
GET_TC (tc )-> itemValue = PyObject_GetAttrString (obj , "name" );
1018
1029
} else if (index == 1 ) {
1019
- GET_TC (tc )-> cStr = "data" ;
1030
+ strcpy ( GET_TC (tc )-> cStr , "data" ) ;
1020
1031
GET_TC (tc )-> itemValue = get_values (obj );
1021
1032
if (!GET_TC (tc )-> itemValue ) {
1022
1033
return 0 ;
@@ -1049,19 +1060,27 @@ static void Series_iterBegin(JSOBJ Py_UNUSED(obj), JSONTypeContext *tc) {
1049
1060
PyObjectEncoder * enc = (PyObjectEncoder * )tc -> encoder ;
1050
1061
GET_TC (tc )-> index = 0 ;
1051
1062
enc -> outputFormat = VALUES ; // for contained series
1063
+ GET_TC (tc )-> cStr = PyObject_Malloc (CSTR_SIZE );
1064
+ if (!GET_TC (tc )-> cStr ) {
1065
+ PyErr_NoMemory ();
1066
+ }
1052
1067
}
1053
1068
1054
1069
static int Series_iterNext (JSOBJ obj , JSONTypeContext * tc ) {
1055
1070
const Py_ssize_t index = GET_TC (tc )-> index ;
1056
1071
Py_XDECREF (GET_TC (tc )-> itemValue );
1072
+ if (!GET_TC (tc )-> cStr ) {
1073
+ return 0 ;
1074
+ }
1075
+
1057
1076
if (index == 0 ) {
1058
- GET_TC (tc )-> cStr = "name" ;
1077
+ strcpy ( GET_TC (tc )-> cStr , "name" ) ;
1059
1078
GET_TC (tc )-> itemValue = PyObject_GetAttrString (obj , "name" );
1060
1079
} else if (index == 1 ) {
1061
- GET_TC (tc )-> cStr = "index" ;
1080
+ strcpy ( GET_TC (tc )-> cStr , "index" ) ;
1062
1081
GET_TC (tc )-> itemValue = PyObject_GetAttrString (obj , "index" );
1063
1082
} else if (index == 2 ) {
1064
- GET_TC (tc )-> cStr = "data" ;
1083
+ strcpy ( GET_TC (tc )-> cStr , "data" ) ;
1065
1084
GET_TC (tc )-> itemValue = get_values (obj );
1066
1085
if (!GET_TC (tc )-> itemValue ) {
1067
1086
return 0 ;
@@ -1096,19 +1115,27 @@ static void DataFrame_iterBegin(JSOBJ Py_UNUSED(obj), JSONTypeContext *tc) {
1096
1115
PyObjectEncoder * enc = (PyObjectEncoder * )tc -> encoder ;
1097
1116
GET_TC (tc )-> index = 0 ;
1098
1117
enc -> outputFormat = VALUES ; // for contained series & index
1118
+ GET_TC (tc )-> cStr = PyObject_Malloc (CSTR_SIZE );
1119
+ if (!GET_TC (tc )-> cStr ) {
1120
+ PyErr_NoMemory ();
1121
+ }
1099
1122
}
1100
1123
1101
1124
static int DataFrame_iterNext (JSOBJ obj , JSONTypeContext * tc ) {
1102
1125
const Py_ssize_t index = GET_TC (tc )-> index ;
1103
1126
Py_XDECREF (GET_TC (tc )-> itemValue );
1127
+ if (!GET_TC (tc )-> cStr ) {
1128
+ return 0 ;
1129
+ }
1130
+
1104
1131
if (index == 0 ) {
1105
- GET_TC (tc )-> cStr = "columns" ;
1132
+ strcpy ( GET_TC (tc )-> cStr , "columns" ) ;
1106
1133
GET_TC (tc )-> itemValue = PyObject_GetAttrString (obj , "columns" );
1107
1134
} else if (index == 1 ) {
1108
- GET_TC (tc )-> cStr = "index" ;
1135
+ strcpy ( GET_TC (tc )-> cStr , "index" ) ;
1109
1136
GET_TC (tc )-> itemValue = PyObject_GetAttrString (obj , "index" );
1110
1137
} else if (index == 2 ) {
1111
- GET_TC (tc )-> cStr = "data" ;
1138
+ strcpy ( GET_TC (tc )-> cStr , "data" ) ;
1112
1139
Py_INCREF (obj );
1113
1140
GET_TC (tc )-> itemValue = obj ;
1114
1141
} else {
@@ -1880,6 +1907,7 @@ static void Object_endTypeContext(JSOBJ Py_UNUSED(obj), JSONTypeContext *tc) {
1880
1907
GET_TC (tc )-> rowLabels = NULL ;
1881
1908
NpyArr_freeLabels (GET_TC (tc )-> columnLabels , GET_TC (tc )-> columnLabelsLen );
1882
1909
GET_TC (tc )-> columnLabels = NULL ;
1910
+ PyObject_Free (GET_TC (tc )-> cStr );
1883
1911
GET_TC (tc )-> cStr = NULL ;
1884
1912
PyObject_Free (tc -> prv );
1885
1913
tc -> prv = NULL ;
0 commit comments