1 | n/a | #include "Python.h" |
---|
2 | n/a | #include "structmember.h" |
---|
3 | n/a | #include "accu.h" |
---|
4 | n/a | |
---|
5 | n/a | #ifdef __GNUC__ |
---|
6 | n/a | #define UNUSED __attribute__((__unused__)) |
---|
7 | n/a | #else |
---|
8 | n/a | #define UNUSED |
---|
9 | n/a | #endif |
---|
10 | n/a | |
---|
11 | n/a | #define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType) |
---|
12 | n/a | #define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType) |
---|
13 | n/a | #define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType) |
---|
14 | n/a | #define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType) |
---|
15 | n/a | |
---|
16 | n/a | static PyTypeObject PyScannerType; |
---|
17 | n/a | static PyTypeObject PyEncoderType; |
---|
18 | n/a | |
---|
19 | n/a | typedef struct _PyScannerObject { |
---|
20 | n/a | PyObject_HEAD |
---|
21 | n/a | PyObject *strict; |
---|
22 | n/a | PyObject *object_hook; |
---|
23 | n/a | PyObject *object_pairs_hook; |
---|
24 | n/a | PyObject *parse_float; |
---|
25 | n/a | PyObject *parse_int; |
---|
26 | n/a | PyObject *parse_constant; |
---|
27 | n/a | PyObject *memo; |
---|
28 | n/a | } PyScannerObject; |
---|
29 | n/a | |
---|
30 | n/a | static PyMemberDef scanner_members[] = { |
---|
31 | n/a | {"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"}, |
---|
32 | n/a | {"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"}, |
---|
33 | n/a | {"object_pairs_hook", T_OBJECT, offsetof(PyScannerObject, object_pairs_hook), READONLY}, |
---|
34 | n/a | {"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"}, |
---|
35 | n/a | {"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "parse_int"}, |
---|
36 | n/a | {"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READONLY, "parse_constant"}, |
---|
37 | n/a | {NULL} |
---|
38 | n/a | }; |
---|
39 | n/a | |
---|
40 | n/a | typedef struct _PyEncoderObject { |
---|
41 | n/a | PyObject_HEAD |
---|
42 | n/a | PyObject *markers; |
---|
43 | n/a | PyObject *defaultfn; |
---|
44 | n/a | PyObject *encoder; |
---|
45 | n/a | PyObject *indent; |
---|
46 | n/a | PyObject *key_separator; |
---|
47 | n/a | PyObject *item_separator; |
---|
48 | n/a | PyObject *sort_keys; |
---|
49 | n/a | PyObject *skipkeys; |
---|
50 | n/a | PyCFunction fast_encode; |
---|
51 | n/a | int allow_nan; |
---|
52 | n/a | } PyEncoderObject; |
---|
53 | n/a | |
---|
54 | n/a | static PyMemberDef encoder_members[] = { |
---|
55 | n/a | {"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers"}, |
---|
56 | n/a | {"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "default"}, |
---|
57 | n/a | {"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder"}, |
---|
58 | n/a | {"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"}, |
---|
59 | n/a | {"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READONLY, "key_separator"}, |
---|
60 | n/a | {"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READONLY, "item_separator"}, |
---|
61 | n/a | {"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sort_keys"}, |
---|
62 | n/a | {"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipkeys"}, |
---|
63 | n/a | {NULL} |
---|
64 | n/a | }; |
---|
65 | n/a | |
---|
66 | n/a | static PyObject * |
---|
67 | n/a | join_list_unicode(PyObject *lst) |
---|
68 | n/a | { |
---|
69 | n/a | /* return u''.join(lst) */ |
---|
70 | n/a | static PyObject *sep = NULL; |
---|
71 | n/a | if (sep == NULL) { |
---|
72 | n/a | sep = PyUnicode_FromStringAndSize("", 0); |
---|
73 | n/a | if (sep == NULL) |
---|
74 | n/a | return NULL; |
---|
75 | n/a | } |
---|
76 | n/a | return PyUnicode_Join(sep, lst); |
---|
77 | n/a | } |
---|
78 | n/a | |
---|
79 | n/a | /* Forward decls */ |
---|
80 | n/a | |
---|
81 | n/a | static PyObject * |
---|
82 | n/a | ascii_escape_unicode(PyObject *pystr); |
---|
83 | n/a | static PyObject * |
---|
84 | n/a | py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr); |
---|
85 | n/a | void init_json(void); |
---|
86 | n/a | static PyObject * |
---|
87 | n/a | scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr); |
---|
88 | n/a | static PyObject * |
---|
89 | n/a | _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx); |
---|
90 | n/a | static PyObject * |
---|
91 | n/a | scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
---|
92 | n/a | static int |
---|
93 | n/a | scanner_init(PyObject *self, PyObject *args, PyObject *kwds); |
---|
94 | n/a | static void |
---|
95 | n/a | scanner_dealloc(PyObject *self); |
---|
96 | n/a | static int |
---|
97 | n/a | scanner_clear(PyObject *self); |
---|
98 | n/a | static PyObject * |
---|
99 | n/a | encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
---|
100 | n/a | static int |
---|
101 | n/a | encoder_init(PyObject *self, PyObject *args, PyObject *kwds); |
---|
102 | n/a | static void |
---|
103 | n/a | encoder_dealloc(PyObject *self); |
---|
104 | n/a | static int |
---|
105 | n/a | encoder_clear(PyObject *self); |
---|
106 | n/a | static int |
---|
107 | n/a | encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc, PyObject *seq, Py_ssize_t indent_level); |
---|
108 | n/a | static int |
---|
109 | n/a | encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, PyObject *obj, Py_ssize_t indent_level); |
---|
110 | n/a | static int |
---|
111 | n/a | encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, PyObject *dct, Py_ssize_t indent_level); |
---|
112 | n/a | static PyObject * |
---|
113 | n/a | _encoded_const(PyObject *obj); |
---|
114 | n/a | static void |
---|
115 | n/a | raise_errmsg(const char *msg, PyObject *s, Py_ssize_t end); |
---|
116 | n/a | static PyObject * |
---|
117 | n/a | encoder_encode_string(PyEncoderObject *s, PyObject *obj); |
---|
118 | n/a | static PyObject * |
---|
119 | n/a | encoder_encode_float(PyEncoderObject *s, PyObject *obj); |
---|
120 | n/a | |
---|
121 | n/a | #define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"') |
---|
122 | n/a | #define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r')) |
---|
123 | n/a | |
---|
124 | n/a | static Py_ssize_t |
---|
125 | n/a | ascii_escape_unichar(Py_UCS4 c, unsigned char *output, Py_ssize_t chars) |
---|
126 | n/a | { |
---|
127 | n/a | /* Escape unicode code point c to ASCII escape sequences |
---|
128 | n/a | in char *output. output must have at least 12 bytes unused to |
---|
129 | n/a | accommodate an escaped surrogate pair "\uXXXX\uXXXX" */ |
---|
130 | n/a | output[chars++] = '\\'; |
---|
131 | n/a | switch (c) { |
---|
132 | n/a | case '\\': output[chars++] = c; break; |
---|
133 | n/a | case '"': output[chars++] = c; break; |
---|
134 | n/a | case '\b': output[chars++] = 'b'; break; |
---|
135 | n/a | case '\f': output[chars++] = 'f'; break; |
---|
136 | n/a | case '\n': output[chars++] = 'n'; break; |
---|
137 | n/a | case '\r': output[chars++] = 'r'; break; |
---|
138 | n/a | case '\t': output[chars++] = 't'; break; |
---|
139 | n/a | default: |
---|
140 | n/a | if (c >= 0x10000) { |
---|
141 | n/a | /* UTF-16 surrogate pair */ |
---|
142 | n/a | Py_UCS4 v = Py_UNICODE_HIGH_SURROGATE(c); |
---|
143 | n/a | output[chars++] = 'u'; |
---|
144 | n/a | output[chars++] = Py_hexdigits[(v >> 12) & 0xf]; |
---|
145 | n/a | output[chars++] = Py_hexdigits[(v >> 8) & 0xf]; |
---|
146 | n/a | output[chars++] = Py_hexdigits[(v >> 4) & 0xf]; |
---|
147 | n/a | output[chars++] = Py_hexdigits[(v ) & 0xf]; |
---|
148 | n/a | c = Py_UNICODE_LOW_SURROGATE(c); |
---|
149 | n/a | output[chars++] = '\\'; |
---|
150 | n/a | } |
---|
151 | n/a | output[chars++] = 'u'; |
---|
152 | n/a | output[chars++] = Py_hexdigits[(c >> 12) & 0xf]; |
---|
153 | n/a | output[chars++] = Py_hexdigits[(c >> 8) & 0xf]; |
---|
154 | n/a | output[chars++] = Py_hexdigits[(c >> 4) & 0xf]; |
---|
155 | n/a | output[chars++] = Py_hexdigits[(c ) & 0xf]; |
---|
156 | n/a | } |
---|
157 | n/a | return chars; |
---|
158 | n/a | } |
---|
159 | n/a | |
---|
160 | n/a | static PyObject * |
---|
161 | n/a | ascii_escape_unicode(PyObject *pystr) |
---|
162 | n/a | { |
---|
163 | n/a | /* Take a PyUnicode pystr and return a new ASCII-only escaped PyUnicode */ |
---|
164 | n/a | Py_ssize_t i; |
---|
165 | n/a | Py_ssize_t input_chars; |
---|
166 | n/a | Py_ssize_t output_size; |
---|
167 | n/a | Py_ssize_t chars; |
---|
168 | n/a | PyObject *rval; |
---|
169 | n/a | void *input; |
---|
170 | n/a | unsigned char *output; |
---|
171 | n/a | int kind; |
---|
172 | n/a | |
---|
173 | n/a | if (PyUnicode_READY(pystr) == -1) |
---|
174 | n/a | return NULL; |
---|
175 | n/a | |
---|
176 | n/a | input_chars = PyUnicode_GET_LENGTH(pystr); |
---|
177 | n/a | input = PyUnicode_DATA(pystr); |
---|
178 | n/a | kind = PyUnicode_KIND(pystr); |
---|
179 | n/a | |
---|
180 | n/a | /* Compute the output size */ |
---|
181 | n/a | for (i = 0, output_size = 2; i < input_chars; i++) { |
---|
182 | n/a | Py_UCS4 c = PyUnicode_READ(kind, input, i); |
---|
183 | n/a | Py_ssize_t d; |
---|
184 | n/a | if (S_CHAR(c)) { |
---|
185 | n/a | d = 1; |
---|
186 | n/a | } |
---|
187 | n/a | else { |
---|
188 | n/a | switch(c) { |
---|
189 | n/a | case '\\': case '"': case '\b': case '\f': |
---|
190 | n/a | case '\n': case '\r': case '\t': |
---|
191 | n/a | d = 2; break; |
---|
192 | n/a | default: |
---|
193 | n/a | d = c >= 0x10000 ? 12 : 6; |
---|
194 | n/a | } |
---|
195 | n/a | } |
---|
196 | n/a | if (output_size > PY_SSIZE_T_MAX - d) { |
---|
197 | n/a | PyErr_SetString(PyExc_OverflowError, "string is too long to escape"); |
---|
198 | n/a | return NULL; |
---|
199 | n/a | } |
---|
200 | n/a | output_size += d; |
---|
201 | n/a | } |
---|
202 | n/a | |
---|
203 | n/a | rval = PyUnicode_New(output_size, 127); |
---|
204 | n/a | if (rval == NULL) { |
---|
205 | n/a | return NULL; |
---|
206 | n/a | } |
---|
207 | n/a | output = PyUnicode_1BYTE_DATA(rval); |
---|
208 | n/a | chars = 0; |
---|
209 | n/a | output[chars++] = '"'; |
---|
210 | n/a | for (i = 0; i < input_chars; i++) { |
---|
211 | n/a | Py_UCS4 c = PyUnicode_READ(kind, input, i); |
---|
212 | n/a | if (S_CHAR(c)) { |
---|
213 | n/a | output[chars++] = c; |
---|
214 | n/a | } |
---|
215 | n/a | else { |
---|
216 | n/a | chars = ascii_escape_unichar(c, output, chars); |
---|
217 | n/a | } |
---|
218 | n/a | } |
---|
219 | n/a | output[chars++] = '"'; |
---|
220 | n/a | #ifdef Py_DEBUG |
---|
221 | n/a | assert(_PyUnicode_CheckConsistency(rval, 1)); |
---|
222 | n/a | #endif |
---|
223 | n/a | return rval; |
---|
224 | n/a | } |
---|
225 | n/a | |
---|
226 | n/a | static PyObject * |
---|
227 | n/a | escape_unicode(PyObject *pystr) |
---|
228 | n/a | { |
---|
229 | n/a | /* Take a PyUnicode pystr and return a new escaped PyUnicode */ |
---|
230 | n/a | Py_ssize_t i; |
---|
231 | n/a | Py_ssize_t input_chars; |
---|
232 | n/a | Py_ssize_t output_size; |
---|
233 | n/a | Py_ssize_t chars; |
---|
234 | n/a | PyObject *rval; |
---|
235 | n/a | void *input; |
---|
236 | n/a | int kind; |
---|
237 | n/a | Py_UCS4 maxchar; |
---|
238 | n/a | |
---|
239 | n/a | if (PyUnicode_READY(pystr) == -1) |
---|
240 | n/a | return NULL; |
---|
241 | n/a | |
---|
242 | n/a | maxchar = PyUnicode_MAX_CHAR_VALUE(pystr); |
---|
243 | n/a | input_chars = PyUnicode_GET_LENGTH(pystr); |
---|
244 | n/a | input = PyUnicode_DATA(pystr); |
---|
245 | n/a | kind = PyUnicode_KIND(pystr); |
---|
246 | n/a | |
---|
247 | n/a | /* Compute the output size */ |
---|
248 | n/a | for (i = 0, output_size = 2; i < input_chars; i++) { |
---|
249 | n/a | Py_UCS4 c = PyUnicode_READ(kind, input, i); |
---|
250 | n/a | Py_ssize_t d; |
---|
251 | n/a | switch (c) { |
---|
252 | n/a | case '\\': case '"': case '\b': case '\f': |
---|
253 | n/a | case '\n': case '\r': case '\t': |
---|
254 | n/a | d = 2; |
---|
255 | n/a | break; |
---|
256 | n/a | default: |
---|
257 | n/a | if (c <= 0x1f) |
---|
258 | n/a | d = 6; |
---|
259 | n/a | else |
---|
260 | n/a | d = 1; |
---|
261 | n/a | } |
---|
262 | n/a | if (output_size > PY_SSIZE_T_MAX - d) { |
---|
263 | n/a | PyErr_SetString(PyExc_OverflowError, "string is too long to escape"); |
---|
264 | n/a | return NULL; |
---|
265 | n/a | } |
---|
266 | n/a | output_size += d; |
---|
267 | n/a | } |
---|
268 | n/a | |
---|
269 | n/a | rval = PyUnicode_New(output_size, maxchar); |
---|
270 | n/a | if (rval == NULL) |
---|
271 | n/a | return NULL; |
---|
272 | n/a | |
---|
273 | n/a | kind = PyUnicode_KIND(rval); |
---|
274 | n/a | |
---|
275 | n/a | #define ENCODE_OUTPUT do { \ |
---|
276 | n/a | chars = 0; \ |
---|
277 | n/a | output[chars++] = '"'; \ |
---|
278 | n/a | for (i = 0; i < input_chars; i++) { \ |
---|
279 | n/a | Py_UCS4 c = PyUnicode_READ(kind, input, i); \ |
---|
280 | n/a | switch (c) { \ |
---|
281 | n/a | case '\\': output[chars++] = '\\'; output[chars++] = c; break; \ |
---|
282 | n/a | case '"': output[chars++] = '\\'; output[chars++] = c; break; \ |
---|
283 | n/a | case '\b': output[chars++] = '\\'; output[chars++] = 'b'; break; \ |
---|
284 | n/a | case '\f': output[chars++] = '\\'; output[chars++] = 'f'; break; \ |
---|
285 | n/a | case '\n': output[chars++] = '\\'; output[chars++] = 'n'; break; \ |
---|
286 | n/a | case '\r': output[chars++] = '\\'; output[chars++] = 'r'; break; \ |
---|
287 | n/a | case '\t': output[chars++] = '\\'; output[chars++] = 't'; break; \ |
---|
288 | n/a | default: \ |
---|
289 | n/a | if (c <= 0x1f) { \ |
---|
290 | n/a | output[chars++] = '\\'; \ |
---|
291 | n/a | output[chars++] = 'u'; \ |
---|
292 | n/a | output[chars++] = '0'; \ |
---|
293 | n/a | output[chars++] = '0'; \ |
---|
294 | n/a | output[chars++] = Py_hexdigits[(c >> 4) & 0xf]; \ |
---|
295 | n/a | output[chars++] = Py_hexdigits[(c ) & 0xf]; \ |
---|
296 | n/a | } else { \ |
---|
297 | n/a | output[chars++] = c; \ |
---|
298 | n/a | } \ |
---|
299 | n/a | } \ |
---|
300 | n/a | } \ |
---|
301 | n/a | output[chars++] = '"'; \ |
---|
302 | n/a | } while (0) |
---|
303 | n/a | |
---|
304 | n/a | if (kind == PyUnicode_1BYTE_KIND) { |
---|
305 | n/a | Py_UCS1 *output = PyUnicode_1BYTE_DATA(rval); |
---|
306 | n/a | ENCODE_OUTPUT; |
---|
307 | n/a | } else if (kind == PyUnicode_2BYTE_KIND) { |
---|
308 | n/a | Py_UCS2 *output = PyUnicode_2BYTE_DATA(rval); |
---|
309 | n/a | ENCODE_OUTPUT; |
---|
310 | n/a | } else { |
---|
311 | n/a | Py_UCS4 *output = PyUnicode_4BYTE_DATA(rval); |
---|
312 | n/a | assert(kind == PyUnicode_4BYTE_KIND); |
---|
313 | n/a | ENCODE_OUTPUT; |
---|
314 | n/a | } |
---|
315 | n/a | #undef ENCODE_OUTPUT |
---|
316 | n/a | |
---|
317 | n/a | #ifdef Py_DEBUG |
---|
318 | n/a | assert(_PyUnicode_CheckConsistency(rval, 1)); |
---|
319 | n/a | #endif |
---|
320 | n/a | return rval; |
---|
321 | n/a | } |
---|
322 | n/a | |
---|
323 | n/a | static void |
---|
324 | n/a | raise_errmsg(const char *msg, PyObject *s, Py_ssize_t end) |
---|
325 | n/a | { |
---|
326 | n/a | /* Use JSONDecodeError exception to raise a nice looking ValueError subclass */ |
---|
327 | n/a | static PyObject *JSONDecodeError = NULL; |
---|
328 | n/a | PyObject *exc; |
---|
329 | n/a | if (JSONDecodeError == NULL) { |
---|
330 | n/a | PyObject *decoder = PyImport_ImportModule("json.decoder"); |
---|
331 | n/a | if (decoder == NULL) |
---|
332 | n/a | return; |
---|
333 | n/a | JSONDecodeError = PyObject_GetAttrString(decoder, "JSONDecodeError"); |
---|
334 | n/a | Py_DECREF(decoder); |
---|
335 | n/a | if (JSONDecodeError == NULL) |
---|
336 | n/a | return; |
---|
337 | n/a | } |
---|
338 | n/a | exc = PyObject_CallFunction(JSONDecodeError, "zOn", msg, s, end); |
---|
339 | n/a | if (exc) { |
---|
340 | n/a | PyErr_SetObject(JSONDecodeError, exc); |
---|
341 | n/a | Py_DECREF(exc); |
---|
342 | n/a | } |
---|
343 | n/a | } |
---|
344 | n/a | |
---|
345 | n/a | static void |
---|
346 | n/a | raise_stop_iteration(Py_ssize_t idx) |
---|
347 | n/a | { |
---|
348 | n/a | PyObject *value = PyLong_FromSsize_t(idx); |
---|
349 | n/a | if (value != NULL) { |
---|
350 | n/a | PyErr_SetObject(PyExc_StopIteration, value); |
---|
351 | n/a | Py_DECREF(value); |
---|
352 | n/a | } |
---|
353 | n/a | } |
---|
354 | n/a | |
---|
355 | n/a | static PyObject * |
---|
356 | n/a | _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) { |
---|
357 | n/a | /* return (rval, idx) tuple, stealing reference to rval */ |
---|
358 | n/a | PyObject *tpl; |
---|
359 | n/a | PyObject *pyidx; |
---|
360 | n/a | /* |
---|
361 | n/a | steal a reference to rval, returns (rval, idx) |
---|
362 | n/a | */ |
---|
363 | n/a | if (rval == NULL) { |
---|
364 | n/a | return NULL; |
---|
365 | n/a | } |
---|
366 | n/a | pyidx = PyLong_FromSsize_t(idx); |
---|
367 | n/a | if (pyidx == NULL) { |
---|
368 | n/a | Py_DECREF(rval); |
---|
369 | n/a | return NULL; |
---|
370 | n/a | } |
---|
371 | n/a | tpl = PyTuple_New(2); |
---|
372 | n/a | if (tpl == NULL) { |
---|
373 | n/a | Py_DECREF(pyidx); |
---|
374 | n/a | Py_DECREF(rval); |
---|
375 | n/a | return NULL; |
---|
376 | n/a | } |
---|
377 | n/a | PyTuple_SET_ITEM(tpl, 0, rval); |
---|
378 | n/a | PyTuple_SET_ITEM(tpl, 1, pyidx); |
---|
379 | n/a | return tpl; |
---|
380 | n/a | } |
---|
381 | n/a | |
---|
382 | n/a | #define APPEND_OLD_CHUNK \ |
---|
383 | n/a | if (chunk != NULL) { \ |
---|
384 | n/a | if (chunks == NULL) { \ |
---|
385 | n/a | chunks = PyList_New(0); \ |
---|
386 | n/a | if (chunks == NULL) { \ |
---|
387 | n/a | goto bail; \ |
---|
388 | n/a | } \ |
---|
389 | n/a | } \ |
---|
390 | n/a | if (PyList_Append(chunks, chunk)) { \ |
---|
391 | n/a | Py_CLEAR(chunk); \ |
---|
392 | n/a | goto bail; \ |
---|
393 | n/a | } \ |
---|
394 | n/a | Py_CLEAR(chunk); \ |
---|
395 | n/a | } |
---|
396 | n/a | |
---|
397 | n/a | static PyObject * |
---|
398 | n/a | scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr) |
---|
399 | n/a | { |
---|
400 | n/a | /* Read the JSON string from PyUnicode pystr. |
---|
401 | n/a | end is the index of the first character after the quote. |
---|
402 | n/a | if strict is zero then literal control characters are allowed |
---|
403 | n/a | *next_end_ptr is a return-by-reference index of the character |
---|
404 | n/a | after the end quote |
---|
405 | n/a | |
---|
406 | n/a | Return value is a new PyUnicode |
---|
407 | n/a | */ |
---|
408 | n/a | PyObject *rval = NULL; |
---|
409 | n/a | Py_ssize_t len; |
---|
410 | n/a | Py_ssize_t begin = end - 1; |
---|
411 | n/a | Py_ssize_t next /* = begin */; |
---|
412 | n/a | const void *buf; |
---|
413 | n/a | int kind; |
---|
414 | n/a | PyObject *chunks = NULL; |
---|
415 | n/a | PyObject *chunk = NULL; |
---|
416 | n/a | |
---|
417 | n/a | if (PyUnicode_READY(pystr) == -1) |
---|
418 | n/a | return 0; |
---|
419 | n/a | |
---|
420 | n/a | len = PyUnicode_GET_LENGTH(pystr); |
---|
421 | n/a | buf = PyUnicode_DATA(pystr); |
---|
422 | n/a | kind = PyUnicode_KIND(pystr); |
---|
423 | n/a | |
---|
424 | n/a | if (end < 0 || len < end) { |
---|
425 | n/a | PyErr_SetString(PyExc_ValueError, "end is out of bounds"); |
---|
426 | n/a | goto bail; |
---|
427 | n/a | } |
---|
428 | n/a | while (1) { |
---|
429 | n/a | /* Find the end of the string or the next escape */ |
---|
430 | n/a | Py_UCS4 c = 0; |
---|
431 | n/a | for (next = end; next < len; next++) { |
---|
432 | n/a | c = PyUnicode_READ(kind, buf, next); |
---|
433 | n/a | if (c == '"' || c == '\\') { |
---|
434 | n/a | break; |
---|
435 | n/a | } |
---|
436 | n/a | else if (strict && c <= 0x1f) { |
---|
437 | n/a | raise_errmsg("Invalid control character at", pystr, next); |
---|
438 | n/a | goto bail; |
---|
439 | n/a | } |
---|
440 | n/a | } |
---|
441 | n/a | if (!(c == '"' || c == '\\')) { |
---|
442 | n/a | raise_errmsg("Unterminated string starting at", pystr, begin); |
---|
443 | n/a | goto bail; |
---|
444 | n/a | } |
---|
445 | n/a | /* Pick up this chunk if it's not zero length */ |
---|
446 | n/a | if (next != end) { |
---|
447 | n/a | APPEND_OLD_CHUNK |
---|
448 | n/a | chunk = PyUnicode_FromKindAndData( |
---|
449 | n/a | kind, |
---|
450 | n/a | (char*)buf + kind * end, |
---|
451 | n/a | next - end); |
---|
452 | n/a | if (chunk == NULL) { |
---|
453 | n/a | goto bail; |
---|
454 | n/a | } |
---|
455 | n/a | } |
---|
456 | n/a | next++; |
---|
457 | n/a | if (c == '"') { |
---|
458 | n/a | end = next; |
---|
459 | n/a | break; |
---|
460 | n/a | } |
---|
461 | n/a | if (next == len) { |
---|
462 | n/a | raise_errmsg("Unterminated string starting at", pystr, begin); |
---|
463 | n/a | goto bail; |
---|
464 | n/a | } |
---|
465 | n/a | c = PyUnicode_READ(kind, buf, next); |
---|
466 | n/a | if (c != 'u') { |
---|
467 | n/a | /* Non-unicode backslash escapes */ |
---|
468 | n/a | end = next + 1; |
---|
469 | n/a | switch (c) { |
---|
470 | n/a | case '"': break; |
---|
471 | n/a | case '\\': break; |
---|
472 | n/a | case '/': break; |
---|
473 | n/a | case 'b': c = '\b'; break; |
---|
474 | n/a | case 'f': c = '\f'; break; |
---|
475 | n/a | case 'n': c = '\n'; break; |
---|
476 | n/a | case 'r': c = '\r'; break; |
---|
477 | n/a | case 't': c = '\t'; break; |
---|
478 | n/a | default: c = 0; |
---|
479 | n/a | } |
---|
480 | n/a | if (c == 0) { |
---|
481 | n/a | raise_errmsg("Invalid \\escape", pystr, end - 2); |
---|
482 | n/a | goto bail; |
---|
483 | n/a | } |
---|
484 | n/a | } |
---|
485 | n/a | else { |
---|
486 | n/a | c = 0; |
---|
487 | n/a | next++; |
---|
488 | n/a | end = next + 4; |
---|
489 | n/a | if (end >= len) { |
---|
490 | n/a | raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1); |
---|
491 | n/a | goto bail; |
---|
492 | n/a | } |
---|
493 | n/a | /* Decode 4 hex digits */ |
---|
494 | n/a | for (; next < end; next++) { |
---|
495 | n/a | Py_UCS4 digit = PyUnicode_READ(kind, buf, next); |
---|
496 | n/a | c <<= 4; |
---|
497 | n/a | switch (digit) { |
---|
498 | n/a | case '0': case '1': case '2': case '3': case '4': |
---|
499 | n/a | case '5': case '6': case '7': case '8': case '9': |
---|
500 | n/a | c |= (digit - '0'); break; |
---|
501 | n/a | case 'a': case 'b': case 'c': case 'd': case 'e': |
---|
502 | n/a | case 'f': |
---|
503 | n/a | c |= (digit - 'a' + 10); break; |
---|
504 | n/a | case 'A': case 'B': case 'C': case 'D': case 'E': |
---|
505 | n/a | case 'F': |
---|
506 | n/a | c |= (digit - 'A' + 10); break; |
---|
507 | n/a | default: |
---|
508 | n/a | raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); |
---|
509 | n/a | goto bail; |
---|
510 | n/a | } |
---|
511 | n/a | } |
---|
512 | n/a | /* Surrogate pair */ |
---|
513 | n/a | if (Py_UNICODE_IS_HIGH_SURROGATE(c) && end + 6 < len && |
---|
514 | n/a | PyUnicode_READ(kind, buf, next++) == '\\' && |
---|
515 | n/a | PyUnicode_READ(kind, buf, next++) == 'u') { |
---|
516 | n/a | Py_UCS4 c2 = 0; |
---|
517 | n/a | end += 6; |
---|
518 | n/a | /* Decode 4 hex digits */ |
---|
519 | n/a | for (; next < end; next++) { |
---|
520 | n/a | Py_UCS4 digit = PyUnicode_READ(kind, buf, next); |
---|
521 | n/a | c2 <<= 4; |
---|
522 | n/a | switch (digit) { |
---|
523 | n/a | case '0': case '1': case '2': case '3': case '4': |
---|
524 | n/a | case '5': case '6': case '7': case '8': case '9': |
---|
525 | n/a | c2 |= (digit - '0'); break; |
---|
526 | n/a | case 'a': case 'b': case 'c': case 'd': case 'e': |
---|
527 | n/a | case 'f': |
---|
528 | n/a | c2 |= (digit - 'a' + 10); break; |
---|
529 | n/a | case 'A': case 'B': case 'C': case 'D': case 'E': |
---|
530 | n/a | case 'F': |
---|
531 | n/a | c2 |= (digit - 'A' + 10); break; |
---|
532 | n/a | default: |
---|
533 | n/a | raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); |
---|
534 | n/a | goto bail; |
---|
535 | n/a | } |
---|
536 | n/a | } |
---|
537 | n/a | if (Py_UNICODE_IS_LOW_SURROGATE(c2)) |
---|
538 | n/a | c = Py_UNICODE_JOIN_SURROGATES(c, c2); |
---|
539 | n/a | else |
---|
540 | n/a | end -= 6; |
---|
541 | n/a | } |
---|
542 | n/a | } |
---|
543 | n/a | APPEND_OLD_CHUNK |
---|
544 | n/a | chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1); |
---|
545 | n/a | if (chunk == NULL) { |
---|
546 | n/a | goto bail; |
---|
547 | n/a | } |
---|
548 | n/a | } |
---|
549 | n/a | |
---|
550 | n/a | if (chunks == NULL) { |
---|
551 | n/a | if (chunk != NULL) |
---|
552 | n/a | rval = chunk; |
---|
553 | n/a | else |
---|
554 | n/a | rval = PyUnicode_FromStringAndSize("", 0); |
---|
555 | n/a | } |
---|
556 | n/a | else { |
---|
557 | n/a | APPEND_OLD_CHUNK |
---|
558 | n/a | rval = join_list_unicode(chunks); |
---|
559 | n/a | if (rval == NULL) { |
---|
560 | n/a | goto bail; |
---|
561 | n/a | } |
---|
562 | n/a | Py_CLEAR(chunks); |
---|
563 | n/a | } |
---|
564 | n/a | |
---|
565 | n/a | *next_end_ptr = end; |
---|
566 | n/a | return rval; |
---|
567 | n/a | bail: |
---|
568 | n/a | *next_end_ptr = -1; |
---|
569 | n/a | Py_XDECREF(chunks); |
---|
570 | n/a | Py_XDECREF(chunk); |
---|
571 | n/a | return NULL; |
---|
572 | n/a | } |
---|
573 | n/a | |
---|
574 | n/a | PyDoc_STRVAR(pydoc_scanstring, |
---|
575 | n/a | "scanstring(string, end, strict=True) -> (string, end)\n" |
---|
576 | n/a | "\n" |
---|
577 | n/a | "Scan the string s for a JSON string. End is the index of the\n" |
---|
578 | n/a | "character in s after the quote that started the JSON string.\n" |
---|
579 | n/a | "Unescapes all valid JSON string escape sequences and raises ValueError\n" |
---|
580 | n/a | "on attempt to decode an invalid string. If strict is False then literal\n" |
---|
581 | n/a | "control characters are allowed in the string.\n" |
---|
582 | n/a | "\n" |
---|
583 | n/a | "Returns a tuple of the decoded string and the index of the character in s\n" |
---|
584 | n/a | "after the end quote." |
---|
585 | n/a | ); |
---|
586 | n/a | |
---|
587 | n/a | static PyObject * |
---|
588 | n/a | py_scanstring(PyObject* self UNUSED, PyObject *args) |
---|
589 | n/a | { |
---|
590 | n/a | PyObject *pystr; |
---|
591 | n/a | PyObject *rval; |
---|
592 | n/a | Py_ssize_t end; |
---|
593 | n/a | Py_ssize_t next_end = -1; |
---|
594 | n/a | int strict = 1; |
---|
595 | n/a | if (!PyArg_ParseTuple(args, "On|i:scanstring", &pystr, &end, &strict)) { |
---|
596 | n/a | return NULL; |
---|
597 | n/a | } |
---|
598 | n/a | if (PyUnicode_Check(pystr)) { |
---|
599 | n/a | rval = scanstring_unicode(pystr, end, strict, &next_end); |
---|
600 | n/a | } |
---|
601 | n/a | else { |
---|
602 | n/a | PyErr_Format(PyExc_TypeError, |
---|
603 | n/a | "first argument must be a string, not %.80s", |
---|
604 | n/a | Py_TYPE(pystr)->tp_name); |
---|
605 | n/a | return NULL; |
---|
606 | n/a | } |
---|
607 | n/a | return _build_rval_index_tuple(rval, next_end); |
---|
608 | n/a | } |
---|
609 | n/a | |
---|
610 | n/a | PyDoc_STRVAR(pydoc_encode_basestring_ascii, |
---|
611 | n/a | "encode_basestring_ascii(string) -> string\n" |
---|
612 | n/a | "\n" |
---|
613 | n/a | "Return an ASCII-only JSON representation of a Python string" |
---|
614 | n/a | ); |
---|
615 | n/a | |
---|
616 | n/a | static PyObject * |
---|
617 | n/a | py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr) |
---|
618 | n/a | { |
---|
619 | n/a | PyObject *rval; |
---|
620 | n/a | /* Return an ASCII-only JSON representation of a Python string */ |
---|
621 | n/a | /* METH_O */ |
---|
622 | n/a | if (PyUnicode_Check(pystr)) { |
---|
623 | n/a | rval = ascii_escape_unicode(pystr); |
---|
624 | n/a | } |
---|
625 | n/a | else { |
---|
626 | n/a | PyErr_Format(PyExc_TypeError, |
---|
627 | n/a | "first argument must be a string, not %.80s", |
---|
628 | n/a | Py_TYPE(pystr)->tp_name); |
---|
629 | n/a | return NULL; |
---|
630 | n/a | } |
---|
631 | n/a | return rval; |
---|
632 | n/a | } |
---|
633 | n/a | |
---|
634 | n/a | |
---|
635 | n/a | PyDoc_STRVAR(pydoc_encode_basestring, |
---|
636 | n/a | "encode_basestring(string) -> string\n" |
---|
637 | n/a | "\n" |
---|
638 | n/a | "Return a JSON representation of a Python string" |
---|
639 | n/a | ); |
---|
640 | n/a | |
---|
641 | n/a | static PyObject * |
---|
642 | n/a | py_encode_basestring(PyObject* self UNUSED, PyObject *pystr) |
---|
643 | n/a | { |
---|
644 | n/a | PyObject *rval; |
---|
645 | n/a | /* Return a JSON representation of a Python string */ |
---|
646 | n/a | /* METH_O */ |
---|
647 | n/a | if (PyUnicode_Check(pystr)) { |
---|
648 | n/a | rval = escape_unicode(pystr); |
---|
649 | n/a | } |
---|
650 | n/a | else { |
---|
651 | n/a | PyErr_Format(PyExc_TypeError, |
---|
652 | n/a | "first argument must be a string, not %.80s", |
---|
653 | n/a | Py_TYPE(pystr)->tp_name); |
---|
654 | n/a | return NULL; |
---|
655 | n/a | } |
---|
656 | n/a | return rval; |
---|
657 | n/a | } |
---|
658 | n/a | |
---|
659 | n/a | static void |
---|
660 | n/a | scanner_dealloc(PyObject *self) |
---|
661 | n/a | { |
---|
662 | n/a | /* Deallocate scanner object */ |
---|
663 | n/a | scanner_clear(self); |
---|
664 | n/a | Py_TYPE(self)->tp_free(self); |
---|
665 | n/a | } |
---|
666 | n/a | |
---|
667 | n/a | static int |
---|
668 | n/a | scanner_traverse(PyObject *self, visitproc visit, void *arg) |
---|
669 | n/a | { |
---|
670 | n/a | PyScannerObject *s; |
---|
671 | n/a | assert(PyScanner_Check(self)); |
---|
672 | n/a | s = (PyScannerObject *)self; |
---|
673 | n/a | Py_VISIT(s->strict); |
---|
674 | n/a | Py_VISIT(s->object_hook); |
---|
675 | n/a | Py_VISIT(s->object_pairs_hook); |
---|
676 | n/a | Py_VISIT(s->parse_float); |
---|
677 | n/a | Py_VISIT(s->parse_int); |
---|
678 | n/a | Py_VISIT(s->parse_constant); |
---|
679 | n/a | return 0; |
---|
680 | n/a | } |
---|
681 | n/a | |
---|
682 | n/a | static int |
---|
683 | n/a | scanner_clear(PyObject *self) |
---|
684 | n/a | { |
---|
685 | n/a | PyScannerObject *s; |
---|
686 | n/a | assert(PyScanner_Check(self)); |
---|
687 | n/a | s = (PyScannerObject *)self; |
---|
688 | n/a | Py_CLEAR(s->strict); |
---|
689 | n/a | Py_CLEAR(s->object_hook); |
---|
690 | n/a | Py_CLEAR(s->object_pairs_hook); |
---|
691 | n/a | Py_CLEAR(s->parse_float); |
---|
692 | n/a | Py_CLEAR(s->parse_int); |
---|
693 | n/a | Py_CLEAR(s->parse_constant); |
---|
694 | n/a | Py_CLEAR(s->memo); |
---|
695 | n/a | return 0; |
---|
696 | n/a | } |
---|
697 | n/a | |
---|
698 | n/a | static PyObject * |
---|
699 | n/a | _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) { |
---|
700 | n/a | /* Read a JSON object from PyUnicode pystr. |
---|
701 | n/a | idx is the index of the first character after the opening curly brace. |
---|
702 | n/a | *next_idx_ptr is a return-by-reference index to the first character after |
---|
703 | n/a | the closing curly brace. |
---|
704 | n/a | |
---|
705 | n/a | Returns a new PyObject (usually a dict, but object_hook can change that) |
---|
706 | n/a | */ |
---|
707 | n/a | void *str; |
---|
708 | n/a | int kind; |
---|
709 | n/a | Py_ssize_t end_idx; |
---|
710 | n/a | PyObject *val = NULL; |
---|
711 | n/a | PyObject *rval = NULL; |
---|
712 | n/a | PyObject *key = NULL; |
---|
713 | n/a | int strict = PyObject_IsTrue(s->strict); |
---|
714 | n/a | int has_pairs_hook = (s->object_pairs_hook != Py_None); |
---|
715 | n/a | Py_ssize_t next_idx; |
---|
716 | n/a | |
---|
717 | n/a | if (strict < 0) |
---|
718 | n/a | return NULL; |
---|
719 | n/a | |
---|
720 | n/a | if (PyUnicode_READY(pystr) == -1) |
---|
721 | n/a | return NULL; |
---|
722 | n/a | |
---|
723 | n/a | str = PyUnicode_DATA(pystr); |
---|
724 | n/a | kind = PyUnicode_KIND(pystr); |
---|
725 | n/a | end_idx = PyUnicode_GET_LENGTH(pystr) - 1; |
---|
726 | n/a | |
---|
727 | n/a | if (has_pairs_hook) |
---|
728 | n/a | rval = PyList_New(0); |
---|
729 | n/a | else |
---|
730 | n/a | rval = PyDict_New(); |
---|
731 | n/a | if (rval == NULL) |
---|
732 | n/a | return NULL; |
---|
733 | n/a | |
---|
734 | n/a | /* skip whitespace after { */ |
---|
735 | n/a | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++; |
---|
736 | n/a | |
---|
737 | n/a | /* only loop if the object is non-empty */ |
---|
738 | n/a | if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') { |
---|
739 | n/a | while (1) { |
---|
740 | n/a | PyObject *memokey; |
---|
741 | n/a | |
---|
742 | n/a | /* read key */ |
---|
743 | n/a | if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '"') { |
---|
744 | n/a | raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx); |
---|
745 | n/a | goto bail; |
---|
746 | n/a | } |
---|
747 | n/a | key = scanstring_unicode(pystr, idx + 1, strict, &next_idx); |
---|
748 | n/a | if (key == NULL) |
---|
749 | n/a | goto bail; |
---|
750 | n/a | memokey = PyDict_GetItem(s->memo, key); |
---|
751 | n/a | if (memokey != NULL) { |
---|
752 | n/a | Py_INCREF(memokey); |
---|
753 | n/a | Py_DECREF(key); |
---|
754 | n/a | key = memokey; |
---|
755 | n/a | } |
---|
756 | n/a | else { |
---|
757 | n/a | if (PyDict_SetItem(s->memo, key, key) < 0) |
---|
758 | n/a | goto bail; |
---|
759 | n/a | } |
---|
760 | n/a | idx = next_idx; |
---|
761 | n/a | |
---|
762 | n/a | /* skip whitespace between key and : delimiter, read :, skip whitespace */ |
---|
763 | n/a | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
---|
764 | n/a | if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') { |
---|
765 | n/a | raise_errmsg("Expecting ':' delimiter", pystr, idx); |
---|
766 | n/a | goto bail; |
---|
767 | n/a | } |
---|
768 | n/a | idx++; |
---|
769 | n/a | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
---|
770 | n/a | |
---|
771 | n/a | /* read any JSON term */ |
---|
772 | n/a | val = scan_once_unicode(s, pystr, idx, &next_idx); |
---|
773 | n/a | if (val == NULL) |
---|
774 | n/a | goto bail; |
---|
775 | n/a | |
---|
776 | n/a | if (has_pairs_hook) { |
---|
777 | n/a | PyObject *item = PyTuple_Pack(2, key, val); |
---|
778 | n/a | if (item == NULL) |
---|
779 | n/a | goto bail; |
---|
780 | n/a | Py_CLEAR(key); |
---|
781 | n/a | Py_CLEAR(val); |
---|
782 | n/a | if (PyList_Append(rval, item) == -1) { |
---|
783 | n/a | Py_DECREF(item); |
---|
784 | n/a | goto bail; |
---|
785 | n/a | } |
---|
786 | n/a | Py_DECREF(item); |
---|
787 | n/a | } |
---|
788 | n/a | else { |
---|
789 | n/a | if (PyDict_SetItem(rval, key, val) < 0) |
---|
790 | n/a | goto bail; |
---|
791 | n/a | Py_CLEAR(key); |
---|
792 | n/a | Py_CLEAR(val); |
---|
793 | n/a | } |
---|
794 | n/a | idx = next_idx; |
---|
795 | n/a | |
---|
796 | n/a | /* skip whitespace before } or , */ |
---|
797 | n/a | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
---|
798 | n/a | |
---|
799 | n/a | /* bail if the object is closed or we didn't get the , delimiter */ |
---|
800 | n/a | if (idx <= end_idx && PyUnicode_READ(kind, str, idx) == '}') |
---|
801 | n/a | break; |
---|
802 | n/a | if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ',') { |
---|
803 | n/a | raise_errmsg("Expecting ',' delimiter", pystr, idx); |
---|
804 | n/a | goto bail; |
---|
805 | n/a | } |
---|
806 | n/a | idx++; |
---|
807 | n/a | |
---|
808 | n/a | /* skip whitespace after , delimiter */ |
---|
809 | n/a | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
---|
810 | n/a | } |
---|
811 | n/a | } |
---|
812 | n/a | |
---|
813 | n/a | *next_idx_ptr = idx + 1; |
---|
814 | n/a | |
---|
815 | n/a | if (has_pairs_hook) { |
---|
816 | n/a | val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL); |
---|
817 | n/a | Py_DECREF(rval); |
---|
818 | n/a | return val; |
---|
819 | n/a | } |
---|
820 | n/a | |
---|
821 | n/a | /* if object_hook is not None: rval = object_hook(rval) */ |
---|
822 | n/a | if (s->object_hook != Py_None) { |
---|
823 | n/a | val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL); |
---|
824 | n/a | Py_DECREF(rval); |
---|
825 | n/a | return val; |
---|
826 | n/a | } |
---|
827 | n/a | return rval; |
---|
828 | n/a | bail: |
---|
829 | n/a | Py_XDECREF(key); |
---|
830 | n/a | Py_XDECREF(val); |
---|
831 | n/a | Py_XDECREF(rval); |
---|
832 | n/a | return NULL; |
---|
833 | n/a | } |
---|
834 | n/a | |
---|
835 | n/a | static PyObject * |
---|
836 | n/a | _parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) { |
---|
837 | n/a | /* Read a JSON array from PyUnicode pystr. |
---|
838 | n/a | idx is the index of the first character after the opening brace. |
---|
839 | n/a | *next_idx_ptr is a return-by-reference index to the first character after |
---|
840 | n/a | the closing brace. |
---|
841 | n/a | |
---|
842 | n/a | Returns a new PyList |
---|
843 | n/a | */ |
---|
844 | n/a | void *str; |
---|
845 | n/a | int kind; |
---|
846 | n/a | Py_ssize_t end_idx; |
---|
847 | n/a | PyObject *val = NULL; |
---|
848 | n/a | PyObject *rval; |
---|
849 | n/a | Py_ssize_t next_idx; |
---|
850 | n/a | |
---|
851 | n/a | if (PyUnicode_READY(pystr) == -1) |
---|
852 | n/a | return NULL; |
---|
853 | n/a | |
---|
854 | n/a | rval = PyList_New(0); |
---|
855 | n/a | if (rval == NULL) |
---|
856 | n/a | return NULL; |
---|
857 | n/a | |
---|
858 | n/a | str = PyUnicode_DATA(pystr); |
---|
859 | n/a | kind = PyUnicode_KIND(pystr); |
---|
860 | n/a | end_idx = PyUnicode_GET_LENGTH(pystr) - 1; |
---|
861 | n/a | |
---|
862 | n/a | /* skip whitespace after [ */ |
---|
863 | n/a | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
---|
864 | n/a | |
---|
865 | n/a | /* only loop if the array is non-empty */ |
---|
866 | n/a | if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') { |
---|
867 | n/a | while (1) { |
---|
868 | n/a | |
---|
869 | n/a | /* read any JSON term */ |
---|
870 | n/a | val = scan_once_unicode(s, pystr, idx, &next_idx); |
---|
871 | n/a | if (val == NULL) |
---|
872 | n/a | goto bail; |
---|
873 | n/a | |
---|
874 | n/a | if (PyList_Append(rval, val) == -1) |
---|
875 | n/a | goto bail; |
---|
876 | n/a | |
---|
877 | n/a | Py_CLEAR(val); |
---|
878 | n/a | idx = next_idx; |
---|
879 | n/a | |
---|
880 | n/a | /* skip whitespace between term and , */ |
---|
881 | n/a | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
---|
882 | n/a | |
---|
883 | n/a | /* bail if the array is closed or we didn't get the , delimiter */ |
---|
884 | n/a | if (idx <= end_idx && PyUnicode_READ(kind, str, idx) == ']') |
---|
885 | n/a | break; |
---|
886 | n/a | if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ',') { |
---|
887 | n/a | raise_errmsg("Expecting ',' delimiter", pystr, idx); |
---|
888 | n/a | goto bail; |
---|
889 | n/a | } |
---|
890 | n/a | idx++; |
---|
891 | n/a | |
---|
892 | n/a | /* skip whitespace after , */ |
---|
893 | n/a | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
---|
894 | n/a | } |
---|
895 | n/a | } |
---|
896 | n/a | |
---|
897 | n/a | /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */ |
---|
898 | n/a | if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') { |
---|
899 | n/a | raise_errmsg("Expecting value", pystr, end_idx); |
---|
900 | n/a | goto bail; |
---|
901 | n/a | } |
---|
902 | n/a | *next_idx_ptr = idx + 1; |
---|
903 | n/a | return rval; |
---|
904 | n/a | bail: |
---|
905 | n/a | Py_XDECREF(val); |
---|
906 | n/a | Py_DECREF(rval); |
---|
907 | n/a | return NULL; |
---|
908 | n/a | } |
---|
909 | n/a | |
---|
910 | n/a | static PyObject * |
---|
911 | n/a | _parse_constant(PyScannerObject *s, const char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) { |
---|
912 | n/a | /* Read a JSON constant. |
---|
913 | n/a | constant is the constant string that was found |
---|
914 | n/a | ("NaN", "Infinity", "-Infinity"). |
---|
915 | n/a | idx is the index of the first character of the constant |
---|
916 | n/a | *next_idx_ptr is a return-by-reference index to the first character after |
---|
917 | n/a | the constant. |
---|
918 | n/a | |
---|
919 | n/a | Returns the result of parse_constant |
---|
920 | n/a | */ |
---|
921 | n/a | PyObject *cstr; |
---|
922 | n/a | PyObject *rval; |
---|
923 | n/a | /* constant is "NaN", "Infinity", or "-Infinity" */ |
---|
924 | n/a | cstr = PyUnicode_InternFromString(constant); |
---|
925 | n/a | if (cstr == NULL) |
---|
926 | n/a | return NULL; |
---|
927 | n/a | |
---|
928 | n/a | /* rval = parse_constant(constant) */ |
---|
929 | n/a | rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL); |
---|
930 | n/a | idx += PyUnicode_GET_LENGTH(cstr); |
---|
931 | n/a | Py_DECREF(cstr); |
---|
932 | n/a | *next_idx_ptr = idx; |
---|
933 | n/a | return rval; |
---|
934 | n/a | } |
---|
935 | n/a | |
---|
936 | n/a | static PyObject * |
---|
937 | n/a | _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) { |
---|
938 | n/a | /* Read a JSON number from PyUnicode pystr. |
---|
939 | n/a | idx is the index of the first character of the number |
---|
940 | n/a | *next_idx_ptr is a return-by-reference index to the first character after |
---|
941 | n/a | the number. |
---|
942 | n/a | |
---|
943 | n/a | Returns a new PyObject representation of that number: |
---|
944 | n/a | PyLong, or PyFloat. |
---|
945 | n/a | May return other types if parse_int or parse_float are set |
---|
946 | n/a | */ |
---|
947 | n/a | void *str; |
---|
948 | n/a | int kind; |
---|
949 | n/a | Py_ssize_t end_idx; |
---|
950 | n/a | Py_ssize_t idx = start; |
---|
951 | n/a | int is_float = 0; |
---|
952 | n/a | PyObject *rval; |
---|
953 | n/a | PyObject *numstr = NULL; |
---|
954 | n/a | PyObject *custom_func; |
---|
955 | n/a | |
---|
956 | n/a | if (PyUnicode_READY(pystr) == -1) |
---|
957 | n/a | return NULL; |
---|
958 | n/a | |
---|
959 | n/a | str = PyUnicode_DATA(pystr); |
---|
960 | n/a | kind = PyUnicode_KIND(pystr); |
---|
961 | n/a | end_idx = PyUnicode_GET_LENGTH(pystr) - 1; |
---|
962 | n/a | |
---|
963 | n/a | /* read a sign if it's there, make sure it's not the end of the string */ |
---|
964 | n/a | if (PyUnicode_READ(kind, str, idx) == '-') { |
---|
965 | n/a | idx++; |
---|
966 | n/a | if (idx > end_idx) { |
---|
967 | n/a | raise_stop_iteration(start); |
---|
968 | n/a | return NULL; |
---|
969 | n/a | } |
---|
970 | n/a | } |
---|
971 | n/a | |
---|
972 | n/a | /* read as many integer digits as we find as long as it doesn't start with 0 */ |
---|
973 | n/a | if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') { |
---|
974 | n/a | idx++; |
---|
975 | n/a | while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++; |
---|
976 | n/a | } |
---|
977 | n/a | /* if it starts with 0 we only expect one integer digit */ |
---|
978 | n/a | else if (PyUnicode_READ(kind, str, idx) == '0') { |
---|
979 | n/a | idx++; |
---|
980 | n/a | } |
---|
981 | n/a | /* no integer digits, error */ |
---|
982 | n/a | else { |
---|
983 | n/a | raise_stop_iteration(start); |
---|
984 | n/a | return NULL; |
---|
985 | n/a | } |
---|
986 | n/a | |
---|
987 | n/a | /* if the next char is '.' followed by a digit then read all float digits */ |
---|
988 | n/a | if (idx < end_idx && PyUnicode_READ(kind, str, idx) == '.' && PyUnicode_READ(kind, str, idx + 1) >= '0' && PyUnicode_READ(kind, str, idx + 1) <= '9') { |
---|
989 | n/a | is_float = 1; |
---|
990 | n/a | idx += 2; |
---|
991 | n/a | while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++; |
---|
992 | n/a | } |
---|
993 | n/a | |
---|
994 | n/a | /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */ |
---|
995 | n/a | if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) { |
---|
996 | n/a | Py_ssize_t e_start = idx; |
---|
997 | n/a | idx++; |
---|
998 | n/a | |
---|
999 | n/a | /* read an exponent sign if present */ |
---|
1000 | n/a | if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++; |
---|
1001 | n/a | |
---|
1002 | n/a | /* read all digits */ |
---|
1003 | n/a | while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++; |
---|
1004 | n/a | |
---|
1005 | n/a | /* if we got a digit, then parse as float. if not, backtrack */ |
---|
1006 | n/a | if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') { |
---|
1007 | n/a | is_float = 1; |
---|
1008 | n/a | } |
---|
1009 | n/a | else { |
---|
1010 | n/a | idx = e_start; |
---|
1011 | n/a | } |
---|
1012 | n/a | } |
---|
1013 | n/a | |
---|
1014 | n/a | if (is_float && s->parse_float != (PyObject *)&PyFloat_Type) |
---|
1015 | n/a | custom_func = s->parse_float; |
---|
1016 | n/a | else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type) |
---|
1017 | n/a | custom_func = s->parse_int; |
---|
1018 | n/a | else |
---|
1019 | n/a | custom_func = NULL; |
---|
1020 | n/a | |
---|
1021 | n/a | if (custom_func) { |
---|
1022 | n/a | /* copy the section we determined to be a number */ |
---|
1023 | n/a | numstr = PyUnicode_FromKindAndData(kind, |
---|
1024 | n/a | (char*)str + kind * start, |
---|
1025 | n/a | idx - start); |
---|
1026 | n/a | if (numstr == NULL) |
---|
1027 | n/a | return NULL; |
---|
1028 | n/a | rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL); |
---|
1029 | n/a | } |
---|
1030 | n/a | else { |
---|
1031 | n/a | Py_ssize_t i, n; |
---|
1032 | n/a | char *buf; |
---|
1033 | n/a | /* Straight conversion to ASCII, to avoid costly conversion of |
---|
1034 | n/a | decimal unicode digits (which cannot appear here) */ |
---|
1035 | n/a | n = idx - start; |
---|
1036 | n/a | numstr = PyBytes_FromStringAndSize(NULL, n); |
---|
1037 | n/a | if (numstr == NULL) |
---|
1038 | n/a | return NULL; |
---|
1039 | n/a | buf = PyBytes_AS_STRING(numstr); |
---|
1040 | n/a | for (i = 0; i < n; i++) { |
---|
1041 | n/a | buf[i] = (char) PyUnicode_READ(kind, str, i + start); |
---|
1042 | n/a | } |
---|
1043 | n/a | if (is_float) |
---|
1044 | n/a | rval = PyFloat_FromString(numstr); |
---|
1045 | n/a | else |
---|
1046 | n/a | rval = PyLong_FromString(buf, NULL, 10); |
---|
1047 | n/a | } |
---|
1048 | n/a | Py_DECREF(numstr); |
---|
1049 | n/a | *next_idx_ptr = idx; |
---|
1050 | n/a | return rval; |
---|
1051 | n/a | } |
---|
1052 | n/a | |
---|
1053 | n/a | static PyObject * |
---|
1054 | n/a | scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) |
---|
1055 | n/a | { |
---|
1056 | n/a | /* Read one JSON term (of any kind) from PyUnicode pystr. |
---|
1057 | n/a | idx is the index of the first character of the term |
---|
1058 | n/a | *next_idx_ptr is a return-by-reference index to the first character after |
---|
1059 | n/a | the number. |
---|
1060 | n/a | |
---|
1061 | n/a | Returns a new PyObject representation of the term. |
---|
1062 | n/a | */ |
---|
1063 | n/a | PyObject *res; |
---|
1064 | n/a | void *str; |
---|
1065 | n/a | int kind; |
---|
1066 | n/a | Py_ssize_t length; |
---|
1067 | n/a | int strict; |
---|
1068 | n/a | |
---|
1069 | n/a | if (PyUnicode_READY(pystr) == -1) |
---|
1070 | n/a | return NULL; |
---|
1071 | n/a | |
---|
1072 | n/a | str = PyUnicode_DATA(pystr); |
---|
1073 | n/a | kind = PyUnicode_KIND(pystr); |
---|
1074 | n/a | length = PyUnicode_GET_LENGTH(pystr); |
---|
1075 | n/a | |
---|
1076 | n/a | if (idx < 0) { |
---|
1077 | n/a | PyErr_SetString(PyExc_ValueError, "idx cannot be negative"); |
---|
1078 | n/a | return NULL; |
---|
1079 | n/a | } |
---|
1080 | n/a | if (idx >= length) { |
---|
1081 | n/a | raise_stop_iteration(idx); |
---|
1082 | n/a | return NULL; |
---|
1083 | n/a | } |
---|
1084 | n/a | |
---|
1085 | n/a | switch (PyUnicode_READ(kind, str, idx)) { |
---|
1086 | n/a | case '"': |
---|
1087 | n/a | /* string */ |
---|
1088 | n/a | strict = PyObject_IsTrue(s->strict); |
---|
1089 | n/a | if (strict < 0) |
---|
1090 | n/a | return NULL; |
---|
1091 | n/a | return scanstring_unicode(pystr, idx + 1, strict, next_idx_ptr); |
---|
1092 | n/a | case '{': |
---|
1093 | n/a | /* object */ |
---|
1094 | n/a | if (Py_EnterRecursiveCall(" while decoding a JSON object " |
---|
1095 | n/a | "from a unicode string")) |
---|
1096 | n/a | return NULL; |
---|
1097 | n/a | res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr); |
---|
1098 | n/a | Py_LeaveRecursiveCall(); |
---|
1099 | n/a | return res; |
---|
1100 | n/a | case '[': |
---|
1101 | n/a | /* array */ |
---|
1102 | n/a | if (Py_EnterRecursiveCall(" while decoding a JSON array " |
---|
1103 | n/a | "from a unicode string")) |
---|
1104 | n/a | return NULL; |
---|
1105 | n/a | res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr); |
---|
1106 | n/a | Py_LeaveRecursiveCall(); |
---|
1107 | n/a | return res; |
---|
1108 | n/a | case 'n': |
---|
1109 | n/a | /* null */ |
---|
1110 | n/a | if ((idx + 3 < length) && PyUnicode_READ(kind, str, idx + 1) == 'u' && PyUnicode_READ(kind, str, idx + 2) == 'l' && PyUnicode_READ(kind, str, idx + 3) == 'l') { |
---|
1111 | n/a | *next_idx_ptr = idx + 4; |
---|
1112 | n/a | Py_RETURN_NONE; |
---|
1113 | n/a | } |
---|
1114 | n/a | break; |
---|
1115 | n/a | case 't': |
---|
1116 | n/a | /* true */ |
---|
1117 | n/a | if ((idx + 3 < length) && PyUnicode_READ(kind, str, idx + 1) == 'r' && PyUnicode_READ(kind, str, idx + 2) == 'u' && PyUnicode_READ(kind, str, idx + 3) == 'e') { |
---|
1118 | n/a | *next_idx_ptr = idx + 4; |
---|
1119 | n/a | Py_RETURN_TRUE; |
---|
1120 | n/a | } |
---|
1121 | n/a | break; |
---|
1122 | n/a | case 'f': |
---|
1123 | n/a | /* false */ |
---|
1124 | n/a | if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' && |
---|
1125 | n/a | PyUnicode_READ(kind, str, idx + 2) == 'l' && |
---|
1126 | n/a | PyUnicode_READ(kind, str, idx + 3) == 's' && |
---|
1127 | n/a | PyUnicode_READ(kind, str, idx + 4) == 'e') { |
---|
1128 | n/a | *next_idx_ptr = idx + 5; |
---|
1129 | n/a | Py_RETURN_FALSE; |
---|
1130 | n/a | } |
---|
1131 | n/a | break; |
---|
1132 | n/a | case 'N': |
---|
1133 | n/a | /* NaN */ |
---|
1134 | n/a | if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' && |
---|
1135 | n/a | PyUnicode_READ(kind, str, idx + 2) == 'N') { |
---|
1136 | n/a | return _parse_constant(s, "NaN", idx, next_idx_ptr); |
---|
1137 | n/a | } |
---|
1138 | n/a | break; |
---|
1139 | n/a | case 'I': |
---|
1140 | n/a | /* Infinity */ |
---|
1141 | n/a | if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' && |
---|
1142 | n/a | PyUnicode_READ(kind, str, idx + 2) == 'f' && |
---|
1143 | n/a | PyUnicode_READ(kind, str, idx + 3) == 'i' && |
---|
1144 | n/a | PyUnicode_READ(kind, str, idx + 4) == 'n' && |
---|
1145 | n/a | PyUnicode_READ(kind, str, idx + 5) == 'i' && |
---|
1146 | n/a | PyUnicode_READ(kind, str, idx + 6) == 't' && |
---|
1147 | n/a | PyUnicode_READ(kind, str, idx + 7) == 'y') { |
---|
1148 | n/a | return _parse_constant(s, "Infinity", idx, next_idx_ptr); |
---|
1149 | n/a | } |
---|
1150 | n/a | break; |
---|
1151 | n/a | case '-': |
---|
1152 | n/a | /* -Infinity */ |
---|
1153 | n/a | if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' && |
---|
1154 | n/a | PyUnicode_READ(kind, str, idx + 2) == 'n' && |
---|
1155 | n/a | PyUnicode_READ(kind, str, idx + 3) == 'f' && |
---|
1156 | n/a | PyUnicode_READ(kind, str, idx + 4) == 'i' && |
---|
1157 | n/a | PyUnicode_READ(kind, str, idx + 5) == 'n' && |
---|
1158 | n/a | PyUnicode_READ(kind, str, idx + 6) == 'i' && |
---|
1159 | n/a | PyUnicode_READ(kind, str, idx + 7) == 't' && |
---|
1160 | n/a | PyUnicode_READ(kind, str, idx + 8) == 'y') { |
---|
1161 | n/a | return _parse_constant(s, "-Infinity", idx, next_idx_ptr); |
---|
1162 | n/a | } |
---|
1163 | n/a | break; |
---|
1164 | n/a | } |
---|
1165 | n/a | /* Didn't find a string, object, array, or named constant. Look for a number. */ |
---|
1166 | n/a | return _match_number_unicode(s, pystr, idx, next_idx_ptr); |
---|
1167 | n/a | } |
---|
1168 | n/a | |
---|
1169 | n/a | static PyObject * |
---|
1170 | n/a | scanner_call(PyObject *self, PyObject *args, PyObject *kwds) |
---|
1171 | n/a | { |
---|
1172 | n/a | /* Python callable interface to scan_once_{str,unicode} */ |
---|
1173 | n/a | PyObject *pystr; |
---|
1174 | n/a | PyObject *rval; |
---|
1175 | n/a | Py_ssize_t idx; |
---|
1176 | n/a | Py_ssize_t next_idx = -1; |
---|
1177 | n/a | static char *kwlist[] = {"string", "idx", NULL}; |
---|
1178 | n/a | PyScannerObject *s; |
---|
1179 | n/a | assert(PyScanner_Check(self)); |
---|
1180 | n/a | s = (PyScannerObject *)self; |
---|
1181 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:scan_once", kwlist, &pystr, &idx)) |
---|
1182 | n/a | return NULL; |
---|
1183 | n/a | |
---|
1184 | n/a | if (PyUnicode_Check(pystr)) { |
---|
1185 | n/a | rval = scan_once_unicode(s, pystr, idx, &next_idx); |
---|
1186 | n/a | } |
---|
1187 | n/a | else { |
---|
1188 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1189 | n/a | "first argument must be a string, not %.80s", |
---|
1190 | n/a | Py_TYPE(pystr)->tp_name); |
---|
1191 | n/a | return NULL; |
---|
1192 | n/a | } |
---|
1193 | n/a | PyDict_Clear(s->memo); |
---|
1194 | n/a | if (rval == NULL) |
---|
1195 | n/a | return NULL; |
---|
1196 | n/a | return _build_rval_index_tuple(rval, next_idx); |
---|
1197 | n/a | } |
---|
1198 | n/a | |
---|
1199 | n/a | static PyObject * |
---|
1200 | n/a | scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
1201 | n/a | { |
---|
1202 | n/a | PyScannerObject *s; |
---|
1203 | n/a | s = (PyScannerObject *)type->tp_alloc(type, 0); |
---|
1204 | n/a | if (s != NULL) { |
---|
1205 | n/a | s->strict = NULL; |
---|
1206 | n/a | s->object_hook = NULL; |
---|
1207 | n/a | s->object_pairs_hook = NULL; |
---|
1208 | n/a | s->parse_float = NULL; |
---|
1209 | n/a | s->parse_int = NULL; |
---|
1210 | n/a | s->parse_constant = NULL; |
---|
1211 | n/a | } |
---|
1212 | n/a | return (PyObject *)s; |
---|
1213 | n/a | } |
---|
1214 | n/a | |
---|
1215 | n/a | static int |
---|
1216 | n/a | scanner_init(PyObject *self, PyObject *args, PyObject *kwds) |
---|
1217 | n/a | { |
---|
1218 | n/a | /* Initialize Scanner object */ |
---|
1219 | n/a | PyObject *ctx; |
---|
1220 | n/a | static char *kwlist[] = {"context", NULL}; |
---|
1221 | n/a | PyScannerObject *s; |
---|
1222 | n/a | |
---|
1223 | n/a | assert(PyScanner_Check(self)); |
---|
1224 | n/a | s = (PyScannerObject *)self; |
---|
1225 | n/a | |
---|
1226 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx)) |
---|
1227 | n/a | return -1; |
---|
1228 | n/a | |
---|
1229 | n/a | if (s->memo == NULL) { |
---|
1230 | n/a | s->memo = PyDict_New(); |
---|
1231 | n/a | if (s->memo == NULL) |
---|
1232 | n/a | goto bail; |
---|
1233 | n/a | } |
---|
1234 | n/a | |
---|
1235 | n/a | /* All of these will fail "gracefully" so we don't need to verify them */ |
---|
1236 | n/a | s->strict = PyObject_GetAttrString(ctx, "strict"); |
---|
1237 | n/a | if (s->strict == NULL) |
---|
1238 | n/a | goto bail; |
---|
1239 | n/a | s->object_hook = PyObject_GetAttrString(ctx, "object_hook"); |
---|
1240 | n/a | if (s->object_hook == NULL) |
---|
1241 | n/a | goto bail; |
---|
1242 | n/a | s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook"); |
---|
1243 | n/a | if (s->object_pairs_hook == NULL) |
---|
1244 | n/a | goto bail; |
---|
1245 | n/a | s->parse_float = PyObject_GetAttrString(ctx, "parse_float"); |
---|
1246 | n/a | if (s->parse_float == NULL) |
---|
1247 | n/a | goto bail; |
---|
1248 | n/a | s->parse_int = PyObject_GetAttrString(ctx, "parse_int"); |
---|
1249 | n/a | if (s->parse_int == NULL) |
---|
1250 | n/a | goto bail; |
---|
1251 | n/a | s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant"); |
---|
1252 | n/a | if (s->parse_constant == NULL) |
---|
1253 | n/a | goto bail; |
---|
1254 | n/a | |
---|
1255 | n/a | return 0; |
---|
1256 | n/a | |
---|
1257 | n/a | bail: |
---|
1258 | n/a | Py_CLEAR(s->strict); |
---|
1259 | n/a | Py_CLEAR(s->object_hook); |
---|
1260 | n/a | Py_CLEAR(s->object_pairs_hook); |
---|
1261 | n/a | Py_CLEAR(s->parse_float); |
---|
1262 | n/a | Py_CLEAR(s->parse_int); |
---|
1263 | n/a | Py_CLEAR(s->parse_constant); |
---|
1264 | n/a | return -1; |
---|
1265 | n/a | } |
---|
1266 | n/a | |
---|
1267 | n/a | PyDoc_STRVAR(scanner_doc, "JSON scanner object"); |
---|
1268 | n/a | |
---|
1269 | n/a | static |
---|
1270 | n/a | PyTypeObject PyScannerType = { |
---|
1271 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
1272 | n/a | "_json.Scanner", /* tp_name */ |
---|
1273 | n/a | sizeof(PyScannerObject), /* tp_basicsize */ |
---|
1274 | n/a | 0, /* tp_itemsize */ |
---|
1275 | n/a | scanner_dealloc, /* tp_dealloc */ |
---|
1276 | n/a | 0, /* tp_print */ |
---|
1277 | n/a | 0, /* tp_getattr */ |
---|
1278 | n/a | 0, /* tp_setattr */ |
---|
1279 | n/a | 0, /* tp_compare */ |
---|
1280 | n/a | 0, /* tp_repr */ |
---|
1281 | n/a | 0, /* tp_as_number */ |
---|
1282 | n/a | 0, /* tp_as_sequence */ |
---|
1283 | n/a | 0, /* tp_as_mapping */ |
---|
1284 | n/a | 0, /* tp_hash */ |
---|
1285 | n/a | scanner_call, /* tp_call */ |
---|
1286 | n/a | 0, /* tp_str */ |
---|
1287 | n/a | 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */ |
---|
1288 | n/a | 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */ |
---|
1289 | n/a | 0, /* tp_as_buffer */ |
---|
1290 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
---|
1291 | n/a | scanner_doc, /* tp_doc */ |
---|
1292 | n/a | scanner_traverse, /* tp_traverse */ |
---|
1293 | n/a | scanner_clear, /* tp_clear */ |
---|
1294 | n/a | 0, /* tp_richcompare */ |
---|
1295 | n/a | 0, /* tp_weaklistoffset */ |
---|
1296 | n/a | 0, /* tp_iter */ |
---|
1297 | n/a | 0, /* tp_iternext */ |
---|
1298 | n/a | 0, /* tp_methods */ |
---|
1299 | n/a | scanner_members, /* tp_members */ |
---|
1300 | n/a | 0, /* tp_getset */ |
---|
1301 | n/a | 0, /* tp_base */ |
---|
1302 | n/a | 0, /* tp_dict */ |
---|
1303 | n/a | 0, /* tp_descr_get */ |
---|
1304 | n/a | 0, /* tp_descr_set */ |
---|
1305 | n/a | 0, /* tp_dictoffset */ |
---|
1306 | n/a | scanner_init, /* tp_init */ |
---|
1307 | n/a | 0,/* PyType_GenericAlloc, */ /* tp_alloc */ |
---|
1308 | n/a | scanner_new, /* tp_new */ |
---|
1309 | n/a | 0,/* PyObject_GC_Del, */ /* tp_free */ |
---|
1310 | n/a | }; |
---|
1311 | n/a | |
---|
1312 | n/a | static PyObject * |
---|
1313 | n/a | encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
1314 | n/a | { |
---|
1315 | n/a | PyEncoderObject *s; |
---|
1316 | n/a | s = (PyEncoderObject *)type->tp_alloc(type, 0); |
---|
1317 | n/a | if (s != NULL) { |
---|
1318 | n/a | s->markers = NULL; |
---|
1319 | n/a | s->defaultfn = NULL; |
---|
1320 | n/a | s->encoder = NULL; |
---|
1321 | n/a | s->indent = NULL; |
---|
1322 | n/a | s->key_separator = NULL; |
---|
1323 | n/a | s->item_separator = NULL; |
---|
1324 | n/a | s->sort_keys = NULL; |
---|
1325 | n/a | s->skipkeys = NULL; |
---|
1326 | n/a | } |
---|
1327 | n/a | return (PyObject *)s; |
---|
1328 | n/a | } |
---|
1329 | n/a | |
---|
1330 | n/a | static int |
---|
1331 | n/a | encoder_init(PyObject *self, PyObject *args, PyObject *kwds) |
---|
1332 | n/a | { |
---|
1333 | n/a | /* initialize Encoder object */ |
---|
1334 | n/a | static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL}; |
---|
1335 | n/a | |
---|
1336 | n/a | PyEncoderObject *s; |
---|
1337 | n/a | PyObject *markers, *defaultfn, *encoder, *indent, *key_separator; |
---|
1338 | n/a | PyObject *item_separator, *sort_keys, *skipkeys; |
---|
1339 | n/a | int allow_nan; |
---|
1340 | n/a | |
---|
1341 | n/a | assert(PyEncoder_Check(self)); |
---|
1342 | n/a | s = (PyEncoderObject *)self; |
---|
1343 | n/a | |
---|
1344 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOUUOOp:make_encoder", kwlist, |
---|
1345 | n/a | &markers, &defaultfn, &encoder, &indent, |
---|
1346 | n/a | &key_separator, &item_separator, |
---|
1347 | n/a | &sort_keys, &skipkeys, &allow_nan)) |
---|
1348 | n/a | return -1; |
---|
1349 | n/a | |
---|
1350 | n/a | if (markers != Py_None && !PyDict_Check(markers)) { |
---|
1351 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1352 | n/a | "make_encoder() argument 1 must be dict or None, " |
---|
1353 | n/a | "not %.200s", Py_TYPE(markers)->tp_name); |
---|
1354 | n/a | return -1; |
---|
1355 | n/a | } |
---|
1356 | n/a | |
---|
1357 | n/a | s->markers = markers; |
---|
1358 | n/a | s->defaultfn = defaultfn; |
---|
1359 | n/a | s->encoder = encoder; |
---|
1360 | n/a | s->indent = indent; |
---|
1361 | n/a | s->key_separator = key_separator; |
---|
1362 | n/a | s->item_separator = item_separator; |
---|
1363 | n/a | s->sort_keys = sort_keys; |
---|
1364 | n/a | s->skipkeys = skipkeys; |
---|
1365 | n/a | s->fast_encode = NULL; |
---|
1366 | n/a | if (PyCFunction_Check(s->encoder)) { |
---|
1367 | n/a | PyCFunction f = PyCFunction_GetFunction(s->encoder); |
---|
1368 | n/a | if (f == (PyCFunction)py_encode_basestring_ascii || |
---|
1369 | n/a | f == (PyCFunction)py_encode_basestring) { |
---|
1370 | n/a | s->fast_encode = f; |
---|
1371 | n/a | } |
---|
1372 | n/a | } |
---|
1373 | n/a | s->allow_nan = allow_nan; |
---|
1374 | n/a | |
---|
1375 | n/a | Py_INCREF(s->markers); |
---|
1376 | n/a | Py_INCREF(s->defaultfn); |
---|
1377 | n/a | Py_INCREF(s->encoder); |
---|
1378 | n/a | Py_INCREF(s->indent); |
---|
1379 | n/a | Py_INCREF(s->key_separator); |
---|
1380 | n/a | Py_INCREF(s->item_separator); |
---|
1381 | n/a | Py_INCREF(s->sort_keys); |
---|
1382 | n/a | Py_INCREF(s->skipkeys); |
---|
1383 | n/a | return 0; |
---|
1384 | n/a | } |
---|
1385 | n/a | |
---|
1386 | n/a | static PyObject * |
---|
1387 | n/a | encoder_call(PyObject *self, PyObject *args, PyObject *kwds) |
---|
1388 | n/a | { |
---|
1389 | n/a | /* Python callable interface to encode_listencode_obj */ |
---|
1390 | n/a | static char *kwlist[] = {"obj", "_current_indent_level", NULL}; |
---|
1391 | n/a | PyObject *obj; |
---|
1392 | n/a | Py_ssize_t indent_level; |
---|
1393 | n/a | PyEncoderObject *s; |
---|
1394 | n/a | _PyAccu acc; |
---|
1395 | n/a | |
---|
1396 | n/a | assert(PyEncoder_Check(self)); |
---|
1397 | n/a | s = (PyEncoderObject *)self; |
---|
1398 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:_iterencode", kwlist, |
---|
1399 | n/a | &obj, &indent_level)) |
---|
1400 | n/a | return NULL; |
---|
1401 | n/a | if (_PyAccu_Init(&acc)) |
---|
1402 | n/a | return NULL; |
---|
1403 | n/a | if (encoder_listencode_obj(s, &acc, obj, indent_level)) { |
---|
1404 | n/a | _PyAccu_Destroy(&acc); |
---|
1405 | n/a | return NULL; |
---|
1406 | n/a | } |
---|
1407 | n/a | return _PyAccu_FinishAsList(&acc); |
---|
1408 | n/a | } |
---|
1409 | n/a | |
---|
1410 | n/a | static PyObject * |
---|
1411 | n/a | _encoded_const(PyObject *obj) |
---|
1412 | n/a | { |
---|
1413 | n/a | /* Return the JSON string representation of None, True, False */ |
---|
1414 | n/a | if (obj == Py_None) { |
---|
1415 | n/a | static PyObject *s_null = NULL; |
---|
1416 | n/a | if (s_null == NULL) { |
---|
1417 | n/a | s_null = PyUnicode_InternFromString("null"); |
---|
1418 | n/a | } |
---|
1419 | n/a | Py_INCREF(s_null); |
---|
1420 | n/a | return s_null; |
---|
1421 | n/a | } |
---|
1422 | n/a | else if (obj == Py_True) { |
---|
1423 | n/a | static PyObject *s_true = NULL; |
---|
1424 | n/a | if (s_true == NULL) { |
---|
1425 | n/a | s_true = PyUnicode_InternFromString("true"); |
---|
1426 | n/a | } |
---|
1427 | n/a | Py_INCREF(s_true); |
---|
1428 | n/a | return s_true; |
---|
1429 | n/a | } |
---|
1430 | n/a | else if (obj == Py_False) { |
---|
1431 | n/a | static PyObject *s_false = NULL; |
---|
1432 | n/a | if (s_false == NULL) { |
---|
1433 | n/a | s_false = PyUnicode_InternFromString("false"); |
---|
1434 | n/a | } |
---|
1435 | n/a | Py_INCREF(s_false); |
---|
1436 | n/a | return s_false; |
---|
1437 | n/a | } |
---|
1438 | n/a | else { |
---|
1439 | n/a | PyErr_SetString(PyExc_ValueError, "not a const"); |
---|
1440 | n/a | return NULL; |
---|
1441 | n/a | } |
---|
1442 | n/a | } |
---|
1443 | n/a | |
---|
1444 | n/a | static PyObject * |
---|
1445 | n/a | encoder_encode_float(PyEncoderObject *s, PyObject *obj) |
---|
1446 | n/a | { |
---|
1447 | n/a | /* Return the JSON representation of a PyFloat. */ |
---|
1448 | n/a | double i = PyFloat_AS_DOUBLE(obj); |
---|
1449 | n/a | if (!Py_IS_FINITE(i)) { |
---|
1450 | n/a | if (!s->allow_nan) { |
---|
1451 | n/a | PyErr_SetString( |
---|
1452 | n/a | PyExc_ValueError, |
---|
1453 | n/a | "Out of range float values are not JSON compliant" |
---|
1454 | n/a | ); |
---|
1455 | n/a | return NULL; |
---|
1456 | n/a | } |
---|
1457 | n/a | if (i > 0) { |
---|
1458 | n/a | return PyUnicode_FromString("Infinity"); |
---|
1459 | n/a | } |
---|
1460 | n/a | else if (i < 0) { |
---|
1461 | n/a | return PyUnicode_FromString("-Infinity"); |
---|
1462 | n/a | } |
---|
1463 | n/a | else { |
---|
1464 | n/a | return PyUnicode_FromString("NaN"); |
---|
1465 | n/a | } |
---|
1466 | n/a | } |
---|
1467 | n/a | return PyFloat_Type.tp_repr(obj); |
---|
1468 | n/a | } |
---|
1469 | n/a | |
---|
1470 | n/a | static PyObject * |
---|
1471 | n/a | encoder_encode_string(PyEncoderObject *s, PyObject *obj) |
---|
1472 | n/a | { |
---|
1473 | n/a | /* Return the JSON representation of a string */ |
---|
1474 | n/a | if (s->fast_encode) |
---|
1475 | n/a | return s->fast_encode(NULL, obj); |
---|
1476 | n/a | else |
---|
1477 | n/a | return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL); |
---|
1478 | n/a | } |
---|
1479 | n/a | |
---|
1480 | n/a | static int |
---|
1481 | n/a | _steal_accumulate(_PyAccu *acc, PyObject *stolen) |
---|
1482 | n/a | { |
---|
1483 | n/a | /* Append stolen and then decrement its reference count */ |
---|
1484 | n/a | int rval = _PyAccu_Accumulate(acc, stolen); |
---|
1485 | n/a | Py_DECREF(stolen); |
---|
1486 | n/a | return rval; |
---|
1487 | n/a | } |
---|
1488 | n/a | |
---|
1489 | n/a | static int |
---|
1490 | n/a | encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, |
---|
1491 | n/a | PyObject *obj, Py_ssize_t indent_level) |
---|
1492 | n/a | { |
---|
1493 | n/a | /* Encode Python object obj to a JSON term */ |
---|
1494 | n/a | PyObject *newobj; |
---|
1495 | n/a | int rv; |
---|
1496 | n/a | |
---|
1497 | n/a | if (obj == Py_None || obj == Py_True || obj == Py_False) { |
---|
1498 | n/a | PyObject *cstr = _encoded_const(obj); |
---|
1499 | n/a | if (cstr == NULL) |
---|
1500 | n/a | return -1; |
---|
1501 | n/a | return _steal_accumulate(acc, cstr); |
---|
1502 | n/a | } |
---|
1503 | n/a | else if (PyUnicode_Check(obj)) |
---|
1504 | n/a | { |
---|
1505 | n/a | PyObject *encoded = encoder_encode_string(s, obj); |
---|
1506 | n/a | if (encoded == NULL) |
---|
1507 | n/a | return -1; |
---|
1508 | n/a | return _steal_accumulate(acc, encoded); |
---|
1509 | n/a | } |
---|
1510 | n/a | else if (PyLong_Check(obj)) { |
---|
1511 | n/a | PyObject *encoded = PyLong_Type.tp_str(obj); |
---|
1512 | n/a | if (encoded == NULL) |
---|
1513 | n/a | return -1; |
---|
1514 | n/a | return _steal_accumulate(acc, encoded); |
---|
1515 | n/a | } |
---|
1516 | n/a | else if (PyFloat_Check(obj)) { |
---|
1517 | n/a | PyObject *encoded = encoder_encode_float(s, obj); |
---|
1518 | n/a | if (encoded == NULL) |
---|
1519 | n/a | return -1; |
---|
1520 | n/a | return _steal_accumulate(acc, encoded); |
---|
1521 | n/a | } |
---|
1522 | n/a | else if (PyList_Check(obj) || PyTuple_Check(obj)) { |
---|
1523 | n/a | if (Py_EnterRecursiveCall(" while encoding a JSON object")) |
---|
1524 | n/a | return -1; |
---|
1525 | n/a | rv = encoder_listencode_list(s, acc, obj, indent_level); |
---|
1526 | n/a | Py_LeaveRecursiveCall(); |
---|
1527 | n/a | return rv; |
---|
1528 | n/a | } |
---|
1529 | n/a | else if (PyDict_Check(obj)) { |
---|
1530 | n/a | if (Py_EnterRecursiveCall(" while encoding a JSON object")) |
---|
1531 | n/a | return -1; |
---|
1532 | n/a | rv = encoder_listencode_dict(s, acc, obj, indent_level); |
---|
1533 | n/a | Py_LeaveRecursiveCall(); |
---|
1534 | n/a | return rv; |
---|
1535 | n/a | } |
---|
1536 | n/a | else { |
---|
1537 | n/a | PyObject *ident = NULL; |
---|
1538 | n/a | if (s->markers != Py_None) { |
---|
1539 | n/a | int has_key; |
---|
1540 | n/a | ident = PyLong_FromVoidPtr(obj); |
---|
1541 | n/a | if (ident == NULL) |
---|
1542 | n/a | return -1; |
---|
1543 | n/a | has_key = PyDict_Contains(s->markers, ident); |
---|
1544 | n/a | if (has_key) { |
---|
1545 | n/a | if (has_key != -1) |
---|
1546 | n/a | PyErr_SetString(PyExc_ValueError, "Circular reference detected"); |
---|
1547 | n/a | Py_DECREF(ident); |
---|
1548 | n/a | return -1; |
---|
1549 | n/a | } |
---|
1550 | n/a | if (PyDict_SetItem(s->markers, ident, obj)) { |
---|
1551 | n/a | Py_DECREF(ident); |
---|
1552 | n/a | return -1; |
---|
1553 | n/a | } |
---|
1554 | n/a | } |
---|
1555 | n/a | newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL); |
---|
1556 | n/a | if (newobj == NULL) { |
---|
1557 | n/a | Py_XDECREF(ident); |
---|
1558 | n/a | return -1; |
---|
1559 | n/a | } |
---|
1560 | n/a | |
---|
1561 | n/a | if (Py_EnterRecursiveCall(" while encoding a JSON object")) { |
---|
1562 | n/a | Py_DECREF(newobj); |
---|
1563 | n/a | Py_XDECREF(ident); |
---|
1564 | n/a | return -1; |
---|
1565 | n/a | } |
---|
1566 | n/a | rv = encoder_listencode_obj(s, acc, newobj, indent_level); |
---|
1567 | n/a | Py_LeaveRecursiveCall(); |
---|
1568 | n/a | |
---|
1569 | n/a | Py_DECREF(newobj); |
---|
1570 | n/a | if (rv) { |
---|
1571 | n/a | Py_XDECREF(ident); |
---|
1572 | n/a | return -1; |
---|
1573 | n/a | } |
---|
1574 | n/a | if (ident != NULL) { |
---|
1575 | n/a | if (PyDict_DelItem(s->markers, ident)) { |
---|
1576 | n/a | Py_XDECREF(ident); |
---|
1577 | n/a | return -1; |
---|
1578 | n/a | } |
---|
1579 | n/a | Py_XDECREF(ident); |
---|
1580 | n/a | } |
---|
1581 | n/a | return rv; |
---|
1582 | n/a | } |
---|
1583 | n/a | } |
---|
1584 | n/a | |
---|
1585 | n/a | static int |
---|
1586 | n/a | encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, |
---|
1587 | n/a | PyObject *dct, Py_ssize_t indent_level) |
---|
1588 | n/a | { |
---|
1589 | n/a | /* Encode Python dict dct a JSON term */ |
---|
1590 | n/a | static PyObject *open_dict = NULL; |
---|
1591 | n/a | static PyObject *close_dict = NULL; |
---|
1592 | n/a | static PyObject *empty_dict = NULL; |
---|
1593 | n/a | PyObject *kstr = NULL; |
---|
1594 | n/a | PyObject *ident = NULL; |
---|
1595 | n/a | PyObject *it = NULL; |
---|
1596 | n/a | PyObject *items; |
---|
1597 | n/a | PyObject *item = NULL; |
---|
1598 | n/a | int skipkeys; |
---|
1599 | n/a | int sortkeys; |
---|
1600 | n/a | Py_ssize_t idx; |
---|
1601 | n/a | |
---|
1602 | n/a | if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) { |
---|
1603 | n/a | open_dict = PyUnicode_InternFromString("{"); |
---|
1604 | n/a | close_dict = PyUnicode_InternFromString("}"); |
---|
1605 | n/a | empty_dict = PyUnicode_InternFromString("{}"); |
---|
1606 | n/a | if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) |
---|
1607 | n/a | return -1; |
---|
1608 | n/a | } |
---|
1609 | n/a | if (PyDict_GET_SIZE(dct) == 0) /* Fast path */ |
---|
1610 | n/a | return _PyAccu_Accumulate(acc, empty_dict); |
---|
1611 | n/a | |
---|
1612 | n/a | if (s->markers != Py_None) { |
---|
1613 | n/a | int has_key; |
---|
1614 | n/a | ident = PyLong_FromVoidPtr(dct); |
---|
1615 | n/a | if (ident == NULL) |
---|
1616 | n/a | goto bail; |
---|
1617 | n/a | has_key = PyDict_Contains(s->markers, ident); |
---|
1618 | n/a | if (has_key) { |
---|
1619 | n/a | if (has_key != -1) |
---|
1620 | n/a | PyErr_SetString(PyExc_ValueError, "Circular reference detected"); |
---|
1621 | n/a | goto bail; |
---|
1622 | n/a | } |
---|
1623 | n/a | if (PyDict_SetItem(s->markers, ident, dct)) { |
---|
1624 | n/a | goto bail; |
---|
1625 | n/a | } |
---|
1626 | n/a | } |
---|
1627 | n/a | |
---|
1628 | n/a | if (_PyAccu_Accumulate(acc, open_dict)) |
---|
1629 | n/a | goto bail; |
---|
1630 | n/a | |
---|
1631 | n/a | if (s->indent != Py_None) { |
---|
1632 | n/a | /* TODO: DOES NOT RUN */ |
---|
1633 | n/a | indent_level += 1; |
---|
1634 | n/a | /* |
---|
1635 | n/a | newline_indent = '\n' + (' ' * (_indent * _current_indent_level)) |
---|
1636 | n/a | separator = _item_separator + newline_indent |
---|
1637 | n/a | buf += newline_indent |
---|
1638 | n/a | */ |
---|
1639 | n/a | } |
---|
1640 | n/a | |
---|
1641 | n/a | items = PyMapping_Items(dct); |
---|
1642 | n/a | if (items == NULL) |
---|
1643 | n/a | goto bail; |
---|
1644 | n/a | sortkeys = PyObject_IsTrue(s->sort_keys); |
---|
1645 | n/a | if (sortkeys < 0 || (sortkeys && PyList_Sort(items) < 0)) |
---|
1646 | n/a | goto bail; |
---|
1647 | n/a | it = PyObject_GetIter(items); |
---|
1648 | n/a | Py_DECREF(items); |
---|
1649 | n/a | if (it == NULL) |
---|
1650 | n/a | goto bail; |
---|
1651 | n/a | skipkeys = PyObject_IsTrue(s->skipkeys); |
---|
1652 | n/a | if (skipkeys < 0) |
---|
1653 | n/a | goto bail; |
---|
1654 | n/a | idx = 0; |
---|
1655 | n/a | while ((item = PyIter_Next(it)) != NULL) { |
---|
1656 | n/a | PyObject *encoded, *key, *value; |
---|
1657 | n/a | if (!PyTuple_Check(item) || Py_SIZE(item) != 2) { |
---|
1658 | n/a | PyErr_SetString(PyExc_ValueError, "items must return 2-tuples"); |
---|
1659 | n/a | goto bail; |
---|
1660 | n/a | } |
---|
1661 | n/a | key = PyTuple_GET_ITEM(item, 0); |
---|
1662 | n/a | if (PyUnicode_Check(key)) { |
---|
1663 | n/a | Py_INCREF(key); |
---|
1664 | n/a | kstr = key; |
---|
1665 | n/a | } |
---|
1666 | n/a | else if (PyFloat_Check(key)) { |
---|
1667 | n/a | kstr = encoder_encode_float(s, key); |
---|
1668 | n/a | if (kstr == NULL) |
---|
1669 | n/a | goto bail; |
---|
1670 | n/a | } |
---|
1671 | n/a | else if (key == Py_True || key == Py_False || key == Py_None) { |
---|
1672 | n/a | /* This must come before the PyLong_Check because |
---|
1673 | n/a | True and False are also 1 and 0.*/ |
---|
1674 | n/a | kstr = _encoded_const(key); |
---|
1675 | n/a | if (kstr == NULL) |
---|
1676 | n/a | goto bail; |
---|
1677 | n/a | } |
---|
1678 | n/a | else if (PyLong_Check(key)) { |
---|
1679 | n/a | kstr = PyLong_Type.tp_str(key); |
---|
1680 | n/a | if (kstr == NULL) { |
---|
1681 | n/a | goto bail; |
---|
1682 | n/a | } |
---|
1683 | n/a | } |
---|
1684 | n/a | else if (skipkeys) { |
---|
1685 | n/a | Py_DECREF(item); |
---|
1686 | n/a | continue; |
---|
1687 | n/a | } |
---|
1688 | n/a | else { |
---|
1689 | n/a | /* TODO: include repr of key */ |
---|
1690 | n/a | PyErr_SetString(PyExc_TypeError, "keys must be a string"); |
---|
1691 | n/a | goto bail; |
---|
1692 | n/a | } |
---|
1693 | n/a | |
---|
1694 | n/a | if (idx) { |
---|
1695 | n/a | if (_PyAccu_Accumulate(acc, s->item_separator)) |
---|
1696 | n/a | goto bail; |
---|
1697 | n/a | } |
---|
1698 | n/a | |
---|
1699 | n/a | encoded = encoder_encode_string(s, kstr); |
---|
1700 | n/a | Py_CLEAR(kstr); |
---|
1701 | n/a | if (encoded == NULL) |
---|
1702 | n/a | goto bail; |
---|
1703 | n/a | if (_PyAccu_Accumulate(acc, encoded)) { |
---|
1704 | n/a | Py_DECREF(encoded); |
---|
1705 | n/a | goto bail; |
---|
1706 | n/a | } |
---|
1707 | n/a | Py_DECREF(encoded); |
---|
1708 | n/a | if (_PyAccu_Accumulate(acc, s->key_separator)) |
---|
1709 | n/a | goto bail; |
---|
1710 | n/a | |
---|
1711 | n/a | value = PyTuple_GET_ITEM(item, 1); |
---|
1712 | n/a | if (encoder_listencode_obj(s, acc, value, indent_level)) |
---|
1713 | n/a | goto bail; |
---|
1714 | n/a | idx += 1; |
---|
1715 | n/a | Py_DECREF(item); |
---|
1716 | n/a | } |
---|
1717 | n/a | if (PyErr_Occurred()) |
---|
1718 | n/a | goto bail; |
---|
1719 | n/a | Py_CLEAR(it); |
---|
1720 | n/a | |
---|
1721 | n/a | if (ident != NULL) { |
---|
1722 | n/a | if (PyDict_DelItem(s->markers, ident)) |
---|
1723 | n/a | goto bail; |
---|
1724 | n/a | Py_CLEAR(ident); |
---|
1725 | n/a | } |
---|
1726 | n/a | /* TODO DOES NOT RUN; dead code |
---|
1727 | n/a | if (s->indent != Py_None) { |
---|
1728 | n/a | indent_level -= 1; |
---|
1729 | n/a | |
---|
1730 | n/a | yield '\n' + (' ' * (_indent * _current_indent_level)) |
---|
1731 | n/a | }*/ |
---|
1732 | n/a | if (_PyAccu_Accumulate(acc, close_dict)) |
---|
1733 | n/a | goto bail; |
---|
1734 | n/a | return 0; |
---|
1735 | n/a | |
---|
1736 | n/a | bail: |
---|
1737 | n/a | Py_XDECREF(it); |
---|
1738 | n/a | Py_XDECREF(item); |
---|
1739 | n/a | Py_XDECREF(kstr); |
---|
1740 | n/a | Py_XDECREF(ident); |
---|
1741 | n/a | return -1; |
---|
1742 | n/a | } |
---|
1743 | n/a | |
---|
1744 | n/a | |
---|
1745 | n/a | static int |
---|
1746 | n/a | encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc, |
---|
1747 | n/a | PyObject *seq, Py_ssize_t indent_level) |
---|
1748 | n/a | { |
---|
1749 | n/a | /* Encode Python list seq to a JSON term */ |
---|
1750 | n/a | static PyObject *open_array = NULL; |
---|
1751 | n/a | static PyObject *close_array = NULL; |
---|
1752 | n/a | static PyObject *empty_array = NULL; |
---|
1753 | n/a | PyObject *ident = NULL; |
---|
1754 | n/a | PyObject *s_fast = NULL; |
---|
1755 | n/a | Py_ssize_t i; |
---|
1756 | n/a | |
---|
1757 | n/a | if (open_array == NULL || close_array == NULL || empty_array == NULL) { |
---|
1758 | n/a | open_array = PyUnicode_InternFromString("["); |
---|
1759 | n/a | close_array = PyUnicode_InternFromString("]"); |
---|
1760 | n/a | empty_array = PyUnicode_InternFromString("[]"); |
---|
1761 | n/a | if (open_array == NULL || close_array == NULL || empty_array == NULL) |
---|
1762 | n/a | return -1; |
---|
1763 | n/a | } |
---|
1764 | n/a | ident = NULL; |
---|
1765 | n/a | s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence"); |
---|
1766 | n/a | if (s_fast == NULL) |
---|
1767 | n/a | return -1; |
---|
1768 | n/a | if (PySequence_Fast_GET_SIZE(s_fast) == 0) { |
---|
1769 | n/a | Py_DECREF(s_fast); |
---|
1770 | n/a | return _PyAccu_Accumulate(acc, empty_array); |
---|
1771 | n/a | } |
---|
1772 | n/a | |
---|
1773 | n/a | if (s->markers != Py_None) { |
---|
1774 | n/a | int has_key; |
---|
1775 | n/a | ident = PyLong_FromVoidPtr(seq); |
---|
1776 | n/a | if (ident == NULL) |
---|
1777 | n/a | goto bail; |
---|
1778 | n/a | has_key = PyDict_Contains(s->markers, ident); |
---|
1779 | n/a | if (has_key) { |
---|
1780 | n/a | if (has_key != -1) |
---|
1781 | n/a | PyErr_SetString(PyExc_ValueError, "Circular reference detected"); |
---|
1782 | n/a | goto bail; |
---|
1783 | n/a | } |
---|
1784 | n/a | if (PyDict_SetItem(s->markers, ident, seq)) { |
---|
1785 | n/a | goto bail; |
---|
1786 | n/a | } |
---|
1787 | n/a | } |
---|
1788 | n/a | |
---|
1789 | n/a | if (_PyAccu_Accumulate(acc, open_array)) |
---|
1790 | n/a | goto bail; |
---|
1791 | n/a | if (s->indent != Py_None) { |
---|
1792 | n/a | /* TODO: DOES NOT RUN */ |
---|
1793 | n/a | indent_level += 1; |
---|
1794 | n/a | /* |
---|
1795 | n/a | newline_indent = '\n' + (' ' * (_indent * _current_indent_level)) |
---|
1796 | n/a | separator = _item_separator + newline_indent |
---|
1797 | n/a | buf += newline_indent |
---|
1798 | n/a | */ |
---|
1799 | n/a | } |
---|
1800 | n/a | for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) { |
---|
1801 | n/a | PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i); |
---|
1802 | n/a | if (i) { |
---|
1803 | n/a | if (_PyAccu_Accumulate(acc, s->item_separator)) |
---|
1804 | n/a | goto bail; |
---|
1805 | n/a | } |
---|
1806 | n/a | if (encoder_listencode_obj(s, acc, obj, indent_level)) |
---|
1807 | n/a | goto bail; |
---|
1808 | n/a | } |
---|
1809 | n/a | if (ident != NULL) { |
---|
1810 | n/a | if (PyDict_DelItem(s->markers, ident)) |
---|
1811 | n/a | goto bail; |
---|
1812 | n/a | Py_CLEAR(ident); |
---|
1813 | n/a | } |
---|
1814 | n/a | |
---|
1815 | n/a | /* TODO: DOES NOT RUN |
---|
1816 | n/a | if (s->indent != Py_None) { |
---|
1817 | n/a | indent_level -= 1; |
---|
1818 | n/a | |
---|
1819 | n/a | yield '\n' + (' ' * (_indent * _current_indent_level)) |
---|
1820 | n/a | }*/ |
---|
1821 | n/a | if (_PyAccu_Accumulate(acc, close_array)) |
---|
1822 | n/a | goto bail; |
---|
1823 | n/a | Py_DECREF(s_fast); |
---|
1824 | n/a | return 0; |
---|
1825 | n/a | |
---|
1826 | n/a | bail: |
---|
1827 | n/a | Py_XDECREF(ident); |
---|
1828 | n/a | Py_DECREF(s_fast); |
---|
1829 | n/a | return -1; |
---|
1830 | n/a | } |
---|
1831 | n/a | |
---|
1832 | n/a | static void |
---|
1833 | n/a | encoder_dealloc(PyObject *self) |
---|
1834 | n/a | { |
---|
1835 | n/a | /* Deallocate Encoder */ |
---|
1836 | n/a | encoder_clear(self); |
---|
1837 | n/a | Py_TYPE(self)->tp_free(self); |
---|
1838 | n/a | } |
---|
1839 | n/a | |
---|
1840 | n/a | static int |
---|
1841 | n/a | encoder_traverse(PyObject *self, visitproc visit, void *arg) |
---|
1842 | n/a | { |
---|
1843 | n/a | PyEncoderObject *s; |
---|
1844 | n/a | assert(PyEncoder_Check(self)); |
---|
1845 | n/a | s = (PyEncoderObject *)self; |
---|
1846 | n/a | Py_VISIT(s->markers); |
---|
1847 | n/a | Py_VISIT(s->defaultfn); |
---|
1848 | n/a | Py_VISIT(s->encoder); |
---|
1849 | n/a | Py_VISIT(s->indent); |
---|
1850 | n/a | Py_VISIT(s->key_separator); |
---|
1851 | n/a | Py_VISIT(s->item_separator); |
---|
1852 | n/a | Py_VISIT(s->sort_keys); |
---|
1853 | n/a | Py_VISIT(s->skipkeys); |
---|
1854 | n/a | return 0; |
---|
1855 | n/a | } |
---|
1856 | n/a | |
---|
1857 | n/a | static int |
---|
1858 | n/a | encoder_clear(PyObject *self) |
---|
1859 | n/a | { |
---|
1860 | n/a | /* Deallocate Encoder */ |
---|
1861 | n/a | PyEncoderObject *s; |
---|
1862 | n/a | assert(PyEncoder_Check(self)); |
---|
1863 | n/a | s = (PyEncoderObject *)self; |
---|
1864 | n/a | Py_CLEAR(s->markers); |
---|
1865 | n/a | Py_CLEAR(s->defaultfn); |
---|
1866 | n/a | Py_CLEAR(s->encoder); |
---|
1867 | n/a | Py_CLEAR(s->indent); |
---|
1868 | n/a | Py_CLEAR(s->key_separator); |
---|
1869 | n/a | Py_CLEAR(s->item_separator); |
---|
1870 | n/a | Py_CLEAR(s->sort_keys); |
---|
1871 | n/a | Py_CLEAR(s->skipkeys); |
---|
1872 | n/a | return 0; |
---|
1873 | n/a | } |
---|
1874 | n/a | |
---|
1875 | n/a | PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable"); |
---|
1876 | n/a | |
---|
1877 | n/a | static |
---|
1878 | n/a | PyTypeObject PyEncoderType = { |
---|
1879 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
1880 | n/a | "_json.Encoder", /* tp_name */ |
---|
1881 | n/a | sizeof(PyEncoderObject), /* tp_basicsize */ |
---|
1882 | n/a | 0, /* tp_itemsize */ |
---|
1883 | n/a | encoder_dealloc, /* tp_dealloc */ |
---|
1884 | n/a | 0, /* tp_print */ |
---|
1885 | n/a | 0, /* tp_getattr */ |
---|
1886 | n/a | 0, /* tp_setattr */ |
---|
1887 | n/a | 0, /* tp_compare */ |
---|
1888 | n/a | 0, /* tp_repr */ |
---|
1889 | n/a | 0, /* tp_as_number */ |
---|
1890 | n/a | 0, /* tp_as_sequence */ |
---|
1891 | n/a | 0, /* tp_as_mapping */ |
---|
1892 | n/a | 0, /* tp_hash */ |
---|
1893 | n/a | encoder_call, /* tp_call */ |
---|
1894 | n/a | 0, /* tp_str */ |
---|
1895 | n/a | 0, /* tp_getattro */ |
---|
1896 | n/a | 0, /* tp_setattro */ |
---|
1897 | n/a | 0, /* tp_as_buffer */ |
---|
1898 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
---|
1899 | n/a | encoder_doc, /* tp_doc */ |
---|
1900 | n/a | encoder_traverse, /* tp_traverse */ |
---|
1901 | n/a | encoder_clear, /* tp_clear */ |
---|
1902 | n/a | 0, /* tp_richcompare */ |
---|
1903 | n/a | 0, /* tp_weaklistoffset */ |
---|
1904 | n/a | 0, /* tp_iter */ |
---|
1905 | n/a | 0, /* tp_iternext */ |
---|
1906 | n/a | 0, /* tp_methods */ |
---|
1907 | n/a | encoder_members, /* tp_members */ |
---|
1908 | n/a | 0, /* tp_getset */ |
---|
1909 | n/a | 0, /* tp_base */ |
---|
1910 | n/a | 0, /* tp_dict */ |
---|
1911 | n/a | 0, /* tp_descr_get */ |
---|
1912 | n/a | 0, /* tp_descr_set */ |
---|
1913 | n/a | 0, /* tp_dictoffset */ |
---|
1914 | n/a | encoder_init, /* tp_init */ |
---|
1915 | n/a | 0, /* tp_alloc */ |
---|
1916 | n/a | encoder_new, /* tp_new */ |
---|
1917 | n/a | 0, /* tp_free */ |
---|
1918 | n/a | }; |
---|
1919 | n/a | |
---|
1920 | n/a | static PyMethodDef speedups_methods[] = { |
---|
1921 | n/a | {"encode_basestring_ascii", |
---|
1922 | n/a | (PyCFunction)py_encode_basestring_ascii, |
---|
1923 | n/a | METH_O, |
---|
1924 | n/a | pydoc_encode_basestring_ascii}, |
---|
1925 | n/a | {"encode_basestring", |
---|
1926 | n/a | (PyCFunction)py_encode_basestring, |
---|
1927 | n/a | METH_O, |
---|
1928 | n/a | pydoc_encode_basestring}, |
---|
1929 | n/a | {"scanstring", |
---|
1930 | n/a | (PyCFunction)py_scanstring, |
---|
1931 | n/a | METH_VARARGS, |
---|
1932 | n/a | pydoc_scanstring}, |
---|
1933 | n/a | {NULL, NULL, 0, NULL} |
---|
1934 | n/a | }; |
---|
1935 | n/a | |
---|
1936 | n/a | PyDoc_STRVAR(module_doc, |
---|
1937 | n/a | "json speedups\n"); |
---|
1938 | n/a | |
---|
1939 | n/a | static struct PyModuleDef jsonmodule = { |
---|
1940 | n/a | PyModuleDef_HEAD_INIT, |
---|
1941 | n/a | "_json", |
---|
1942 | n/a | module_doc, |
---|
1943 | n/a | -1, |
---|
1944 | n/a | speedups_methods, |
---|
1945 | n/a | NULL, |
---|
1946 | n/a | NULL, |
---|
1947 | n/a | NULL, |
---|
1948 | n/a | NULL |
---|
1949 | n/a | }; |
---|
1950 | n/a | |
---|
1951 | n/a | PyMODINIT_FUNC |
---|
1952 | n/a | PyInit__json(void) |
---|
1953 | n/a | { |
---|
1954 | n/a | PyObject *m = PyModule_Create(&jsonmodule); |
---|
1955 | n/a | if (!m) |
---|
1956 | n/a | return NULL; |
---|
1957 | n/a | PyScannerType.tp_new = PyType_GenericNew; |
---|
1958 | n/a | if (PyType_Ready(&PyScannerType) < 0) |
---|
1959 | n/a | goto fail; |
---|
1960 | n/a | PyEncoderType.tp_new = PyType_GenericNew; |
---|
1961 | n/a | if (PyType_Ready(&PyEncoderType) < 0) |
---|
1962 | n/a | goto fail; |
---|
1963 | n/a | Py_INCREF((PyObject*)&PyScannerType); |
---|
1964 | n/a | if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) { |
---|
1965 | n/a | Py_DECREF((PyObject*)&PyScannerType); |
---|
1966 | n/a | goto fail; |
---|
1967 | n/a | } |
---|
1968 | n/a | Py_INCREF((PyObject*)&PyEncoderType); |
---|
1969 | n/a | if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) { |
---|
1970 | n/a | Py_DECREF((PyObject*)&PyEncoderType); |
---|
1971 | n/a | goto fail; |
---|
1972 | n/a | } |
---|
1973 | n/a | return m; |
---|
1974 | n/a | fail: |
---|
1975 | n/a | Py_DECREF(m); |
---|
1976 | n/a | return NULL; |
---|
1977 | n/a | } |
---|