@@ -18,7 +18,7 @@ static PyTypeObject PyEncoderType;
18
18
19
19
typedef struct _PyScannerObject {
20
20
PyObject_HEAD
21
- PyObject * strict ;
21
+ char strict ;
22
22
PyObject * object_hook ;
23
23
PyObject * object_pairs_hook ;
24
24
PyObject * parse_float ;
@@ -28,7 +28,7 @@ typedef struct _PyScannerObject {
28
28
} PyScannerObject ;
29
29
30
30
static PyMemberDef scanner_members [] = {
31
- {"strict" , T_OBJECT , offsetof(PyScannerObject , strict ), READONLY , "strict" },
31
+ {"strict" , T_BOOL , offsetof(PyScannerObject , strict ), READONLY , "strict" },
32
32
{"object_hook" , T_OBJECT , offsetof(PyScannerObject , object_hook ), READONLY , "object_hook" },
33
33
{"object_pairs_hook" , T_OBJECT , offsetof(PyScannerObject , object_pairs_hook ), READONLY },
34
34
{"parse_float" , T_OBJECT , offsetof(PyScannerObject , parse_float ), READONLY , "parse_float" },
@@ -45,10 +45,10 @@ typedef struct _PyEncoderObject {
45
45
PyObject * indent ;
46
46
PyObject * key_separator ;
47
47
PyObject * item_separator ;
48
- PyObject * sort_keys ;
49
- PyObject * skipkeys ;
50
- PyCFunction fast_encode ;
48
+ char sort_keys ;
49
+ char skipkeys ;
51
50
int allow_nan ;
51
+ PyCFunction fast_encode ;
52
52
} PyEncoderObject ;
53
53
54
54
static PyMemberDef encoder_members [] = {
@@ -58,8 +58,8 @@ static PyMemberDef encoder_members[] = {
58
58
{"indent" , T_OBJECT , offsetof(PyEncoderObject , indent ), READONLY , "indent" },
59
59
{"key_separator" , T_OBJECT , offsetof(PyEncoderObject , key_separator ), READONLY , "key_separator" },
60
60
{"item_separator" , T_OBJECT , offsetof(PyEncoderObject , item_separator ), READONLY , "item_separator" },
61
- {"sort_keys" , T_OBJECT , offsetof(PyEncoderObject , sort_keys ), READONLY , "sort_keys" },
62
- {"skipkeys" , T_OBJECT , offsetof(PyEncoderObject , skipkeys ), READONLY , "skipkeys" },
61
+ {"sort_keys" , T_BOOL , offsetof(PyEncoderObject , sort_keys ), READONLY , "sort_keys" },
62
+ {"skipkeys" , T_BOOL , offsetof(PyEncoderObject , skipkeys ), READONLY , "skipkeys" },
63
63
{NULL }
64
64
};
65
65
@@ -666,7 +666,6 @@ scanner_traverse(PyObject *self, visitproc visit, void *arg)
666
666
PyScannerObject * s ;
667
667
assert (PyScanner_Check (self ));
668
668
s = (PyScannerObject * )self ;
669
- Py_VISIT (s -> strict );
670
669
Py_VISIT (s -> object_hook );
671
670
Py_VISIT (s -> object_pairs_hook );
672
671
Py_VISIT (s -> parse_float );
@@ -681,7 +680,6 @@ scanner_clear(PyObject *self)
681
680
PyScannerObject * s ;
682
681
assert (PyScanner_Check (self ));
683
682
s = (PyScannerObject * )self ;
684
- Py_CLEAR (s -> strict );
685
683
Py_CLEAR (s -> object_hook );
686
684
Py_CLEAR (s -> object_pairs_hook );
687
685
Py_CLEAR (s -> parse_float );
@@ -692,7 +690,8 @@ scanner_clear(PyObject *self)
692
690
}
693
691
694
692
static PyObject *
695
- _parse_object_unicode (PyScannerObject * s , PyObject * pystr , Py_ssize_t idx , Py_ssize_t * next_idx_ptr ) {
693
+ _parse_object_unicode (PyScannerObject * s , PyObject * pystr , Py_ssize_t idx , Py_ssize_t * next_idx_ptr )
694
+ {
696
695
/* Read a JSON object from PyUnicode pystr.
697
696
idx is the index of the first character after the opening curly brace.
698
697
*next_idx_ptr is a return-by-reference index to the first character after
@@ -706,13 +705,9 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
706
705
PyObject * val = NULL ;
707
706
PyObject * rval = NULL ;
708
707
PyObject * key = NULL ;
709
- int strict = PyObject_IsTrue (s -> strict );
710
708
int has_pairs_hook = (s -> object_pairs_hook != Py_None );
711
709
Py_ssize_t next_idx ;
712
710
713
- if (strict < 0 )
714
- return NULL ;
715
-
716
711
if (PyUnicode_READY (pystr ) == -1 )
717
712
return NULL ;
718
713
@@ -740,7 +735,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
740
735
raise_errmsg ("Expecting property name enclosed in double quotes" , pystr , idx );
741
736
goto bail ;
742
737
}
743
- key = scanstring_unicode (pystr , idx + 1 , strict , & next_idx );
738
+ key = scanstring_unicode (pystr , idx + 1 , s -> strict , & next_idx );
744
739
if (key == NULL )
745
740
goto bail ;
746
741
memokey = PyDict_GetItem (s -> memo , key );
@@ -1060,7 +1055,6 @@ scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_
1060
1055
void * str ;
1061
1056
int kind ;
1062
1057
Py_ssize_t length ;
1063
- int strict ;
1064
1058
1065
1059
if (PyUnicode_READY (pystr ) == -1 )
1066
1060
return NULL ;
@@ -1081,10 +1075,7 @@ scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_
1081
1075
switch (PyUnicode_READ (kind , str , idx )) {
1082
1076
case '"' :
1083
1077
/* string */
1084
- strict = PyObject_IsTrue (s -> strict );
1085
- if (strict < 0 )
1086
- return NULL ;
1087
- return scanstring_unicode (pystr , idx + 1 , strict , next_idx_ptr );
1078
+ return scanstring_unicode (pystr , idx + 1 , s -> strict , next_idx_ptr );
1088
1079
case '{' :
1089
1080
/* object */
1090
1081
if (Py_EnterRecursiveCall (" while decoding a JSON object "
@@ -1197,6 +1188,7 @@ scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1197
1188
{
1198
1189
PyScannerObject * s ;
1199
1190
PyObject * ctx ;
1191
+ PyObject * strict ;
1200
1192
static char * kwlist [] = {"context" , NULL };
1201
1193
1202
1194
if (!PyArg_ParseTupleAndKeywords (args , kwds , "O:make_scanner" , kwlist , & ctx ))
@@ -1212,8 +1204,12 @@ scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1212
1204
goto bail ;
1213
1205
1214
1206
/* All of these will fail "gracefully" so we don't need to verify them */
1215
- s -> strict = PyObject_GetAttrString (ctx , "strict" );
1216
- if (s -> strict == NULL )
1207
+ strict = PyObject_GetAttrString (ctx , "strict" );
1208
+ if (strict == NULL )
1209
+ goto bail ;
1210
+ s -> strict = PyObject_IsTrue (strict );
1211
+ Py_DECREF (strict );
1212
+ if (s -> strict < 0 )
1217
1213
goto bail ;
1218
1214
s -> object_hook = PyObject_GetAttrString (ctx , "object_hook" );
1219
1215
if (s -> object_hook == NULL )
@@ -1290,10 +1286,10 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1290
1286
1291
1287
PyEncoderObject * s ;
1292
1288
PyObject * markers , * defaultfn , * encoder , * indent , * key_separator ;
1293
- PyObject * item_separator , * sort_keys , * skipkeys ;
1294
- int allow_nan ;
1289
+ PyObject * item_separator ;
1290
+ int sort_keys , skipkeys , allow_nan ;
1295
1291
1296
- if (!PyArg_ParseTupleAndKeywords (args , kwds , "OOOOUUOOp :make_encoder" , kwlist ,
1292
+ if (!PyArg_ParseTupleAndKeywords (args , kwds , "OOOOUUppp :make_encoder" , kwlist ,
1297
1293
& markers , & defaultfn , & encoder , & indent ,
1298
1294
& key_separator , & item_separator ,
1299
1295
& sort_keys , & skipkeys , & allow_nan ))
@@ -1318,6 +1314,7 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1318
1314
s -> item_separator = item_separator ;
1319
1315
s -> sort_keys = sort_keys ;
1320
1316
s -> skipkeys = skipkeys ;
1317
+ s -> allow_nan = allow_nan ;
1321
1318
s -> fast_encode = NULL ;
1322
1319
if (PyCFunction_Check (s -> encoder )) {
1323
1320
PyCFunction f = PyCFunction_GetFunction (s -> encoder );
@@ -1326,16 +1323,13 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1326
1323
s -> fast_encode = f ;
1327
1324
}
1328
1325
}
1329
- s -> allow_nan = allow_nan ;
1330
1326
1331
1327
Py_INCREF (s -> markers );
1332
1328
Py_INCREF (s -> defaultfn );
1333
1329
Py_INCREF (s -> encoder );
1334
1330
Py_INCREF (s -> indent );
1335
1331
Py_INCREF (s -> key_separator );
1336
1332
Py_INCREF (s -> item_separator );
1337
- Py_INCREF (s -> sort_keys );
1338
- Py_INCREF (s -> skipkeys );
1339
1333
return (PyObject * )s ;
1340
1334
}
1341
1335
@@ -1551,8 +1545,6 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
1551
1545
PyObject * it = NULL ;
1552
1546
PyObject * items ;
1553
1547
PyObject * item = NULL ;
1554
- int skipkeys ;
1555
- int sortkeys ;
1556
1548
Py_ssize_t idx ;
1557
1549
1558
1550
if (open_dict == NULL || close_dict == NULL || empty_dict == NULL ) {
@@ -1597,16 +1589,12 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
1597
1589
items = PyMapping_Items (dct );
1598
1590
if (items == NULL )
1599
1591
goto bail ;
1600
- sortkeys = PyObject_IsTrue (s -> sort_keys );
1601
- if (sortkeys < 0 || (sortkeys && PyList_Sort (items ) < 0 ))
1592
+ if (s -> sort_keys && PyList_Sort (items ) < 0 )
1602
1593
goto bail ;
1603
1594
it = PyObject_GetIter (items );
1604
1595
Py_DECREF (items );
1605
1596
if (it == NULL )
1606
1597
goto bail ;
1607
- skipkeys = PyObject_IsTrue (s -> skipkeys );
1608
- if (skipkeys < 0 )
1609
- goto bail ;
1610
1598
idx = 0 ;
1611
1599
while ((item = PyIter_Next (it )) != NULL ) {
1612
1600
PyObject * encoded , * key , * value ;
@@ -1637,7 +1625,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
1637
1625
goto bail ;
1638
1626
}
1639
1627
}
1640
- else if (skipkeys ) {
1628
+ else if (s -> skipkeys ) {
1641
1629
Py_DECREF (item );
1642
1630
continue ;
1643
1631
}
@@ -1805,8 +1793,6 @@ encoder_traverse(PyObject *self, visitproc visit, void *arg)
1805
1793
Py_VISIT (s -> indent );
1806
1794
Py_VISIT (s -> key_separator );
1807
1795
Py_VISIT (s -> item_separator );
1808
- Py_VISIT (s -> sort_keys );
1809
- Py_VISIT (s -> skipkeys );
1810
1796
return 0 ;
1811
1797
}
1812
1798
@@ -1823,8 +1809,6 @@ encoder_clear(PyObject *self)
1823
1809
Py_CLEAR (s -> indent );
1824
1810
Py_CLEAR (s -> key_separator );
1825
1811
Py_CLEAR (s -> item_separator );
1826
- Py_CLEAR (s -> sort_keys );
1827
- Py_CLEAR (s -> skipkeys );
1828
1812
return 0 ;
1829
1813
}
1830
1814
0 commit comments