blob: 397037e56b99ebd502c3c9bcf38c63fcd6b10771 [file] [log] [blame]
Christian Heimes90540002008-05-08 14:29:10 +00001#include "Python.h"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00002#include "structmember.h"
Antoine Pitroud0acb412012-03-22 14:42:18 +01003#include "accu.h"
4
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00005#if PY_VERSION_HEX < 0x02060000 && !defined(Py_TYPE)
6#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
7#endif
8#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
9typedef int Py_ssize_t;
10#define PY_SSIZE_T_MAX INT_MAX
11#define PY_SSIZE_T_MIN INT_MIN
12#define PyInt_FromSsize_t PyInt_FromLong
13#define PyInt_AsSsize_t PyInt_AsLong
14#endif
15#ifndef Py_IS_FINITE
16#define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X))
17#endif
Christian Heimes90540002008-05-08 14:29:10 +000018
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000019#ifdef __GNUC__
20#define UNUSED __attribute__((__unused__))
21#else
22#define UNUSED
23#endif
24
25#define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType)
26#define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType)
27#define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType)
28#define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType)
29
30static PyTypeObject PyScannerType;
31static PyTypeObject PyEncoderType;
32
33typedef struct _PyScannerObject {
34 PyObject_HEAD
35 PyObject *strict;
36 PyObject *object_hook;
37 PyObject *object_pairs_hook;
38 PyObject *parse_float;
39 PyObject *parse_int;
40 PyObject *parse_constant;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +000041 PyObject *memo;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000042} PyScannerObject;
43
44static PyMemberDef scanner_members[] = {
45 {"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"},
46 {"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"},
47 {"object_pairs_hook", T_OBJECT, offsetof(PyScannerObject, object_pairs_hook), READONLY},
48 {"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"},
49 {"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "parse_int"},
50 {"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READONLY, "parse_constant"},
51 {NULL}
52};
53
54typedef struct _PyEncoderObject {
55 PyObject_HEAD
56 PyObject *markers;
57 PyObject *defaultfn;
58 PyObject *encoder;
59 PyObject *indent;
60 PyObject *key_separator;
61 PyObject *item_separator;
62 PyObject *sort_keys;
63 PyObject *skipkeys;
64 int fast_encode;
65 int allow_nan;
66} PyEncoderObject;
67
68static PyMemberDef encoder_members[] = {
69 {"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers"},
70 {"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "default"},
71 {"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder"},
72 {"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"},
73 {"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READONLY, "key_separator"},
74 {"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READONLY, "item_separator"},
75 {"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sort_keys"},
76 {"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipkeys"},
77 {NULL}
78};
79
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +020080static PyObject *
81join_list_unicode(PyObject *lst)
82{
83 /* return u''.join(lst) */
84 static PyObject *sep = NULL;
85 if (sep == NULL) {
86 sep = PyUnicode_FromStringAndSize("", 0);
87 if (sep == NULL)
88 return NULL;
89 }
90 return PyUnicode_Join(sep, lst);
91}
92
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +020093/* Forward decls */
94
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000095static PyObject *
96ascii_escape_unicode(PyObject *pystr);
97static PyObject *
98py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr);
99void init_json(void);
100static PyObject *
101scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr);
102static PyObject *
103_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx);
104static PyObject *
105scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
106static int
107scanner_init(PyObject *self, PyObject *args, PyObject *kwds);
108static void
109scanner_dealloc(PyObject *self);
110static int
111scanner_clear(PyObject *self);
112static PyObject *
113encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
114static int
115encoder_init(PyObject *self, PyObject *args, PyObject *kwds);
116static void
117encoder_dealloc(PyObject *self);
118static int
119encoder_clear(PyObject *self);
120static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200121encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc, PyObject *seq, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000122static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200123encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, PyObject *obj, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000124static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200125encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, PyObject *dct, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000126static PyObject *
Hirokazu Yamamotofecf5d12009-05-02 15:55:19 +0000127_encoded_const(PyObject *obj);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000128static void
129raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
130static PyObject *
131encoder_encode_string(PyEncoderObject *s, PyObject *obj);
132static int
133_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr);
134static PyObject *
135_convertPyInt_FromSsize_t(Py_ssize_t *size_ptr);
136static PyObject *
137encoder_encode_float(PyEncoderObject *s, PyObject *obj);
138
Christian Heimes90540002008-05-08 14:29:10 +0000139#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000140#define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
Christian Heimes90540002008-05-08 14:29:10 +0000141
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000142static int
143_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000144{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000145 /* PyObject to Py_ssize_t converter */
146 *size_ptr = PyLong_AsSsize_t(o);
Georg Brandl59682052009-05-05 07:52:05 +0000147 if (*size_ptr == -1 && PyErr_Occurred())
148 return 0;
149 return 1;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000150}
151
152static PyObject *
153_convertPyInt_FromSsize_t(Py_ssize_t *size_ptr)
154{
155 /* Py_ssize_t to PyObject converter */
156 return PyLong_FromSsize_t(*size_ptr);
157}
158
159static Py_ssize_t
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200160ascii_escape_unichar(Py_UCS4 c, unsigned char *output, Py_ssize_t chars)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000161{
162 /* Escape unicode code point c to ASCII escape sequences
163 in char *output. output must have at least 12 bytes unused to
164 accommodate an escaped surrogate pair "\uXXXX\uXXXX" */
Christian Heimes90540002008-05-08 14:29:10 +0000165 output[chars++] = '\\';
166 switch (c) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000167 case '\\': output[chars++] = c; break;
168 case '"': output[chars++] = c; break;
Christian Heimes90540002008-05-08 14:29:10 +0000169 case '\b': output[chars++] = 'b'; break;
170 case '\f': output[chars++] = 'f'; break;
171 case '\n': output[chars++] = 'n'; break;
172 case '\r': output[chars++] = 'r'; break;
173 case '\t': output[chars++] = 't'; break;
174 default:
Christian Heimes90540002008-05-08 14:29:10 +0000175 if (c >= 0x10000) {
176 /* UTF-16 surrogate pair */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200177 Py_UCS4 v = c - 0x10000;
Christian Heimes90540002008-05-08 14:29:10 +0000178 c = 0xd800 | ((v >> 10) & 0x3ff);
179 output[chars++] = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200180 output[chars++] = Py_hexdigits[(c >> 12) & 0xf];
181 output[chars++] = Py_hexdigits[(c >> 8) & 0xf];
182 output[chars++] = Py_hexdigits[(c >> 4) & 0xf];
183 output[chars++] = Py_hexdigits[(c ) & 0xf];
Christian Heimes90540002008-05-08 14:29:10 +0000184 c = 0xdc00 | (v & 0x3ff);
185 output[chars++] = '\\';
186 }
Christian Heimes90540002008-05-08 14:29:10 +0000187 output[chars++] = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200188 output[chars++] = Py_hexdigits[(c >> 12) & 0xf];
189 output[chars++] = Py_hexdigits[(c >> 8) & 0xf];
190 output[chars++] = Py_hexdigits[(c >> 4) & 0xf];
191 output[chars++] = Py_hexdigits[(c ) & 0xf];
Christian Heimes90540002008-05-08 14:29:10 +0000192 }
193 return chars;
194}
195
196static PyObject *
197ascii_escape_unicode(PyObject *pystr)
198{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000199 /* Take a PyUnicode pystr and return a new ASCII-only escaped PyUnicode */
Christian Heimes90540002008-05-08 14:29:10 +0000200 Py_ssize_t i;
201 Py_ssize_t input_chars;
202 Py_ssize_t output_size;
203 Py_ssize_t chars;
204 PyObject *rval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200205 void *input;
206 unsigned char *output;
207 int kind;
Christian Heimes90540002008-05-08 14:29:10 +0000208
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200209 if (PyUnicode_READY(pystr) == -1)
210 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000211
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200212 input_chars = PyUnicode_GET_LENGTH(pystr);
213 input = PyUnicode_DATA(pystr);
214 kind = PyUnicode_KIND(pystr);
215
216 /* Compute the output size */
217 for (i = 0, output_size = 2; i < input_chars; i++) {
218 Py_UCS4 c = PyUnicode_READ(kind, input, i);
Benjamin Petersone3bfe192015-02-01 17:53:53 -0500219 Py_ssize_t d;
220 if (S_CHAR(c)) {
221 d = 1;
222 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200223 else {
224 switch(c) {
Victor Stinnerd9c06312011-10-11 21:56:19 +0200225 case '\\': case '"': case '\b': case '\f':
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200226 case '\n': case '\r': case '\t':
Benjamin Petersone3bfe192015-02-01 17:53:53 -0500227 d = 2; break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200228 default:
Benjamin Petersone3bfe192015-02-01 17:53:53 -0500229 d = c >= 0x10000 ? 12 : 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200230 }
231 }
Benjamin Petersone3bfe192015-02-01 17:53:53 -0500232 if (output_size > PY_SSIZE_T_MAX - d) {
233 PyErr_SetString(PyExc_OverflowError, "string is too long to escape");
234 return NULL;
235 }
236 output_size += d;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200237 }
238
239 rval = PyUnicode_New(output_size, 127);
Christian Heimes90540002008-05-08 14:29:10 +0000240 if (rval == NULL) {
241 return NULL;
242 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200243 output = PyUnicode_1BYTE_DATA(rval);
Christian Heimes90540002008-05-08 14:29:10 +0000244 chars = 0;
245 output[chars++] = '"';
246 for (i = 0; i < input_chars; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200247 Py_UCS4 c = PyUnicode_READ(kind, input, i);
Christian Heimes90540002008-05-08 14:29:10 +0000248 if (S_CHAR(c)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000249 output[chars++] = c;
Christian Heimes90540002008-05-08 14:29:10 +0000250 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000251 else {
252 chars = ascii_escape_unichar(c, output, chars);
Christian Heimes90540002008-05-08 14:29:10 +0000253 }
Christian Heimes90540002008-05-08 14:29:10 +0000254 }
255 output[chars++] = '"';
Christian Heimesf402e922013-01-03 09:21:55 +0100256#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200257 assert(_PyUnicode_CheckConsistency(rval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100258#endif
Christian Heimes90540002008-05-08 14:29:10 +0000259 return rval;
260}
261
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000262static void
Christian Heimes90540002008-05-08 14:29:10 +0000263raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
264{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000265 /* Use the Python function json.decoder.errmsg to raise a nice
266 looking ValueError exception */
Christian Heimes90540002008-05-08 14:29:10 +0000267 static PyObject *errmsg_fn = NULL;
268 PyObject *pymsg;
269 if (errmsg_fn == NULL) {
270 PyObject *decoder = PyImport_ImportModule("json.decoder");
271 if (decoder == NULL)
272 return;
273 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000274 Py_DECREF(decoder);
Christian Heimes90540002008-05-08 14:29:10 +0000275 if (errmsg_fn == NULL)
276 return;
Christian Heimes90540002008-05-08 14:29:10 +0000277 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000278 pymsg = PyObject_CallFunction(errmsg_fn, "(zOO&)", msg, s, _convertPyInt_FromSsize_t, &end);
Benjamin Petersona13d4752008-10-16 21:17:24 +0000279 if (pymsg) {
280 PyErr_SetObject(PyExc_ValueError, pymsg);
281 Py_DECREF(pymsg);
282 }
Christian Heimes90540002008-05-08 14:29:10 +0000283}
284
285static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000286_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
287 /* return (rval, idx) tuple, stealing reference to rval */
288 PyObject *tpl;
289 PyObject *pyidx;
290 /*
291 steal a reference to rval, returns (rval, idx)
292 */
293 if (rval == NULL) {
Christian Heimes90540002008-05-08 14:29:10 +0000294 return NULL;
295 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000296 pyidx = PyLong_FromSsize_t(idx);
297 if (pyidx == NULL) {
298 Py_DECREF(rval);
299 return NULL;
300 }
301 tpl = PyTuple_New(2);
302 if (tpl == NULL) {
303 Py_DECREF(pyidx);
304 Py_DECREF(rval);
305 return NULL;
306 }
307 PyTuple_SET_ITEM(tpl, 0, rval);
308 PyTuple_SET_ITEM(tpl, 1, pyidx);
309 return tpl;
Christian Heimes90540002008-05-08 14:29:10 +0000310}
311
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000312#define APPEND_OLD_CHUNK \
313 if (chunk != NULL) { \
314 if (chunks == NULL) { \
315 chunks = PyList_New(0); \
316 if (chunks == NULL) { \
317 goto bail; \
318 } \
319 } \
320 if (PyList_Append(chunks, chunk)) { \
321 Py_DECREF(chunk); \
322 goto bail; \
323 } \
324 Py_CLEAR(chunk); \
325 }
326
Christian Heimes90540002008-05-08 14:29:10 +0000327static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000328scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000329{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000330 /* Read the JSON string from PyUnicode pystr.
331 end is the index of the first character after the quote.
332 if strict is zero then literal control characters are allowed
333 *next_end_ptr is a return-by-reference index of the character
334 after the end quote
Christian Heimes90540002008-05-08 14:29:10 +0000335
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000336 Return value is a new PyUnicode
337 */
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000338 PyObject *rval = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200339 Py_ssize_t len;
Christian Heimes90540002008-05-08 14:29:10 +0000340 Py_ssize_t begin = end - 1;
Brett Cannonb94767f2011-02-22 20:15:44 +0000341 Py_ssize_t next /* = begin */;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200342 const void *buf;
343 int kind;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000344 PyObject *chunks = NULL;
345 PyObject *chunk = NULL;
346
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200347 if (PyUnicode_READY(pystr) == -1)
348 return 0;
349
350 len = PyUnicode_GET_LENGTH(pystr);
351 buf = PyUnicode_DATA(pystr);
352 kind = PyUnicode_KIND(pystr);
353
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000354 if (end < 0 || len <= end) {
355 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
356 goto bail;
357 }
Christian Heimes90540002008-05-08 14:29:10 +0000358 while (1) {
359 /* Find the end of the string or the next escape */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200360 Py_UCS4 c = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000361 for (next = end; next < len; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200362 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000363 if (c == '"' || c == '\\') {
364 break;
365 }
366 else if (strict && c <= 0x1f) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000367 raise_errmsg("Invalid control character at", pystr, next);
Christian Heimes90540002008-05-08 14:29:10 +0000368 goto bail;
369 }
370 }
371 if (!(c == '"' || c == '\\')) {
372 raise_errmsg("Unterminated string starting at", pystr, begin);
373 goto bail;
374 }
375 /* Pick up this chunk if it's not zero length */
376 if (next != end) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000377 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200378 chunk = PyUnicode_FromKindAndData(
379 kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200380 (char*)buf + kind * end,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200381 next - end);
Christian Heimes90540002008-05-08 14:29:10 +0000382 if (chunk == NULL) {
383 goto bail;
384 }
Christian Heimes90540002008-05-08 14:29:10 +0000385 }
386 next++;
387 if (c == '"') {
388 end = next;
389 break;
390 }
391 if (next == len) {
392 raise_errmsg("Unterminated string starting at", pystr, begin);
393 goto bail;
394 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200395 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000396 if (c != 'u') {
397 /* Non-unicode backslash escapes */
398 end = next + 1;
399 switch (c) {
400 case '"': break;
401 case '\\': break;
402 case '/': break;
403 case 'b': c = '\b'; break;
404 case 'f': c = '\f'; break;
405 case 'n': c = '\n'; break;
406 case 'r': c = '\r'; break;
407 case 't': c = '\t'; break;
408 default: c = 0;
409 }
410 if (c == 0) {
411 raise_errmsg("Invalid \\escape", pystr, end - 2);
412 goto bail;
413 }
414 }
415 else {
416 c = 0;
417 next++;
418 end = next + 4;
419 if (end >= len) {
420 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
421 goto bail;
422 }
423 /* Decode 4 hex digits */
424 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200425 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000426 c <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000427 switch (digit) {
428 case '0': case '1': case '2': case '3': case '4':
429 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000430 c |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000431 case 'a': case 'b': case 'c': case 'd': case 'e':
432 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000433 c |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000434 case 'A': case 'B': case 'C': case 'D': case 'E':
435 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000436 c |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000437 default:
438 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
439 goto bail;
440 }
441 }
Christian Heimes90540002008-05-08 14:29:10 +0000442 /* Surrogate pair */
Serhiy Storchakac93329b2013-11-26 21:25:28 +0200443 if (Py_UNICODE_IS_HIGH_SURROGATE(c) && end + 6 < len &&
444 PyUnicode_READ(kind, buf, next++) == '\\' &&
445 PyUnicode_READ(kind, buf, next++) == 'u') {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200446 Py_UCS4 c2 = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000447 end += 6;
448 /* Decode 4 hex digits */
449 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200450 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Antoine Pitrou5b0e9e82010-10-09 15:24:28 +0000451 c2 <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000452 switch (digit) {
453 case '0': case '1': case '2': case '3': case '4':
454 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000455 c2 |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000456 case 'a': case 'b': case 'c': case 'd': case 'e':
457 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000458 c2 |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000459 case 'A': case 'B': case 'C': case 'D': case 'E':
460 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000461 c2 |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000462 default:
463 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
464 goto bail;
465 }
466 }
Serhiy Storchakac93329b2013-11-26 21:25:28 +0200467 if (Py_UNICODE_IS_LOW_SURROGATE(c2))
468 c = Py_UNICODE_JOIN_SURROGATES(c, c2);
469 else
470 end -= 6;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000471 }
Christian Heimes90540002008-05-08 14:29:10 +0000472 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000473 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200474 chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1);
Christian Heimes90540002008-05-08 14:29:10 +0000475 if (chunk == NULL) {
476 goto bail;
477 }
Christian Heimes90540002008-05-08 14:29:10 +0000478 }
479
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000480 if (chunks == NULL) {
481 if (chunk != NULL)
482 rval = chunk;
483 else
484 rval = PyUnicode_FromStringAndSize("", 0);
Christian Heimes90540002008-05-08 14:29:10 +0000485 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000486 else {
487 APPEND_OLD_CHUNK
488 rval = join_list_unicode(chunks);
489 if (rval == NULL) {
490 goto bail;
491 }
492 Py_CLEAR(chunks);
493 }
494
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000495 *next_end_ptr = end;
496 return rval;
Christian Heimes90540002008-05-08 14:29:10 +0000497bail:
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000498 *next_end_ptr = -1;
Christian Heimes90540002008-05-08 14:29:10 +0000499 Py_XDECREF(chunks);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000500 Py_XDECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000501 return NULL;
502}
503
504PyDoc_STRVAR(pydoc_scanstring,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000505 "scanstring(string, end, strict=True) -> (string, end)\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000506 "\n"
507 "Scan the string s for a JSON string. End is the index of the\n"
508 "character in s after the quote that started the JSON string.\n"
509 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
510 "on attempt to decode an invalid string. If strict is False then literal\n"
511 "control characters are allowed in the string.\n"
512 "\n"
513 "Returns a tuple of the decoded string and the index of the character in s\n"
514 "after the end quote."
515);
Christian Heimes90540002008-05-08 14:29:10 +0000516
517static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000518py_scanstring(PyObject* self UNUSED, PyObject *args)
Christian Heimes90540002008-05-08 14:29:10 +0000519{
520 PyObject *pystr;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000521 PyObject *rval;
Christian Heimes90540002008-05-08 14:29:10 +0000522 Py_ssize_t end;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000523 Py_ssize_t next_end = -1;
524 int strict = 1;
525 if (!PyArg_ParseTuple(args, "OO&|i:scanstring", &pystr, _convertPyInt_AsSsize_t, &end, &strict)) {
Christian Heimes90540002008-05-08 14:29:10 +0000526 return NULL;
527 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000528 if (PyUnicode_Check(pystr)) {
529 rval = scanstring_unicode(pystr, end, strict, &next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000530 }
531 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 PyErr_Format(PyExc_TypeError,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000533 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000534 Py_TYPE(pystr)->tp_name);
535 return NULL;
536 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000537 return _build_rval_index_tuple(rval, next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000538}
539
540PyDoc_STRVAR(pydoc_encode_basestring_ascii,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000541 "encode_basestring_ascii(string) -> string\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000542 "\n"
543 "Return an ASCII-only JSON representation of a Python string"
544);
Christian Heimes90540002008-05-08 14:29:10 +0000545
546static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000547py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
Christian Heimes90540002008-05-08 14:29:10 +0000548{
549 PyObject *rval;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000550 /* Return an ASCII-only JSON representation of a Python string */
Christian Heimes90540002008-05-08 14:29:10 +0000551 /* METH_O */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000552 if (PyUnicode_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000553 rval = ascii_escape_unicode(pystr);
554 }
555 else {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000556 PyErr_Format(PyExc_TypeError,
557 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000558 Py_TYPE(pystr)->tp_name);
559 return NULL;
560 }
Christian Heimes90540002008-05-08 14:29:10 +0000561 return rval;
562}
563
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000564static void
565scanner_dealloc(PyObject *self)
566{
567 /* Deallocate scanner object */
568 scanner_clear(self);
569 Py_TYPE(self)->tp_free(self);
570}
571
572static int
573scanner_traverse(PyObject *self, visitproc visit, void *arg)
574{
575 PyScannerObject *s;
576 assert(PyScanner_Check(self));
577 s = (PyScannerObject *)self;
578 Py_VISIT(s->strict);
579 Py_VISIT(s->object_hook);
580 Py_VISIT(s->object_pairs_hook);
581 Py_VISIT(s->parse_float);
582 Py_VISIT(s->parse_int);
583 Py_VISIT(s->parse_constant);
584 return 0;
585}
586
587static int
588scanner_clear(PyObject *self)
589{
590 PyScannerObject *s;
591 assert(PyScanner_Check(self));
592 s = (PyScannerObject *)self;
593 Py_CLEAR(s->strict);
594 Py_CLEAR(s->object_hook);
595 Py_CLEAR(s->object_pairs_hook);
596 Py_CLEAR(s->parse_float);
597 Py_CLEAR(s->parse_int);
598 Py_CLEAR(s->parse_constant);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000599 Py_CLEAR(s->memo);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000600 return 0;
601}
602
603static PyObject *
604_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
605 /* Read a JSON object from PyUnicode pystr.
606 idx is the index of the first character after the opening curly brace.
607 *next_idx_ptr is a return-by-reference index to the first character after
608 the closing curly brace.
609
610 Returns a new PyObject (usually a dict, but object_hook can change that)
611 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200612 void *str;
613 int kind;
614 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000615 PyObject *val = NULL;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000616 PyObject *rval = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000617 PyObject *key = NULL;
618 int strict = PyObject_IsTrue(s->strict);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000619 int has_pairs_hook = (s->object_pairs_hook != Py_None);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000620 Py_ssize_t next_idx;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000621
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200622 if (PyUnicode_READY(pystr) == -1)
623 return NULL;
624
625 str = PyUnicode_DATA(pystr);
626 kind = PyUnicode_KIND(pystr);
627 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
628
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000629 if (has_pairs_hook)
630 rval = PyList_New(0);
631 else
632 rval = PyDict_New();
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000633 if (rval == NULL)
634 return NULL;
635
636 /* skip whitespace after { */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200637 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000638
639 /* only loop if the object is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200640 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000641 while (idx <= end_idx) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000642 PyObject *memokey;
643
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000644 /* read key */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200645 if (PyUnicode_READ(kind, str, idx) != '"') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200646 raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000647 goto bail;
648 }
649 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
650 if (key == NULL)
651 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000652 memokey = PyDict_GetItem(s->memo, key);
653 if (memokey != NULL) {
654 Py_INCREF(memokey);
655 Py_DECREF(key);
656 key = memokey;
657 }
658 else {
659 if (PyDict_SetItem(s->memo, key, key) < 0)
660 goto bail;
661 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000662 idx = next_idx;
663
664 /* skip whitespace between key and : delimiter, read :, skip whitespace */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200665 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
666 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200667 raise_errmsg("Expecting ':' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000668 goto bail;
669 }
670 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200671 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000672
673 /* read any JSON term */
674 val = scan_once_unicode(s, pystr, idx, &next_idx);
675 if (val == NULL)
676 goto bail;
677
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000678 if (has_pairs_hook) {
679 PyObject *item = PyTuple_Pack(2, key, val);
680 if (item == NULL)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000681 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000682 Py_CLEAR(key);
683 Py_CLEAR(val);
684 if (PyList_Append(rval, item) == -1) {
685 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000686 goto bail;
687 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000688 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000689 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000690 else {
691 if (PyDict_SetItem(rval, key, val) < 0)
692 goto bail;
693 Py_CLEAR(key);
694 Py_CLEAR(val);
695 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000696 idx = next_idx;
697
698 /* skip whitespace before } or , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200699 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000700
701 /* bail if the object is closed or we didn't get the , delimiter */
702 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200703 if (PyUnicode_READ(kind, str, idx) == '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000704 break;
705 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200706 else if (PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200707 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000708 goto bail;
709 }
710 idx++;
711
712 /* skip whitespace after , delimiter */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200713 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000714 }
715 }
716
717 /* verify that idx < end_idx, str[idx] should be '}' */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200718 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000719 raise_errmsg("Expecting object", pystr, end_idx);
720 goto bail;
721 }
722
723 *next_idx_ptr = idx + 1;
724
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000725 if (has_pairs_hook) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000726 val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000727 Py_DECREF(rval);
728 return val;
729 }
730
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000731 /* if object_hook is not None: rval = object_hook(rval) */
732 if (s->object_hook != Py_None) {
733 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000734 Py_DECREF(rval);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000735 return val;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000736 }
737 return rval;
738bail:
739 Py_XDECREF(key);
740 Py_XDECREF(val);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000741 Py_XDECREF(rval);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000742 return NULL;
743}
744
745static PyObject *
746_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
747 /* Read a JSON array from PyString pystr.
748 idx is the index of the first character after the opening brace.
749 *next_idx_ptr is a return-by-reference index to the first character after
750 the closing brace.
751
752 Returns a new PyList
753 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200754 void *str;
755 int kind;
756 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000757 PyObject *val = NULL;
758 PyObject *rval = PyList_New(0);
759 Py_ssize_t next_idx;
760 if (rval == NULL)
761 return NULL;
762
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200763 if (PyUnicode_READY(pystr) == -1)
764 return NULL;
765
766 str = PyUnicode_DATA(pystr);
767 kind = PyUnicode_KIND(pystr);
768 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
769
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000770 /* skip whitespace after [ */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200771 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000772
773 /* only loop if the array is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200774 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000775 while (idx <= end_idx) {
776
777 /* read any JSON term */
778 val = scan_once_unicode(s, pystr, idx, &next_idx);
779 if (val == NULL)
780 goto bail;
781
782 if (PyList_Append(rval, val) == -1)
783 goto bail;
784
785 Py_CLEAR(val);
786 idx = next_idx;
787
788 /* skip whitespace between term and , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200789 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000790
791 /* bail if the array is closed or we didn't get the , delimiter */
792 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200793 if (PyUnicode_READ(kind, str, idx) == ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000794 break;
795 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200796 else if (PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200797 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000798 goto bail;
799 }
800 idx++;
801
802 /* skip whitespace after , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200803 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000804 }
805 }
806
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200807 /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */
808 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000809 raise_errmsg("Expecting object", pystr, end_idx);
810 goto bail;
811 }
812 *next_idx_ptr = idx + 1;
813 return rval;
814bail:
815 Py_XDECREF(val);
816 Py_DECREF(rval);
817 return NULL;
818}
819
820static PyObject *
821_parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
822 /* Read a JSON constant from PyString pystr.
823 constant is the constant string that was found
824 ("NaN", "Infinity", "-Infinity").
825 idx is the index of the first character of the constant
826 *next_idx_ptr is a return-by-reference index to the first character after
827 the constant.
828
829 Returns the result of parse_constant
830 */
831 PyObject *cstr;
832 PyObject *rval;
833 /* constant is "NaN", "Infinity", or "-Infinity" */
834 cstr = PyUnicode_InternFromString(constant);
835 if (cstr == NULL)
836 return NULL;
837
838 /* rval = parse_constant(constant) */
839 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
Victor Stinnerc4f281e2011-10-11 22:11:42 +0200840 idx += PyUnicode_GET_LENGTH(cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000841 Py_DECREF(cstr);
842 *next_idx_ptr = idx;
843 return rval;
844}
845
846static PyObject *
847_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
848 /* Read a JSON number from PyUnicode pystr.
849 idx is the index of the first character of the number
850 *next_idx_ptr is a return-by-reference index to the first character after
851 the number.
852
853 Returns a new PyObject representation of that number:
854 PyInt, PyLong, or PyFloat.
855 May return other types if parse_int or parse_float are set
856 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200857 void *str;
858 int kind;
859 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000860 Py_ssize_t idx = start;
861 int is_float = 0;
862 PyObject *rval;
Antoine Pitrouf6454512011-04-25 19:16:06 +0200863 PyObject *numstr = NULL;
864 PyObject *custom_func;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000865
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200866 if (PyUnicode_READY(pystr) == -1)
867 return NULL;
868
869 str = PyUnicode_DATA(pystr);
870 kind = PyUnicode_KIND(pystr);
871 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
872
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000873 /* read a sign if it's there, make sure it's not the end of the string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200874 if (PyUnicode_READ(kind, str, idx) == '-') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000875 idx++;
876 if (idx > end_idx) {
877 PyErr_SetNone(PyExc_StopIteration);
878 return NULL;
879 }
880 }
881
882 /* read as many integer digits as we find as long as it doesn't start with 0 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200883 if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000884 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200885 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000886 }
887 /* if it starts with 0 we only expect one integer digit */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200888 else if (PyUnicode_READ(kind, str, idx) == '0') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000889 idx++;
890 }
891 /* no integer digits, error */
892 else {
893 PyErr_SetNone(PyExc_StopIteration);
894 return NULL;
895 }
896
897 /* if the next char is '.' followed by a digit then read all float digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200898 if (idx < end_idx && PyUnicode_READ(kind, str, idx) == '.' && PyUnicode_READ(kind, str, idx + 1) >= '0' && PyUnicode_READ(kind, str, idx + 1) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000899 is_float = 1;
900 idx += 2;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200901 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000902 }
903
904 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200905 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000906 Py_ssize_t e_start = idx;
907 idx++;
908
909 /* read an exponent sign if present */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200910 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000911
912 /* read all digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200913 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000914
915 /* if we got a digit, then parse as float. if not, backtrack */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200916 if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000917 is_float = 1;
918 }
919 else {
920 idx = e_start;
921 }
922 }
923
Antoine Pitrouf6454512011-04-25 19:16:06 +0200924 if (is_float && s->parse_float != (PyObject *)&PyFloat_Type)
925 custom_func = s->parse_float;
926 else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type)
927 custom_func = s->parse_int;
928 else
929 custom_func = NULL;
930
931 if (custom_func) {
932 /* copy the section we determined to be a number */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200933 numstr = PyUnicode_FromKindAndData(kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200934 (char*)str + kind * start,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200935 idx - start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200936 if (numstr == NULL)
937 return NULL;
938 rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000939 }
940 else {
Antoine Pitrouf6454512011-04-25 19:16:06 +0200941 Py_ssize_t i, n;
942 char *buf;
943 /* Straight conversion to ASCII, to avoid costly conversion of
944 decimal unicode digits (which cannot appear here) */
945 n = idx - start;
946 numstr = PyBytes_FromStringAndSize(NULL, n);
947 if (numstr == NULL)
948 return NULL;
949 buf = PyBytes_AS_STRING(numstr);
950 for (i = 0; i < n; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200951 buf[i] = (char) PyUnicode_READ(kind, str, i + start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200952 }
953 if (is_float)
954 rval = PyFloat_FromString(numstr);
955 else
956 rval = PyLong_FromString(buf, NULL, 10);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000957 }
958 Py_DECREF(numstr);
959 *next_idx_ptr = idx;
960 return rval;
961}
962
963static PyObject *
964scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
965{
966 /* Read one JSON term (of any kind) from PyUnicode pystr.
967 idx is the index of the first character of the term
968 *next_idx_ptr is a return-by-reference index to the first character after
969 the number.
970
971 Returns a new PyObject representation of the term.
972 */
Ezio Melotti362b9512011-05-07 17:58:09 +0300973 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200974 void *str;
975 int kind;
976 Py_ssize_t length;
977
978 if (PyUnicode_READY(pystr) == -1)
979 return NULL;
980
981 str = PyUnicode_DATA(pystr);
982 kind = PyUnicode_KIND(pystr);
983 length = PyUnicode_GET_LENGTH(pystr);
984
Benjamin Peterson6ef2b362014-04-14 11:45:21 -0400985 if (idx < 0) {
Benjamin Peterson9beee042014-04-14 11:46:51 -0400986 PyErr_SetString(PyExc_ValueError, "idx cannot be negative");
Benjamin Peterson6ef2b362014-04-14 11:45:21 -0400987 return NULL;
988 }
989 if (idx >= length) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000990 PyErr_SetNone(PyExc_StopIteration);
991 return NULL;
992 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200993
994 switch (PyUnicode_READ(kind, str, idx)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000995 case '"':
996 /* string */
997 return scanstring_unicode(pystr, idx + 1,
998 PyObject_IsTrue(s->strict),
999 next_idx_ptr);
1000 case '{':
1001 /* object */
Ezio Melotti362b9512011-05-07 17:58:09 +03001002 if (Py_EnterRecursiveCall(" while decoding a JSON object "
1003 "from a unicode string"))
1004 return NULL;
1005 res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
1006 Py_LeaveRecursiveCall();
1007 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001008 case '[':
1009 /* array */
Ezio Melotti362b9512011-05-07 17:58:09 +03001010 if (Py_EnterRecursiveCall(" while decoding a JSON array "
1011 "from a unicode string"))
1012 return NULL;
1013 res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
1014 Py_LeaveRecursiveCall();
1015 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001016 case 'n':
1017 /* null */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001018 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') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001019 Py_INCREF(Py_None);
1020 *next_idx_ptr = idx + 4;
1021 return Py_None;
1022 }
1023 break;
1024 case 't':
1025 /* true */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001026 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') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001027 Py_INCREF(Py_True);
1028 *next_idx_ptr = idx + 4;
1029 return Py_True;
1030 }
1031 break;
1032 case 'f':
1033 /* false */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001034 if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
1035 PyUnicode_READ(kind, str, idx + 2) == 'l' &&
1036 PyUnicode_READ(kind, str, idx + 3) == 's' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001037 PyUnicode_READ(kind, str, idx + 4) == 'e') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001038 Py_INCREF(Py_False);
1039 *next_idx_ptr = idx + 5;
1040 return Py_False;
1041 }
1042 break;
1043 case 'N':
1044 /* NaN */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001045 if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001046 PyUnicode_READ(kind, str, idx + 2) == 'N') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001047 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1048 }
1049 break;
1050 case 'I':
1051 /* Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001052 if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
1053 PyUnicode_READ(kind, str, idx + 2) == 'f' &&
1054 PyUnicode_READ(kind, str, idx + 3) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001055 PyUnicode_READ(kind, str, idx + 4) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001056 PyUnicode_READ(kind, str, idx + 5) == 'i' &&
1057 PyUnicode_READ(kind, str, idx + 6) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001058 PyUnicode_READ(kind, str, idx + 7) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001059 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1060 }
1061 break;
1062 case '-':
1063 /* -Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001064 if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001065 PyUnicode_READ(kind, str, idx + 2) == 'n' &&
1066 PyUnicode_READ(kind, str, idx + 3) == 'f' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001067 PyUnicode_READ(kind, str, idx + 4) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001068 PyUnicode_READ(kind, str, idx + 5) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001069 PyUnicode_READ(kind, str, idx + 6) == 'i' &&
1070 PyUnicode_READ(kind, str, idx + 7) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001071 PyUnicode_READ(kind, str, idx + 8) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001072 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1073 }
1074 break;
1075 }
1076 /* Didn't find a string, object, array, or named constant. Look for a number. */
1077 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
1078}
1079
1080static PyObject *
1081scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
1082{
1083 /* Python callable interface to scan_once_{str,unicode} */
1084 PyObject *pystr;
1085 PyObject *rval;
1086 Py_ssize_t idx;
1087 Py_ssize_t next_idx = -1;
1088 static char *kwlist[] = {"string", "idx", NULL};
1089 PyScannerObject *s;
1090 assert(PyScanner_Check(self));
1091 s = (PyScannerObject *)self;
1092 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:scan_once", kwlist, &pystr, _convertPyInt_AsSsize_t, &idx))
1093 return NULL;
1094
1095 if (PyUnicode_Check(pystr)) {
1096 rval = scan_once_unicode(s, pystr, idx, &next_idx);
1097 }
1098 else {
1099 PyErr_Format(PyExc_TypeError,
1100 "first argument must be a string, not %.80s",
1101 Py_TYPE(pystr)->tp_name);
1102 return NULL;
1103 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001104 PyDict_Clear(s->memo);
1105 if (rval == NULL)
1106 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001107 return _build_rval_index_tuple(rval, next_idx);
1108}
1109
1110static PyObject *
1111scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1112{
1113 PyScannerObject *s;
1114 s = (PyScannerObject *)type->tp_alloc(type, 0);
1115 if (s != NULL) {
1116 s->strict = NULL;
1117 s->object_hook = NULL;
1118 s->object_pairs_hook = NULL;
1119 s->parse_float = NULL;
1120 s->parse_int = NULL;
1121 s->parse_constant = NULL;
1122 }
1123 return (PyObject *)s;
1124}
1125
1126static int
1127scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1128{
1129 /* Initialize Scanner object */
1130 PyObject *ctx;
1131 static char *kwlist[] = {"context", NULL};
1132 PyScannerObject *s;
1133
1134 assert(PyScanner_Check(self));
1135 s = (PyScannerObject *)self;
1136
1137 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1138 return -1;
1139
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001140 if (s->memo == NULL) {
1141 s->memo = PyDict_New();
1142 if (s->memo == NULL)
1143 goto bail;
1144 }
1145
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001146 /* All of these will fail "gracefully" so we don't need to verify them */
1147 s->strict = PyObject_GetAttrString(ctx, "strict");
1148 if (s->strict == NULL)
1149 goto bail;
1150 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1151 if (s->object_hook == NULL)
1152 goto bail;
1153 s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1154 if (s->object_pairs_hook == NULL)
1155 goto bail;
1156 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1157 if (s->parse_float == NULL)
1158 goto bail;
1159 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1160 if (s->parse_int == NULL)
1161 goto bail;
1162 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1163 if (s->parse_constant == NULL)
1164 goto bail;
1165
1166 return 0;
1167
1168bail:
1169 Py_CLEAR(s->strict);
1170 Py_CLEAR(s->object_hook);
1171 Py_CLEAR(s->object_pairs_hook);
1172 Py_CLEAR(s->parse_float);
1173 Py_CLEAR(s->parse_int);
1174 Py_CLEAR(s->parse_constant);
1175 return -1;
1176}
1177
1178PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1179
1180static
1181PyTypeObject PyScannerType = {
1182 PyVarObject_HEAD_INIT(NULL, 0)
1183 "_json.Scanner", /* tp_name */
1184 sizeof(PyScannerObject), /* tp_basicsize */
1185 0, /* tp_itemsize */
1186 scanner_dealloc, /* tp_dealloc */
1187 0, /* tp_print */
1188 0, /* tp_getattr */
1189 0, /* tp_setattr */
1190 0, /* tp_compare */
1191 0, /* tp_repr */
1192 0, /* tp_as_number */
1193 0, /* tp_as_sequence */
1194 0, /* tp_as_mapping */
1195 0, /* tp_hash */
1196 scanner_call, /* tp_call */
1197 0, /* tp_str */
1198 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1199 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1200 0, /* tp_as_buffer */
1201 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1202 scanner_doc, /* tp_doc */
1203 scanner_traverse, /* tp_traverse */
1204 scanner_clear, /* tp_clear */
1205 0, /* tp_richcompare */
1206 0, /* tp_weaklistoffset */
1207 0, /* tp_iter */
1208 0, /* tp_iternext */
1209 0, /* tp_methods */
1210 scanner_members, /* tp_members */
1211 0, /* tp_getset */
1212 0, /* tp_base */
1213 0, /* tp_dict */
1214 0, /* tp_descr_get */
1215 0, /* tp_descr_set */
1216 0, /* tp_dictoffset */
1217 scanner_init, /* tp_init */
1218 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1219 scanner_new, /* tp_new */
1220 0,/* PyObject_GC_Del, */ /* tp_free */
1221};
1222
1223static PyObject *
1224encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1225{
1226 PyEncoderObject *s;
1227 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1228 if (s != NULL) {
1229 s->markers = NULL;
1230 s->defaultfn = NULL;
1231 s->encoder = NULL;
1232 s->indent = NULL;
1233 s->key_separator = NULL;
1234 s->item_separator = NULL;
1235 s->sort_keys = NULL;
1236 s->skipkeys = NULL;
1237 }
1238 return (PyObject *)s;
1239}
1240
1241static int
1242encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1243{
1244 /* initialize Encoder object */
1245 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1246
1247 PyEncoderObject *s;
Antoine Pitrou781eba72009-12-08 15:57:31 +00001248 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1249 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001250
1251 assert(PyEncoder_Check(self));
1252 s = (PyEncoderObject *)self;
1253
1254 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
Antoine Pitrou781eba72009-12-08 15:57:31 +00001255 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1256 &sort_keys, &skipkeys, &allow_nan))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001257 return -1;
1258
Antoine Pitrou781eba72009-12-08 15:57:31 +00001259 s->markers = markers;
1260 s->defaultfn = defaultfn;
1261 s->encoder = encoder;
1262 s->indent = indent;
1263 s->key_separator = key_separator;
1264 s->item_separator = item_separator;
1265 s->sort_keys = sort_keys;
1266 s->skipkeys = skipkeys;
1267 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1268 s->allow_nan = PyObject_IsTrue(allow_nan);
1269
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001270 Py_INCREF(s->markers);
1271 Py_INCREF(s->defaultfn);
1272 Py_INCREF(s->encoder);
1273 Py_INCREF(s->indent);
1274 Py_INCREF(s->key_separator);
1275 Py_INCREF(s->item_separator);
1276 Py_INCREF(s->sort_keys);
1277 Py_INCREF(s->skipkeys);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001278 return 0;
1279}
1280
1281static PyObject *
1282encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1283{
1284 /* Python callable interface to encode_listencode_obj */
1285 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1286 PyObject *obj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001287 Py_ssize_t indent_level;
1288 PyEncoderObject *s;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001289 _PyAccu acc;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001290
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001291 assert(PyEncoder_Check(self));
1292 s = (PyEncoderObject *)self;
1293 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:_iterencode", kwlist,
1294 &obj, _convertPyInt_AsSsize_t, &indent_level))
1295 return NULL;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001296 if (_PyAccu_Init(&acc))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001297 return NULL;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001298 if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001299 _PyAccu_Destroy(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001300 return NULL;
1301 }
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001302 return _PyAccu_FinishAsList(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001303}
1304
1305static PyObject *
1306_encoded_const(PyObject *obj)
1307{
1308 /* Return the JSON string representation of None, True, False */
1309 if (obj == Py_None) {
1310 static PyObject *s_null = NULL;
1311 if (s_null == NULL) {
1312 s_null = PyUnicode_InternFromString("null");
1313 }
1314 Py_INCREF(s_null);
1315 return s_null;
1316 }
1317 else if (obj == Py_True) {
1318 static PyObject *s_true = NULL;
1319 if (s_true == NULL) {
1320 s_true = PyUnicode_InternFromString("true");
1321 }
1322 Py_INCREF(s_true);
1323 return s_true;
1324 }
1325 else if (obj == Py_False) {
1326 static PyObject *s_false = NULL;
1327 if (s_false == NULL) {
1328 s_false = PyUnicode_InternFromString("false");
1329 }
1330 Py_INCREF(s_false);
1331 return s_false;
1332 }
1333 else {
1334 PyErr_SetString(PyExc_ValueError, "not a const");
1335 return NULL;
1336 }
1337}
1338
1339static PyObject *
1340encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1341{
1342 /* Return the JSON representation of a PyFloat */
1343 double i = PyFloat_AS_DOUBLE(obj);
1344 if (!Py_IS_FINITE(i)) {
1345 if (!s->allow_nan) {
1346 PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant");
1347 return NULL;
1348 }
1349 if (i > 0) {
1350 return PyUnicode_FromString("Infinity");
1351 }
1352 else if (i < 0) {
1353 return PyUnicode_FromString("-Infinity");
1354 }
1355 else {
1356 return PyUnicode_FromString("NaN");
1357 }
1358 }
1359 /* Use a better float format here? */
1360 return PyObject_Repr(obj);
1361}
1362
1363static PyObject *
1364encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1365{
1366 /* Return the JSON representation of a string */
1367 if (s->fast_encode)
1368 return py_encode_basestring_ascii(NULL, obj);
1369 else
1370 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1371}
1372
1373static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001374_steal_accumulate(_PyAccu *acc, PyObject *stolen)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001375{
1376 /* Append stolen and then decrement its reference count */
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001377 int rval = _PyAccu_Accumulate(acc, stolen);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001378 Py_DECREF(stolen);
1379 return rval;
1380}
1381
1382static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001383encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001384 PyObject *obj, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001385{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001386 /* Encode Python object obj to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001387 PyObject *newobj;
1388 int rv;
1389
1390 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1391 PyObject *cstr = _encoded_const(obj);
1392 if (cstr == NULL)
1393 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001394 return _steal_accumulate(acc, cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001395 }
1396 else if (PyUnicode_Check(obj))
1397 {
1398 PyObject *encoded = encoder_encode_string(s, obj);
1399 if (encoded == NULL)
1400 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001401 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001402 }
1403 else if (PyLong_Check(obj)) {
1404 PyObject *encoded = PyObject_Str(obj);
1405 if (encoded == NULL)
1406 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001407 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001408 }
1409 else if (PyFloat_Check(obj)) {
1410 PyObject *encoded = encoder_encode_float(s, obj);
1411 if (encoded == NULL)
1412 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001413 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001414 }
1415 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001416 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1417 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001418 rv = encoder_listencode_list(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001419 Py_LeaveRecursiveCall();
1420 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001421 }
1422 else if (PyDict_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001423 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1424 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001425 rv = encoder_listencode_dict(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001426 Py_LeaveRecursiveCall();
1427 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001428 }
1429 else {
1430 PyObject *ident = NULL;
1431 if (s->markers != Py_None) {
1432 int has_key;
1433 ident = PyLong_FromVoidPtr(obj);
1434 if (ident == NULL)
1435 return -1;
1436 has_key = PyDict_Contains(s->markers, ident);
1437 if (has_key) {
1438 if (has_key != -1)
1439 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1440 Py_DECREF(ident);
1441 return -1;
1442 }
1443 if (PyDict_SetItem(s->markers, ident, obj)) {
1444 Py_DECREF(ident);
1445 return -1;
1446 }
1447 }
1448 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1449 if (newobj == NULL) {
1450 Py_XDECREF(ident);
1451 return -1;
1452 }
Ezio Melotti13672652011-05-11 01:02:56 +03001453
1454 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1455 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001456 rv = encoder_listencode_obj(s, acc, newobj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001457 Py_LeaveRecursiveCall();
1458
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001459 Py_DECREF(newobj);
1460 if (rv) {
1461 Py_XDECREF(ident);
1462 return -1;
1463 }
1464 if (ident != NULL) {
1465 if (PyDict_DelItem(s->markers, ident)) {
1466 Py_XDECREF(ident);
1467 return -1;
1468 }
1469 Py_XDECREF(ident);
1470 }
1471 return rv;
1472 }
1473}
1474
1475static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001476encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001477 PyObject *dct, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001478{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001479 /* Encode Python dict dct a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001480 static PyObject *open_dict = NULL;
1481 static PyObject *close_dict = NULL;
1482 static PyObject *empty_dict = NULL;
1483 PyObject *kstr = NULL;
1484 PyObject *ident = NULL;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001485 PyObject *it = NULL;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001486 PyObject *items;
1487 PyObject *item = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001488 int skipkeys;
1489 Py_ssize_t idx;
1490
1491 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
1492 open_dict = PyUnicode_InternFromString("{");
1493 close_dict = PyUnicode_InternFromString("}");
1494 empty_dict = PyUnicode_InternFromString("{}");
1495 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
1496 return -1;
1497 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001498 if (Py_SIZE(dct) == 0)
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001499 return _PyAccu_Accumulate(acc, empty_dict);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001500
1501 if (s->markers != Py_None) {
1502 int has_key;
1503 ident = PyLong_FromVoidPtr(dct);
1504 if (ident == NULL)
1505 goto bail;
1506 has_key = PyDict_Contains(s->markers, ident);
1507 if (has_key) {
1508 if (has_key != -1)
1509 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1510 goto bail;
1511 }
1512 if (PyDict_SetItem(s->markers, ident, dct)) {
1513 goto bail;
1514 }
1515 }
1516
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001517 if (_PyAccu_Accumulate(acc, open_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001518 goto bail;
1519
1520 if (s->indent != Py_None) {
1521 /* TODO: DOES NOT RUN */
1522 indent_level += 1;
1523 /*
1524 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1525 separator = _item_separator + newline_indent
1526 buf += newline_indent
1527 */
1528 }
1529
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001530 if (PyObject_IsTrue(s->sort_keys)) {
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001531 /* First sort the keys then replace them with (key, value) tuples. */
1532 Py_ssize_t i, nitems;
1533 items = PyMapping_Keys(dct);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 if (items == NULL)
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001535 goto bail;
1536 if (!PyList_Check(items)) {
1537 PyErr_SetString(PyExc_ValueError, "keys must return list");
1538 goto bail;
1539 }
1540 if (PyList_Sort(items) < 0)
1541 goto bail;
1542 nitems = PyList_GET_SIZE(items);
1543 for (i = 0; i < nitems; i++) {
1544 PyObject *key, *value;
1545 key = PyList_GET_ITEM(items, i);
1546 value = PyDict_GetItem(dct, key);
1547 item = PyTuple_Pack(2, key, value);
1548 if (item == NULL)
1549 goto bail;
1550 PyList_SET_ITEM(items, i, item);
1551 Py_DECREF(key);
1552 }
1553 }
1554 else {
1555 items = PyMapping_Items(dct);
1556 }
1557 if (items == NULL)
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001558 goto bail;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001559 it = PyObject_GetIter(items);
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001560 Py_DECREF(items);
1561 if (it == NULL)
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001562 goto bail;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001563 skipkeys = PyObject_IsTrue(s->skipkeys);
1564 idx = 0;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001565 while ((item = PyIter_Next(it)) != NULL) {
1566 PyObject *encoded, *key, *value;
1567 if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
1568 PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
1569 goto bail;
1570 }
1571 key = PyTuple_GET_ITEM(item, 0);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001572 if (PyUnicode_Check(key)) {
1573 Py_INCREF(key);
1574 kstr = key;
1575 }
1576 else if (PyFloat_Check(key)) {
1577 kstr = encoder_encode_float(s, key);
1578 if (kstr == NULL)
1579 goto bail;
1580 }
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001581 else if (key == Py_True || key == Py_False || key == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 /* This must come before the PyLong_Check because
1583 True and False are also 1 and 0.*/
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001584 kstr = _encoded_const(key);
1585 if (kstr == NULL)
1586 goto bail;
1587 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001588 else if (PyLong_Check(key)) {
1589 kstr = PyObject_Str(key);
1590 if (kstr == NULL)
1591 goto bail;
1592 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001593 else if (skipkeys) {
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001594 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001595 continue;
1596 }
1597 else {
1598 /* TODO: include repr of key */
Doug Hellmann1c524752010-07-21 12:29:04 +00001599 PyErr_SetString(PyExc_TypeError, "keys must be a string");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001600 goto bail;
1601 }
1602
1603 if (idx) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001604 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001605 goto bail;
1606 }
1607
1608 encoded = encoder_encode_string(s, kstr);
1609 Py_CLEAR(kstr);
1610 if (encoded == NULL)
1611 goto bail;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001612 if (_PyAccu_Accumulate(acc, encoded)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001613 Py_DECREF(encoded);
1614 goto bail;
1615 }
1616 Py_DECREF(encoded);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001617 if (_PyAccu_Accumulate(acc, s->key_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001618 goto bail;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001619
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001620 value = PyTuple_GET_ITEM(item, 1);
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001621 if (encoder_listencode_obj(s, acc, value, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001622 goto bail;
1623 idx += 1;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001624 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001625 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001626 if (PyErr_Occurred())
1627 goto bail;
1628 Py_CLEAR(it);
1629
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001630 if (ident != NULL) {
1631 if (PyDict_DelItem(s->markers, ident))
1632 goto bail;
1633 Py_CLEAR(ident);
1634 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001635 /* TODO DOES NOT RUN; dead code
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001636 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001637 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001638
1639 yield '\n' + (' ' * (_indent * _current_indent_level))
1640 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001641 if (_PyAccu_Accumulate(acc, close_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001642 goto bail;
1643 return 0;
1644
1645bail:
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001646 Py_XDECREF(it);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001647 Py_XDECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001648 Py_XDECREF(kstr);
1649 Py_XDECREF(ident);
1650 return -1;
1651}
1652
1653
1654static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001655encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001656 PyObject *seq, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001657{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001658 /* Encode Python list seq to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001659 static PyObject *open_array = NULL;
1660 static PyObject *close_array = NULL;
1661 static PyObject *empty_array = NULL;
1662 PyObject *ident = NULL;
1663 PyObject *s_fast = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001664 Py_ssize_t i;
1665
1666 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
1667 open_array = PyUnicode_InternFromString("[");
1668 close_array = PyUnicode_InternFromString("]");
1669 empty_array = PyUnicode_InternFromString("[]");
1670 if (open_array == NULL || close_array == NULL || empty_array == NULL)
1671 return -1;
1672 }
1673 ident = NULL;
1674 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
1675 if (s_fast == NULL)
1676 return -1;
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001677 if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001678 Py_DECREF(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001679 return _PyAccu_Accumulate(acc, empty_array);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001680 }
1681
1682 if (s->markers != Py_None) {
1683 int has_key;
1684 ident = PyLong_FromVoidPtr(seq);
1685 if (ident == NULL)
1686 goto bail;
1687 has_key = PyDict_Contains(s->markers, ident);
1688 if (has_key) {
1689 if (has_key != -1)
1690 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1691 goto bail;
1692 }
1693 if (PyDict_SetItem(s->markers, ident, seq)) {
1694 goto bail;
1695 }
1696 }
1697
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001698 if (_PyAccu_Accumulate(acc, open_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001699 goto bail;
1700 if (s->indent != Py_None) {
1701 /* TODO: DOES NOT RUN */
1702 indent_level += 1;
1703 /*
1704 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1705 separator = _item_separator + newline_indent
1706 buf += newline_indent
1707 */
1708 }
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001709 for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
1710 PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001711 if (i) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001712 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001713 goto bail;
1714 }
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001715 if (encoder_listencode_obj(s, acc, obj, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001716 goto bail;
1717 }
1718 if (ident != NULL) {
1719 if (PyDict_DelItem(s->markers, ident))
1720 goto bail;
1721 Py_CLEAR(ident);
1722 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001723
1724 /* TODO: DOES NOT RUN
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001725 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001726 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001727
1728 yield '\n' + (' ' * (_indent * _current_indent_level))
1729 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001730 if (_PyAccu_Accumulate(acc, close_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001731 goto bail;
1732 Py_DECREF(s_fast);
1733 return 0;
1734
1735bail:
1736 Py_XDECREF(ident);
1737 Py_DECREF(s_fast);
1738 return -1;
1739}
1740
1741static void
1742encoder_dealloc(PyObject *self)
1743{
1744 /* Deallocate Encoder */
1745 encoder_clear(self);
1746 Py_TYPE(self)->tp_free(self);
1747}
1748
1749static int
1750encoder_traverse(PyObject *self, visitproc visit, void *arg)
1751{
1752 PyEncoderObject *s;
1753 assert(PyEncoder_Check(self));
1754 s = (PyEncoderObject *)self;
1755 Py_VISIT(s->markers);
1756 Py_VISIT(s->defaultfn);
1757 Py_VISIT(s->encoder);
1758 Py_VISIT(s->indent);
1759 Py_VISIT(s->key_separator);
1760 Py_VISIT(s->item_separator);
1761 Py_VISIT(s->sort_keys);
1762 Py_VISIT(s->skipkeys);
1763 return 0;
1764}
1765
1766static int
1767encoder_clear(PyObject *self)
1768{
1769 /* Deallocate Encoder */
1770 PyEncoderObject *s;
1771 assert(PyEncoder_Check(self));
1772 s = (PyEncoderObject *)self;
1773 Py_CLEAR(s->markers);
1774 Py_CLEAR(s->defaultfn);
1775 Py_CLEAR(s->encoder);
1776 Py_CLEAR(s->indent);
1777 Py_CLEAR(s->key_separator);
1778 Py_CLEAR(s->item_separator);
1779 Py_CLEAR(s->sort_keys);
1780 Py_CLEAR(s->skipkeys);
1781 return 0;
1782}
1783
1784PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
1785
1786static
1787PyTypeObject PyEncoderType = {
1788 PyVarObject_HEAD_INIT(NULL, 0)
1789 "_json.Encoder", /* tp_name */
1790 sizeof(PyEncoderObject), /* tp_basicsize */
1791 0, /* tp_itemsize */
1792 encoder_dealloc, /* tp_dealloc */
1793 0, /* tp_print */
1794 0, /* tp_getattr */
1795 0, /* tp_setattr */
1796 0, /* tp_compare */
1797 0, /* tp_repr */
1798 0, /* tp_as_number */
1799 0, /* tp_as_sequence */
1800 0, /* tp_as_mapping */
1801 0, /* tp_hash */
1802 encoder_call, /* tp_call */
1803 0, /* tp_str */
1804 0, /* tp_getattro */
1805 0, /* tp_setattro */
1806 0, /* tp_as_buffer */
1807 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1808 encoder_doc, /* tp_doc */
1809 encoder_traverse, /* tp_traverse */
1810 encoder_clear, /* tp_clear */
1811 0, /* tp_richcompare */
1812 0, /* tp_weaklistoffset */
1813 0, /* tp_iter */
1814 0, /* tp_iternext */
1815 0, /* tp_methods */
1816 encoder_members, /* tp_members */
1817 0, /* tp_getset */
1818 0, /* tp_base */
1819 0, /* tp_dict */
1820 0, /* tp_descr_get */
1821 0, /* tp_descr_set */
1822 0, /* tp_dictoffset */
1823 encoder_init, /* tp_init */
1824 0, /* tp_alloc */
1825 encoder_new, /* tp_new */
1826 0, /* tp_free */
1827};
1828
1829static PyMethodDef speedups_methods[] = {
1830 {"encode_basestring_ascii",
1831 (PyCFunction)py_encode_basestring_ascii,
1832 METH_O,
1833 pydoc_encode_basestring_ascii},
1834 {"scanstring",
1835 (PyCFunction)py_scanstring,
1836 METH_VARARGS,
1837 pydoc_scanstring},
Christian Heimes90540002008-05-08 14:29:10 +00001838 {NULL, NULL, 0, NULL}
1839};
1840
1841PyDoc_STRVAR(module_doc,
1842"json speedups\n");
1843
Martin v. Löwis1a214512008-06-11 05:26:20 +00001844static struct PyModuleDef jsonmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 PyModuleDef_HEAD_INIT,
1846 "_json",
1847 module_doc,
1848 -1,
1849 speedups_methods,
1850 NULL,
1851 NULL,
1852 NULL,
1853 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001854};
1855
1856PyObject*
1857PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +00001858{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001859 PyObject *m = PyModule_Create(&jsonmodule);
1860 if (!m)
1861 return NULL;
1862 PyScannerType.tp_new = PyType_GenericNew;
1863 if (PyType_Ready(&PyScannerType) < 0)
1864 goto fail;
1865 PyEncoderType.tp_new = PyType_GenericNew;
1866 if (PyType_Ready(&PyEncoderType) < 0)
1867 goto fail;
1868 Py_INCREF((PyObject*)&PyScannerType);
1869 if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1870 Py_DECREF((PyObject*)&PyScannerType);
1871 goto fail;
1872 }
1873 Py_INCREF((PyObject*)&PyEncoderType);
1874 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1875 Py_DECREF((PyObject*)&PyEncoderType);
1876 goto fail;
1877 }
1878 return m;
1879 fail:
1880 Py_DECREF(m);
1881 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +00001882}