2 #include "structmember.h"
3 #if PY_VERSION_HEX < 0x02060000 && !defined(Py_TYPE)
4 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
6 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
7 typedef int Py_ssize_t
;
8 #define PY_SSIZE_T_MAX INT_MAX
9 #define PY_SSIZE_T_MIN INT_MIN
10 #define PyInt_FromSsize_t PyInt_FromLong
11 #define PyInt_AsSsize_t PyInt_AsLong
14 #define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X))
18 #define UNUSED __attribute__((__unused__))
23 #define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType)
24 #define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType)
25 #define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType)
26 #define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType)
28 static PyTypeObject PyScannerType
;
29 static PyTypeObject PyEncoderType
;
31 typedef struct _PyScannerObject
{
34 PyObject
*object_hook
;
35 PyObject
*object_pairs_hook
;
36 PyObject
*parse_float
;
38 PyObject
*parse_constant
;
41 static PyMemberDef scanner_members
[] = {
42 {"strict", T_OBJECT
, offsetof(PyScannerObject
, strict
), READONLY
, "strict"},
43 {"object_hook", T_OBJECT
, offsetof(PyScannerObject
, object_hook
), READONLY
, "object_hook"},
44 {"object_pairs_hook", T_OBJECT
, offsetof(PyScannerObject
, object_pairs_hook
), READONLY
},
45 {"parse_float", T_OBJECT
, offsetof(PyScannerObject
, parse_float
), READONLY
, "parse_float"},
46 {"parse_int", T_OBJECT
, offsetof(PyScannerObject
, parse_int
), READONLY
, "parse_int"},
47 {"parse_constant", T_OBJECT
, offsetof(PyScannerObject
, parse_constant
), READONLY
, "parse_constant"},
51 typedef struct _PyEncoderObject
{
57 PyObject
*key_separator
;
58 PyObject
*item_separator
;
65 static PyMemberDef encoder_members
[] = {
66 {"markers", T_OBJECT
, offsetof(PyEncoderObject
, markers
), READONLY
, "markers"},
67 {"default", T_OBJECT
, offsetof(PyEncoderObject
, defaultfn
), READONLY
, "default"},
68 {"encoder", T_OBJECT
, offsetof(PyEncoderObject
, encoder
), READONLY
, "encoder"},
69 {"indent", T_OBJECT
, offsetof(PyEncoderObject
, indent
), READONLY
, "indent"},
70 {"key_separator", T_OBJECT
, offsetof(PyEncoderObject
, key_separator
), READONLY
, "key_separator"},
71 {"item_separator", T_OBJECT
, offsetof(PyEncoderObject
, item_separator
), READONLY
, "item_separator"},
72 {"sort_keys", T_OBJECT
, offsetof(PyEncoderObject
, sort_keys
), READONLY
, "sort_keys"},
73 {"skipkeys", T_OBJECT
, offsetof(PyEncoderObject
, skipkeys
), READONLY
, "skipkeys"},
78 ascii_escape_unicode(PyObject
*pystr
);
80 py_encode_basestring_ascii(PyObject
* self UNUSED
, PyObject
*pystr
);
83 scan_once_unicode(PyScannerObject
*s
, PyObject
*pystr
, Py_ssize_t idx
, Py_ssize_t
*next_idx_ptr
);
85 _build_rval_index_tuple(PyObject
*rval
, Py_ssize_t idx
);
87 scanner_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
);
89 scanner_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
);
91 scanner_dealloc(PyObject
*self
);
93 scanner_clear(PyObject
*self
);
95 encoder_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
);
97 encoder_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
);
99 encoder_dealloc(PyObject
*self
);
101 encoder_clear(PyObject
*self
);
103 encoder_listencode_list(PyEncoderObject
*s
, PyObject
*rval
, PyObject
*seq
, Py_ssize_t indent_level
);
105 encoder_listencode_obj(PyEncoderObject
*s
, PyObject
*rval
, PyObject
*obj
, Py_ssize_t indent_level
);
107 encoder_listencode_dict(PyEncoderObject
*s
, PyObject
*rval
, PyObject
*dct
, Py_ssize_t indent_level
);
109 _encoded_const(PyObject
*obj
);
111 raise_errmsg(char *msg
, PyObject
*s
, Py_ssize_t end
);
113 encoder_encode_string(PyEncoderObject
*s
, PyObject
*obj
);
115 _convertPyInt_AsSsize_t(PyObject
*o
, Py_ssize_t
*size_ptr
);
117 _convertPyInt_FromSsize_t(Py_ssize_t
*size_ptr
);
119 encoder_encode_float(PyEncoderObject
*s
, PyObject
*obj
);
121 #define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
122 #define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
124 #define MIN_EXPANSION 6
125 #ifdef Py_UNICODE_WIDE
126 #define MAX_EXPANSION (2 * MIN_EXPANSION)
128 #define MAX_EXPANSION MIN_EXPANSION
132 _convertPyInt_AsSsize_t(PyObject
*o
, Py_ssize_t
*size_ptr
)
134 /* PyObject to Py_ssize_t converter */
135 *size_ptr
= PyLong_AsSsize_t(o
);
136 if (*size_ptr
== -1 && PyErr_Occurred())
142 _convertPyInt_FromSsize_t(Py_ssize_t
*size_ptr
)
144 /* Py_ssize_t to PyObject converter */
145 return PyLong_FromSsize_t(*size_ptr
);
149 ascii_escape_unichar(Py_UNICODE c
, Py_UNICODE
*output
, Py_ssize_t chars
)
151 /* Escape unicode code point c to ASCII escape sequences
152 in char *output. output must have at least 12 bytes unused to
153 accommodate an escaped surrogate pair "\uXXXX\uXXXX" */
154 output
[chars
++] = '\\';
156 case '\\': output
[chars
++] = c
; break;
157 case '"': output
[chars
++] = c
; break;
158 case '\b': output
[chars
++] = 'b'; break;
159 case '\f': output
[chars
++] = 'f'; break;
160 case '\n': output
[chars
++] = 'n'; break;
161 case '\r': output
[chars
++] = 'r'; break;
162 case '\t': output
[chars
++] = 't'; break;
164 #ifdef Py_UNICODE_WIDE
166 /* UTF-16 surrogate pair */
167 Py_UNICODE v
= c
- 0x10000;
168 c
= 0xd800 | ((v
>> 10) & 0x3ff);
169 output
[chars
++] = 'u';
170 output
[chars
++] = "0123456789abcdef"[(c
>> 12) & 0xf];
171 output
[chars
++] = "0123456789abcdef"[(c
>> 8) & 0xf];
172 output
[chars
++] = "0123456789abcdef"[(c
>> 4) & 0xf];
173 output
[chars
++] = "0123456789abcdef"[(c
) & 0xf];
174 c
= 0xdc00 | (v
& 0x3ff);
175 output
[chars
++] = '\\';
178 output
[chars
++] = 'u';
179 output
[chars
++] = "0123456789abcdef"[(c
>> 12) & 0xf];
180 output
[chars
++] = "0123456789abcdef"[(c
>> 8) & 0xf];
181 output
[chars
++] = "0123456789abcdef"[(c
>> 4) & 0xf];
182 output
[chars
++] = "0123456789abcdef"[(c
) & 0xf];
188 ascii_escape_unicode(PyObject
*pystr
)
190 /* Take a PyUnicode pystr and return a new ASCII-only escaped PyUnicode */
192 Py_ssize_t input_chars
;
193 Py_ssize_t output_size
;
194 Py_ssize_t max_output_size
;
198 Py_UNICODE
*input_unicode
;
200 input_chars
= PyUnicode_GET_SIZE(pystr
);
201 input_unicode
= PyUnicode_AS_UNICODE(pystr
);
203 /* One char input can be up to 6 chars output, estimate 4 of these */
204 output_size
= 2 + (MIN_EXPANSION
* 4) + input_chars
;
205 max_output_size
= 2 + (input_chars
* MAX_EXPANSION
);
206 rval
= PyUnicode_FromStringAndSize(NULL
, output_size
);
210 output
= PyUnicode_AS_UNICODE(rval
);
212 output
[chars
++] = '"';
213 for (i
= 0; i
< input_chars
; i
++) {
214 Py_UNICODE c
= input_unicode
[i
];
219 chars
= ascii_escape_unichar(c
, output
, chars
);
221 if (output_size
- chars
< (1 + MAX_EXPANSION
)) {
222 /* There's more than four, so let's resize by a lot */
223 Py_ssize_t new_output_size
= output_size
* 2;
224 /* This is an upper bound */
225 if (new_output_size
> max_output_size
) {
226 new_output_size
= max_output_size
;
228 /* Make sure that the output size changed before resizing */
229 if (new_output_size
!= output_size
) {
230 output_size
= new_output_size
;
231 if (PyUnicode_Resize(&rval
, output_size
) == -1) {
234 output
= PyUnicode_AS_UNICODE(rval
);
238 output
[chars
++] = '"';
239 if (PyUnicode_Resize(&rval
, chars
) == -1) {
246 raise_errmsg(char *msg
, PyObject
*s
, Py_ssize_t end
)
248 /* Use the Python function json.decoder.errmsg to raise a nice
249 looking ValueError exception */
250 static PyObject
*errmsg_fn
= NULL
;
252 if (errmsg_fn
== NULL
) {
253 PyObject
*decoder
= PyImport_ImportModule("json.decoder");
256 errmsg_fn
= PyObject_GetAttrString(decoder
, "errmsg");
258 if (errmsg_fn
== NULL
)
261 pymsg
= PyObject_CallFunction(errmsg_fn
, "(zOO&)", msg
, s
, _convertPyInt_FromSsize_t
, &end
);
263 PyErr_SetObject(PyExc_ValueError
, pymsg
);
269 join_list_unicode(PyObject
*lst
)
271 /* return u''.join(lst) */
272 static PyObject
*sep
= NULL
;
274 sep
= PyUnicode_FromStringAndSize("", 0);
278 return PyUnicode_Join(sep
, lst
);
282 _build_rval_index_tuple(PyObject
*rval
, Py_ssize_t idx
) {
283 /* return (rval, idx) tuple, stealing reference to rval */
287 steal a reference to rval, returns (rval, idx)
292 pyidx
= PyLong_FromSsize_t(idx
);
297 tpl
= PyTuple_New(2);
303 PyTuple_SET_ITEM(tpl
, 0, rval
);
304 PyTuple_SET_ITEM(tpl
, 1, pyidx
);
309 scanstring_unicode(PyObject
*pystr
, Py_ssize_t end
, int strict
, Py_ssize_t
*next_end_ptr
)
311 /* Read the JSON string from PyUnicode pystr.
312 end is the index of the first character after the quote.
313 if strict is zero then literal control characters are allowed
314 *next_end_ptr is a return-by-reference index of the character
317 Return value is a new PyUnicode
320 Py_ssize_t len
= PyUnicode_GET_SIZE(pystr
);
321 Py_ssize_t begin
= end
- 1;
322 Py_ssize_t next
= begin
;
323 const Py_UNICODE
*buf
= PyUnicode_AS_UNICODE(pystr
);
324 PyObject
*chunks
= PyList_New(0);
325 if (chunks
== NULL
) {
328 if (end
< 0 || len
<= end
) {
329 PyErr_SetString(PyExc_ValueError
, "end is out of bounds");
333 /* Find the end of the string or the next escape */
335 PyObject
*chunk
= NULL
;
336 for (next
= end
; next
< len
; next
++) {
338 if (c
== '"' || c
== '\\') {
341 else if (strict
&& c
<= 0x1f) {
342 raise_errmsg("Invalid control character at", pystr
, next
);
346 if (!(c
== '"' || c
== '\\')) {
347 raise_errmsg("Unterminated string starting at", pystr
, begin
);
350 /* Pick up this chunk if it's not zero length */
352 chunk
= PyUnicode_FromUnicode(&buf
[end
], next
- end
);
356 if (PyList_Append(chunks
, chunk
)) {
368 raise_errmsg("Unterminated string starting at", pystr
, begin
);
373 /* Non-unicode backslash escapes */
379 case 'b': c
= '\b'; break;
380 case 'f': c
= '\f'; break;
381 case 'n': c
= '\n'; break;
382 case 'r': c
= '\r'; break;
383 case 't': c
= '\t'; break;
387 raise_errmsg("Invalid \\escape", pystr
, end
- 2);
396 raise_errmsg("Invalid \\uXXXX escape", pystr
, next
- 1);
399 /* Decode 4 hex digits */
400 for (; next
< end
; next
++) {
401 Py_UNICODE digit
= buf
[next
];
404 case '0': case '1': case '2': case '3': case '4':
405 case '5': case '6': case '7': case '8': case '9':
406 c
|= (digit
- '0'); break;
407 case 'a': case 'b': case 'c': case 'd': case 'e':
409 c
|= (digit
- 'a' + 10); break;
410 case 'A': case 'B': case 'C': case 'D': case 'E':
412 c
|= (digit
- 'A' + 10); break;
414 raise_errmsg("Invalid \\uXXXX escape", pystr
, end
- 5);
418 #ifdef Py_UNICODE_WIDE
420 if ((c
& 0xfc00) == 0xd800) {
422 if (end
+ 6 >= len
) {
423 raise_errmsg("Unpaired high surrogate", pystr
, end
- 5);
426 if (buf
[next
++] != '\\' || buf
[next
++] != 'u') {
427 raise_errmsg("Unpaired high surrogate", pystr
, end
- 5);
431 /* Decode 4 hex digits */
432 for (; next
< end
; next
++) {
434 Py_UNICODE digit
= buf
[next
];
436 case '0': case '1': case '2': case '3': case '4':
437 case '5': case '6': case '7': case '8': case '9':
438 c2
|= (digit
- '0'); break;
439 case 'a': case 'b': case 'c': case 'd': case 'e':
441 c2
|= (digit
- 'a' + 10); break;
442 case 'A': case 'B': case 'C': case 'D': case 'E':
444 c2
|= (digit
- 'A' + 10); break;
446 raise_errmsg("Invalid \\uXXXX escape", pystr
, end
- 5);
450 if ((c2
& 0xfc00) != 0xdc00) {
451 raise_errmsg("Unpaired high surrogate", pystr
, end
- 5);
454 c
= 0x10000 + (((c
- 0xd800) << 10) | (c2
- 0xdc00));
456 else if ((c
& 0xfc00) == 0xdc00) {
457 raise_errmsg("Unpaired low surrogate", pystr
, end
- 5);
462 chunk
= PyUnicode_FromUnicode(&c
, 1);
466 if (PyList_Append(chunks
, chunk
)) {
473 rval
= join_list_unicode(chunks
);
486 PyDoc_STRVAR(pydoc_scanstring
,
487 "scanstring(basestring, end, strict=True) -> (bytes, end)\n"
489 "Scan the string s for a JSON string. End is the index of the\n"
490 "character in s after the quote that started the JSON string.\n"
491 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
492 "on attempt to decode an invalid string. If strict is False then literal\n"
493 "control characters are allowed in the string.\n"
495 "Returns a tuple of the decoded string and the index of the character in s\n"
496 "after the end quote."
500 py_scanstring(PyObject
* self UNUSED
, PyObject
*args
)
505 Py_ssize_t next_end
= -1;
507 if (!PyArg_ParseTuple(args
, "OO&|i:scanstring", &pystr
, _convertPyInt_AsSsize_t
, &end
, &strict
)) {
510 if (PyUnicode_Check(pystr
)) {
511 rval
= scanstring_unicode(pystr
, end
, strict
, &next_end
);
514 PyErr_Format(PyExc_TypeError
,
515 "first argument must be a string or bytes, not %.80s",
516 Py_TYPE(pystr
)->tp_name
);
519 return _build_rval_index_tuple(rval
, next_end
);
522 PyDoc_STRVAR(pydoc_encode_basestring_ascii
,
523 "encode_basestring_ascii(basestring) -> bytes\n"
525 "Return an ASCII-only JSON representation of a Python string"
529 py_encode_basestring_ascii(PyObject
* self UNUSED
, PyObject
*pystr
)
532 /* Return an ASCII-only JSON representation of a Python string */
534 if (PyUnicode_Check(pystr
)) {
535 rval
= ascii_escape_unicode(pystr
);
538 PyErr_Format(PyExc_TypeError
,
539 "first argument must be a string, not %.80s",
540 Py_TYPE(pystr
)->tp_name
);
547 scanner_dealloc(PyObject
*self
)
549 /* Deallocate scanner object */
551 Py_TYPE(self
)->tp_free(self
);
555 scanner_traverse(PyObject
*self
, visitproc visit
, void *arg
)
558 assert(PyScanner_Check(self
));
559 s
= (PyScannerObject
*)self
;
561 Py_VISIT(s
->object_hook
);
562 Py_VISIT(s
->object_pairs_hook
);
563 Py_VISIT(s
->parse_float
);
564 Py_VISIT(s
->parse_int
);
565 Py_VISIT(s
->parse_constant
);
570 scanner_clear(PyObject
*self
)
573 assert(PyScanner_Check(self
));
574 s
= (PyScannerObject
*)self
;
576 Py_CLEAR(s
->object_hook
);
577 Py_CLEAR(s
->object_pairs_hook
);
578 Py_CLEAR(s
->parse_float
);
579 Py_CLEAR(s
->parse_int
);
580 Py_CLEAR(s
->parse_constant
);
585 _parse_object_unicode(PyScannerObject
*s
, PyObject
*pystr
, Py_ssize_t idx
, Py_ssize_t
*next_idx_ptr
) {
586 /* Read a JSON object from PyUnicode pystr.
587 idx is the index of the first character after the opening curly brace.
588 *next_idx_ptr is a return-by-reference index to the first character after
589 the closing curly brace.
591 Returns a new PyObject (usually a dict, but object_hook can change that)
593 Py_UNICODE
*str
= PyUnicode_AS_UNICODE(pystr
);
594 Py_ssize_t end_idx
= PyUnicode_GET_SIZE(pystr
) - 1;
595 PyObject
*val
= NULL
;
596 PyObject
*rval
= PyList_New(0);
597 PyObject
*key
= NULL
;
598 int strict
= PyObject_IsTrue(s
->strict
);
603 /* skip whitespace after { */
604 while (idx
<= end_idx
&& IS_WHITESPACE(str
[idx
])) idx
++;
606 /* only loop if the object is non-empty */
607 if (idx
<= end_idx
&& str
[idx
] != '}') {
608 while (idx
<= end_idx
) {
610 if (str
[idx
] != '"') {
611 raise_errmsg("Expecting property name", pystr
, idx
);
614 key
= scanstring_unicode(pystr
, idx
+ 1, strict
, &next_idx
);
619 /* skip whitespace between key and : delimiter, read :, skip whitespace */
620 while (idx
<= end_idx
&& IS_WHITESPACE(str
[idx
])) idx
++;
621 if (idx
> end_idx
|| str
[idx
] != ':') {
622 raise_errmsg("Expecting : delimiter", pystr
, idx
);
626 while (idx
<= end_idx
&& IS_WHITESPACE(str
[idx
])) idx
++;
628 /* read any JSON term */
629 val
= scan_once_unicode(s
, pystr
, idx
, &next_idx
);
634 PyObject
*tuple
= PyTuple_Pack(2, key
, val
);
637 if (PyList_Append(rval
, tuple
) == -1) {
648 /* skip whitespace before } or , */
649 while (idx
<= end_idx
&& IS_WHITESPACE(str
[idx
])) idx
++;
651 /* bail if the object is closed or we didn't get the , delimiter */
652 if (idx
> end_idx
) break;
653 if (str
[idx
] == '}') {
656 else if (str
[idx
] != ',') {
657 raise_errmsg("Expecting , delimiter", pystr
, idx
);
662 /* skip whitespace after , delimiter */
663 while (idx
<= end_idx
&& IS_WHITESPACE(str
[idx
])) idx
++;
667 /* verify that idx < end_idx, str[idx] should be '}' */
668 if (idx
> end_idx
|| str
[idx
] != '}') {
669 raise_errmsg("Expecting object", pystr
, end_idx
);
673 *next_idx_ptr
= idx
+ 1;
675 if (s
->object_pairs_hook
!= Py_None
) {
676 val
= PyObject_CallFunctionObjArgs(s
->object_pairs_hook
, rval
, NULL
);
686 if (PyDict_MergeFromSeq2(val
, rval
, 1) == -1)
691 /* if object_hook is not None: rval = object_hook(rval) */
692 if (s
->object_hook
!= Py_None
) {
693 val
= PyObject_CallFunctionObjArgs(s
->object_hook
, rval
, NULL
);
709 _parse_array_unicode(PyScannerObject
*s
, PyObject
*pystr
, Py_ssize_t idx
, Py_ssize_t
*next_idx_ptr
) {
710 /* Read a JSON array from PyString pystr.
711 idx is the index of the first character after the opening brace.
712 *next_idx_ptr is a return-by-reference index to the first character after
717 Py_UNICODE
*str
= PyUnicode_AS_UNICODE(pystr
);
718 Py_ssize_t end_idx
= PyUnicode_GET_SIZE(pystr
) - 1;
719 PyObject
*val
= NULL
;
720 PyObject
*rval
= PyList_New(0);
725 /* skip whitespace after [ */
726 while (idx
<= end_idx
&& IS_WHITESPACE(str
[idx
])) idx
++;
728 /* only loop if the array is non-empty */
729 if (idx
<= end_idx
&& str
[idx
] != ']') {
730 while (idx
<= end_idx
) {
732 /* read any JSON term */
733 val
= scan_once_unicode(s
, pystr
, idx
, &next_idx
);
737 if (PyList_Append(rval
, val
) == -1)
743 /* skip whitespace between term and , */
744 while (idx
<= end_idx
&& IS_WHITESPACE(str
[idx
])) idx
++;
746 /* bail if the array is closed or we didn't get the , delimiter */
747 if (idx
> end_idx
) break;
748 if (str
[idx
] == ']') {
751 else if (str
[idx
] != ',') {
752 raise_errmsg("Expecting , delimiter", pystr
, idx
);
757 /* skip whitespace after , */
758 while (idx
<= end_idx
&& IS_WHITESPACE(str
[idx
])) idx
++;
762 /* verify that idx < end_idx, str[idx] should be ']' */
763 if (idx
> end_idx
|| str
[idx
] != ']') {
764 raise_errmsg("Expecting object", pystr
, end_idx
);
767 *next_idx_ptr
= idx
+ 1;
776 _parse_constant(PyScannerObject
*s
, char *constant
, Py_ssize_t idx
, Py_ssize_t
*next_idx_ptr
) {
777 /* Read a JSON constant from PyString pystr.
778 constant is the constant string that was found
779 ("NaN", "Infinity", "-Infinity").
780 idx is the index of the first character of the constant
781 *next_idx_ptr is a return-by-reference index to the first character after
784 Returns the result of parse_constant
788 /* constant is "NaN", "Infinity", or "-Infinity" */
789 cstr
= PyUnicode_InternFromString(constant
);
793 /* rval = parse_constant(constant) */
794 rval
= PyObject_CallFunctionObjArgs(s
->parse_constant
, cstr
, NULL
);
795 idx
+= PyUnicode_GET_SIZE(cstr
);
802 _match_number_unicode(PyScannerObject
*s
, PyObject
*pystr
, Py_ssize_t start
, Py_ssize_t
*next_idx_ptr
) {
803 /* Read a JSON number from PyUnicode pystr.
804 idx is the index of the first character of the number
805 *next_idx_ptr is a return-by-reference index to the first character after
808 Returns a new PyObject representation of that number:
809 PyInt, PyLong, or PyFloat.
810 May return other types if parse_int or parse_float are set
812 Py_UNICODE
*str
= PyUnicode_AS_UNICODE(pystr
);
813 Py_ssize_t end_idx
= PyUnicode_GET_SIZE(pystr
) - 1;
814 Py_ssize_t idx
= start
;
819 /* read a sign if it's there, make sure it's not the end of the string */
820 if (str
[idx
] == '-') {
823 PyErr_SetNone(PyExc_StopIteration
);
828 /* read as many integer digits as we find as long as it doesn't start with 0 */
829 if (str
[idx
] >= '1' && str
[idx
] <= '9') {
831 while (idx
<= end_idx
&& str
[idx
] >= '0' && str
[idx
] <= '9') idx
++;
833 /* if it starts with 0 we only expect one integer digit */
834 else if (str
[idx
] == '0') {
837 /* no integer digits, error */
839 PyErr_SetNone(PyExc_StopIteration
);
843 /* if the next char is '.' followed by a digit then read all float digits */
844 if (idx
< end_idx
&& str
[idx
] == '.' && str
[idx
+ 1] >= '0' && str
[idx
+ 1] <= '9') {
847 while (idx
<= end_idx
&& str
[idx
] >= '0' && str
[idx
] <= '9') idx
++;
850 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
851 if (idx
< end_idx
&& (str
[idx
] == 'e' || str
[idx
] == 'E')) {
852 Py_ssize_t e_start
= idx
;
855 /* read an exponent sign if present */
856 if (idx
< end_idx
&& (str
[idx
] == '-' || str
[idx
] == '+')) idx
++;
858 /* read all digits */
859 while (idx
<= end_idx
&& str
[idx
] >= '0' && str
[idx
] <= '9') idx
++;
861 /* if we got a digit, then parse as float. if not, backtrack */
862 if (str
[idx
- 1] >= '0' && str
[idx
- 1] <= '9') {
870 /* copy the section we determined to be a number */
871 numstr
= PyUnicode_FromUnicode(&str
[start
], idx
- start
);
875 /* parse as a float using a fast path if available, otherwise call user defined method */
876 if (s
->parse_float
!= (PyObject
*)&PyFloat_Type
) {
877 rval
= PyObject_CallFunctionObjArgs(s
->parse_float
, numstr
, NULL
);
880 rval
= PyFloat_FromString(numstr
);
884 /* no fast path for unicode -> int, just call */
885 rval
= PyObject_CallFunctionObjArgs(s
->parse_int
, numstr
, NULL
);
893 scan_once_unicode(PyScannerObject
*s
, PyObject
*pystr
, Py_ssize_t idx
, Py_ssize_t
*next_idx_ptr
)
895 /* Read one JSON term (of any kind) from PyUnicode pystr.
896 idx is the index of the first character of the term
897 *next_idx_ptr is a return-by-reference index to the first character after
900 Returns a new PyObject representation of the term.
902 Py_UNICODE
*str
= PyUnicode_AS_UNICODE(pystr
);
903 Py_ssize_t length
= PyUnicode_GET_SIZE(pystr
);
905 PyErr_SetNone(PyExc_StopIteration
);
911 return scanstring_unicode(pystr
, idx
+ 1,
912 PyObject_IsTrue(s
->strict
),
916 return _parse_object_unicode(s
, pystr
, idx
+ 1, next_idx_ptr
);
919 return _parse_array_unicode(s
, pystr
, idx
+ 1, next_idx_ptr
);
922 if ((idx
+ 3 < length
) && str
[idx
+ 1] == 'u' && str
[idx
+ 2] == 'l' && str
[idx
+ 3] == 'l') {
924 *next_idx_ptr
= idx
+ 4;
930 if ((idx
+ 3 < length
) && str
[idx
+ 1] == 'r' && str
[idx
+ 2] == 'u' && str
[idx
+ 3] == 'e') {
932 *next_idx_ptr
= idx
+ 4;
938 if ((idx
+ 4 < length
) && str
[idx
+ 1] == 'a' && str
[idx
+ 2] == 'l' && str
[idx
+ 3] == 's' && str
[idx
+ 4] == 'e') {
940 *next_idx_ptr
= idx
+ 5;
946 if ((idx
+ 2 < length
) && str
[idx
+ 1] == 'a' && str
[idx
+ 2] == 'N') {
947 return _parse_constant(s
, "NaN", idx
, next_idx_ptr
);
952 if ((idx
+ 7 < length
) && str
[idx
+ 1] == 'n' && str
[idx
+ 2] == 'f' && str
[idx
+ 3] == 'i' && str
[idx
+ 4] == 'n' && str
[idx
+ 5] == 'i' && str
[idx
+ 6] == 't' && str
[idx
+ 7] == 'y') {
953 return _parse_constant(s
, "Infinity", idx
, next_idx_ptr
);
958 if ((idx
+ 8 < length
) && str
[idx
+ 1] == 'I' && str
[idx
+ 2] == 'n' && str
[idx
+ 3] == 'f' && str
[idx
+ 4] == 'i' && str
[idx
+ 5] == 'n' && str
[idx
+ 6] == 'i' && str
[idx
+ 7] == 't' && str
[idx
+ 8] == 'y') {
959 return _parse_constant(s
, "-Infinity", idx
, next_idx_ptr
);
963 /* Didn't find a string, object, array, or named constant. Look for a number. */
964 return _match_number_unicode(s
, pystr
, idx
, next_idx_ptr
);
968 scanner_call(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
970 /* Python callable interface to scan_once_{str,unicode} */
974 Py_ssize_t next_idx
= -1;
975 static char *kwlist
[] = {"string", "idx", NULL
};
977 assert(PyScanner_Check(self
));
978 s
= (PyScannerObject
*)self
;
979 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "OO&:scan_once", kwlist
, &pystr
, _convertPyInt_AsSsize_t
, &idx
))
982 if (PyUnicode_Check(pystr
)) {
983 rval
= scan_once_unicode(s
, pystr
, idx
, &next_idx
);
986 PyErr_Format(PyExc_TypeError
,
987 "first argument must be a string, not %.80s",
988 Py_TYPE(pystr
)->tp_name
);
991 return _build_rval_index_tuple(rval
, next_idx
);
995 scanner_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
998 s
= (PyScannerObject
*)type
->tp_alloc(type
, 0);
1001 s
->object_hook
= NULL
;
1002 s
->object_pairs_hook
= NULL
;
1003 s
->parse_float
= NULL
;
1004 s
->parse_int
= NULL
;
1005 s
->parse_constant
= NULL
;
1007 return (PyObject
*)s
;
1011 scanner_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1013 /* Initialize Scanner object */
1015 static char *kwlist
[] = {"context", NULL
};
1018 assert(PyScanner_Check(self
));
1019 s
= (PyScannerObject
*)self
;
1021 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O:make_scanner", kwlist
, &ctx
))
1024 /* All of these will fail "gracefully" so we don't need to verify them */
1025 s
->strict
= PyObject_GetAttrString(ctx
, "strict");
1026 if (s
->strict
== NULL
)
1028 s
->object_hook
= PyObject_GetAttrString(ctx
, "object_hook");
1029 if (s
->object_hook
== NULL
)
1031 s
->object_pairs_hook
= PyObject_GetAttrString(ctx
, "object_pairs_hook");
1032 if (s
->object_pairs_hook
== NULL
)
1034 s
->parse_float
= PyObject_GetAttrString(ctx
, "parse_float");
1035 if (s
->parse_float
== NULL
)
1037 s
->parse_int
= PyObject_GetAttrString(ctx
, "parse_int");
1038 if (s
->parse_int
== NULL
)
1040 s
->parse_constant
= PyObject_GetAttrString(ctx
, "parse_constant");
1041 if (s
->parse_constant
== NULL
)
1047 Py_CLEAR(s
->strict
);
1048 Py_CLEAR(s
->object_hook
);
1049 Py_CLEAR(s
->object_pairs_hook
);
1050 Py_CLEAR(s
->parse_float
);
1051 Py_CLEAR(s
->parse_int
);
1052 Py_CLEAR(s
->parse_constant
);
1056 PyDoc_STRVAR(scanner_doc
, "JSON scanner object");
1059 PyTypeObject PyScannerType
= {
1060 PyVarObject_HEAD_INIT(NULL
, 0)
1061 "_json.Scanner", /* tp_name */
1062 sizeof(PyScannerObject
), /* tp_basicsize */
1063 0, /* tp_itemsize */
1064 scanner_dealloc
, /* tp_dealloc */
1070 0, /* tp_as_number */
1071 0, /* tp_as_sequence */
1072 0, /* tp_as_mapping */
1074 scanner_call
, /* tp_call */
1076 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1077 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1078 0, /* tp_as_buffer */
1079 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
1080 scanner_doc
, /* tp_doc */
1081 scanner_traverse
, /* tp_traverse */
1082 scanner_clear
, /* tp_clear */
1083 0, /* tp_richcompare */
1084 0, /* tp_weaklistoffset */
1086 0, /* tp_iternext */
1088 scanner_members
, /* tp_members */
1092 0, /* tp_descr_get */
1093 0, /* tp_descr_set */
1094 0, /* tp_dictoffset */
1095 scanner_init
, /* tp_init */
1096 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1097 scanner_new
, /* tp_new */
1098 0,/* PyObject_GC_Del, */ /* tp_free */
1102 encoder_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1105 s
= (PyEncoderObject
*)type
->tp_alloc(type
, 0);
1108 s
->defaultfn
= NULL
;
1111 s
->key_separator
= NULL
;
1112 s
->item_separator
= NULL
;
1113 s
->sort_keys
= NULL
;
1116 return (PyObject
*)s
;
1120 encoder_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1122 /* initialize Encoder object */
1123 static char *kwlist
[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL
};
1126 PyObject
*markers
, *defaultfn
, *encoder
, *indent
, *key_separator
;
1127 PyObject
*item_separator
, *sort_keys
, *skipkeys
, *allow_nan
;
1129 assert(PyEncoder_Check(self
));
1130 s
= (PyEncoderObject
*)self
;
1132 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "OOOOOOOOO:make_encoder", kwlist
,
1133 &markers
, &defaultfn
, &encoder
, &indent
, &key_separator
, &item_separator
,
1134 &sort_keys
, &skipkeys
, &allow_nan
))
1137 s
->markers
= markers
;
1138 s
->defaultfn
= defaultfn
;
1139 s
->encoder
= encoder
;
1141 s
->key_separator
= key_separator
;
1142 s
->item_separator
= item_separator
;
1143 s
->sort_keys
= sort_keys
;
1144 s
->skipkeys
= skipkeys
;
1145 s
->fast_encode
= (PyCFunction_Check(s
->encoder
) && PyCFunction_GetFunction(s
->encoder
) == (PyCFunction
)py_encode_basestring_ascii
);
1146 s
->allow_nan
= PyObject_IsTrue(allow_nan
);
1148 Py_INCREF(s
->markers
);
1149 Py_INCREF(s
->defaultfn
);
1150 Py_INCREF(s
->encoder
);
1151 Py_INCREF(s
->indent
);
1152 Py_INCREF(s
->key_separator
);
1153 Py_INCREF(s
->item_separator
);
1154 Py_INCREF(s
->sort_keys
);
1155 Py_INCREF(s
->skipkeys
);
1160 encoder_call(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1162 /* Python callable interface to encode_listencode_obj */
1163 static char *kwlist
[] = {"obj", "_current_indent_level", NULL
};
1166 Py_ssize_t indent_level
;
1168 assert(PyEncoder_Check(self
));
1169 s
= (PyEncoderObject
*)self
;
1170 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "OO&:_iterencode", kwlist
,
1171 &obj
, _convertPyInt_AsSsize_t
, &indent_level
))
1173 rval
= PyList_New(0);
1176 if (encoder_listencode_obj(s
, rval
, obj
, indent_level
)) {
1184 _encoded_const(PyObject
*obj
)
1186 /* Return the JSON string representation of None, True, False */
1187 if (obj
== Py_None
) {
1188 static PyObject
*s_null
= NULL
;
1189 if (s_null
== NULL
) {
1190 s_null
= PyUnicode_InternFromString("null");
1195 else if (obj
== Py_True
) {
1196 static PyObject
*s_true
= NULL
;
1197 if (s_true
== NULL
) {
1198 s_true
= PyUnicode_InternFromString("true");
1203 else if (obj
== Py_False
) {
1204 static PyObject
*s_false
= NULL
;
1205 if (s_false
== NULL
) {
1206 s_false
= PyUnicode_InternFromString("false");
1212 PyErr_SetString(PyExc_ValueError
, "not a const");
1218 encoder_encode_float(PyEncoderObject
*s
, PyObject
*obj
)
1220 /* Return the JSON representation of a PyFloat */
1221 double i
= PyFloat_AS_DOUBLE(obj
);
1222 if (!Py_IS_FINITE(i
)) {
1223 if (!s
->allow_nan
) {
1224 PyErr_SetString(PyExc_ValueError
, "Out of range float values are not JSON compliant");
1228 return PyUnicode_FromString("Infinity");
1231 return PyUnicode_FromString("-Infinity");
1234 return PyUnicode_FromString("NaN");
1237 /* Use a better float format here? */
1238 return PyObject_Repr(obj
);
1242 encoder_encode_string(PyEncoderObject
*s
, PyObject
*obj
)
1244 /* Return the JSON representation of a string */
1246 return py_encode_basestring_ascii(NULL
, obj
);
1248 return PyObject_CallFunctionObjArgs(s
->encoder
, obj
, NULL
);
1252 _steal_list_append(PyObject
*lst
, PyObject
*stolen
)
1254 /* Append stolen and then decrement its reference count */
1255 int rval
= PyList_Append(lst
, stolen
);
1261 encoder_listencode_obj(PyEncoderObject
*s
, PyObject
*rval
, PyObject
*obj
, Py_ssize_t indent_level
)
1263 /* Encode Python object obj to a JSON term, rval is a PyList */
1267 if (obj
== Py_None
|| obj
== Py_True
|| obj
== Py_False
) {
1268 PyObject
*cstr
= _encoded_const(obj
);
1271 return _steal_list_append(rval
, cstr
);
1273 else if (PyUnicode_Check(obj
))
1275 PyObject
*encoded
= encoder_encode_string(s
, obj
);
1276 if (encoded
== NULL
)
1278 return _steal_list_append(rval
, encoded
);
1280 else if (PyLong_Check(obj
)) {
1281 PyObject
*encoded
= PyObject_Str(obj
);
1282 if (encoded
== NULL
)
1284 return _steal_list_append(rval
, encoded
);
1286 else if (PyFloat_Check(obj
)) {
1287 PyObject
*encoded
= encoder_encode_float(s
, obj
);
1288 if (encoded
== NULL
)
1290 return _steal_list_append(rval
, encoded
);
1292 else if (PyList_Check(obj
) || PyTuple_Check(obj
)) {
1293 return encoder_listencode_list(s
, rval
, obj
, indent_level
);
1295 else if (PyDict_Check(obj
)) {
1296 return encoder_listencode_dict(s
, rval
, obj
, indent_level
);
1299 PyObject
*ident
= NULL
;
1300 if (s
->markers
!= Py_None
) {
1302 ident
= PyLong_FromVoidPtr(obj
);
1305 has_key
= PyDict_Contains(s
->markers
, ident
);
1308 PyErr_SetString(PyExc_ValueError
, "Circular reference detected");
1312 if (PyDict_SetItem(s
->markers
, ident
, obj
)) {
1317 newobj
= PyObject_CallFunctionObjArgs(s
->defaultfn
, obj
, NULL
);
1318 if (newobj
== NULL
) {
1322 rv
= encoder_listencode_obj(s
, rval
, newobj
, indent_level
);
1328 if (ident
!= NULL
) {
1329 if (PyDict_DelItem(s
->markers
, ident
)) {
1340 encoder_listencode_dict(PyEncoderObject
*s
, PyObject
*rval
, PyObject
*dct
, Py_ssize_t indent_level
)
1342 /* Encode Python dict dct a JSON term, rval is a PyList */
1343 static PyObject
*open_dict
= NULL
;
1344 static PyObject
*close_dict
= NULL
;
1345 static PyObject
*empty_dict
= NULL
;
1346 PyObject
*kstr
= NULL
;
1347 PyObject
*ident
= NULL
;
1348 PyObject
*it
= NULL
;
1350 PyObject
*item
= NULL
;
1354 static PyObject
*code
= NULL
;
1356 if (open_dict
== NULL
|| close_dict
== NULL
|| empty_dict
== NULL
) {
1357 open_dict
= PyUnicode_InternFromString("{");
1358 close_dict
= PyUnicode_InternFromString("}");
1359 empty_dict
= PyUnicode_InternFromString("{}");
1360 if (open_dict
== NULL
|| close_dict
== NULL
|| empty_dict
== NULL
)
1363 if (Py_SIZE(dct
) == 0)
1364 return PyList_Append(rval
, empty_dict
);
1366 if (s
->markers
!= Py_None
) {
1368 ident
= PyLong_FromVoidPtr(dct
);
1371 has_key
= PyDict_Contains(s
->markers
, ident
);
1374 PyErr_SetString(PyExc_ValueError
, "Circular reference detected");
1377 if (PyDict_SetItem(s
->markers
, ident
, dct
)) {
1382 if (PyList_Append(rval
, open_dict
))
1385 if (s
->indent
!= Py_None
) {
1386 /* TODO: DOES NOT RUN */
1389 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1390 separator = _item_separator + newline_indent
1391 buf += newline_indent
1395 if (PyObject_IsTrue(s
->sort_keys
)) {
1397 code
= Py_CompileString("sorted(d.items(), key=lambda kv: kv[0])",
1398 "_json.c", Py_eval_input
);
1403 mapping
= PyDict_New();
1404 if (mapping
== NULL
)
1406 if (PyDict_SetItemString(mapping
, "d", dct
) == -1) {
1410 items
= PyEval_EvalCode((PyCodeObject
*)code
, PyEval_GetGlobals(), mapping
);
1413 items
= PyMapping_Items(dct
);
1417 it
= PyObject_GetIter(items
);
1421 skipkeys
= PyObject_IsTrue(s
->skipkeys
);
1423 while ((item
= PyIter_Next(it
)) != NULL
) {
1424 PyObject
*encoded
, *key
, *value
;
1425 if (!PyTuple_Check(item
) || Py_SIZE(item
) != 2) {
1426 PyErr_SetString(PyExc_ValueError
, "items must return 2-tuples");
1429 key
= PyTuple_GET_ITEM(item
, 0);
1430 if (PyUnicode_Check(key
)) {
1434 else if (PyFloat_Check(key
)) {
1435 kstr
= encoder_encode_float(s
, key
);
1439 else if (key
== Py_True
|| key
== Py_False
|| key
== Py_None
) {
1440 /* This must come before the PyLong_Check because
1441 True and False are also 1 and 0.*/
1442 kstr
= _encoded_const(key
);
1446 else if (PyLong_Check(key
)) {
1447 kstr
= PyObject_Str(key
);
1451 else if (skipkeys
) {
1456 /* TODO: include repr of key */
1457 PyErr_SetString(PyExc_TypeError
, "keys must be a string");
1462 if (PyList_Append(rval
, s
->item_separator
))
1466 encoded
= encoder_encode_string(s
, kstr
);
1468 if (encoded
== NULL
)
1470 if (PyList_Append(rval
, encoded
)) {
1475 if (PyList_Append(rval
, s
->key_separator
))
1478 value
= PyTuple_GET_ITEM(item
, 1);
1479 if (encoder_listencode_obj(s
, rval
, value
, indent_level
))
1484 if (PyErr_Occurred())
1488 if (ident
!= NULL
) {
1489 if (PyDict_DelItem(s
->markers
, ident
))
1493 if (s
->indent
!= Py_None
) {
1494 /* TODO: DOES NOT RUN */
1497 yield '\n' + (' ' * (_indent * _current_indent_level))
1500 if (PyList_Append(rval
, close_dict
))
1514 encoder_listencode_list(PyEncoderObject
*s
, PyObject
*rval
, PyObject
*seq
, Py_ssize_t indent_level
)
1516 /* Encode Python list seq to a JSON term, rval is a PyList */
1517 static PyObject
*open_array
= NULL
;
1518 static PyObject
*close_array
= NULL
;
1519 static PyObject
*empty_array
= NULL
;
1520 PyObject
*ident
= NULL
;
1521 PyObject
*s_fast
= NULL
;
1522 Py_ssize_t num_items
;
1523 PyObject
**seq_items
;
1526 if (open_array
== NULL
|| close_array
== NULL
|| empty_array
== NULL
) {
1527 open_array
= PyUnicode_InternFromString("[");
1528 close_array
= PyUnicode_InternFromString("]");
1529 empty_array
= PyUnicode_InternFromString("[]");
1530 if (open_array
== NULL
|| close_array
== NULL
|| empty_array
== NULL
)
1534 s_fast
= PySequence_Fast(seq
, "_iterencode_list needs a sequence");
1537 num_items
= PySequence_Fast_GET_SIZE(s_fast
);
1538 if (num_items
== 0) {
1540 return PyList_Append(rval
, empty_array
);
1543 if (s
->markers
!= Py_None
) {
1545 ident
= PyLong_FromVoidPtr(seq
);
1548 has_key
= PyDict_Contains(s
->markers
, ident
);
1551 PyErr_SetString(PyExc_ValueError
, "Circular reference detected");
1554 if (PyDict_SetItem(s
->markers
, ident
, seq
)) {
1559 seq_items
= PySequence_Fast_ITEMS(s_fast
);
1560 if (PyList_Append(rval
, open_array
))
1562 if (s
->indent
!= Py_None
) {
1563 /* TODO: DOES NOT RUN */
1566 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1567 separator = _item_separator + newline_indent
1568 buf += newline_indent
1571 for (i
= 0; i
< num_items
; i
++) {
1572 PyObject
*obj
= seq_items
[i
];
1574 if (PyList_Append(rval
, s
->item_separator
))
1577 if (encoder_listencode_obj(s
, rval
, obj
, indent_level
))
1580 if (ident
!= NULL
) {
1581 if (PyDict_DelItem(s
->markers
, ident
))
1585 if (s
->indent
!= Py_None
) {
1586 /* TODO: DOES NOT RUN */
1589 yield '\n' + (' ' * (_indent * _current_indent_level))
1592 if (PyList_Append(rval
, close_array
))
1604 encoder_dealloc(PyObject
*self
)
1606 /* Deallocate Encoder */
1607 encoder_clear(self
);
1608 Py_TYPE(self
)->tp_free(self
);
1612 encoder_traverse(PyObject
*self
, visitproc visit
, void *arg
)
1615 assert(PyEncoder_Check(self
));
1616 s
= (PyEncoderObject
*)self
;
1617 Py_VISIT(s
->markers
);
1618 Py_VISIT(s
->defaultfn
);
1619 Py_VISIT(s
->encoder
);
1620 Py_VISIT(s
->indent
);
1621 Py_VISIT(s
->key_separator
);
1622 Py_VISIT(s
->item_separator
);
1623 Py_VISIT(s
->sort_keys
);
1624 Py_VISIT(s
->skipkeys
);
1629 encoder_clear(PyObject
*self
)
1631 /* Deallocate Encoder */
1633 assert(PyEncoder_Check(self
));
1634 s
= (PyEncoderObject
*)self
;
1635 Py_CLEAR(s
->markers
);
1636 Py_CLEAR(s
->defaultfn
);
1637 Py_CLEAR(s
->encoder
);
1638 Py_CLEAR(s
->indent
);
1639 Py_CLEAR(s
->key_separator
);
1640 Py_CLEAR(s
->item_separator
);
1641 Py_CLEAR(s
->sort_keys
);
1642 Py_CLEAR(s
->skipkeys
);
1646 PyDoc_STRVAR(encoder_doc
, "_iterencode(obj, _current_indent_level) -> iterable");
1649 PyTypeObject PyEncoderType
= {
1650 PyVarObject_HEAD_INIT(NULL
, 0)
1651 "_json.Encoder", /* tp_name */
1652 sizeof(PyEncoderObject
), /* tp_basicsize */
1653 0, /* tp_itemsize */
1654 encoder_dealloc
, /* tp_dealloc */
1660 0, /* tp_as_number */
1661 0, /* tp_as_sequence */
1662 0, /* tp_as_mapping */
1664 encoder_call
, /* tp_call */
1666 0, /* tp_getattro */
1667 0, /* tp_setattro */
1668 0, /* tp_as_buffer */
1669 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
1670 encoder_doc
, /* tp_doc */
1671 encoder_traverse
, /* tp_traverse */
1672 encoder_clear
, /* tp_clear */
1673 0, /* tp_richcompare */
1674 0, /* tp_weaklistoffset */
1676 0, /* tp_iternext */
1678 encoder_members
, /* tp_members */
1682 0, /* tp_descr_get */
1683 0, /* tp_descr_set */
1684 0, /* tp_dictoffset */
1685 encoder_init
, /* tp_init */
1687 encoder_new
, /* tp_new */
1691 static PyMethodDef speedups_methods
[] = {
1692 {"encode_basestring_ascii",
1693 (PyCFunction
)py_encode_basestring_ascii
,
1695 pydoc_encode_basestring_ascii
},
1697 (PyCFunction
)py_scanstring
,
1700 {NULL
, NULL
, 0, NULL
}
1703 PyDoc_STRVAR(module_doc
,
1706 static struct PyModuleDef jsonmodule
= {
1707 PyModuleDef_HEAD_INIT
,
1721 PyObject
*m
= PyModule_Create(&jsonmodule
);
1724 PyScannerType
.tp_new
= PyType_GenericNew
;
1725 if (PyType_Ready(&PyScannerType
) < 0)
1727 PyEncoderType
.tp_new
= PyType_GenericNew
;
1728 if (PyType_Ready(&PyEncoderType
) < 0)
1730 Py_INCREF((PyObject
*)&PyScannerType
);
1731 if (PyModule_AddObject(m
, "make_scanner", (PyObject
*)&PyScannerType
) < 0) {
1732 Py_DECREF((PyObject
*)&PyScannerType
);
1735 Py_INCREF((PyObject
*)&PyEncoderType
);
1736 if (PyModule_AddObject(m
, "make_encoder", (PyObject
*)&PyEncoderType
) < 0) {
1737 Py_DECREF((PyObject
*)&PyEncoderType
);