@@ -36,19 +36,16 @@ Numeric decoder derived from from TCL library
36
36
*/
37
37
#define PY_ARRAY_UNIQUE_SYMBOL UJSON_NUMPY
38
38
39
- // "py_defines.h" needs to be included first to
40
- // avoid compilation errors, but it does violate
41
- // styleguide checks with regards to include order.
42
- #include "py_defines.h" // NOLINT(build/include_order)
43
- #include <math.h> // NOLINT(build/include_order)
44
- #include <numpy/arrayobject.h> // NOLINT(build/include_order)
45
- #include <numpy/arrayscalars.h> // NOLINT(build/include_order)
46
- #include <numpy/ndarraytypes.h> // NOLINT(build/include_order)
47
- #include <numpy/npy_math.h> // NOLINT(build/include_order)
48
- #include <stdio.h> // NOLINT(build/include_order)
49
- #include <ultrajson.h> // NOLINT(build/include_order)
50
- #include <../../../tslibs/src/datetime/np_datetime.h> // NOLINT(build/include_order)
51
- #include <../../../tslibs/src/datetime/np_datetime_strings.h> // NOLINT(build/include_order)
39
+ #define PY_SSIZE_T_CLEAN
40
+ #include <Python.h>
41
+ #include <math.h>
42
+ #include <numpy/arrayobject.h>
43
+ #include <numpy/arrayscalars.h>
44
+ #include <numpy/ndarraytypes.h>
45
+ #include <numpy/npy_math.h>
46
+ #include <ultrajson.h>
47
+ #include <../../../tslibs/src/datetime/np_datetime.h>
48
+ #include <../../../tslibs/src/datetime/np_datetime_strings.h>
52
49
#include "datetime.h"
53
50
54
51
static PyObject * type_decimal ;
@@ -281,7 +278,7 @@ static PyObject *get_values(PyObject *obj) {
281
278
repr = PyObject_Repr (dtype );
282
279
Py_DECREF (dtype );
283
280
} else {
284
- repr = PyString_FromString ("<unknown dtype>" );
281
+ repr = PyUnicode_FromString ("<unknown dtype>" );
285
282
}
286
283
287
284
PyErr_Format (PyExc_ValueError , "%R or %R are not JSON serializable yet" ,
@@ -341,7 +338,7 @@ static npy_int64 get_long_attr(PyObject *o, const char *attr) {
341
338
npy_int64 long_val ;
342
339
PyObject * value = PyObject_GetAttrString (o , attr );
343
340
long_val = (PyLong_Check (value ) ?
344
- PyLong_AsLongLong (value ) : PyInt_AS_LONG (value ));
341
+ PyLong_AsLongLong (value ) : PyLong_AsLong (value ));
345
342
Py_DECREF (value );
346
343
return long_val ;
347
344
}
@@ -355,7 +352,7 @@ static npy_float64 total_seconds(PyObject *td) {
355
352
}
356
353
357
354
static PyObject * get_item (PyObject * obj , Py_ssize_t i ) {
358
- PyObject * tmp = PyInt_FromSsize_t (i );
355
+ PyObject * tmp = PyLong_FromSsize_t (i );
359
356
PyObject * ret ;
360
357
361
358
if (tmp == 0 ) {
@@ -385,14 +382,14 @@ static void *CLong(JSOBJ obj, JSONTypeContext *tc, void *outValue,
385
382
static void * PyIntToINT64 (JSOBJ _obj , JSONTypeContext * tc , void * outValue ,
386
383
size_t * _outLen ) {
387
384
PyObject * obj = (PyObject * )_obj ;
388
- * ((JSINT64 * )outValue ) = PyInt_AS_LONG (obj );
385
+ * ((JSINT64 * )outValue ) = PyLong_AsLong (obj );
389
386
return NULL ;
390
387
}
391
388
#else
392
389
static void * PyIntToINT32 (JSOBJ _obj , JSONTypeContext * tc , void * outValue ,
393
390
size_t * _outLen ) {
394
391
PyObject * obj = (PyObject * )_obj ;
395
- * ((JSINT32 * )outValue ) = PyInt_AS_LONG (obj );
392
+ * ((JSINT32 * )outValue ) = PyLong_AsLong (obj );
396
393
return NULL ;
397
394
}
398
395
#endif
@@ -420,8 +417,8 @@ static void *PyFloatToDOUBLE(JSOBJ _obj, JSONTypeContext *tc, void *outValue,
420
417
static void * PyStringToUTF8 (JSOBJ _obj , JSONTypeContext * tc , void * outValue ,
421
418
size_t * _outLen ) {
422
419
PyObject * obj = (PyObject * )_obj ;
423
- * _outLen = PyString_GET_SIZE (obj );
424
- return PyString_AS_STRING (obj );
420
+ * _outLen = PyBytes_GET_SIZE (obj );
421
+ return PyBytes_AS_STRING (obj );
425
422
}
426
423
427
424
static void * PyUnicodeToUTF8 (JSOBJ _obj , JSONTypeContext * tc , void * outValue ,
@@ -442,8 +439,8 @@ static void *PyUnicodeToUTF8(JSOBJ _obj, JSONTypeContext *tc, void *outValue,
442
439
443
440
GET_TC (tc )-> newObj = newObj ;
444
441
445
- * _outLen = PyString_GET_SIZE (newObj );
446
- return PyString_AS_STRING (newObj );
442
+ * _outLen = PyBytes_GET_SIZE (newObj );
443
+ return PyBytes_AS_STRING (newObj );
447
444
}
448
445
449
446
static void * PandasDateTimeStructToJSON (npy_datetimestruct * dts ,
@@ -546,8 +543,8 @@ static void *PyTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, void *outValue,
546
543
547
544
GET_TC (tc )-> newObj = str ;
548
545
549
- * outLen = PyString_GET_SIZE (str );
550
- outValue = (void * )PyString_AS_STRING (str );
546
+ * outLen = PyBytes_GET_SIZE (str );
547
+ outValue = (void * )PyBytes_AS_STRING (str );
551
548
return outValue ;
552
549
}
553
550
@@ -1248,13 +1245,8 @@ int Dir_iterNext(JSOBJ _obj, JSONTypeContext *tc) {
1248
1245
1249
1246
for (; GET_TC (tc )-> index < GET_TC (tc )-> size ; GET_TC (tc )-> index ++ ) {
1250
1247
attrName = PyList_GET_ITEM (GET_TC (tc )-> attrList , GET_TC (tc )-> index );
1251
- #if PY_MAJOR_VERSION >= 3
1252
1248
attr = PyUnicode_AsUTF8String (attrName );
1253
- #else
1254
- attr = attrName ;
1255
- Py_INCREF (attr );
1256
- #endif
1257
- attrStr = PyString_AS_STRING (attr );
1249
+ attrStr = PyBytes_AS_STRING (attr );
1258
1250
1259
1251
if (attrStr [0 ] == '_' ) {
1260
1252
PRINTMARK ();
@@ -1307,8 +1299,8 @@ JSOBJ Dir_iterGetValue(JSOBJ obj, JSONTypeContext *tc) {
1307
1299
1308
1300
char * Dir_iterGetName (JSOBJ obj , JSONTypeContext * tc , size_t * outLen ) {
1309
1301
PRINTMARK ();
1310
- * outLen = PyString_GET_SIZE (GET_TC (tc )-> itemName );
1311
- return PyString_AS_STRING (GET_TC (tc )-> itemName );
1302
+ * outLen = PyBytes_GET_SIZE (GET_TC (tc )-> itemName );
1303
+ return PyBytes_AS_STRING (GET_TC (tc )-> itemName );
1312
1304
}
1313
1305
1314
1306
//=============================================================================
@@ -1525,9 +1517,7 @@ void Dict_iterBegin(JSOBJ obj, JSONTypeContext *tc) {
1525
1517
}
1526
1518
1527
1519
int Dict_iterNext (JSOBJ obj , JSONTypeContext * tc ) {
1528
- #if PY_MAJOR_VERSION >= 3
1529
1520
PyObject * itemNameTmp ;
1530
- #endif
1531
1521
1532
1522
if (GET_TC (tc )-> itemName ) {
1533
1523
Py_DECREF (GET_TC (tc )-> itemName );
@@ -1542,13 +1532,11 @@ int Dict_iterNext(JSOBJ obj, JSONTypeContext *tc) {
1542
1532
1543
1533
if (PyUnicode_Check (GET_TC (tc )-> itemName )) {
1544
1534
GET_TC (tc )-> itemName = PyUnicode_AsUTF8String (GET_TC (tc )-> itemName );
1545
- } else if (!PyString_Check (GET_TC (tc )-> itemName )) {
1535
+ } else if (!PyBytes_Check (GET_TC (tc )-> itemName )) {
1546
1536
GET_TC (tc )-> itemName = PyObject_Str (GET_TC (tc )-> itemName );
1547
- #if PY_MAJOR_VERSION >= 3
1548
1537
itemNameTmp = GET_TC (tc )-> itemName ;
1549
1538
GET_TC (tc )-> itemName = PyUnicode_AsUTF8String (GET_TC (tc )-> itemName );
1550
1539
Py_DECREF (itemNameTmp );
1551
- #endif
1552
1540
} else {
1553
1541
Py_INCREF (GET_TC (tc )-> itemName );
1554
1542
}
@@ -1570,8 +1558,8 @@ JSOBJ Dict_iterGetValue(JSOBJ obj, JSONTypeContext *tc) {
1570
1558
}
1571
1559
1572
1560
char * Dict_iterGetName (JSOBJ obj , JSONTypeContext * tc , size_t * outLen ) {
1573
- * outLen = PyString_GET_SIZE (GET_TC (tc )-> itemName );
1574
- return PyString_AS_STRING (GET_TC (tc )-> itemName );
1561
+ * outLen = PyBytes_GET_SIZE (GET_TC (tc )-> itemName );
1562
+ return PyBytes_AS_STRING (GET_TC (tc )-> itemName );
1575
1563
}
1576
1564
1577
1565
void NpyArr_freeLabels (char * * labels , npy_intp len ) {
@@ -1788,7 +1776,7 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
1788
1776
}
1789
1777
1790
1778
return ;
1791
- } else if (PyInt_Check (obj )) {
1779
+ } else if (PyLong_Check (obj )) {
1792
1780
PRINTMARK ();
1793
1781
1794
1782
#ifdef _LP64
@@ -1809,7 +1797,7 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
1809
1797
tc -> type = JT_DOUBLE ;
1810
1798
}
1811
1799
return ;
1812
- } else if (PyString_Check (obj )) {
1800
+ } else if (PyBytes_Check (obj )) {
1813
1801
PRINTMARK ();
1814
1802
pc -> PyTypeToJSON = PyStringToUTF8 ;
1815
1803
tc -> type = JT_UTF8 ;
@@ -1932,7 +1920,7 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
1932
1920
tmpObj = PyObject_Repr (obj );
1933
1921
PyErr_Format (PyExc_TypeError ,
1934
1922
"%s (0d array) is not JSON serializable at the moment" ,
1935
- PyString_AS_STRING (tmpObj ));
1923
+ PyBytes_AS_STRING (tmpObj ));
1936
1924
Py_DECREF (tmpObj );
1937
1925
goto INVALID ;
1938
1926
}
@@ -2469,7 +2457,7 @@ PyObject *objToJSON(PyObject *self, PyObject *args, PyObject *kwargs) {
2469
2457
return NULL ;
2470
2458
}
2471
2459
2472
- newobj = PyString_FromString (ret );
2460
+ newobj = PyUnicode_FromString (ret );
2473
2461
2474
2462
if (ret != buffer ) {
2475
2463
encoder -> free (ret );
0 commit comments