blob: cfe87087d7f3c6bd78b98213516fb4430ae9556c [file] [log] [blame]
Brett Cannon4b964f92008-05-05 20:21:38 +00001#include "Python.h"
Bob Ippolitod914e3f2009-03-17 23:19:00 +00002#include "structmember.h"
3#if PY_VERSION_HEX < 0x02060000 && !defined(Py_TYPE)
4#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
5#endif
6#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
7typedef 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
12#endif
13#ifndef Py_IS_FINITE
14#define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X))
15#endif
16
17#ifdef __GNUC__
18#define UNUSED __attribute__((__unused__))
19#else
20#define UNUSED
21#endif
Brett Cannon4b964f92008-05-05 20:21:38 +000022
23#define DEFAULT_ENCODING "utf-8"
Brett Cannon4b964f92008-05-05 20:21:38 +000024
Bob Ippolitod914e3f2009-03-17 23:19:00 +000025#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 *encoding;
36 PyObject *strict;
37 PyObject *object_hook;
Raymond Hettinger91852ca2009-03-19 19:19:03 +000038 PyObject *pairs_hook;
Bob Ippolitod914e3f2009-03-17 23:19:00 +000039 PyObject *parse_float;
40 PyObject *parse_int;
41 PyObject *parse_constant;
42} PyScannerObject;
43
44static PyMemberDef scanner_members[] = {
45 {"encoding", T_OBJECT, offsetof(PyScannerObject, encoding), READONLY, "encoding"},
46 {"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"},
47 {"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"},
Raymond Hettinger91852ca2009-03-19 19:19:03 +000048 {"object_pairs_hook", T_OBJECT, offsetof(PyScannerObject, pairs_hook), READONLY, "object_pairs_hook"},
Bob Ippolitod914e3f2009-03-17 23:19:00 +000049 {"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"},
50 {"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "parse_int"},
51 {"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READONLY, "parse_constant"},
52 {NULL}
53};
54
55typedef struct _PyEncoderObject {
56 PyObject_HEAD
57 PyObject *markers;
58 PyObject *defaultfn;
59 PyObject *encoder;
60 PyObject *indent;
61 PyObject *key_separator;
62 PyObject *item_separator;
63 PyObject *sort_keys;
64 PyObject *skipkeys;
65 int fast_encode;
66 int allow_nan;
67} PyEncoderObject;
68
69static PyMemberDef encoder_members[] = {
70 {"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers"},
71 {"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "default"},
72 {"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder"},
73 {"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"},
74 {"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READONLY, "key_separator"},
75 {"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READONLY, "item_separator"},
76 {"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sort_keys"},
77 {"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipkeys"},
78 {NULL}
79};
80
81static Py_ssize_t
82ascii_escape_char(Py_UNICODE c, char *output, Py_ssize_t chars);
83static PyObject *
84ascii_escape_unicode(PyObject *pystr);
85static PyObject *
86ascii_escape_str(PyObject *pystr);
87static PyObject *
88py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr);
89void init_json(void);
90static PyObject *
91scan_once_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr);
92static PyObject *
93scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr);
94static PyObject *
95_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx);
96static PyObject *
97scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
98static int
99scanner_init(PyObject *self, PyObject *args, PyObject *kwds);
100static void
101scanner_dealloc(PyObject *self);
102static int
103scanner_clear(PyObject *self);
104static PyObject *
105encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
106static int
107encoder_init(PyObject *self, PyObject *args, PyObject *kwds);
108static void
109encoder_dealloc(PyObject *self);
110static int
111encoder_clear(PyObject *self);
112static int
113encoder_listencode_list(PyEncoderObject *s, PyObject *rval, PyObject *seq, Py_ssize_t indent_level);
114static int
115encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssize_t indent_level);
116static int
117encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ssize_t indent_level);
118static PyObject *
Raymond Hettingera0b8d9a2009-03-19 19:24:43 +0000119_encoded_const(PyObject *obj);
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000120static void
121raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
122static PyObject *
123encoder_encode_string(PyEncoderObject *s, PyObject *obj);
124static int
125_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr);
126static PyObject *
127_convertPyInt_FromSsize_t(Py_ssize_t *size_ptr);
128static PyObject *
129encoder_encode_float(PyEncoderObject *s, PyObject *obj);
130
131#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
132#define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
133
134#define MIN_EXPANSION 6
Brett Cannon4b964f92008-05-05 20:21:38 +0000135#ifdef Py_UNICODE_WIDE
136#define MAX_EXPANSION (2 * MIN_EXPANSION)
137#else
138#define MAX_EXPANSION MIN_EXPANSION
139#endif
140
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000141static int
142_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr)
143{
144 /* PyObject to Py_ssize_t converter */
145 *size_ptr = PyInt_AsSsize_t(o);
Georg Brandlf71ba952009-05-05 07:48:12 +0000146 if (*size_ptr == -1 && PyErr_Occurred())
147 return 0;
148 return 1;
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000149}
150
151static PyObject *
152_convertPyInt_FromSsize_t(Py_ssize_t *size_ptr)
153{
154 /* Py_ssize_t to PyObject converter */
155 return PyInt_FromSsize_t(*size_ptr);
156}
157
Brett Cannon4b964f92008-05-05 20:21:38 +0000158static Py_ssize_t
159ascii_escape_char(Py_UNICODE c, char *output, Py_ssize_t chars)
160{
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000161 /* Escape unicode code point c to ASCII escape sequences
162 in char *output. output must have at least 12 bytes unused to
163 accommodate an escaped surrogate pair "\uXXXX\uXXXX" */
Brett Cannon4b964f92008-05-05 20:21:38 +0000164 output[chars++] = '\\';
165 switch (c) {
166 case '\\': output[chars++] = (char)c; break;
167 case '"': output[chars++] = (char)c; break;
168 case '\b': output[chars++] = 'b'; break;
169 case '\f': output[chars++] = 'f'; break;
170 case '\n': output[chars++] = 'n'; break;
171 case '\r': output[chars++] = 'r'; break;
172 case '\t': output[chars++] = 't'; break;
173 default:
174#ifdef Py_UNICODE_WIDE
175 if (c >= 0x10000) {
176 /* UTF-16 surrogate pair */
177 Py_UNICODE v = c - 0x10000;
178 c = 0xd800 | ((v >> 10) & 0x3ff);
179 output[chars++] = 'u';
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000180 output[chars++] = "0123456789abcdef"[(c >> 12) & 0xf];
181 output[chars++] = "0123456789abcdef"[(c >> 8) & 0xf];
182 output[chars++] = "0123456789abcdef"[(c >> 4) & 0xf];
183 output[chars++] = "0123456789abcdef"[(c ) & 0xf];
Brett Cannon4b964f92008-05-05 20:21:38 +0000184 c = 0xdc00 | (v & 0x3ff);
185 output[chars++] = '\\';
186 }
187#endif
188 output[chars++] = 'u';
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000189 output[chars++] = "0123456789abcdef"[(c >> 12) & 0xf];
190 output[chars++] = "0123456789abcdef"[(c >> 8) & 0xf];
191 output[chars++] = "0123456789abcdef"[(c >> 4) & 0xf];
192 output[chars++] = "0123456789abcdef"[(c ) & 0xf];
Brett Cannon4b964f92008-05-05 20:21:38 +0000193 }
194 return chars;
195}
196
197static PyObject *
198ascii_escape_unicode(PyObject *pystr)
199{
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000200 /* Take a PyUnicode pystr and return a new ASCII-only escaped PyString */
Brett Cannon4b964f92008-05-05 20:21:38 +0000201 Py_ssize_t i;
202 Py_ssize_t input_chars;
203 Py_ssize_t output_size;
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000204 Py_ssize_t max_output_size;
Brett Cannon4b964f92008-05-05 20:21:38 +0000205 Py_ssize_t chars;
206 PyObject *rval;
207 char *output;
208 Py_UNICODE *input_unicode;
209
210 input_chars = PyUnicode_GET_SIZE(pystr);
211 input_unicode = PyUnicode_AS_UNICODE(pystr);
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000212
Brett Cannon4b964f92008-05-05 20:21:38 +0000213 /* One char input can be up to 6 chars output, estimate 4 of these */
214 output_size = 2 + (MIN_EXPANSION * 4) + input_chars;
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000215 max_output_size = 2 + (input_chars * MAX_EXPANSION);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000216 rval = PyString_FromStringAndSize(NULL, output_size);
Brett Cannon4b964f92008-05-05 20:21:38 +0000217 if (rval == NULL) {
218 return NULL;
219 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000220 output = PyString_AS_STRING(rval);
Brett Cannon4b964f92008-05-05 20:21:38 +0000221 chars = 0;
222 output[chars++] = '"';
223 for (i = 0; i < input_chars; i++) {
224 Py_UNICODE c = input_unicode[i];
225 if (S_CHAR(c)) {
226 output[chars++] = (char)c;
227 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000228 else {
Brett Cannon4b964f92008-05-05 20:21:38 +0000229 chars = ascii_escape_char(c, output, chars);
230 }
231 if (output_size - chars < (1 + MAX_EXPANSION)) {
232 /* There's more than four, so let's resize by a lot */
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000233 Py_ssize_t new_output_size = output_size * 2;
Brett Cannon4b964f92008-05-05 20:21:38 +0000234 /* This is an upper bound */
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000235 if (new_output_size > max_output_size) {
236 new_output_size = max_output_size;
Brett Cannon4b964f92008-05-05 20:21:38 +0000237 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000238 /* Make sure that the output size changed before resizing */
239 if (new_output_size != output_size) {
240 output_size = new_output_size;
241 if (_PyString_Resize(&rval, output_size) == -1) {
242 return NULL;
243 }
244 output = PyString_AS_STRING(rval);
Brett Cannon4b964f92008-05-05 20:21:38 +0000245 }
Brett Cannon4b964f92008-05-05 20:21:38 +0000246 }
247 }
248 output[chars++] = '"';
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000249 if (_PyString_Resize(&rval, chars) == -1) {
Brett Cannon4b964f92008-05-05 20:21:38 +0000250 return NULL;
251 }
252 return rval;
253}
254
255static PyObject *
256ascii_escape_str(PyObject *pystr)
257{
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000258 /* Take a PyString pystr and return a new ASCII-only escaped PyString */
Brett Cannon4b964f92008-05-05 20:21:38 +0000259 Py_ssize_t i;
260 Py_ssize_t input_chars;
261 Py_ssize_t output_size;
262 Py_ssize_t chars;
263 PyObject *rval;
264 char *output;
265 char *input_str;
266
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000267 input_chars = PyString_GET_SIZE(pystr);
268 input_str = PyString_AS_STRING(pystr);
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000269
270 /* Fast path for a string that's already ASCII */
271 for (i = 0; i < input_chars; i++) {
272 Py_UNICODE c = (Py_UNICODE)(unsigned char)input_str[i];
273 if (!S_CHAR(c)) {
274 /* If we have to escape something, scan the string for unicode */
275 Py_ssize_t j;
276 for (j = i; j < input_chars; j++) {
277 c = (Py_UNICODE)(unsigned char)input_str[j];
278 if (c > 0x7f) {
279 /* We hit a non-ASCII character, bail to unicode mode */
280 PyObject *uni;
281 uni = PyUnicode_DecodeUTF8(input_str, input_chars, "strict");
282 if (uni == NULL) {
283 return NULL;
284 }
285 rval = ascii_escape_unicode(uni);
286 Py_DECREF(uni);
287 return rval;
288 }
289 }
290 break;
291 }
292 }
293
294 if (i == input_chars) {
295 /* Input is already ASCII */
296 output_size = 2 + input_chars;
297 }
298 else {
299 /* One char input can be up to 6 chars output, estimate 4 of these */
300 output_size = 2 + (MIN_EXPANSION * 4) + input_chars;
301 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000302 rval = PyString_FromStringAndSize(NULL, output_size);
Brett Cannon4b964f92008-05-05 20:21:38 +0000303 if (rval == NULL) {
304 return NULL;
305 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000306 output = PyString_AS_STRING(rval);
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000307 output[0] = '"';
308
309 /* We know that everything up to i is ASCII already */
310 chars = i + 1;
311 memcpy(&output[1], input_str, i);
312
313 for (; i < input_chars; i++) {
314 Py_UNICODE c = (Py_UNICODE)(unsigned char)input_str[i];
Brett Cannon4b964f92008-05-05 20:21:38 +0000315 if (S_CHAR(c)) {
316 output[chars++] = (char)c;
317 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000318 else {
Brett Cannon4b964f92008-05-05 20:21:38 +0000319 chars = ascii_escape_char(c, output, chars);
320 }
321 /* An ASCII char can't possibly expand to a surrogate! */
322 if (output_size - chars < (1 + MIN_EXPANSION)) {
323 /* There's more than four, so let's resize by a lot */
324 output_size *= 2;
325 if (output_size > 2 + (input_chars * MIN_EXPANSION)) {
326 output_size = 2 + (input_chars * MIN_EXPANSION);
327 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000328 if (_PyString_Resize(&rval, output_size) == -1) {
Brett Cannon4b964f92008-05-05 20:21:38 +0000329 return NULL;
330 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000331 output = PyString_AS_STRING(rval);
Brett Cannon4b964f92008-05-05 20:21:38 +0000332 }
333 }
334 output[chars++] = '"';
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000335 if (_PyString_Resize(&rval, chars) == -1) {
Brett Cannon4b964f92008-05-05 20:21:38 +0000336 return NULL;
337 }
338 return rval;
339}
340
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000341static void
Brett Cannon4b964f92008-05-05 20:21:38 +0000342raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
343{
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000344 /* Use the Python function json.decoder.errmsg to raise a nice
345 looking ValueError exception */
Brett Cannon4b964f92008-05-05 20:21:38 +0000346 static PyObject *errmsg_fn = NULL;
347 PyObject *pymsg;
348 if (errmsg_fn == NULL) {
349 PyObject *decoder = PyImport_ImportModule("json.decoder");
350 if (decoder == NULL)
351 return;
352 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000353 Py_DECREF(decoder);
Brett Cannon4b964f92008-05-05 20:21:38 +0000354 if (errmsg_fn == NULL)
355 return;
Brett Cannon4b964f92008-05-05 20:21:38 +0000356 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000357 pymsg = PyObject_CallFunction(errmsg_fn, "(zOO&)", msg, s, _convertPyInt_FromSsize_t, &end);
Benjamin Peterson595e3cb2008-10-16 21:09:28 +0000358 if (pymsg) {
359 PyErr_SetObject(PyExc_ValueError, pymsg);
360 Py_DECREF(pymsg);
361 }
Brett Cannon4b964f92008-05-05 20:21:38 +0000362}
363
364static PyObject *
365join_list_unicode(PyObject *lst)
366{
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000367 /* return u''.join(lst) */
368 static PyObject *joinfn = NULL;
369 if (joinfn == NULL) {
370 PyObject *ustr = PyUnicode_FromUnicode(NULL, 0);
371 if (ustr == NULL)
372 return NULL;
373
374 joinfn = PyObject_GetAttrString(ustr, "join");
375 Py_DECREF(ustr);
376 if (joinfn == NULL)
377 return NULL;
Brett Cannon4b964f92008-05-05 20:21:38 +0000378 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000379 return PyObject_CallFunctionObjArgs(joinfn, lst, NULL);
Brett Cannon4b964f92008-05-05 20:21:38 +0000380}
381
382static PyObject *
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000383join_list_string(PyObject *lst)
Brett Cannon4b964f92008-05-05 20:21:38 +0000384{
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000385 /* return ''.join(lst) */
386 static PyObject *joinfn = NULL;
387 if (joinfn == NULL) {
388 PyObject *ustr = PyString_FromStringAndSize(NULL, 0);
389 if (ustr == NULL)
390 return NULL;
391
392 joinfn = PyObject_GetAttrString(ustr, "join");
393 Py_DECREF(ustr);
394 if (joinfn == NULL)
395 return NULL;
396 }
397 return PyObject_CallFunctionObjArgs(joinfn, lst, NULL);
398}
399
400static PyObject *
401_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
402 /* return (rval, idx) tuple, stealing reference to rval */
403 PyObject *tpl;
404 PyObject *pyidx;
405 /*
406 steal a reference to rval, returns (rval, idx)
407 */
408 if (rval == NULL) {
409 return NULL;
410 }
411 pyidx = PyInt_FromSsize_t(idx);
412 if (pyidx == NULL) {
413 Py_DECREF(rval);
414 return NULL;
415 }
416 tpl = PyTuple_New(2);
417 if (tpl == NULL) {
418 Py_DECREF(pyidx);
419 Py_DECREF(rval);
420 return NULL;
421 }
422 PyTuple_SET_ITEM(tpl, 0, rval);
423 PyTuple_SET_ITEM(tpl, 1, pyidx);
424 return tpl;
425}
426
427static PyObject *
428scanstring_str(PyObject *pystr, Py_ssize_t end, char *encoding, int strict, Py_ssize_t *next_end_ptr)
429{
430 /* Read the JSON string from PyString pystr.
431 end is the index of the first character after the quote.
432 encoding is the encoding of pystr (must be an ASCII superset)
433 if strict is zero then literal control characters are allowed
434 *next_end_ptr is a return-by-reference index of the character
435 after the end quote
436
437 Return value is a new PyString (if ASCII-only) or PyUnicode
438 */
Brett Cannon4b964f92008-05-05 20:21:38 +0000439 PyObject *rval;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000440 Py_ssize_t len = PyString_GET_SIZE(pystr);
Brett Cannon4b964f92008-05-05 20:21:38 +0000441 Py_ssize_t begin = end - 1;
442 Py_ssize_t next = begin;
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000443 int has_unicode = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000444 char *buf = PyString_AS_STRING(pystr);
Brett Cannon4b964f92008-05-05 20:21:38 +0000445 PyObject *chunks = PyList_New(0);
446 if (chunks == NULL) {
447 goto bail;
448 }
Bob Ippolitod648f642008-07-19 21:59:50 +0000449 if (end < 0 || len <= end) {
450 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
451 goto bail;
452 }
Brett Cannon4b964f92008-05-05 20:21:38 +0000453 while (1) {
454 /* Find the end of the string or the next escape */
455 Py_UNICODE c = 0;
456 PyObject *chunk = NULL;
457 for (next = end; next < len; next++) {
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000458 c = (unsigned char)buf[next];
Brett Cannon4b964f92008-05-05 20:21:38 +0000459 if (c == '"' || c == '\\') {
460 break;
461 }
462 else if (strict && c <= 0x1f) {
Bob Ippolitod648f642008-07-19 21:59:50 +0000463 raise_errmsg("Invalid control character at", pystr, next);
Brett Cannon4b964f92008-05-05 20:21:38 +0000464 goto bail;
465 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000466 else if (c > 0x7f) {
467 has_unicode = 1;
468 }
Brett Cannon4b964f92008-05-05 20:21:38 +0000469 }
470 if (!(c == '"' || c == '\\')) {
471 raise_errmsg("Unterminated string starting at", pystr, begin);
472 goto bail;
473 }
474 /* Pick up this chunk if it's not zero length */
475 if (next != end) {
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000476 PyObject *strchunk = PyString_FromStringAndSize(&buf[end], next - end);
Brett Cannon4b964f92008-05-05 20:21:38 +0000477 if (strchunk == NULL) {
478 goto bail;
479 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000480 if (has_unicode) {
481 chunk = PyUnicode_FromEncodedObject(strchunk, encoding, NULL);
482 Py_DECREF(strchunk);
483 if (chunk == NULL) {
484 goto bail;
485 }
486 }
487 else {
488 chunk = strchunk;
Brett Cannon4b964f92008-05-05 20:21:38 +0000489 }
490 if (PyList_Append(chunks, chunk)) {
Benjamin Peterson336680e2008-10-16 21:48:06 +0000491 Py_DECREF(chunk);
Brett Cannon4b964f92008-05-05 20:21:38 +0000492 goto bail;
493 }
494 Py_DECREF(chunk);
495 }
496 next++;
497 if (c == '"') {
498 end = next;
499 break;
500 }
501 if (next == len) {
502 raise_errmsg("Unterminated string starting at", pystr, begin);
503 goto bail;
504 }
505 c = buf[next];
506 if (c != 'u') {
507 /* Non-unicode backslash escapes */
508 end = next + 1;
509 switch (c) {
510 case '"': break;
511 case '\\': break;
512 case '/': break;
513 case 'b': c = '\b'; break;
514 case 'f': c = '\f'; break;
515 case 'n': c = '\n'; break;
516 case 'r': c = '\r'; break;
517 case 't': c = '\t'; break;
518 default: c = 0;
519 }
520 if (c == 0) {
521 raise_errmsg("Invalid \\escape", pystr, end - 2);
522 goto bail;
523 }
524 }
525 else {
526 c = 0;
527 next++;
528 end = next + 4;
529 if (end >= len) {
530 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
531 goto bail;
532 }
533 /* Decode 4 hex digits */
534 for (; next < end; next++) {
Brett Cannon4b964f92008-05-05 20:21:38 +0000535 Py_UNICODE digit = buf[next];
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000536 c <<= 4;
Brett Cannon4b964f92008-05-05 20:21:38 +0000537 switch (digit) {
538 case '0': case '1': case '2': case '3': case '4':
539 case '5': case '6': case '7': case '8': case '9':
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000540 c |= (digit - '0'); break;
Brett Cannon4b964f92008-05-05 20:21:38 +0000541 case 'a': case 'b': case 'c': case 'd': case 'e':
542 case 'f':
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000543 c |= (digit - 'a' + 10); break;
Brett Cannon4b964f92008-05-05 20:21:38 +0000544 case 'A': case 'B': case 'C': case 'D': case 'E':
545 case 'F':
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000546 c |= (digit - 'A' + 10); break;
Brett Cannon4b964f92008-05-05 20:21:38 +0000547 default:
548 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
549 goto bail;
550 }
551 }
552#ifdef Py_UNICODE_WIDE
553 /* Surrogate pair */
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000554 if ((c & 0xfc00) == 0xd800) {
Brett Cannon4b964f92008-05-05 20:21:38 +0000555 Py_UNICODE c2 = 0;
556 if (end + 6 >= len) {
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000557 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
558 goto bail;
Brett Cannon4b964f92008-05-05 20:21:38 +0000559 }
560 if (buf[next++] != '\\' || buf[next++] != 'u') {
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000561 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
562 goto bail;
Brett Cannon4b964f92008-05-05 20:21:38 +0000563 }
564 end += 6;
565 /* Decode 4 hex digits */
566 for (; next < end; next++) {
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000567 c2 <<= 4;
Brett Cannon4b964f92008-05-05 20:21:38 +0000568 Py_UNICODE digit = buf[next];
569 switch (digit) {
570 case '0': case '1': case '2': case '3': case '4':
571 case '5': case '6': case '7': case '8': case '9':
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000572 c2 |= (digit - '0'); break;
Brett Cannon4b964f92008-05-05 20:21:38 +0000573 case 'a': case 'b': case 'c': case 'd': case 'e':
574 case 'f':
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000575 c2 |= (digit - 'a' + 10); break;
Brett Cannon4b964f92008-05-05 20:21:38 +0000576 case 'A': case 'B': case 'C': case 'D': case 'E':
577 case 'F':
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000578 c2 |= (digit - 'A' + 10); break;
Brett Cannon4b964f92008-05-05 20:21:38 +0000579 default:
580 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
581 goto bail;
582 }
583 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000584 if ((c2 & 0xfc00) != 0xdc00) {
585 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
586 goto bail;
587 }
Brett Cannon4b964f92008-05-05 20:21:38 +0000588 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
589 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000590 else if ((c & 0xfc00) == 0xdc00) {
591 raise_errmsg("Unpaired low surrogate", pystr, end - 5);
592 goto bail;
593 }
Brett Cannon4b964f92008-05-05 20:21:38 +0000594#endif
595 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000596 if (c > 0x7f) {
597 has_unicode = 1;
598 }
599 if (has_unicode) {
600 chunk = PyUnicode_FromUnicode(&c, 1);
601 if (chunk == NULL) {
602 goto bail;
603 }
604 }
605 else {
606 char c_char = Py_CHARMASK(c);
607 chunk = PyString_FromStringAndSize(&c_char, 1);
608 if (chunk == NULL) {
609 goto bail;
610 }
Brett Cannon4b964f92008-05-05 20:21:38 +0000611 }
612 if (PyList_Append(chunks, chunk)) {
Benjamin Peterson336680e2008-10-16 21:48:06 +0000613 Py_DECREF(chunk);
Brett Cannon4b964f92008-05-05 20:21:38 +0000614 goto bail;
615 }
616 Py_DECREF(chunk);
617 }
618
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000619 rval = join_list_string(chunks);
Brett Cannon4b964f92008-05-05 20:21:38 +0000620 if (rval == NULL) {
621 goto bail;
622 }
Benjamin Peterson336680e2008-10-16 21:48:06 +0000623 Py_CLEAR(chunks);
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000624 *next_end_ptr = end;
625 return rval;
Brett Cannon4b964f92008-05-05 20:21:38 +0000626bail:
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000627 *next_end_ptr = -1;
Brett Cannon4b964f92008-05-05 20:21:38 +0000628 Py_XDECREF(chunks);
629 return NULL;
630}
631
632
633static PyObject *
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000634scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
Brett Cannon4b964f92008-05-05 20:21:38 +0000635{
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000636 /* Read the JSON string from PyUnicode pystr.
637 end is the index of the first character after the quote.
638 if strict is zero then literal control characters are allowed
639 *next_end_ptr is a return-by-reference index of the character
640 after the end quote
641
642 Return value is a new PyUnicode
643 */
Brett Cannon4b964f92008-05-05 20:21:38 +0000644 PyObject *rval;
645 Py_ssize_t len = PyUnicode_GET_SIZE(pystr);
646 Py_ssize_t begin = end - 1;
647 Py_ssize_t next = begin;
648 const Py_UNICODE *buf = PyUnicode_AS_UNICODE(pystr);
649 PyObject *chunks = PyList_New(0);
650 if (chunks == NULL) {
651 goto bail;
652 }
Bob Ippolitod648f642008-07-19 21:59:50 +0000653 if (end < 0 || len <= end) {
654 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
655 goto bail;
656 }
Brett Cannon4b964f92008-05-05 20:21:38 +0000657 while (1) {
658 /* Find the end of the string or the next escape */
659 Py_UNICODE c = 0;
660 PyObject *chunk = NULL;
661 for (next = end; next < len; next++) {
662 c = buf[next];
663 if (c == '"' || c == '\\') {
664 break;
665 }
666 else if (strict && c <= 0x1f) {
Bob Ippolitod648f642008-07-19 21:59:50 +0000667 raise_errmsg("Invalid control character at", pystr, next);
Brett Cannon4b964f92008-05-05 20:21:38 +0000668 goto bail;
669 }
670 }
671 if (!(c == '"' || c == '\\')) {
672 raise_errmsg("Unterminated string starting at", pystr, begin);
673 goto bail;
674 }
675 /* Pick up this chunk if it's not zero length */
676 if (next != end) {
677 chunk = PyUnicode_FromUnicode(&buf[end], next - end);
678 if (chunk == NULL) {
679 goto bail;
680 }
681 if (PyList_Append(chunks, chunk)) {
Benjamin Peterson87e6ad22008-10-16 21:27:54 +0000682 Py_DECREF(chunk);
Brett Cannon4b964f92008-05-05 20:21:38 +0000683 goto bail;
684 }
685 Py_DECREF(chunk);
686 }
687 next++;
688 if (c == '"') {
689 end = next;
690 break;
691 }
692 if (next == len) {
693 raise_errmsg("Unterminated string starting at", pystr, begin);
694 goto bail;
695 }
696 c = buf[next];
697 if (c != 'u') {
698 /* Non-unicode backslash escapes */
699 end = next + 1;
700 switch (c) {
701 case '"': break;
702 case '\\': break;
703 case '/': break;
704 case 'b': c = '\b'; break;
705 case 'f': c = '\f'; break;
706 case 'n': c = '\n'; break;
707 case 'r': c = '\r'; break;
708 case 't': c = '\t'; break;
709 default: c = 0;
710 }
711 if (c == 0) {
712 raise_errmsg("Invalid \\escape", pystr, end - 2);
713 goto bail;
714 }
715 }
716 else {
717 c = 0;
718 next++;
719 end = next + 4;
720 if (end >= len) {
721 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
722 goto bail;
723 }
724 /* Decode 4 hex digits */
725 for (; next < end; next++) {
Brett Cannon4b964f92008-05-05 20:21:38 +0000726 Py_UNICODE digit = buf[next];
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000727 c <<= 4;
Brett Cannon4b964f92008-05-05 20:21:38 +0000728 switch (digit) {
729 case '0': case '1': case '2': case '3': case '4':
730 case '5': case '6': case '7': case '8': case '9':
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000731 c |= (digit - '0'); break;
Brett Cannon4b964f92008-05-05 20:21:38 +0000732 case 'a': case 'b': case 'c': case 'd': case 'e':
733 case 'f':
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000734 c |= (digit - 'a' + 10); break;
Brett Cannon4b964f92008-05-05 20:21:38 +0000735 case 'A': case 'B': case 'C': case 'D': case 'E':
736 case 'F':
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000737 c |= (digit - 'A' + 10); break;
Brett Cannon4b964f92008-05-05 20:21:38 +0000738 default:
739 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
740 goto bail;
741 }
742 }
743#ifdef Py_UNICODE_WIDE
744 /* Surrogate pair */
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000745 if ((c & 0xfc00) == 0xd800) {
Brett Cannon4b964f92008-05-05 20:21:38 +0000746 Py_UNICODE c2 = 0;
747 if (end + 6 >= len) {
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000748 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
749 goto bail;
Brett Cannon4b964f92008-05-05 20:21:38 +0000750 }
751 if (buf[next++] != '\\' || buf[next++] != 'u') {
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000752 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
753 goto bail;
Brett Cannon4b964f92008-05-05 20:21:38 +0000754 }
755 end += 6;
756 /* Decode 4 hex digits */
757 for (; next < end; next++) {
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000758 c2 <<= 4;
Brett Cannon4b964f92008-05-05 20:21:38 +0000759 Py_UNICODE digit = buf[next];
760 switch (digit) {
761 case '0': case '1': case '2': case '3': case '4':
762 case '5': case '6': case '7': case '8': case '9':
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000763 c2 |= (digit - '0'); break;
Brett Cannon4b964f92008-05-05 20:21:38 +0000764 case 'a': case 'b': case 'c': case 'd': case 'e':
765 case 'f':
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000766 c2 |= (digit - 'a' + 10); break;
Brett Cannon4b964f92008-05-05 20:21:38 +0000767 case 'A': case 'B': case 'C': case 'D': case 'E':
768 case 'F':
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000769 c2 |= (digit - 'A' + 10); break;
Brett Cannon4b964f92008-05-05 20:21:38 +0000770 default:
771 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
772 goto bail;
773 }
774 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000775 if ((c2 & 0xfc00) != 0xdc00) {
776 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
777 goto bail;
778 }
Brett Cannon4b964f92008-05-05 20:21:38 +0000779 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
780 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000781 else if ((c & 0xfc00) == 0xdc00) {
782 raise_errmsg("Unpaired low surrogate", pystr, end - 5);
783 goto bail;
784 }
Brett Cannon4b964f92008-05-05 20:21:38 +0000785#endif
786 }
787 chunk = PyUnicode_FromUnicode(&c, 1);
788 if (chunk == NULL) {
789 goto bail;
790 }
791 if (PyList_Append(chunks, chunk)) {
Benjamin Peterson336680e2008-10-16 21:48:06 +0000792 Py_DECREF(chunk);
Brett Cannon4b964f92008-05-05 20:21:38 +0000793 goto bail;
794 }
795 Py_DECREF(chunk);
796 }
797
798 rval = join_list_unicode(chunks);
799 if (rval == NULL) {
800 goto bail;
801 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000802 Py_DECREF(chunks);
803 *next_end_ptr = end;
804 return rval;
Brett Cannon4b964f92008-05-05 20:21:38 +0000805bail:
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000806 *next_end_ptr = -1;
Brett Cannon4b964f92008-05-05 20:21:38 +0000807 Py_XDECREF(chunks);
808 return NULL;
809}
810
811PyDoc_STRVAR(pydoc_scanstring,
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000812 "scanstring(basestring, end, encoding, strict=True) -> (str, end)\n"
813 "\n"
814 "Scan the string s for a JSON string. End is the index of the\n"
815 "character in s after the quote that started the JSON string.\n"
816 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
817 "on attempt to decode an invalid string. If strict is False then literal\n"
818 "control characters are allowed in the string.\n"
819 "\n"
820 "Returns a tuple of the decoded string and the index of the character in s\n"
821 "after the end quote."
822);
Brett Cannon4b964f92008-05-05 20:21:38 +0000823
824static PyObject *
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000825py_scanstring(PyObject* self UNUSED, PyObject *args)
Brett Cannon4b964f92008-05-05 20:21:38 +0000826{
827 PyObject *pystr;
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000828 PyObject *rval;
Brett Cannon4b964f92008-05-05 20:21:38 +0000829 Py_ssize_t end;
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000830 Py_ssize_t next_end = -1;
Brett Cannon4b964f92008-05-05 20:21:38 +0000831 char *encoding = NULL;
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000832 int strict = 1;
833 if (!PyArg_ParseTuple(args, "OO&|zi:scanstring", &pystr, _convertPyInt_AsSsize_t, &end, &encoding, &strict)) {
Brett Cannon4b964f92008-05-05 20:21:38 +0000834 return NULL;
835 }
836 if (encoding == NULL) {
837 encoding = DEFAULT_ENCODING;
838 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000839 if (PyString_Check(pystr)) {
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000840 rval = scanstring_str(pystr, end, encoding, strict, &next_end);
Brett Cannon4b964f92008-05-05 20:21:38 +0000841 }
842 else if (PyUnicode_Check(pystr)) {
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000843 rval = scanstring_unicode(pystr, end, strict, &next_end);
Brett Cannon4b964f92008-05-05 20:21:38 +0000844 }
845 else {
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000846 PyErr_Format(PyExc_TypeError,
847 "first argument must be a string, not %.80s",
Brett Cannon4b964f92008-05-05 20:21:38 +0000848 Py_TYPE(pystr)->tp_name);
849 return NULL;
850 }
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000851 return _build_rval_index_tuple(rval, next_end);
Brett Cannon4b964f92008-05-05 20:21:38 +0000852}
853
854PyDoc_STRVAR(pydoc_encode_basestring_ascii,
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000855 "encode_basestring_ascii(basestring) -> str\n"
856 "\n"
857 "Return an ASCII-only JSON representation of a Python string"
858);
Brett Cannon4b964f92008-05-05 20:21:38 +0000859
860static PyObject *
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000861py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
Brett Cannon4b964f92008-05-05 20:21:38 +0000862{
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000863 /* Return an ASCII-only JSON representation of a Python string */
Brett Cannon4b964f92008-05-05 20:21:38 +0000864 /* METH_O */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000865 if (PyString_Check(pystr)) {
Brett Cannon4b964f92008-05-05 20:21:38 +0000866 return ascii_escape_str(pystr);
867 }
868 else if (PyUnicode_Check(pystr)) {
869 return ascii_escape_unicode(pystr);
870 }
871 else {
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000872 PyErr_Format(PyExc_TypeError,
873 "first argument must be a string, not %.80s",
Brett Cannon4b964f92008-05-05 20:21:38 +0000874 Py_TYPE(pystr)->tp_name);
875 return NULL;
876 }
877}
878
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000879static void
880scanner_dealloc(PyObject *self)
881{
882 /* Deallocate scanner object */
883 scanner_clear(self);
884 Py_TYPE(self)->tp_free(self);
885}
886
887static int
888scanner_traverse(PyObject *self, visitproc visit, void *arg)
889{
890 PyScannerObject *s;
891 assert(PyScanner_Check(self));
892 s = (PyScannerObject *)self;
893 Py_VISIT(s->encoding);
894 Py_VISIT(s->strict);
895 Py_VISIT(s->object_hook);
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000896 Py_VISIT(s->pairs_hook);
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000897 Py_VISIT(s->parse_float);
898 Py_VISIT(s->parse_int);
899 Py_VISIT(s->parse_constant);
900 return 0;
901}
902
903static int
904scanner_clear(PyObject *self)
905{
906 PyScannerObject *s;
907 assert(PyScanner_Check(self));
908 s = (PyScannerObject *)self;
909 Py_CLEAR(s->encoding);
910 Py_CLEAR(s->strict);
911 Py_CLEAR(s->object_hook);
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000912 Py_CLEAR(s->pairs_hook);
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000913 Py_CLEAR(s->parse_float);
914 Py_CLEAR(s->parse_int);
915 Py_CLEAR(s->parse_constant);
916 return 0;
917}
918
919static PyObject *
920_parse_object_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
921 /* Read a JSON object from PyString pystr.
922 idx is the index of the first character after the opening curly brace.
923 *next_idx_ptr is a return-by-reference index to the first character after
924 the closing curly brace.
925
926 Returns a new PyObject (usually a dict, but object_hook can change that)
927 */
928 char *str = PyString_AS_STRING(pystr);
929 Py_ssize_t end_idx = PyString_GET_SIZE(pystr) - 1;
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000930 PyObject *rval;
931 PyObject *pairs;
932 PyObject *item;
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000933 PyObject *key = NULL;
934 PyObject *val = NULL;
935 char *encoding = PyString_AS_STRING(s->encoding);
936 int strict = PyObject_IsTrue(s->strict);
937 Py_ssize_t next_idx;
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000938
939 pairs = PyList_New(0);
940 if (pairs == NULL)
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000941 return NULL;
942
943 /* skip whitespace after { */
944 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
945
946 /* only loop if the object is non-empty */
947 if (idx <= end_idx && str[idx] != '}') {
948 while (idx <= end_idx) {
949 /* read key */
950 if (str[idx] != '"') {
951 raise_errmsg("Expecting property name", pystr, idx);
952 goto bail;
953 }
954 key = scanstring_str(pystr, idx + 1, encoding, strict, &next_idx);
955 if (key == NULL)
956 goto bail;
957 idx = next_idx;
958
959 /* skip whitespace between key and : delimiter, read :, skip whitespace */
960 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
961 if (idx > end_idx || str[idx] != ':') {
962 raise_errmsg("Expecting : delimiter", pystr, idx);
963 goto bail;
964 }
965 idx++;
966 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
967
968 /* read any JSON data type */
969 val = scan_once_str(s, pystr, idx, &next_idx);
970 if (val == NULL)
971 goto bail;
972
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000973 item = PyTuple_Pack(2, key, val);
974 if (item == NULL)
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000975 goto bail;
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000976 Py_CLEAR(key);
977 Py_CLEAR(val);
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000978 if (PyList_Append(pairs, item) == -1) {
979 Py_DECREF(item);
980 goto bail;
981 }
982 Py_DECREF(item);
Bob Ippolitod914e3f2009-03-17 23:19:00 +0000983 idx = next_idx;
984
985 /* skip whitespace before } or , */
986 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
987
988 /* bail if the object is closed or we didn't get the , delimiter */
989 if (idx > end_idx) break;
990 if (str[idx] == '}') {
991 break;
992 }
993 else if (str[idx] != ',') {
994 raise_errmsg("Expecting , delimiter", pystr, idx);
995 goto bail;
996 }
997 idx++;
998
999 /* skip whitespace after , delimiter */
1000 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
1001 }
1002 }
1003 /* verify that idx < end_idx, str[idx] should be '}' */
1004 if (idx > end_idx || str[idx] != '}') {
1005 raise_errmsg("Expecting object", pystr, end_idx);
1006 goto bail;
1007 }
Raymond Hettinger91852ca2009-03-19 19:19:03 +00001008
1009 /* if pairs_hook is not None: rval = object_pairs_hook(pairs) */
1010 if (s->pairs_hook != Py_None) {
1011 val = PyObject_CallFunctionObjArgs(s->pairs_hook, pairs, NULL);
1012 if (val == NULL)
1013 goto bail;
1014 Py_DECREF(pairs);
1015 *next_idx_ptr = idx + 1;
1016 return val;
1017 }
1018
1019 rval = PyObject_CallFunctionObjArgs((PyObject *)(&PyDict_Type),
1020 pairs, NULL);
1021 if (rval == NULL)
1022 goto bail;
1023 Py_CLEAR(pairs);
1024
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001025 /* if object_hook is not None: rval = object_hook(rval) */
1026 if (s->object_hook != Py_None) {
1027 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
1028 if (val == NULL)
1029 goto bail;
1030 Py_DECREF(rval);
1031 rval = val;
1032 val = NULL;
1033 }
1034 *next_idx_ptr = idx + 1;
1035 return rval;
1036bail:
1037 Py_XDECREF(key);
1038 Py_XDECREF(val);
Raymond Hettinger91852ca2009-03-19 19:19:03 +00001039 Py_XDECREF(pairs);
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001040 return NULL;
1041}
1042
1043static PyObject *
1044_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
1045 /* Read a JSON object from PyUnicode pystr.
1046 idx is the index of the first character after the opening curly brace.
1047 *next_idx_ptr is a return-by-reference index to the first character after
1048 the closing curly brace.
1049
1050 Returns a new PyObject (usually a dict, but object_hook can change that)
1051 */
1052 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
1053 Py_ssize_t end_idx = PyUnicode_GET_SIZE(pystr) - 1;
Raymond Hettinger91852ca2009-03-19 19:19:03 +00001054 PyObject *rval;
1055 PyObject *pairs;
1056 PyObject *item;
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001057 PyObject *key = NULL;
Raymond Hettinger91852ca2009-03-19 19:19:03 +00001058 PyObject *val = NULL;
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001059 int strict = PyObject_IsTrue(s->strict);
1060 Py_ssize_t next_idx;
Raymond Hettinger91852ca2009-03-19 19:19:03 +00001061
1062 pairs = PyList_New(0);
1063 if (pairs == NULL)
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001064 return NULL;
1065
1066 /* skip whitespace after { */
1067 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
1068
1069 /* only loop if the object is non-empty */
1070 if (idx <= end_idx && str[idx] != '}') {
1071 while (idx <= end_idx) {
1072 /* read key */
1073 if (str[idx] != '"') {
1074 raise_errmsg("Expecting property name", pystr, idx);
1075 goto bail;
1076 }
1077 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
1078 if (key == NULL)
1079 goto bail;
1080 idx = next_idx;
1081
1082 /* skip whitespace between key and : delimiter, read :, skip whitespace */
1083 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
1084 if (idx > end_idx || str[idx] != ':') {
1085 raise_errmsg("Expecting : delimiter", pystr, idx);
1086 goto bail;
1087 }
1088 idx++;
1089 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
1090
1091 /* read any JSON term */
1092 val = scan_once_unicode(s, pystr, idx, &next_idx);
1093 if (val == NULL)
1094 goto bail;
1095
Raymond Hettinger91852ca2009-03-19 19:19:03 +00001096 item = PyTuple_Pack(2, key, val);
1097 if (item == NULL)
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001098 goto bail;
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001099 Py_CLEAR(key);
1100 Py_CLEAR(val);
Raymond Hettinger91852ca2009-03-19 19:19:03 +00001101 if (PyList_Append(pairs, item) == -1) {
1102 Py_DECREF(item);
1103 goto bail;
1104 }
1105 Py_DECREF(item);
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001106 idx = next_idx;
1107
1108 /* skip whitespace before } or , */
1109 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
1110
1111 /* bail if the object is closed or we didn't get the , delimiter */
1112 if (idx > end_idx) break;
1113 if (str[idx] == '}') {
1114 break;
1115 }
1116 else if (str[idx] != ',') {
1117 raise_errmsg("Expecting , delimiter", pystr, idx);
1118 goto bail;
1119 }
1120 idx++;
1121
1122 /* skip whitespace after , delimiter */
1123 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
1124 }
1125 }
1126
1127 /* verify that idx < end_idx, str[idx] should be '}' */
1128 if (idx > end_idx || str[idx] != '}') {
1129 raise_errmsg("Expecting object", pystr, end_idx);
1130 goto bail;
1131 }
1132
Raymond Hettinger91852ca2009-03-19 19:19:03 +00001133 /* if pairs_hook is not None: rval = object_pairs_hook(pairs) */
1134 if (s->pairs_hook != Py_None) {
1135 val = PyObject_CallFunctionObjArgs(s->pairs_hook, pairs, NULL);
1136 if (val == NULL)
1137 goto bail;
1138 Py_DECREF(pairs);
1139 *next_idx_ptr = idx + 1;
1140 return val;
1141 }
1142
1143 rval = PyObject_CallFunctionObjArgs((PyObject *)(&PyDict_Type),
1144 pairs, NULL);
1145 if (rval == NULL)
1146 goto bail;
1147 Py_CLEAR(pairs);
1148
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001149 /* if object_hook is not None: rval = object_hook(rval) */
1150 if (s->object_hook != Py_None) {
1151 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
1152 if (val == NULL)
1153 goto bail;
1154 Py_DECREF(rval);
1155 rval = val;
1156 val = NULL;
1157 }
1158 *next_idx_ptr = idx + 1;
1159 return rval;
1160bail:
1161 Py_XDECREF(key);
1162 Py_XDECREF(val);
Raymond Hettinger91852ca2009-03-19 19:19:03 +00001163 Py_XDECREF(pairs);
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001164 return NULL;
1165}
1166
1167static PyObject *
1168_parse_array_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
1169 /* Read a JSON array from PyString pystr.
1170 idx is the index of the first character after the opening brace.
1171 *next_idx_ptr is a return-by-reference index to the first character after
1172 the closing brace.
1173
1174 Returns a new PyList
1175 */
1176 char *str = PyString_AS_STRING(pystr);
1177 Py_ssize_t end_idx = PyString_GET_SIZE(pystr) - 1;
1178 PyObject *val = NULL;
1179 PyObject *rval = PyList_New(0);
1180 Py_ssize_t next_idx;
1181 if (rval == NULL)
1182 return NULL;
1183
1184 /* skip whitespace after [ */
1185 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
1186
1187 /* only loop if the array is non-empty */
1188 if (idx <= end_idx && str[idx] != ']') {
1189 while (idx <= end_idx) {
1190
1191 /* read any JSON term and de-tuplefy the (rval, idx) */
1192 val = scan_once_str(s, pystr, idx, &next_idx);
1193 if (val == NULL)
1194 goto bail;
1195
1196 if (PyList_Append(rval, val) == -1)
1197 goto bail;
1198
1199 Py_CLEAR(val);
1200 idx = next_idx;
1201
1202 /* skip whitespace between term and , */
1203 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
1204
1205 /* bail if the array is closed or we didn't get the , delimiter */
1206 if (idx > end_idx) break;
1207 if (str[idx] == ']') {
1208 break;
1209 }
1210 else if (str[idx] != ',') {
1211 raise_errmsg("Expecting , delimiter", pystr, idx);
1212 goto bail;
1213 }
1214 idx++;
1215
1216 /* skip whitespace after , */
1217 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
1218 }
1219 }
1220
1221 /* verify that idx < end_idx, str[idx] should be ']' */
1222 if (idx > end_idx || str[idx] != ']') {
1223 raise_errmsg("Expecting object", pystr, end_idx);
1224 goto bail;
1225 }
1226 *next_idx_ptr = idx + 1;
1227 return rval;
1228bail:
1229 Py_XDECREF(val);
1230 Py_DECREF(rval);
1231 return NULL;
1232}
1233
1234static PyObject *
1235_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
1236 /* Read a JSON array from PyString pystr.
1237 idx is the index of the first character after the opening brace.
1238 *next_idx_ptr is a return-by-reference index to the first character after
1239 the closing brace.
1240
1241 Returns a new PyList
1242 */
1243 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
1244 Py_ssize_t end_idx = PyUnicode_GET_SIZE(pystr) - 1;
1245 PyObject *val = NULL;
1246 PyObject *rval = PyList_New(0);
1247 Py_ssize_t next_idx;
1248 if (rval == NULL)
1249 return NULL;
1250
1251 /* skip whitespace after [ */
1252 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
1253
1254 /* only loop if the array is non-empty */
1255 if (idx <= end_idx && str[idx] != ']') {
1256 while (idx <= end_idx) {
1257
1258 /* read any JSON term */
1259 val = scan_once_unicode(s, pystr, idx, &next_idx);
1260 if (val == NULL)
1261 goto bail;
1262
1263 if (PyList_Append(rval, val) == -1)
1264 goto bail;
1265
1266 Py_CLEAR(val);
1267 idx = next_idx;
1268
1269 /* skip whitespace between term and , */
1270 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
1271
1272 /* bail if the array is closed or we didn't get the , delimiter */
1273 if (idx > end_idx) break;
1274 if (str[idx] == ']') {
1275 break;
1276 }
1277 else if (str[idx] != ',') {
1278 raise_errmsg("Expecting , delimiter", pystr, idx);
1279 goto bail;
1280 }
1281 idx++;
1282
1283 /* skip whitespace after , */
1284 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
1285 }
1286 }
1287
1288 /* verify that idx < end_idx, str[idx] should be ']' */
1289 if (idx > end_idx || str[idx] != ']') {
1290 raise_errmsg("Expecting object", pystr, end_idx);
1291 goto bail;
1292 }
1293 *next_idx_ptr = idx + 1;
1294 return rval;
1295bail:
1296 Py_XDECREF(val);
1297 Py_DECREF(rval);
1298 return NULL;
1299}
1300
1301static PyObject *
1302_parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
1303 /* Read a JSON constant from PyString pystr.
1304 constant is the constant string that was found
1305 ("NaN", "Infinity", "-Infinity").
1306 idx is the index of the first character of the constant
1307 *next_idx_ptr is a return-by-reference index to the first character after
1308 the constant.
1309
1310 Returns the result of parse_constant
1311 */
1312 PyObject *cstr;
1313 PyObject *rval;
1314 /* constant is "NaN", "Infinity", or "-Infinity" */
1315 cstr = PyString_InternFromString(constant);
1316 if (cstr == NULL)
1317 return NULL;
1318
1319 /* rval = parse_constant(constant) */
1320 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
1321 idx += PyString_GET_SIZE(cstr);
1322 Py_DECREF(cstr);
1323 *next_idx_ptr = idx;
1324 return rval;
1325}
1326
1327static PyObject *
1328_match_number_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
1329 /* Read a JSON number from PyString pystr.
1330 idx is the index of the first character of the number
1331 *next_idx_ptr is a return-by-reference index to the first character after
1332 the number.
1333
1334 Returns a new PyObject representation of that number:
1335 PyInt, PyLong, or PyFloat.
1336 May return other types if parse_int or parse_float are set
1337 */
1338 char *str = PyString_AS_STRING(pystr);
1339 Py_ssize_t end_idx = PyString_GET_SIZE(pystr) - 1;
1340 Py_ssize_t idx = start;
1341 int is_float = 0;
1342 PyObject *rval;
1343 PyObject *numstr;
1344
1345 /* read a sign if it's there, make sure it's not the end of the string */
1346 if (str[idx] == '-') {
1347 idx++;
1348 if (idx > end_idx) {
1349 PyErr_SetNone(PyExc_StopIteration);
1350 return NULL;
1351 }
1352 }
1353
1354 /* read as many integer digits as we find as long as it doesn't start with 0 */
1355 if (str[idx] >= '1' && str[idx] <= '9') {
1356 idx++;
1357 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++;
1358 }
1359 /* if it starts with 0 we only expect one integer digit */
1360 else if (str[idx] == '0') {
1361 idx++;
1362 }
1363 /* no integer digits, error */
1364 else {
1365 PyErr_SetNone(PyExc_StopIteration);
1366 return NULL;
1367 }
1368
1369 /* if the next char is '.' followed by a digit then read all float digits */
1370 if (idx < end_idx && str[idx] == '.' && str[idx + 1] >= '0' && str[idx + 1] <= '9') {
1371 is_float = 1;
1372 idx += 2;
1373 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++;
1374 }
1375
1376 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
1377 if (idx < end_idx && (str[idx] == 'e' || str[idx] == 'E')) {
1378
1379 /* save the index of the 'e' or 'E' just in case we need to backtrack */
1380 Py_ssize_t e_start = idx;
1381 idx++;
1382
1383 /* read an exponent sign if present */
1384 if (idx < end_idx && (str[idx] == '-' || str[idx] == '+')) idx++;
1385
1386 /* read all digits */
1387 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++;
1388
1389 /* if we got a digit, then parse as float. if not, backtrack */
1390 if (str[idx - 1] >= '0' && str[idx - 1] <= '9') {
1391 is_float = 1;
1392 }
1393 else {
1394 idx = e_start;
1395 }
1396 }
1397
1398 /* copy the section we determined to be a number */
1399 numstr = PyString_FromStringAndSize(&str[start], idx - start);
1400 if (numstr == NULL)
1401 return NULL;
1402 if (is_float) {
1403 /* parse as a float using a fast path if available, otherwise call user defined method */
1404 if (s->parse_float != (PyObject *)&PyFloat_Type) {
1405 rval = PyObject_CallFunctionObjArgs(s->parse_float, numstr, NULL);
1406 }
1407 else {
1408 rval = PyFloat_FromDouble(PyOS_ascii_atof(PyString_AS_STRING(numstr)));
1409 }
1410 }
1411 else {
1412 /* parse as an int using a fast path if available, otherwise call user defined method */
1413 if (s->parse_int != (PyObject *)&PyInt_Type) {
1414 rval = PyObject_CallFunctionObjArgs(s->parse_int, numstr, NULL);
1415 }
1416 else {
1417 rval = PyInt_FromString(PyString_AS_STRING(numstr), NULL, 10);
1418 }
1419 }
1420 Py_DECREF(numstr);
1421 *next_idx_ptr = idx;
1422 return rval;
1423}
1424
1425static PyObject *
1426_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
1427 /* Read a JSON number from PyUnicode pystr.
1428 idx is the index of the first character of the number
1429 *next_idx_ptr is a return-by-reference index to the first character after
1430 the number.
1431
1432 Returns a new PyObject representation of that number:
1433 PyInt, PyLong, or PyFloat.
1434 May return other types if parse_int or parse_float are set
1435 */
1436 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
1437 Py_ssize_t end_idx = PyUnicode_GET_SIZE(pystr) - 1;
1438 Py_ssize_t idx = start;
1439 int is_float = 0;
1440 PyObject *rval;
1441 PyObject *numstr;
1442
1443 /* read a sign if it's there, make sure it's not the end of the string */
1444 if (str[idx] == '-') {
1445 idx++;
1446 if (idx > end_idx) {
1447 PyErr_SetNone(PyExc_StopIteration);
1448 return NULL;
1449 }
1450 }
1451
1452 /* read as many integer digits as we find as long as it doesn't start with 0 */
1453 if (str[idx] >= '1' && str[idx] <= '9') {
1454 idx++;
1455 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++;
1456 }
1457 /* if it starts with 0 we only expect one integer digit */
1458 else if (str[idx] == '0') {
1459 idx++;
1460 }
1461 /* no integer digits, error */
1462 else {
1463 PyErr_SetNone(PyExc_StopIteration);
1464 return NULL;
1465 }
1466
1467 /* if the next char is '.' followed by a digit then read all float digits */
1468 if (idx < end_idx && str[idx] == '.' && str[idx + 1] >= '0' && str[idx + 1] <= '9') {
1469 is_float = 1;
1470 idx += 2;
Bob Ippolito76a982a2009-03-29 22:33:58 +00001471 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++;
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001472 }
1473
1474 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
1475 if (idx < end_idx && (str[idx] == 'e' || str[idx] == 'E')) {
1476 Py_ssize_t e_start = idx;
1477 idx++;
1478
1479 /* read an exponent sign if present */
1480 if (idx < end_idx && (str[idx] == '-' || str[idx] == '+')) idx++;
1481
1482 /* read all digits */
1483 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++;
1484
1485 /* if we got a digit, then parse as float. if not, backtrack */
1486 if (str[idx - 1] >= '0' && str[idx - 1] <= '9') {
1487 is_float = 1;
1488 }
1489 else {
1490 idx = e_start;
1491 }
1492 }
1493
1494 /* copy the section we determined to be a number */
1495 numstr = PyUnicode_FromUnicode(&str[start], idx - start);
1496 if (numstr == NULL)
1497 return NULL;
1498 if (is_float) {
1499 /* parse as a float using a fast path if available, otherwise call user defined method */
1500 if (s->parse_float != (PyObject *)&PyFloat_Type) {
1501 rval = PyObject_CallFunctionObjArgs(s->parse_float, numstr, NULL);
1502 }
1503 else {
1504 rval = PyFloat_FromString(numstr, NULL);
1505 }
1506 }
1507 else {
1508 /* no fast path for unicode -> int, just call */
1509 rval = PyObject_CallFunctionObjArgs(s->parse_int, numstr, NULL);
1510 }
1511 Py_DECREF(numstr);
1512 *next_idx_ptr = idx;
1513 return rval;
1514}
1515
1516static PyObject *
1517scan_once_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
1518{
1519 /* Read one JSON term (of any kind) from PyString pystr.
1520 idx is the index of the first character of the term
1521 *next_idx_ptr is a return-by-reference index to the first character after
1522 the number.
1523
1524 Returns a new PyObject representation of the term.
1525 */
1526 char *str = PyString_AS_STRING(pystr);
1527 Py_ssize_t length = PyString_GET_SIZE(pystr);
1528 if (idx >= length) {
1529 PyErr_SetNone(PyExc_StopIteration);
1530 return NULL;
1531 }
1532 switch (str[idx]) {
1533 case '"':
1534 /* string */
1535 return scanstring_str(pystr, idx + 1,
1536 PyString_AS_STRING(s->encoding),
1537 PyObject_IsTrue(s->strict),
1538 next_idx_ptr);
1539 case '{':
1540 /* object */
1541 return _parse_object_str(s, pystr, idx + 1, next_idx_ptr);
1542 case '[':
1543 /* array */
1544 return _parse_array_str(s, pystr, idx + 1, next_idx_ptr);
1545 case 'n':
1546 /* null */
1547 if ((idx + 3 < length) && str[idx + 1] == 'u' && str[idx + 2] == 'l' && str[idx + 3] == 'l') {
1548 Py_INCREF(Py_None);
1549 *next_idx_ptr = idx + 4;
1550 return Py_None;
1551 }
1552 break;
1553 case 't':
1554 /* true */
1555 if ((idx + 3 < length) && str[idx + 1] == 'r' && str[idx + 2] == 'u' && str[idx + 3] == 'e') {
1556 Py_INCREF(Py_True);
1557 *next_idx_ptr = idx + 4;
1558 return Py_True;
1559 }
1560 break;
1561 case 'f':
1562 /* false */
1563 if ((idx + 4 < length) && str[idx + 1] == 'a' && str[idx + 2] == 'l' && str[idx + 3] == 's' && str[idx + 4] == 'e') {
1564 Py_INCREF(Py_False);
1565 *next_idx_ptr = idx + 5;
1566 return Py_False;
1567 }
1568 break;
1569 case 'N':
1570 /* NaN */
1571 if ((idx + 2 < length) && str[idx + 1] == 'a' && str[idx + 2] == 'N') {
1572 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1573 }
1574 break;
1575 case 'I':
1576 /* Infinity */
1577 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') {
1578 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1579 }
1580 break;
1581 case '-':
1582 /* -Infinity */
1583 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') {
1584 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1585 }
1586 break;
1587 }
1588 /* Didn't find a string, object, array, or named constant. Look for a number. */
1589 return _match_number_str(s, pystr, idx, next_idx_ptr);
1590}
1591
1592static PyObject *
1593scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
1594{
1595 /* Read one JSON term (of any kind) from PyUnicode pystr.
1596 idx is the index of the first character of the term
1597 *next_idx_ptr is a return-by-reference index to the first character after
1598 the number.
1599
1600 Returns a new PyObject representation of the term.
1601 */
1602 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
1603 Py_ssize_t length = PyUnicode_GET_SIZE(pystr);
1604 if (idx >= length) {
1605 PyErr_SetNone(PyExc_StopIteration);
1606 return NULL;
1607 }
1608 switch (str[idx]) {
1609 case '"':
1610 /* string */
1611 return scanstring_unicode(pystr, idx + 1,
1612 PyObject_IsTrue(s->strict),
1613 next_idx_ptr);
1614 case '{':
1615 /* object */
1616 return _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
1617 case '[':
1618 /* array */
1619 return _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
1620 case 'n':
1621 /* null */
1622 if ((idx + 3 < length) && str[idx + 1] == 'u' && str[idx + 2] == 'l' && str[idx + 3] == 'l') {
1623 Py_INCREF(Py_None);
1624 *next_idx_ptr = idx + 4;
1625 return Py_None;
1626 }
1627 break;
1628 case 't':
1629 /* true */
1630 if ((idx + 3 < length) && str[idx + 1] == 'r' && str[idx + 2] == 'u' && str[idx + 3] == 'e') {
1631 Py_INCREF(Py_True);
1632 *next_idx_ptr = idx + 4;
1633 return Py_True;
1634 }
1635 break;
1636 case 'f':
1637 /* false */
1638 if ((idx + 4 < length) && str[idx + 1] == 'a' && str[idx + 2] == 'l' && str[idx + 3] == 's' && str[idx + 4] == 'e') {
1639 Py_INCREF(Py_False);
1640 *next_idx_ptr = idx + 5;
1641 return Py_False;
1642 }
1643 break;
1644 case 'N':
1645 /* NaN */
1646 if ((idx + 2 < length) && str[idx + 1] == 'a' && str[idx + 2] == 'N') {
1647 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1648 }
1649 break;
1650 case 'I':
1651 /* Infinity */
1652 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') {
1653 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1654 }
1655 break;
1656 case '-':
1657 /* -Infinity */
1658 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') {
1659 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1660 }
1661 break;
1662 }
1663 /* Didn't find a string, object, array, or named constant. Look for a number. */
1664 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
1665}
1666
1667static PyObject *
1668scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
1669{
1670 /* Python callable interface to scan_once_{str,unicode} */
1671 PyObject *pystr;
1672 PyObject *rval;
1673 Py_ssize_t idx;
1674 Py_ssize_t next_idx = -1;
1675 static char *kwlist[] = {"string", "idx", NULL};
1676 PyScannerObject *s;
1677 assert(PyScanner_Check(self));
1678 s = (PyScannerObject *)self;
1679 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:scan_once", kwlist, &pystr, _convertPyInt_AsSsize_t, &idx))
1680 return NULL;
1681
1682 if (PyString_Check(pystr)) {
1683 rval = scan_once_str(s, pystr, idx, &next_idx);
1684 }
1685 else if (PyUnicode_Check(pystr)) {
1686 rval = scan_once_unicode(s, pystr, idx, &next_idx);
1687 }
1688 else {
1689 PyErr_Format(PyExc_TypeError,
1690 "first argument must be a string, not %.80s",
1691 Py_TYPE(pystr)->tp_name);
1692 return NULL;
1693 }
1694 return _build_rval_index_tuple(rval, next_idx);
1695}
1696
1697static PyObject *
1698scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1699{
1700 PyScannerObject *s;
1701 s = (PyScannerObject *)type->tp_alloc(type, 0);
1702 if (s != NULL) {
1703 s->encoding = NULL;
1704 s->strict = NULL;
1705 s->object_hook = NULL;
Raymond Hettinger91852ca2009-03-19 19:19:03 +00001706 s->pairs_hook = NULL;
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001707 s->parse_float = NULL;
1708 s->parse_int = NULL;
1709 s->parse_constant = NULL;
1710 }
1711 return (PyObject *)s;
1712}
1713
1714static int
1715scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1716{
1717 /* Initialize Scanner object */
1718 PyObject *ctx;
1719 static char *kwlist[] = {"context", NULL};
1720 PyScannerObject *s;
1721
1722 assert(PyScanner_Check(self));
1723 s = (PyScannerObject *)self;
1724
1725 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1726 return -1;
1727
1728 /* PyString_AS_STRING is used on encoding */
1729 s->encoding = PyObject_GetAttrString(ctx, "encoding");
1730 if (s->encoding == Py_None) {
1731 Py_DECREF(Py_None);
1732 s->encoding = PyString_InternFromString(DEFAULT_ENCODING);
1733 }
1734 else if (PyUnicode_Check(s->encoding)) {
1735 PyObject *tmp = PyUnicode_AsEncodedString(s->encoding, NULL, NULL);
1736 Py_DECREF(s->encoding);
1737 s->encoding = tmp;
1738 }
1739 if (s->encoding == NULL || !PyString_Check(s->encoding))
1740 goto bail;
1741
1742 /* All of these will fail "gracefully" so we don't need to verify them */
1743 s->strict = PyObject_GetAttrString(ctx, "strict");
1744 if (s->strict == NULL)
1745 goto bail;
1746 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1747 if (s->object_hook == NULL)
1748 goto bail;
Raymond Hettinger91852ca2009-03-19 19:19:03 +00001749 s->pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1750 if (s->object_hook == NULL)
1751 goto bail;
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001752 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1753 if (s->parse_float == NULL)
1754 goto bail;
1755 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1756 if (s->parse_int == NULL)
1757 goto bail;
1758 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1759 if (s->parse_constant == NULL)
1760 goto bail;
1761
1762 return 0;
1763
1764bail:
1765 Py_CLEAR(s->encoding);
1766 Py_CLEAR(s->strict);
1767 Py_CLEAR(s->object_hook);
Raymond Hettinger91852ca2009-03-19 19:19:03 +00001768 Py_CLEAR(s->pairs_hook);
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001769 Py_CLEAR(s->parse_float);
1770 Py_CLEAR(s->parse_int);
1771 Py_CLEAR(s->parse_constant);
1772 return -1;
1773}
1774
1775PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1776
1777static
1778PyTypeObject PyScannerType = {
1779 PyObject_HEAD_INIT(NULL)
1780 0, /* tp_internal */
1781 "_json.Scanner", /* tp_name */
1782 sizeof(PyScannerObject), /* tp_basicsize */
1783 0, /* tp_itemsize */
1784 scanner_dealloc, /* tp_dealloc */
1785 0, /* tp_print */
1786 0, /* tp_getattr */
1787 0, /* tp_setattr */
1788 0, /* tp_compare */
1789 0, /* tp_repr */
1790 0, /* tp_as_number */
1791 0, /* tp_as_sequence */
1792 0, /* tp_as_mapping */
1793 0, /* tp_hash */
1794 scanner_call, /* tp_call */
1795 0, /* tp_str */
1796 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1797 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1798 0, /* tp_as_buffer */
1799 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1800 scanner_doc, /* tp_doc */
1801 scanner_traverse, /* tp_traverse */
1802 scanner_clear, /* tp_clear */
1803 0, /* tp_richcompare */
1804 0, /* tp_weaklistoffset */
1805 0, /* tp_iter */
1806 0, /* tp_iternext */
1807 0, /* tp_methods */
1808 scanner_members, /* tp_members */
1809 0, /* tp_getset */
1810 0, /* tp_base */
1811 0, /* tp_dict */
1812 0, /* tp_descr_get */
1813 0, /* tp_descr_set */
1814 0, /* tp_dictoffset */
1815 scanner_init, /* tp_init */
1816 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1817 scanner_new, /* tp_new */
1818 0,/* PyObject_GC_Del, */ /* tp_free */
1819};
1820
1821static PyObject *
1822encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1823{
1824 PyEncoderObject *s;
1825 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1826 if (s != NULL) {
1827 s->markers = NULL;
1828 s->defaultfn = NULL;
1829 s->encoder = NULL;
1830 s->indent = NULL;
1831 s->key_separator = NULL;
1832 s->item_separator = NULL;
1833 s->sort_keys = NULL;
1834 s->skipkeys = NULL;
1835 }
1836 return (PyObject *)s;
1837}
1838
1839static int
1840encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1841{
1842 /* initialize Encoder object */
1843 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1844
1845 PyEncoderObject *s;
1846 PyObject *allow_nan;
1847
1848 assert(PyEncoder_Check(self));
1849 s = (PyEncoderObject *)self;
1850
1851 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
1852 &s->markers, &s->defaultfn, &s->encoder, &s->indent, &s->key_separator, &s->item_separator, &s->sort_keys, &s->skipkeys, &allow_nan))
1853 return -1;
1854
1855 Py_INCREF(s->markers);
1856 Py_INCREF(s->defaultfn);
1857 Py_INCREF(s->encoder);
1858 Py_INCREF(s->indent);
1859 Py_INCREF(s->key_separator);
1860 Py_INCREF(s->item_separator);
1861 Py_INCREF(s->sort_keys);
1862 Py_INCREF(s->skipkeys);
1863 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1864 s->allow_nan = PyObject_IsTrue(allow_nan);
1865 return 0;
1866}
1867
1868static PyObject *
1869encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1870{
1871 /* Python callable interface to encode_listencode_obj */
1872 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1873 PyObject *obj;
1874 PyObject *rval;
1875 Py_ssize_t indent_level;
1876 PyEncoderObject *s;
1877 assert(PyEncoder_Check(self));
1878 s = (PyEncoderObject *)self;
1879 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:_iterencode", kwlist,
1880 &obj, _convertPyInt_AsSsize_t, &indent_level))
1881 return NULL;
1882 rval = PyList_New(0);
1883 if (rval == NULL)
1884 return NULL;
1885 if (encoder_listencode_obj(s, rval, obj, indent_level)) {
1886 Py_DECREF(rval);
1887 return NULL;
1888 }
1889 return rval;
1890}
1891
1892static PyObject *
1893_encoded_const(PyObject *obj)
1894{
1895 /* Return the JSON string representation of None, True, False */
1896 if (obj == Py_None) {
1897 static PyObject *s_null = NULL;
1898 if (s_null == NULL) {
1899 s_null = PyString_InternFromString("null");
1900 }
1901 Py_INCREF(s_null);
1902 return s_null;
1903 }
1904 else if (obj == Py_True) {
1905 static PyObject *s_true = NULL;
1906 if (s_true == NULL) {
1907 s_true = PyString_InternFromString("true");
1908 }
1909 Py_INCREF(s_true);
1910 return s_true;
1911 }
1912 else if (obj == Py_False) {
1913 static PyObject *s_false = NULL;
1914 if (s_false == NULL) {
1915 s_false = PyString_InternFromString("false");
1916 }
1917 Py_INCREF(s_false);
1918 return s_false;
1919 }
1920 else {
1921 PyErr_SetString(PyExc_ValueError, "not a const");
1922 return NULL;
1923 }
1924}
1925
1926static PyObject *
1927encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1928{
1929 /* Return the JSON representation of a PyFloat */
1930 double i = PyFloat_AS_DOUBLE(obj);
1931 if (!Py_IS_FINITE(i)) {
1932 if (!s->allow_nan) {
1933 PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant");
1934 return NULL;
1935 }
1936 if (i > 0) {
1937 return PyString_FromString("Infinity");
1938 }
1939 else if (i < 0) {
1940 return PyString_FromString("-Infinity");
1941 }
1942 else {
1943 return PyString_FromString("NaN");
1944 }
1945 }
1946 /* Use a better float format here? */
1947 return PyObject_Repr(obj);
1948}
1949
1950static PyObject *
1951encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1952{
1953 /* Return the JSON representation of a string */
1954 if (s->fast_encode)
1955 return py_encode_basestring_ascii(NULL, obj);
1956 else
1957 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1958}
1959
1960static int
1961_steal_list_append(PyObject *lst, PyObject *stolen)
1962{
1963 /* Append stolen and then decrement its reference count */
1964 int rval = PyList_Append(lst, stolen);
1965 Py_DECREF(stolen);
1966 return rval;
1967}
1968
1969static int
1970encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssize_t indent_level)
1971{
1972 /* Encode Python object obj to a JSON term, rval is a PyList */
1973 PyObject *newobj;
1974 int rv;
1975
1976 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1977 PyObject *cstr = _encoded_const(obj);
1978 if (cstr == NULL)
1979 return -1;
1980 return _steal_list_append(rval, cstr);
1981 }
1982 else if (PyString_Check(obj) || PyUnicode_Check(obj))
1983 {
1984 PyObject *encoded = encoder_encode_string(s, obj);
1985 if (encoded == NULL)
1986 return -1;
1987 return _steal_list_append(rval, encoded);
1988 }
1989 else if (PyInt_Check(obj) || PyLong_Check(obj)) {
1990 PyObject *encoded = PyObject_Str(obj);
1991 if (encoded == NULL)
1992 return -1;
1993 return _steal_list_append(rval, encoded);
1994 }
1995 else if (PyFloat_Check(obj)) {
1996 PyObject *encoded = encoder_encode_float(s, obj);
1997 if (encoded == NULL)
1998 return -1;
1999 return _steal_list_append(rval, encoded);
2000 }
2001 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
2002 return encoder_listencode_list(s, rval, obj, indent_level);
2003 }
2004 else if (PyDict_Check(obj)) {
2005 return encoder_listencode_dict(s, rval, obj, indent_level);
2006 }
2007 else {
2008 PyObject *ident = NULL;
2009 if (s->markers != Py_None) {
2010 int has_key;
2011 ident = PyLong_FromVoidPtr(obj);
2012 if (ident == NULL)
2013 return -1;
2014 has_key = PyDict_Contains(s->markers, ident);
2015 if (has_key) {
2016 if (has_key != -1)
2017 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
2018 Py_DECREF(ident);
2019 return -1;
2020 }
2021 if (PyDict_SetItem(s->markers, ident, obj)) {
2022 Py_DECREF(ident);
2023 return -1;
2024 }
2025 }
2026 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
2027 if (newobj == NULL) {
2028 Py_XDECREF(ident);
2029 return -1;
2030 }
2031 rv = encoder_listencode_obj(s, rval, newobj, indent_level);
2032 Py_DECREF(newobj);
2033 if (rv) {
2034 Py_XDECREF(ident);
2035 return -1;
2036 }
2037 if (ident != NULL) {
2038 if (PyDict_DelItem(s->markers, ident)) {
2039 Py_XDECREF(ident);
2040 return -1;
2041 }
2042 Py_XDECREF(ident);
2043 }
2044 return rv;
2045 }
2046}
2047
2048static int
2049encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ssize_t indent_level)
2050{
2051 /* Encode Python dict dct a JSON term, rval is a PyList */
2052 static PyObject *open_dict = NULL;
2053 static PyObject *close_dict = NULL;
2054 static PyObject *empty_dict = NULL;
2055 PyObject *kstr = NULL;
2056 PyObject *ident = NULL;
2057 PyObject *key, *value;
2058 Py_ssize_t pos;
2059 int skipkeys;
2060 Py_ssize_t idx;
2061
2062 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
2063 open_dict = PyString_InternFromString("{");
2064 close_dict = PyString_InternFromString("}");
2065 empty_dict = PyString_InternFromString("{}");
2066 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
2067 return -1;
2068 }
2069 if (PyDict_Size(dct) == 0)
2070 return PyList_Append(rval, empty_dict);
2071
2072 if (s->markers != Py_None) {
2073 int has_key;
2074 ident = PyLong_FromVoidPtr(dct);
2075 if (ident == NULL)
2076 goto bail;
2077 has_key = PyDict_Contains(s->markers, ident);
2078 if (has_key) {
2079 if (has_key != -1)
2080 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
2081 goto bail;
2082 }
2083 if (PyDict_SetItem(s->markers, ident, dct)) {
2084 goto bail;
2085 }
2086 }
2087
2088 if (PyList_Append(rval, open_dict))
2089 goto bail;
2090
2091 if (s->indent != Py_None) {
2092 /* TODO: DOES NOT RUN */
2093 indent_level += 1;
2094 /*
2095 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
2096 separator = _item_separator + newline_indent
2097 buf += newline_indent
2098 */
2099 }
2100
2101 /* TODO: C speedup not implemented for sort_keys */
2102
2103 pos = 0;
2104 skipkeys = PyObject_IsTrue(s->skipkeys);
2105 idx = 0;
2106 while (PyDict_Next(dct, &pos, &key, &value)) {
2107 PyObject *encoded;
2108
2109 if (PyString_Check(key) || PyUnicode_Check(key)) {
2110 Py_INCREF(key);
2111 kstr = key;
2112 }
2113 else if (PyFloat_Check(key)) {
2114 kstr = encoder_encode_float(s, key);
2115 if (kstr == NULL)
2116 goto bail;
2117 }
2118 else if (PyInt_Check(key) || PyLong_Check(key)) {
2119 kstr = PyObject_Str(key);
2120 if (kstr == NULL)
2121 goto bail;
2122 }
2123 else if (key == Py_True || key == Py_False || key == Py_None) {
2124 kstr = _encoded_const(key);
2125 if (kstr == NULL)
2126 goto bail;
2127 }
2128 else if (skipkeys) {
2129 continue;
2130 }
2131 else {
2132 /* TODO: include repr of key */
2133 PyErr_SetString(PyExc_ValueError, "keys must be a string");
2134 goto bail;
2135 }
2136
2137 if (idx) {
2138 if (PyList_Append(rval, s->item_separator))
2139 goto bail;
2140 }
2141
2142 encoded = encoder_encode_string(s, kstr);
2143 Py_CLEAR(kstr);
2144 if (encoded == NULL)
2145 goto bail;
2146 if (PyList_Append(rval, encoded)) {
2147 Py_DECREF(encoded);
2148 goto bail;
2149 }
2150 Py_DECREF(encoded);
2151 if (PyList_Append(rval, s->key_separator))
2152 goto bail;
2153 if (encoder_listencode_obj(s, rval, value, indent_level))
2154 goto bail;
2155 idx += 1;
2156 }
2157 if (ident != NULL) {
2158 if (PyDict_DelItem(s->markers, ident))
2159 goto bail;
2160 Py_CLEAR(ident);
2161 }
2162 if (s->indent != Py_None) {
2163 /* TODO: DOES NOT RUN */
2164 indent_level -= 1;
2165 /*
2166 yield '\n' + (' ' * (_indent * _current_indent_level))
2167 */
2168 }
2169 if (PyList_Append(rval, close_dict))
2170 goto bail;
2171 return 0;
2172
2173bail:
2174 Py_XDECREF(kstr);
2175 Py_XDECREF(ident);
2176 return -1;
2177}
2178
2179
2180static int
2181encoder_listencode_list(PyEncoderObject *s, PyObject *rval, PyObject *seq, Py_ssize_t indent_level)
2182{
2183 /* Encode Python list seq to a JSON term, rval is a PyList */
2184 static PyObject *open_array = NULL;
2185 static PyObject *close_array = NULL;
2186 static PyObject *empty_array = NULL;
2187 PyObject *ident = NULL;
2188 PyObject *s_fast = NULL;
2189 Py_ssize_t num_items;
2190 PyObject **seq_items;
2191 Py_ssize_t i;
2192
2193 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
2194 open_array = PyString_InternFromString("[");
2195 close_array = PyString_InternFromString("]");
2196 empty_array = PyString_InternFromString("[]");
2197 if (open_array == NULL || close_array == NULL || empty_array == NULL)
2198 return -1;
2199 }
2200 ident = NULL;
2201 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
2202 if (s_fast == NULL)
2203 return -1;
2204 num_items = PySequence_Fast_GET_SIZE(s_fast);
2205 if (num_items == 0) {
2206 Py_DECREF(s_fast);
2207 return PyList_Append(rval, empty_array);
2208 }
2209
2210 if (s->markers != Py_None) {
2211 int has_key;
2212 ident = PyLong_FromVoidPtr(seq);
2213 if (ident == NULL)
2214 goto bail;
2215 has_key = PyDict_Contains(s->markers, ident);
2216 if (has_key) {
2217 if (has_key != -1)
2218 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
2219 goto bail;
2220 }
2221 if (PyDict_SetItem(s->markers, ident, seq)) {
2222 goto bail;
2223 }
2224 }
2225
2226 seq_items = PySequence_Fast_ITEMS(s_fast);
2227 if (PyList_Append(rval, open_array))
2228 goto bail;
2229 if (s->indent != Py_None) {
2230 /* TODO: DOES NOT RUN */
2231 indent_level += 1;
2232 /*
2233 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
2234 separator = _item_separator + newline_indent
2235 buf += newline_indent
2236 */
2237 }
2238 for (i = 0; i < num_items; i++) {
2239 PyObject *obj = seq_items[i];
2240 if (i) {
2241 if (PyList_Append(rval, s->item_separator))
2242 goto bail;
2243 }
2244 if (encoder_listencode_obj(s, rval, obj, indent_level))
2245 goto bail;
2246 }
2247 if (ident != NULL) {
2248 if (PyDict_DelItem(s->markers, ident))
2249 goto bail;
2250 Py_CLEAR(ident);
2251 }
2252 if (s->indent != Py_None) {
2253 /* TODO: DOES NOT RUN */
2254 indent_level -= 1;
2255 /*
2256 yield '\n' + (' ' * (_indent * _current_indent_level))
2257 */
2258 }
2259 if (PyList_Append(rval, close_array))
2260 goto bail;
2261 Py_DECREF(s_fast);
2262 return 0;
2263
2264bail:
2265 Py_XDECREF(ident);
2266 Py_DECREF(s_fast);
2267 return -1;
2268}
2269
2270static void
2271encoder_dealloc(PyObject *self)
2272{
2273 /* Deallocate Encoder */
2274 encoder_clear(self);
2275 Py_TYPE(self)->tp_free(self);
2276}
2277
2278static int
2279encoder_traverse(PyObject *self, visitproc visit, void *arg)
2280{
2281 PyEncoderObject *s;
2282 assert(PyEncoder_Check(self));
2283 s = (PyEncoderObject *)self;
2284 Py_VISIT(s->markers);
2285 Py_VISIT(s->defaultfn);
2286 Py_VISIT(s->encoder);
2287 Py_VISIT(s->indent);
2288 Py_VISIT(s->key_separator);
2289 Py_VISIT(s->item_separator);
2290 Py_VISIT(s->sort_keys);
2291 Py_VISIT(s->skipkeys);
2292 return 0;
2293}
2294
2295static int
2296encoder_clear(PyObject *self)
2297{
2298 /* Deallocate Encoder */
2299 PyEncoderObject *s;
2300 assert(PyEncoder_Check(self));
2301 s = (PyEncoderObject *)self;
2302 Py_CLEAR(s->markers);
2303 Py_CLEAR(s->defaultfn);
2304 Py_CLEAR(s->encoder);
2305 Py_CLEAR(s->indent);
2306 Py_CLEAR(s->key_separator);
2307 Py_CLEAR(s->item_separator);
2308 Py_CLEAR(s->sort_keys);
2309 Py_CLEAR(s->skipkeys);
2310 return 0;
2311}
2312
2313PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
2314
2315static
2316PyTypeObject PyEncoderType = {
2317 PyObject_HEAD_INIT(NULL)
2318 0, /* tp_internal */
2319 "_json.Encoder", /* tp_name */
2320 sizeof(PyEncoderObject), /* tp_basicsize */
2321 0, /* tp_itemsize */
2322 encoder_dealloc, /* tp_dealloc */
2323 0, /* tp_print */
2324 0, /* tp_getattr */
2325 0, /* tp_setattr */
2326 0, /* tp_compare */
2327 0, /* tp_repr */
2328 0, /* tp_as_number */
2329 0, /* tp_as_sequence */
2330 0, /* tp_as_mapping */
2331 0, /* tp_hash */
2332 encoder_call, /* tp_call */
2333 0, /* tp_str */
2334 0, /* tp_getattro */
2335 0, /* tp_setattro */
2336 0, /* tp_as_buffer */
2337 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2338 encoder_doc, /* tp_doc */
2339 encoder_traverse, /* tp_traverse */
2340 encoder_clear, /* tp_clear */
2341 0, /* tp_richcompare */
2342 0, /* tp_weaklistoffset */
2343 0, /* tp_iter */
2344 0, /* tp_iternext */
2345 0, /* tp_methods */
2346 encoder_members, /* tp_members */
2347 0, /* tp_getset */
2348 0, /* tp_base */
2349 0, /* tp_dict */
2350 0, /* tp_descr_get */
2351 0, /* tp_descr_set */
2352 0, /* tp_dictoffset */
2353 encoder_init, /* tp_init */
2354 0, /* tp_alloc */
2355 encoder_new, /* tp_new */
2356 0, /* tp_free */
2357};
2358
2359static PyMethodDef speedups_methods[] = {
2360 {"encode_basestring_ascii",
2361 (PyCFunction)py_encode_basestring_ascii,
2362 METH_O,
2363 pydoc_encode_basestring_ascii},
2364 {"scanstring",
2365 (PyCFunction)py_scanstring,
2366 METH_VARARGS,
2367 pydoc_scanstring},
Brett Cannon4b964f92008-05-05 20:21:38 +00002368 {NULL, NULL, 0, NULL}
2369};
2370
2371PyDoc_STRVAR(module_doc,
2372"json speedups\n");
2373
2374void
2375init_json(void)
2376{
2377 PyObject *m;
Bob Ippolitod914e3f2009-03-17 23:19:00 +00002378 PyScannerType.tp_new = PyType_GenericNew;
2379 if (PyType_Ready(&PyScannerType) < 0)
2380 return;
2381 PyEncoderType.tp_new = PyType_GenericNew;
2382 if (PyType_Ready(&PyEncoderType) < 0)
2383 return;
2384 m = Py_InitModule3("_json", speedups_methods, module_doc);
2385 Py_INCREF((PyObject*)&PyScannerType);
2386 PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType);
2387 Py_INCREF((PyObject*)&PyEncoderType);
2388 PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType);
Brett Cannon4b964f92008-05-05 20:21:38 +00002389}