@@ -24,7 +24,6 @@ typedef struct _PyScannerObject {
24
24
PyObject * parse_float ;
25
25
PyObject * parse_int ;
26
26
PyObject * parse_constant ;
27
- PyObject * memo ;
28
27
} PyScannerObject ;
29
28
30
29
static PyMemberDef scanner_members [] = {
@@ -70,7 +69,7 @@ ascii_escape_unicode(PyObject *pystr);
70
69
static PyObject *
71
70
py_encode_basestring_ascii (PyObject * Py_UNUSED (self ), PyObject * pystr );
72
71
static PyObject *
73
- scan_once_unicode (PyScannerObject * s , PyObject * pystr , Py_ssize_t idx , Py_ssize_t * next_idx_ptr );
72
+ scan_once_unicode (PyScannerObject * s , PyObject * memo , PyObject * pystr , Py_ssize_t idx , Py_ssize_t * next_idx_ptr );
74
73
static PyObject *
75
74
_build_rval_index_tuple (PyObject * rval , Py_ssize_t idx );
76
75
static PyObject *
@@ -631,7 +630,6 @@ scanner_traverse(PyScannerObject *self, visitproc visit, void *arg)
631
630
Py_VISIT (self -> parse_float );
632
631
Py_VISIT (self -> parse_int );
633
632
Py_VISIT (self -> parse_constant );
634
- Py_VISIT (self -> memo );
635
633
return 0 ;
636
634
}
637
635
@@ -643,12 +641,11 @@ scanner_clear(PyScannerObject *self)
643
641
Py_CLEAR (self -> parse_float );
644
642
Py_CLEAR (self -> parse_int );
645
643
Py_CLEAR (self -> parse_constant );
646
- Py_CLEAR (self -> memo );
647
644
return 0 ;
648
645
}
649
646
650
647
static PyObject *
651
- _parse_object_unicode (PyScannerObject * s , PyObject * pystr , Py_ssize_t idx , Py_ssize_t * next_idx_ptr )
648
+ _parse_object_unicode (PyScannerObject * s , PyObject * memo , PyObject * pystr , Py_ssize_t idx , Py_ssize_t * next_idx_ptr )
652
649
{
653
650
/* Read a JSON object from PyUnicode pystr.
654
651
idx is the index of the first character after the opening curly brace.
@@ -693,7 +690,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
693
690
key = scanstring_unicode (pystr , idx + 1 , s -> strict , & next_idx );
694
691
if (key == NULL )
695
692
goto bail ;
696
- memokey = PyDict_SetDefault (s -> memo , key , key );
693
+ memokey = PyDict_SetDefault (memo , key , key );
697
694
if (memokey == NULL ) {
698
695
goto bail ;
699
696
}
@@ -710,7 +707,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
710
707
while (idx <= end_idx && IS_WHITESPACE (PyUnicode_READ (kind , str , idx ))) idx ++ ;
711
708
712
709
/* read any JSON term */
713
- val = scan_once_unicode (s , pystr , idx , & next_idx );
710
+ val = scan_once_unicode (s , memo , pystr , idx , & next_idx );
714
711
if (val == NULL )
715
712
goto bail ;
716
713
@@ -774,7 +771,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
774
771
}
775
772
776
773
static PyObject *
777
- _parse_array_unicode (PyScannerObject * s , PyObject * pystr , Py_ssize_t idx , Py_ssize_t * next_idx_ptr ) {
774
+ _parse_array_unicode (PyScannerObject * s , PyObject * memo , PyObject * pystr , Py_ssize_t idx , Py_ssize_t * next_idx_ptr ) {
778
775
/* Read a JSON array from PyUnicode pystr.
779
776
idx is the index of the first character after the opening brace.
780
777
*next_idx_ptr is a return-by-reference index to the first character after
@@ -805,7 +802,7 @@ _parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssi
805
802
while (1 ) {
806
803
807
804
/* read any JSON term */
808
- val = scan_once_unicode (s , pystr , idx , & next_idx );
805
+ val = scan_once_unicode (s , memo , pystr , idx , & next_idx );
809
806
if (val == NULL )
810
807
goto bail ;
811
808
@@ -986,7 +983,7 @@ _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_
986
983
}
987
984
988
985
static PyObject *
989
- scan_once_unicode (PyScannerObject * s , PyObject * pystr , Py_ssize_t idx , Py_ssize_t * next_idx_ptr )
986
+ scan_once_unicode (PyScannerObject * s , PyObject * memo , PyObject * pystr , Py_ssize_t idx , Py_ssize_t * next_idx_ptr )
990
987
{
991
988
/* Read one JSON term (of any kind) from PyUnicode pystr.
992
989
idx is the index of the first character of the term
@@ -1022,15 +1019,15 @@ scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_
1022
1019
if (_Py_EnterRecursiveCall (" while decoding a JSON object "
1023
1020
"from a unicode string" ))
1024
1021
return NULL ;
1025
- res = _parse_object_unicode (s , pystr , idx + 1 , next_idx_ptr );
1022
+ res = _parse_object_unicode (s , memo , pystr , idx + 1 , next_idx_ptr );
1026
1023
_Py_LeaveRecursiveCall ();
1027
1024
return res ;
1028
1025
case '[' :
1029
1026
/* array */
1030
1027
if (_Py_EnterRecursiveCall (" while decoding a JSON array "
1031
1028
"from a unicode string" ))
1032
1029
return NULL ;
1033
- res = _parse_array_unicode (s , pystr , idx + 1 , next_idx_ptr );
1030
+ res = _parse_array_unicode (s , memo , pystr , idx + 1 , next_idx_ptr );
1034
1031
_Py_LeaveRecursiveCall ();
1035
1032
return res ;
1036
1033
case 'n' :
@@ -1106,16 +1103,19 @@ scanner_call(PyScannerObject *self, PyObject *args, PyObject *kwds)
1106
1103
if (!PyArg_ParseTupleAndKeywords (args , kwds , "On:scan_once" , kwlist , & pystr , & idx ))
1107
1104
return NULL ;
1108
1105
1109
- if (PyUnicode_Check (pystr )) {
1110
- rval = scan_once_unicode (self , pystr , idx , & next_idx );
1111
- }
1112
- else {
1106
+ if (!PyUnicode_Check (pystr )) {
1113
1107
PyErr_Format (PyExc_TypeError ,
1114
- "first argument must be a string, not %.80s" ,
1115
- Py_TYPE (pystr )-> tp_name );
1108
+ "first argument must be a string, not %.80s" ,
1109
+ Py_TYPE (pystr )-> tp_name );
1116
1110
return NULL ;
1117
1111
}
1118
- PyDict_Clear (self -> memo );
1112
+
1113
+ PyObject * memo = PyDict_New ();
1114
+ if (memo == NULL ) {
1115
+ return NULL ;
1116
+ }
1117
+ rval = scan_once_unicode (self , memo , pystr , idx , & next_idx );
1118
+ Py_DECREF (memo );
1119
1119
if (rval == NULL )
1120
1120
return NULL ;
1121
1121
return _build_rval_index_tuple (rval , next_idx );
@@ -1137,10 +1137,6 @@ scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1137
1137
return NULL ;
1138
1138
}
1139
1139
1140
- s -> memo = PyDict_New ();
1141
- if (s -> memo == NULL )
1142
- goto bail ;
1143
-
1144
1140
/* All of these will fail "gracefully" so we don't need to verify them */
1145
1141
strict = PyObject_GetAttrString (ctx , "strict" );
1146
1142
if (strict == NULL )
0 commit comments