ยปCore Development>Code coverage>Modules/_json.c

Python code coverage for Modules/_json.c

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