blob: 5ced5c9704579dc081e593621e08b6ed184990dc [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"
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
Christian Heimes90540002008-05-08 14:29:10 +000016
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000017#ifdef __GNUC__
18#define UNUSED __attribute__((__unused__))
19#else
20#define UNUSED
21#endif
22
23#define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType)
24#define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType)
25#define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType)
26#define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType)
27
28static PyTypeObject PyScannerType;
29static PyTypeObject PyEncoderType;
30
31typedef struct _PyScannerObject {
32 PyObject_HEAD
33 PyObject *strict;
34 PyObject *object_hook;
35 PyObject *object_pairs_hook;
36 PyObject *parse_float;
37 PyObject *parse_int;
38 PyObject *parse_constant;
39} PyScannerObject;
40
41static PyMemberDef scanner_members[] = {
42 {"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"},
43 {"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"},
44 {"object_pairs_hook", T_OBJECT, offsetof(PyScannerObject, object_pairs_hook), READONLY},
45 {"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"},
46 {"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "parse_int"},
47 {"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READONLY, "parse_constant"},
48 {NULL}
49};
50
51typedef struct _PyEncoderObject {
52 PyObject_HEAD
53 PyObject *markers;
54 PyObject *defaultfn;
55 PyObject *encoder;
56 PyObject *indent;
57 PyObject *key_separator;
58 PyObject *item_separator;
59 PyObject *sort_keys;
60 PyObject *skipkeys;
61 int fast_encode;
62 int allow_nan;
63} PyEncoderObject;
64
65static PyMemberDef encoder_members[] = {
66 {"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers"},
67 {"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "default"},
68 {"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder"},
69 {"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"},
70 {"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READONLY, "key_separator"},
71 {"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READONLY, "item_separator"},
72 {"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sort_keys"},
73 {"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipkeys"},
74 {NULL}
75};
76
77static PyObject *
78ascii_escape_unicode(PyObject *pystr);
79static PyObject *
80py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr);
81void init_json(void);
82static PyObject *
83scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr);
84static PyObject *
85_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx);
86static PyObject *
87scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
88static int
89scanner_init(PyObject *self, PyObject *args, PyObject *kwds);
90static void
91scanner_dealloc(PyObject *self);
92static int
93scanner_clear(PyObject *self);
94static PyObject *
95encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
96static int
97encoder_init(PyObject *self, PyObject *args, PyObject *kwds);
98static void
99encoder_dealloc(PyObject *self);
100static int
101encoder_clear(PyObject *self);
102static int
103encoder_listencode_list(PyEncoderObject *s, PyObject *rval, PyObject *seq, Py_ssize_t indent_level);
104static int
105encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssize_t indent_level);
106static int
107encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ssize_t indent_level);
108static PyObject *
Hirokazu Yamamotofecf5d12009-05-02 15:55:19 +0000109_encoded_const(PyObject *obj);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000110static void
111raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
112static PyObject *
113encoder_encode_string(PyEncoderObject *s, PyObject *obj);
114static int
115_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr);
116static PyObject *
117_convertPyInt_FromSsize_t(Py_ssize_t *size_ptr);
118static PyObject *
119encoder_encode_float(PyEncoderObject *s, PyObject *obj);
120
Christian Heimes90540002008-05-08 14:29:10 +0000121#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000122#define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
Christian Heimes90540002008-05-08 14:29:10 +0000123
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000124#define MIN_EXPANSION 6
Christian Heimes90540002008-05-08 14:29:10 +0000125#ifdef Py_UNICODE_WIDE
126#define MAX_EXPANSION (2 * MIN_EXPANSION)
127#else
128#define MAX_EXPANSION MIN_EXPANSION
129#endif
130
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000131static int
132_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000133{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000134 /* PyObject to Py_ssize_t converter */
135 *size_ptr = PyLong_AsSsize_t(o);
Georg Brandl59682052009-05-05 07:52:05 +0000136 if (*size_ptr == -1 && PyErr_Occurred())
137 return 0;
138 return 1;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000139}
140
141static PyObject *
142_convertPyInt_FromSsize_t(Py_ssize_t *size_ptr)
143{
144 /* Py_ssize_t to PyObject converter */
145 return PyLong_FromSsize_t(*size_ptr);
146}
147
148static Py_ssize_t
149ascii_escape_unichar(Py_UNICODE c, Py_UNICODE *output, Py_ssize_t chars)
150{
151 /* Escape unicode code point c to ASCII escape sequences
152 in char *output. output must have at least 12 bytes unused to
153 accommodate an escaped surrogate pair "\uXXXX\uXXXX" */
Christian Heimes90540002008-05-08 14:29:10 +0000154 output[chars++] = '\\';
155 switch (c) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000156 case '\\': output[chars++] = c; break;
157 case '"': output[chars++] = c; break;
Christian Heimes90540002008-05-08 14:29:10 +0000158 case '\b': output[chars++] = 'b'; break;
159 case '\f': output[chars++] = 'f'; break;
160 case '\n': output[chars++] = 'n'; break;
161 case '\r': output[chars++] = 'r'; break;
162 case '\t': output[chars++] = 't'; break;
163 default:
164#ifdef Py_UNICODE_WIDE
165 if (c >= 0x10000) {
166 /* UTF-16 surrogate pair */
167 Py_UNICODE v = c - 0x10000;
168 c = 0xd800 | ((v >> 10) & 0x3ff);
169 output[chars++] = 'u';
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000170 output[chars++] = "0123456789abcdef"[(c >> 12) & 0xf];
171 output[chars++] = "0123456789abcdef"[(c >> 8) & 0xf];
172 output[chars++] = "0123456789abcdef"[(c >> 4) & 0xf];
173 output[chars++] = "0123456789abcdef"[(c ) & 0xf];
Christian Heimes90540002008-05-08 14:29:10 +0000174 c = 0xdc00 | (v & 0x3ff);
175 output[chars++] = '\\';
176 }
177#endif
178 output[chars++] = 'u';
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000179 output[chars++] = "0123456789abcdef"[(c >> 12) & 0xf];
180 output[chars++] = "0123456789abcdef"[(c >> 8) & 0xf];
181 output[chars++] = "0123456789abcdef"[(c >> 4) & 0xf];
182 output[chars++] = "0123456789abcdef"[(c ) & 0xf];
Christian Heimes90540002008-05-08 14:29:10 +0000183 }
184 return chars;
185}
186
187static PyObject *
188ascii_escape_unicode(PyObject *pystr)
189{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000190 /* Take a PyUnicode pystr and return a new ASCII-only escaped PyUnicode */
Christian Heimes90540002008-05-08 14:29:10 +0000191 Py_ssize_t i;
192 Py_ssize_t input_chars;
193 Py_ssize_t output_size;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000194 Py_ssize_t max_output_size;
Christian Heimes90540002008-05-08 14:29:10 +0000195 Py_ssize_t chars;
196 PyObject *rval;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000197 Py_UNICODE *output;
Christian Heimes90540002008-05-08 14:29:10 +0000198 Py_UNICODE *input_unicode;
199
200 input_chars = PyUnicode_GET_SIZE(pystr);
201 input_unicode = PyUnicode_AS_UNICODE(pystr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000202
Christian Heimes90540002008-05-08 14:29:10 +0000203 /* One char input can be up to 6 chars output, estimate 4 of these */
204 output_size = 2 + (MIN_EXPANSION * 4) + input_chars;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000205 max_output_size = 2 + (input_chars * MAX_EXPANSION);
206 rval = PyUnicode_FromStringAndSize(NULL, output_size);
Christian Heimes90540002008-05-08 14:29:10 +0000207 if (rval == NULL) {
208 return NULL;
209 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000210 output = PyUnicode_AS_UNICODE(rval);
Christian Heimes90540002008-05-08 14:29:10 +0000211 chars = 0;
212 output[chars++] = '"';
213 for (i = 0; i < input_chars; i++) {
214 Py_UNICODE c = input_unicode[i];
215 if (S_CHAR(c)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000216 output[chars++] = c;
Christian Heimes90540002008-05-08 14:29:10 +0000217 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000218 else {
219 chars = ascii_escape_unichar(c, output, chars);
Christian Heimes90540002008-05-08 14:29:10 +0000220 }
221 if (output_size - chars < (1 + MAX_EXPANSION)) {
222 /* There's more than four, so let's resize by a lot */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000223 Py_ssize_t new_output_size = output_size * 2;
Christian Heimes90540002008-05-08 14:29:10 +0000224 /* This is an upper bound */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000225 if (new_output_size > max_output_size) {
226 new_output_size = max_output_size;
Christian Heimes90540002008-05-08 14:29:10 +0000227 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000228 /* Make sure that the output size changed before resizing */
229 if (new_output_size != output_size) {
230 output_size = new_output_size;
231 if (PyUnicode_Resize(&rval, output_size) == -1) {
232 return NULL;
233 }
234 output = PyUnicode_AS_UNICODE(rval);
Christian Heimes90540002008-05-08 14:29:10 +0000235 }
Christian Heimes90540002008-05-08 14:29:10 +0000236 }
237 }
238 output[chars++] = '"';
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000239 if (PyUnicode_Resize(&rval, chars) == -1) {
Christian Heimes90540002008-05-08 14:29:10 +0000240 return NULL;
241 }
242 return rval;
243}
244
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000245static void
Christian Heimes90540002008-05-08 14:29:10 +0000246raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
247{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000248 /* Use the Python function json.decoder.errmsg to raise a nice
249 looking ValueError exception */
Christian Heimes90540002008-05-08 14:29:10 +0000250 static PyObject *errmsg_fn = NULL;
251 PyObject *pymsg;
252 if (errmsg_fn == NULL) {
253 PyObject *decoder = PyImport_ImportModule("json.decoder");
254 if (decoder == NULL)
255 return;
256 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000257 Py_DECREF(decoder);
Christian Heimes90540002008-05-08 14:29:10 +0000258 if (errmsg_fn == NULL)
259 return;
Christian Heimes90540002008-05-08 14:29:10 +0000260 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000261 pymsg = PyObject_CallFunction(errmsg_fn, "(zOO&)", msg, s, _convertPyInt_FromSsize_t, &end);
Benjamin Petersona13d4752008-10-16 21:17:24 +0000262 if (pymsg) {
263 PyErr_SetObject(PyExc_ValueError, pymsg);
264 Py_DECREF(pymsg);
265 }
Christian Heimes90540002008-05-08 14:29:10 +0000266}
267
268static PyObject *
269join_list_unicode(PyObject *lst)
270{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000271 /* return u''.join(lst) */
272 static PyObject *sep = NULL;
273 if (sep == NULL) {
274 sep = PyUnicode_FromStringAndSize("", 0);
275 if (sep == NULL)
276 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +0000277 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000278 return PyUnicode_Join(sep, lst);
279}
280
281static PyObject *
282_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
283 /* return (rval, idx) tuple, stealing reference to rval */
284 PyObject *tpl;
285 PyObject *pyidx;
286 /*
287 steal a reference to rval, returns (rval, idx)
288 */
289 if (rval == NULL) {
Christian Heimes90540002008-05-08 14:29:10 +0000290 return NULL;
291 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000292 pyidx = PyLong_FromSsize_t(idx);
293 if (pyidx == NULL) {
294 Py_DECREF(rval);
295 return NULL;
296 }
297 tpl = PyTuple_New(2);
298 if (tpl == NULL) {
299 Py_DECREF(pyidx);
300 Py_DECREF(rval);
301 return NULL;
302 }
303 PyTuple_SET_ITEM(tpl, 0, rval);
304 PyTuple_SET_ITEM(tpl, 1, pyidx);
305 return tpl;
Christian Heimes90540002008-05-08 14:29:10 +0000306}
307
308static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000309scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000310{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000311 /* Read the JSON string from PyUnicode pystr.
312 end is the index of the first character after the quote.
313 if strict is zero then literal control characters are allowed
314 *next_end_ptr is a return-by-reference index of the character
315 after the end quote
Christian Heimes90540002008-05-08 14:29:10 +0000316
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000317 Return value is a new PyUnicode
318 */
Christian Heimes90540002008-05-08 14:29:10 +0000319 PyObject *rval;
320 Py_ssize_t len = PyUnicode_GET_SIZE(pystr);
321 Py_ssize_t begin = end - 1;
322 Py_ssize_t next = begin;
323 const Py_UNICODE *buf = PyUnicode_AS_UNICODE(pystr);
324 PyObject *chunks = PyList_New(0);
325 if (chunks == NULL) {
326 goto bail;
327 }
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000328 if (end < 0 || len <= end) {
329 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
330 goto bail;
331 }
Christian Heimes90540002008-05-08 14:29:10 +0000332 while (1) {
333 /* Find the end of the string or the next escape */
334 Py_UNICODE c = 0;
335 PyObject *chunk = NULL;
336 for (next = end; next < len; next++) {
337 c = buf[next];
338 if (c == '"' || c == '\\') {
339 break;
340 }
341 else if (strict && c <= 0x1f) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000342 raise_errmsg("Invalid control character at", pystr, next);
Christian Heimes90540002008-05-08 14:29:10 +0000343 goto bail;
344 }
345 }
346 if (!(c == '"' || c == '\\')) {
347 raise_errmsg("Unterminated string starting at", pystr, begin);
348 goto bail;
349 }
350 /* Pick up this chunk if it's not zero length */
351 if (next != end) {
352 chunk = PyUnicode_FromUnicode(&buf[end], next - end);
353 if (chunk == NULL) {
354 goto bail;
355 }
356 if (PyList_Append(chunks, chunk)) {
Benjamin Peterson8e8c2152008-10-16 21:56:24 +0000357 Py_DECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000358 goto bail;
359 }
360 Py_DECREF(chunk);
361 }
362 next++;
363 if (c == '"') {
364 end = next;
365 break;
366 }
367 if (next == len) {
368 raise_errmsg("Unterminated string starting at", pystr, begin);
369 goto bail;
370 }
371 c = buf[next];
372 if (c != 'u') {
373 /* Non-unicode backslash escapes */
374 end = next + 1;
375 switch (c) {
376 case '"': break;
377 case '\\': break;
378 case '/': break;
379 case 'b': c = '\b'; break;
380 case 'f': c = '\f'; break;
381 case 'n': c = '\n'; break;
382 case 'r': c = '\r'; break;
383 case 't': c = '\t'; break;
384 default: c = 0;
385 }
386 if (c == 0) {
387 raise_errmsg("Invalid \\escape", pystr, end - 2);
388 goto bail;
389 }
390 }
391 else {
392 c = 0;
393 next++;
394 end = next + 4;
395 if (end >= len) {
396 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
397 goto bail;
398 }
399 /* Decode 4 hex digits */
400 for (; next < end; next++) {
Christian Heimes90540002008-05-08 14:29:10 +0000401 Py_UNICODE digit = buf[next];
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000402 c <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000403 switch (digit) {
404 case '0': case '1': case '2': case '3': case '4':
405 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000406 c |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000407 case 'a': case 'b': case 'c': case 'd': case 'e':
408 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000409 c |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000410 case 'A': case 'B': case 'C': case 'D': case 'E':
411 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000412 c |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000413 default:
414 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
415 goto bail;
416 }
417 }
418#ifdef Py_UNICODE_WIDE
419 /* Surrogate pair */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000420 if ((c & 0xfc00) == 0xd800) {
Christian Heimes90540002008-05-08 14:29:10 +0000421 Py_UNICODE c2 = 0;
422 if (end + 6 >= len) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000423 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
424 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000425 }
426 if (buf[next++] != '\\' || buf[next++] != 'u') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000427 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
428 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000429 }
430 end += 6;
431 /* Decode 4 hex digits */
432 for (; next < end; next++) {
Christian Heimes90540002008-05-08 14:29:10 +0000433 Py_UNICODE digit = buf[next];
Antoine Pitrouae136da2010-10-09 15:26:41 +0000434 c2 <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000435 switch (digit) {
436 case '0': case '1': case '2': case '3': case '4':
437 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000438 c2 |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000439 case 'a': case 'b': case 'c': case 'd': case 'e':
440 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000441 c2 |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000442 case 'A': case 'B': case 'C': case 'D': case 'E':
443 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000444 c2 |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000445 default:
446 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
447 goto bail;
448 }
449 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000450 if ((c2 & 0xfc00) != 0xdc00) {
451 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
452 goto bail;
453 }
Christian Heimes90540002008-05-08 14:29:10 +0000454 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
455 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000456 else if ((c & 0xfc00) == 0xdc00) {
457 raise_errmsg("Unpaired low surrogate", pystr, end - 5);
458 goto bail;
459 }
Christian Heimes90540002008-05-08 14:29:10 +0000460#endif
461 }
462 chunk = PyUnicode_FromUnicode(&c, 1);
463 if (chunk == NULL) {
464 goto bail;
465 }
466 if (PyList_Append(chunks, chunk)) {
Benjamin Peterson8e8c2152008-10-16 21:56:24 +0000467 Py_DECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000468 goto bail;
469 }
470 Py_DECREF(chunk);
471 }
472
473 rval = join_list_unicode(chunks);
474 if (rval == NULL) {
475 goto bail;
476 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000477 Py_DECREF(chunks);
478 *next_end_ptr = end;
479 return rval;
Christian Heimes90540002008-05-08 14:29:10 +0000480bail:
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000481 *next_end_ptr = -1;
Christian Heimes90540002008-05-08 14:29:10 +0000482 Py_XDECREF(chunks);
483 return NULL;
484}
485
486PyDoc_STRVAR(pydoc_scanstring,
Georg Brandl4009c9e2010-10-06 08:26:09 +0000487 "scanstring(string, end, strict=True) -> (string, end)\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000488 "\n"
489 "Scan the string s for a JSON string. End is the index of the\n"
490 "character in s after the quote that started the JSON string.\n"
491 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
492 "on attempt to decode an invalid string. If strict is False then literal\n"
493 "control characters are allowed in the string.\n"
494 "\n"
495 "Returns a tuple of the decoded string and the index of the character in s\n"
496 "after the end quote."
497);
Christian Heimes90540002008-05-08 14:29:10 +0000498
499static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000500py_scanstring(PyObject* self UNUSED, PyObject *args)
Christian Heimes90540002008-05-08 14:29:10 +0000501{
502 PyObject *pystr;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000503 PyObject *rval;
Christian Heimes90540002008-05-08 14:29:10 +0000504 Py_ssize_t end;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000505 Py_ssize_t next_end = -1;
506 int strict = 1;
507 if (!PyArg_ParseTuple(args, "OO&|i:scanstring", &pystr, _convertPyInt_AsSsize_t, &end, &strict)) {
Christian Heimes90540002008-05-08 14:29:10 +0000508 return NULL;
509 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000510 if (PyUnicode_Check(pystr)) {
511 rval = scanstring_unicode(pystr, end, strict, &next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000512 }
513 else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000514 PyErr_Format(PyExc_TypeError,
Georg Brandl4009c9e2010-10-06 08:26:09 +0000515 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000516 Py_TYPE(pystr)->tp_name);
517 return NULL;
518 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000519 return _build_rval_index_tuple(rval, next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000520}
521
522PyDoc_STRVAR(pydoc_encode_basestring_ascii,
Georg Brandl4009c9e2010-10-06 08:26:09 +0000523 "encode_basestring_ascii(string) -> string\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000524 "\n"
525 "Return an ASCII-only JSON representation of a Python string"
526);
Christian Heimes90540002008-05-08 14:29:10 +0000527
528static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000529py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
Christian Heimes90540002008-05-08 14:29:10 +0000530{
531 PyObject *rval;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000532 /* Return an ASCII-only JSON representation of a Python string */
Christian Heimes90540002008-05-08 14:29:10 +0000533 /* METH_O */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000534 if (PyUnicode_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000535 rval = ascii_escape_unicode(pystr);
536 }
537 else {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000538 PyErr_Format(PyExc_TypeError,
539 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000540 Py_TYPE(pystr)->tp_name);
541 return NULL;
542 }
Christian Heimes90540002008-05-08 14:29:10 +0000543 return rval;
544}
545
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000546static void
547scanner_dealloc(PyObject *self)
548{
549 /* Deallocate scanner object */
550 scanner_clear(self);
551 Py_TYPE(self)->tp_free(self);
552}
553
554static int
555scanner_traverse(PyObject *self, visitproc visit, void *arg)
556{
557 PyScannerObject *s;
558 assert(PyScanner_Check(self));
559 s = (PyScannerObject *)self;
560 Py_VISIT(s->strict);
561 Py_VISIT(s->object_hook);
562 Py_VISIT(s->object_pairs_hook);
563 Py_VISIT(s->parse_float);
564 Py_VISIT(s->parse_int);
565 Py_VISIT(s->parse_constant);
566 return 0;
567}
568
569static int
570scanner_clear(PyObject *self)
571{
572 PyScannerObject *s;
573 assert(PyScanner_Check(self));
574 s = (PyScannerObject *)self;
575 Py_CLEAR(s->strict);
576 Py_CLEAR(s->object_hook);
577 Py_CLEAR(s->object_pairs_hook);
578 Py_CLEAR(s->parse_float);
579 Py_CLEAR(s->parse_int);
580 Py_CLEAR(s->parse_constant);
581 return 0;
582}
583
584static PyObject *
585_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
586 /* Read a JSON object from PyUnicode pystr.
587 idx is the index of the first character after the opening curly brace.
588 *next_idx_ptr is a return-by-reference index to the first character after
589 the closing curly brace.
590
591 Returns a new PyObject (usually a dict, but object_hook can change that)
592 */
593 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
594 Py_ssize_t end_idx = PyUnicode_GET_SIZE(pystr) - 1;
595 PyObject *val = NULL;
596 PyObject *rval = PyList_New(0);
597 PyObject *key = NULL;
598 int strict = PyObject_IsTrue(s->strict);
599 Py_ssize_t next_idx;
600 if (rval == NULL)
601 return NULL;
602
603 /* skip whitespace after { */
604 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
605
606 /* only loop if the object is non-empty */
607 if (idx <= end_idx && str[idx] != '}') {
608 while (idx <= end_idx) {
609 /* read key */
610 if (str[idx] != '"') {
611 raise_errmsg("Expecting property name", pystr, idx);
612 goto bail;
613 }
614 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
615 if (key == NULL)
616 goto bail;
617 idx = next_idx;
618
619 /* skip whitespace between key and : delimiter, read :, skip whitespace */
620 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
621 if (idx > end_idx || str[idx] != ':') {
622 raise_errmsg("Expecting : delimiter", pystr, idx);
623 goto bail;
624 }
625 idx++;
626 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
627
628 /* read any JSON term */
629 val = scan_once_unicode(s, pystr, idx, &next_idx);
630 if (val == NULL)
631 goto bail;
632
633 {
634 PyObject *tuple = PyTuple_Pack(2, key, val);
635 if (tuple == NULL)
636 goto bail;
637 if (PyList_Append(rval, tuple) == -1) {
638 Py_DECREF(tuple);
639 goto bail;
640 }
641 Py_DECREF(tuple);
642 }
643
644 Py_CLEAR(key);
645 Py_CLEAR(val);
646 idx = next_idx;
647
648 /* skip whitespace before } or , */
649 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
650
651 /* bail if the object is closed or we didn't get the , delimiter */
652 if (idx > end_idx) break;
653 if (str[idx] == '}') {
654 break;
655 }
656 else if (str[idx] != ',') {
657 raise_errmsg("Expecting , delimiter", pystr, idx);
658 goto bail;
659 }
660 idx++;
661
662 /* skip whitespace after , delimiter */
663 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
664 }
665 }
666
667 /* verify that idx < end_idx, str[idx] should be '}' */
668 if (idx > end_idx || str[idx] != '}') {
669 raise_errmsg("Expecting object", pystr, end_idx);
670 goto bail;
671 }
672
673 *next_idx_ptr = idx + 1;
674
675 if (s->object_pairs_hook != Py_None) {
676 val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
677 if (val == NULL)
678 goto bail;
679 Py_DECREF(rval);
680 return val;
681 }
682
683 val = PyDict_New();
684 if (val == NULL)
685 goto bail;
686 if (PyDict_MergeFromSeq2(val, rval, 1) == -1)
687 goto bail;
688 Py_DECREF(rval);
689 rval = val;
690
691 /* if object_hook is not None: rval = object_hook(rval) */
692 if (s->object_hook != Py_None) {
693 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
694 if (val == NULL)
695 goto bail;
696 Py_DECREF(rval);
697 rval = val;
698 val = NULL;
699 }
700 return rval;
701bail:
702 Py_XDECREF(key);
703 Py_XDECREF(val);
704 Py_DECREF(rval);
705 return NULL;
706}
707
708static PyObject *
709_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
710 /* Read a JSON array from PyString pystr.
711 idx is the index of the first character after the opening brace.
712 *next_idx_ptr is a return-by-reference index to the first character after
713 the closing brace.
714
715 Returns a new PyList
716 */
717 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
718 Py_ssize_t end_idx = PyUnicode_GET_SIZE(pystr) - 1;
719 PyObject *val = NULL;
720 PyObject *rval = PyList_New(0);
721 Py_ssize_t next_idx;
722 if (rval == NULL)
723 return NULL;
724
725 /* skip whitespace after [ */
726 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
727
728 /* only loop if the array is non-empty */
729 if (idx <= end_idx && str[idx] != ']') {
730 while (idx <= end_idx) {
731
732 /* read any JSON term */
733 val = scan_once_unicode(s, pystr, idx, &next_idx);
734 if (val == NULL)
735 goto bail;
736
737 if (PyList_Append(rval, val) == -1)
738 goto bail;
739
740 Py_CLEAR(val);
741 idx = next_idx;
742
743 /* skip whitespace between term and , */
744 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
745
746 /* bail if the array is closed or we didn't get the , delimiter */
747 if (idx > end_idx) break;
748 if (str[idx] == ']') {
749 break;
750 }
751 else if (str[idx] != ',') {
752 raise_errmsg("Expecting , delimiter", pystr, idx);
753 goto bail;
754 }
755 idx++;
756
757 /* skip whitespace after , */
758 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
759 }
760 }
761
762 /* verify that idx < end_idx, str[idx] should be ']' */
763 if (idx > end_idx || str[idx] != ']') {
764 raise_errmsg("Expecting object", pystr, end_idx);
765 goto bail;
766 }
767 *next_idx_ptr = idx + 1;
768 return rval;
769bail:
770 Py_XDECREF(val);
771 Py_DECREF(rval);
772 return NULL;
773}
774
775static PyObject *
776_parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
777 /* Read a JSON constant from PyString pystr.
778 constant is the constant string that was found
779 ("NaN", "Infinity", "-Infinity").
780 idx is the index of the first character of the constant
781 *next_idx_ptr is a return-by-reference index to the first character after
782 the constant.
783
784 Returns the result of parse_constant
785 */
786 PyObject *cstr;
787 PyObject *rval;
788 /* constant is "NaN", "Infinity", or "-Infinity" */
789 cstr = PyUnicode_InternFromString(constant);
790 if (cstr == NULL)
791 return NULL;
792
793 /* rval = parse_constant(constant) */
794 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
795 idx += PyUnicode_GET_SIZE(cstr);
796 Py_DECREF(cstr);
797 *next_idx_ptr = idx;
798 return rval;
799}
800
801static PyObject *
802_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
803 /* Read a JSON number from PyUnicode pystr.
804 idx is the index of the first character of the number
805 *next_idx_ptr is a return-by-reference index to the first character after
806 the number.
807
808 Returns a new PyObject representation of that number:
809 PyInt, PyLong, or PyFloat.
810 May return other types if parse_int or parse_float are set
811 */
812 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
813 Py_ssize_t end_idx = PyUnicode_GET_SIZE(pystr) - 1;
814 Py_ssize_t idx = start;
815 int is_float = 0;
816 PyObject *rval;
817 PyObject *numstr;
818
819 /* read a sign if it's there, make sure it's not the end of the string */
820 if (str[idx] == '-') {
821 idx++;
822 if (idx > end_idx) {
823 PyErr_SetNone(PyExc_StopIteration);
824 return NULL;
825 }
826 }
827
828 /* read as many integer digits as we find as long as it doesn't start with 0 */
829 if (str[idx] >= '1' && str[idx] <= '9') {
830 idx++;
831 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++;
832 }
833 /* if it starts with 0 we only expect one integer digit */
834 else if (str[idx] == '0') {
835 idx++;
836 }
837 /* no integer digits, error */
838 else {
839 PyErr_SetNone(PyExc_StopIteration);
840 return NULL;
841 }
842
843 /* if the next char is '.' followed by a digit then read all float digits */
844 if (idx < end_idx && str[idx] == '.' && str[idx + 1] >= '0' && str[idx + 1] <= '9') {
845 is_float = 1;
846 idx += 2;
847 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++;
848 }
849
850 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
851 if (idx < end_idx && (str[idx] == 'e' || str[idx] == 'E')) {
852 Py_ssize_t e_start = idx;
853 idx++;
854
855 /* read an exponent sign if present */
856 if (idx < end_idx && (str[idx] == '-' || str[idx] == '+')) idx++;
857
858 /* read all digits */
859 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++;
860
861 /* if we got a digit, then parse as float. if not, backtrack */
862 if (str[idx - 1] >= '0' && str[idx - 1] <= '9') {
863 is_float = 1;
864 }
865 else {
866 idx = e_start;
867 }
868 }
869
870 /* copy the section we determined to be a number */
871 numstr = PyUnicode_FromUnicode(&str[start], idx - start);
872 if (numstr == NULL)
873 return NULL;
874 if (is_float) {
875 /* parse as a float using a fast path if available, otherwise call user defined method */
876 if (s->parse_float != (PyObject *)&PyFloat_Type) {
877 rval = PyObject_CallFunctionObjArgs(s->parse_float, numstr, NULL);
878 }
879 else {
880 rval = PyFloat_FromString(numstr);
881 }
882 }
883 else {
884 /* no fast path for unicode -> int, just call */
885 rval = PyObject_CallFunctionObjArgs(s->parse_int, numstr, NULL);
886 }
887 Py_DECREF(numstr);
888 *next_idx_ptr = idx;
889 return rval;
890}
891
892static PyObject *
893scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
894{
895 /* Read one JSON term (of any kind) from PyUnicode pystr.
896 idx is the index of the first character of the term
897 *next_idx_ptr is a return-by-reference index to the first character after
898 the number.
899
900 Returns a new PyObject representation of the term.
901 */
Ezio Melotti362b9512011-05-07 17:58:09 +0300902 PyObject *res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000903 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
904 Py_ssize_t length = PyUnicode_GET_SIZE(pystr);
905 if (idx >= length) {
906 PyErr_SetNone(PyExc_StopIteration);
907 return NULL;
908 }
909 switch (str[idx]) {
910 case '"':
911 /* string */
912 return scanstring_unicode(pystr, idx + 1,
913 PyObject_IsTrue(s->strict),
914 next_idx_ptr);
915 case '{':
916 /* object */
Ezio Melotti362b9512011-05-07 17:58:09 +0300917 if (Py_EnterRecursiveCall(" while decoding a JSON object "
918 "from a unicode string"))
919 return NULL;
920 res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
921 Py_LeaveRecursiveCall();
922 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000923 case '[':
924 /* array */
Ezio Melotti362b9512011-05-07 17:58:09 +0300925 if (Py_EnterRecursiveCall(" while decoding a JSON array "
926 "from a unicode string"))
927 return NULL;
928 res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
929 Py_LeaveRecursiveCall();
930 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000931 case 'n':
932 /* null */
933 if ((idx + 3 < length) && str[idx + 1] == 'u' && str[idx + 2] == 'l' && str[idx + 3] == 'l') {
934 Py_INCREF(Py_None);
935 *next_idx_ptr = idx + 4;
936 return Py_None;
937 }
938 break;
939 case 't':
940 /* true */
941 if ((idx + 3 < length) && str[idx + 1] == 'r' && str[idx + 2] == 'u' && str[idx + 3] == 'e') {
942 Py_INCREF(Py_True);
943 *next_idx_ptr = idx + 4;
944 return Py_True;
945 }
946 break;
947 case 'f':
948 /* false */
949 if ((idx + 4 < length) && str[idx + 1] == 'a' && str[idx + 2] == 'l' && str[idx + 3] == 's' && str[idx + 4] == 'e') {
950 Py_INCREF(Py_False);
951 *next_idx_ptr = idx + 5;
952 return Py_False;
953 }
954 break;
955 case 'N':
956 /* NaN */
957 if ((idx + 2 < length) && str[idx + 1] == 'a' && str[idx + 2] == 'N') {
958 return _parse_constant(s, "NaN", idx, next_idx_ptr);
959 }
960 break;
961 case 'I':
962 /* Infinity */
963 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') {
964 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
965 }
966 break;
967 case '-':
968 /* -Infinity */
969 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') {
970 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
971 }
972 break;
973 }
974 /* Didn't find a string, object, array, or named constant. Look for a number. */
975 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
976}
977
978static PyObject *
979scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
980{
981 /* Python callable interface to scan_once_{str,unicode} */
982 PyObject *pystr;
983 PyObject *rval;
984 Py_ssize_t idx;
985 Py_ssize_t next_idx = -1;
986 static char *kwlist[] = {"string", "idx", NULL};
987 PyScannerObject *s;
988 assert(PyScanner_Check(self));
989 s = (PyScannerObject *)self;
990 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:scan_once", kwlist, &pystr, _convertPyInt_AsSsize_t, &idx))
991 return NULL;
992
993 if (PyUnicode_Check(pystr)) {
994 rval = scan_once_unicode(s, pystr, idx, &next_idx);
995 }
996 else {
997 PyErr_Format(PyExc_TypeError,
998 "first argument must be a string, not %.80s",
999 Py_TYPE(pystr)->tp_name);
1000 return NULL;
1001 }
1002 return _build_rval_index_tuple(rval, next_idx);
1003}
1004
1005static PyObject *
1006scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1007{
1008 PyScannerObject *s;
1009 s = (PyScannerObject *)type->tp_alloc(type, 0);
1010 if (s != NULL) {
1011 s->strict = NULL;
1012 s->object_hook = NULL;
1013 s->object_pairs_hook = NULL;
1014 s->parse_float = NULL;
1015 s->parse_int = NULL;
1016 s->parse_constant = NULL;
1017 }
1018 return (PyObject *)s;
1019}
1020
1021static int
1022scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1023{
1024 /* Initialize Scanner object */
1025 PyObject *ctx;
1026 static char *kwlist[] = {"context", NULL};
1027 PyScannerObject *s;
1028
1029 assert(PyScanner_Check(self));
1030 s = (PyScannerObject *)self;
1031
1032 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1033 return -1;
1034
1035 /* All of these will fail "gracefully" so we don't need to verify them */
1036 s->strict = PyObject_GetAttrString(ctx, "strict");
1037 if (s->strict == NULL)
1038 goto bail;
1039 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1040 if (s->object_hook == NULL)
1041 goto bail;
1042 s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1043 if (s->object_pairs_hook == NULL)
1044 goto bail;
1045 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1046 if (s->parse_float == NULL)
1047 goto bail;
1048 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1049 if (s->parse_int == NULL)
1050 goto bail;
1051 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1052 if (s->parse_constant == NULL)
1053 goto bail;
1054
1055 return 0;
1056
1057bail:
1058 Py_CLEAR(s->strict);
1059 Py_CLEAR(s->object_hook);
1060 Py_CLEAR(s->object_pairs_hook);
1061 Py_CLEAR(s->parse_float);
1062 Py_CLEAR(s->parse_int);
1063 Py_CLEAR(s->parse_constant);
1064 return -1;
1065}
1066
1067PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1068
1069static
1070PyTypeObject PyScannerType = {
1071 PyVarObject_HEAD_INIT(NULL, 0)
1072 "_json.Scanner", /* tp_name */
1073 sizeof(PyScannerObject), /* tp_basicsize */
1074 0, /* tp_itemsize */
1075 scanner_dealloc, /* tp_dealloc */
1076 0, /* tp_print */
1077 0, /* tp_getattr */
1078 0, /* tp_setattr */
1079 0, /* tp_compare */
1080 0, /* tp_repr */
1081 0, /* tp_as_number */
1082 0, /* tp_as_sequence */
1083 0, /* tp_as_mapping */
1084 0, /* tp_hash */
1085 scanner_call, /* tp_call */
1086 0, /* tp_str */
1087 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1088 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1089 0, /* tp_as_buffer */
1090 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1091 scanner_doc, /* tp_doc */
1092 scanner_traverse, /* tp_traverse */
1093 scanner_clear, /* tp_clear */
1094 0, /* tp_richcompare */
1095 0, /* tp_weaklistoffset */
1096 0, /* tp_iter */
1097 0, /* tp_iternext */
1098 0, /* tp_methods */
1099 scanner_members, /* tp_members */
1100 0, /* tp_getset */
1101 0, /* tp_base */
1102 0, /* tp_dict */
1103 0, /* tp_descr_get */
1104 0, /* tp_descr_set */
1105 0, /* tp_dictoffset */
1106 scanner_init, /* tp_init */
1107 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1108 scanner_new, /* tp_new */
1109 0,/* PyObject_GC_Del, */ /* tp_free */
1110};
1111
1112static PyObject *
1113encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1114{
1115 PyEncoderObject *s;
1116 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1117 if (s != NULL) {
1118 s->markers = NULL;
1119 s->defaultfn = NULL;
1120 s->encoder = NULL;
1121 s->indent = NULL;
1122 s->key_separator = NULL;
1123 s->item_separator = NULL;
1124 s->sort_keys = NULL;
1125 s->skipkeys = NULL;
1126 }
1127 return (PyObject *)s;
1128}
1129
1130static int
1131encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1132{
1133 /* initialize Encoder object */
1134 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1135
1136 PyEncoderObject *s;
Antoine Pitrou4fad6bd2009-12-08 16:00:03 +00001137 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1138 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001139
1140 assert(PyEncoder_Check(self));
1141 s = (PyEncoderObject *)self;
1142
1143 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
Antoine Pitrou4fad6bd2009-12-08 16:00:03 +00001144 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1145 &sort_keys, &skipkeys, &allow_nan))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001146 return -1;
1147
Antoine Pitrou4fad6bd2009-12-08 16:00:03 +00001148 s->markers = markers;
1149 s->defaultfn = defaultfn;
1150 s->encoder = encoder;
1151 s->indent = indent;
1152 s->key_separator = key_separator;
1153 s->item_separator = item_separator;
1154 s->sort_keys = sort_keys;
1155 s->skipkeys = skipkeys;
1156 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1157 s->allow_nan = PyObject_IsTrue(allow_nan);
1158
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001159 Py_INCREF(s->markers);
1160 Py_INCREF(s->defaultfn);
1161 Py_INCREF(s->encoder);
1162 Py_INCREF(s->indent);
1163 Py_INCREF(s->key_separator);
1164 Py_INCREF(s->item_separator);
1165 Py_INCREF(s->sort_keys);
1166 Py_INCREF(s->skipkeys);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001167 return 0;
1168}
1169
1170static PyObject *
1171encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1172{
1173 /* Python callable interface to encode_listencode_obj */
1174 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1175 PyObject *obj;
1176 PyObject *rval;
1177 Py_ssize_t indent_level;
1178 PyEncoderObject *s;
1179 assert(PyEncoder_Check(self));
1180 s = (PyEncoderObject *)self;
1181 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:_iterencode", kwlist,
1182 &obj, _convertPyInt_AsSsize_t, &indent_level))
1183 return NULL;
1184 rval = PyList_New(0);
1185 if (rval == NULL)
1186 return NULL;
1187 if (encoder_listencode_obj(s, rval, obj, indent_level)) {
1188 Py_DECREF(rval);
1189 return NULL;
1190 }
1191 return rval;
1192}
1193
1194static PyObject *
1195_encoded_const(PyObject *obj)
1196{
1197 /* Return the JSON string representation of None, True, False */
1198 if (obj == Py_None) {
1199 static PyObject *s_null = NULL;
1200 if (s_null == NULL) {
1201 s_null = PyUnicode_InternFromString("null");
1202 }
1203 Py_INCREF(s_null);
1204 return s_null;
1205 }
1206 else if (obj == Py_True) {
1207 static PyObject *s_true = NULL;
1208 if (s_true == NULL) {
1209 s_true = PyUnicode_InternFromString("true");
1210 }
1211 Py_INCREF(s_true);
1212 return s_true;
1213 }
1214 else if (obj == Py_False) {
1215 static PyObject *s_false = NULL;
1216 if (s_false == NULL) {
1217 s_false = PyUnicode_InternFromString("false");
1218 }
1219 Py_INCREF(s_false);
1220 return s_false;
1221 }
1222 else {
1223 PyErr_SetString(PyExc_ValueError, "not a const");
1224 return NULL;
1225 }
1226}
1227
1228static PyObject *
1229encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1230{
1231 /* Return the JSON representation of a PyFloat */
1232 double i = PyFloat_AS_DOUBLE(obj);
1233 if (!Py_IS_FINITE(i)) {
1234 if (!s->allow_nan) {
1235 PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant");
1236 return NULL;
1237 }
1238 if (i > 0) {
1239 return PyUnicode_FromString("Infinity");
1240 }
1241 else if (i < 0) {
1242 return PyUnicode_FromString("-Infinity");
1243 }
1244 else {
1245 return PyUnicode_FromString("NaN");
1246 }
1247 }
1248 /* Use a better float format here? */
1249 return PyObject_Repr(obj);
1250}
1251
1252static PyObject *
1253encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1254{
1255 /* Return the JSON representation of a string */
1256 if (s->fast_encode)
1257 return py_encode_basestring_ascii(NULL, obj);
1258 else
1259 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1260}
1261
1262static int
1263_steal_list_append(PyObject *lst, PyObject *stolen)
1264{
1265 /* Append stolen and then decrement its reference count */
1266 int rval = PyList_Append(lst, stolen);
1267 Py_DECREF(stolen);
1268 return rval;
1269}
1270
1271static int
1272encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssize_t indent_level)
1273{
1274 /* Encode Python object obj to a JSON term, rval is a PyList */
1275 PyObject *newobj;
1276 int rv;
1277
1278 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1279 PyObject *cstr = _encoded_const(obj);
1280 if (cstr == NULL)
1281 return -1;
1282 return _steal_list_append(rval, cstr);
1283 }
1284 else if (PyUnicode_Check(obj))
1285 {
1286 PyObject *encoded = encoder_encode_string(s, obj);
1287 if (encoded == NULL)
1288 return -1;
1289 return _steal_list_append(rval, encoded);
1290 }
1291 else if (PyLong_Check(obj)) {
1292 PyObject *encoded = PyObject_Str(obj);
1293 if (encoded == NULL)
1294 return -1;
1295 return _steal_list_append(rval, encoded);
1296 }
1297 else if (PyFloat_Check(obj)) {
1298 PyObject *encoded = encoder_encode_float(s, obj);
1299 if (encoded == NULL)
1300 return -1;
1301 return _steal_list_append(rval, encoded);
1302 }
1303 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001304 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1305 return -1;
1306 rv = encoder_listencode_list(s, rval, obj, indent_level);
1307 Py_LeaveRecursiveCall();
1308 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001309 }
1310 else if (PyDict_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001311 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1312 return -1;
1313 rv = encoder_listencode_dict(s, rval, obj, indent_level);
1314 Py_LeaveRecursiveCall();
1315 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001316 }
1317 else {
1318 PyObject *ident = NULL;
1319 if (s->markers != Py_None) {
1320 int has_key;
1321 ident = PyLong_FromVoidPtr(obj);
1322 if (ident == NULL)
1323 return -1;
1324 has_key = PyDict_Contains(s->markers, ident);
1325 if (has_key) {
1326 if (has_key != -1)
1327 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1328 Py_DECREF(ident);
1329 return -1;
1330 }
1331 if (PyDict_SetItem(s->markers, ident, obj)) {
1332 Py_DECREF(ident);
1333 return -1;
1334 }
1335 }
1336 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1337 if (newobj == NULL) {
1338 Py_XDECREF(ident);
1339 return -1;
1340 }
Ezio Melotti13672652011-05-11 01:02:56 +03001341
1342 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1343 return -1;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001344 rv = encoder_listencode_obj(s, rval, newobj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001345 Py_LeaveRecursiveCall();
1346
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001347 Py_DECREF(newobj);
1348 if (rv) {
1349 Py_XDECREF(ident);
1350 return -1;
1351 }
1352 if (ident != NULL) {
1353 if (PyDict_DelItem(s->markers, ident)) {
1354 Py_XDECREF(ident);
1355 return -1;
1356 }
1357 Py_XDECREF(ident);
1358 }
1359 return rv;
1360 }
1361}
1362
1363static int
1364encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ssize_t indent_level)
1365{
1366 /* Encode Python dict dct a JSON term, rval is a PyList */
1367 static PyObject *open_dict = NULL;
1368 static PyObject *close_dict = NULL;
1369 static PyObject *empty_dict = NULL;
1370 PyObject *kstr = NULL;
1371 PyObject *ident = NULL;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001372 PyObject *it = NULL;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001373 PyObject *items;
1374 PyObject *item = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001375 int skipkeys;
1376 Py_ssize_t idx;
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001377 PyObject *mapping;
1378 static PyObject *code = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001379
1380 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
1381 open_dict = PyUnicode_InternFromString("{");
1382 close_dict = PyUnicode_InternFromString("}");
1383 empty_dict = PyUnicode_InternFromString("{}");
1384 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
1385 return -1;
1386 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001387 if (Py_SIZE(dct) == 0)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001388 return PyList_Append(rval, empty_dict);
1389
1390 if (s->markers != Py_None) {
1391 int has_key;
1392 ident = PyLong_FromVoidPtr(dct);
1393 if (ident == NULL)
1394 goto bail;
1395 has_key = PyDict_Contains(s->markers, ident);
1396 if (has_key) {
1397 if (has_key != -1)
1398 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1399 goto bail;
1400 }
1401 if (PyDict_SetItem(s->markers, ident, dct)) {
1402 goto bail;
1403 }
1404 }
1405
1406 if (PyList_Append(rval, open_dict))
1407 goto bail;
1408
1409 if (s->indent != Py_None) {
1410 /* TODO: DOES NOT RUN */
1411 indent_level += 1;
1412 /*
1413 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1414 separator = _item_separator + newline_indent
1415 buf += newline_indent
1416 */
1417 }
1418
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001419 if (PyObject_IsTrue(s->sort_keys)) {
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001420 if (code == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001421 code = Py_CompileString("sorted(d.items(), key=lambda kv: kv[0])",
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001422 "_json.c", Py_eval_input);
1423 if (code == NULL)
1424 goto bail;
1425 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001426
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001427 mapping = PyDict_New();
1428 if (mapping == NULL)
1429 goto bail;
1430 if (PyDict_SetItemString(mapping, "d", dct) == -1) {
1431 Py_DECREF(mapping);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001432 goto bail;
1433 }
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001434 items = PyEval_EvalCode((PyCodeObject *)code, PyEval_GetGlobals(), mapping);
1435 Py_DECREF(mapping);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001436 } else {
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001437 items = PyMapping_Items(dct);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001438 }
1439 if (items == NULL)
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001440 goto bail;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001441 it = PyObject_GetIter(items);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001442 Py_DECREF(items);
1443 if (it == NULL)
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001444 goto bail;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001445 skipkeys = PyObject_IsTrue(s->skipkeys);
1446 idx = 0;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001447 while ((item = PyIter_Next(it)) != NULL) {
1448 PyObject *encoded, *key, *value;
1449 if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
1450 PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
1451 goto bail;
1452 }
1453 key = PyTuple_GET_ITEM(item, 0);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001454 if (PyUnicode_Check(key)) {
1455 Py_INCREF(key);
1456 kstr = key;
1457 }
1458 else if (PyFloat_Check(key)) {
1459 kstr = encoder_encode_float(s, key);
1460 if (kstr == NULL)
1461 goto bail;
1462 }
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001463 else if (key == Py_True || key == Py_False || key == Py_None) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001464 /* This must come before the PyLong_Check because
1465 True and False are also 1 and 0.*/
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001466 kstr = _encoded_const(key);
1467 if (kstr == NULL)
1468 goto bail;
1469 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001470 else if (PyLong_Check(key)) {
1471 kstr = PyObject_Str(key);
1472 if (kstr == NULL)
1473 goto bail;
1474 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001475 else if (skipkeys) {
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001476 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001477 continue;
1478 }
1479 else {
1480 /* TODO: include repr of key */
Doug Hellmann76e57942010-07-21 12:35:38 +00001481 PyErr_SetString(PyExc_TypeError, "keys must be a string");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001482 goto bail;
1483 }
1484
1485 if (idx) {
1486 if (PyList_Append(rval, s->item_separator))
1487 goto bail;
1488 }
1489
1490 encoded = encoder_encode_string(s, kstr);
1491 Py_CLEAR(kstr);
1492 if (encoded == NULL)
1493 goto bail;
1494 if (PyList_Append(rval, encoded)) {
1495 Py_DECREF(encoded);
1496 goto bail;
1497 }
1498 Py_DECREF(encoded);
1499 if (PyList_Append(rval, s->key_separator))
1500 goto bail;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001501
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001502 value = PyTuple_GET_ITEM(item, 1);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001503 if (encoder_listencode_obj(s, rval, value, indent_level))
1504 goto bail;
1505 idx += 1;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001506 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001507 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001508 if (PyErr_Occurred())
1509 goto bail;
1510 Py_CLEAR(it);
1511
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001512 if (ident != NULL) {
1513 if (PyDict_DelItem(s->markers, ident))
1514 goto bail;
1515 Py_CLEAR(ident);
1516 }
1517 if (s->indent != Py_None) {
1518 /* TODO: DOES NOT RUN */
1519 indent_level -= 1;
1520 /*
1521 yield '\n' + (' ' * (_indent * _current_indent_level))
1522 */
1523 }
1524 if (PyList_Append(rval, close_dict))
1525 goto bail;
1526 return 0;
1527
1528bail:
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001529 Py_XDECREF(it);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001530 Py_XDECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001531 Py_XDECREF(kstr);
1532 Py_XDECREF(ident);
1533 return -1;
1534}
1535
1536
1537static int
1538encoder_listencode_list(PyEncoderObject *s, PyObject *rval, PyObject *seq, Py_ssize_t indent_level)
1539{
1540 /* Encode Python list seq to a JSON term, rval is a PyList */
1541 static PyObject *open_array = NULL;
1542 static PyObject *close_array = NULL;
1543 static PyObject *empty_array = NULL;
1544 PyObject *ident = NULL;
1545 PyObject *s_fast = NULL;
1546 Py_ssize_t num_items;
1547 PyObject **seq_items;
1548 Py_ssize_t i;
1549
1550 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
1551 open_array = PyUnicode_InternFromString("[");
1552 close_array = PyUnicode_InternFromString("]");
1553 empty_array = PyUnicode_InternFromString("[]");
1554 if (open_array == NULL || close_array == NULL || empty_array == NULL)
1555 return -1;
1556 }
1557 ident = NULL;
1558 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
1559 if (s_fast == NULL)
1560 return -1;
1561 num_items = PySequence_Fast_GET_SIZE(s_fast);
1562 if (num_items == 0) {
1563 Py_DECREF(s_fast);
1564 return PyList_Append(rval, empty_array);
1565 }
1566
1567 if (s->markers != Py_None) {
1568 int has_key;
1569 ident = PyLong_FromVoidPtr(seq);
1570 if (ident == NULL)
1571 goto bail;
1572 has_key = PyDict_Contains(s->markers, ident);
1573 if (has_key) {
1574 if (has_key != -1)
1575 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1576 goto bail;
1577 }
1578 if (PyDict_SetItem(s->markers, ident, seq)) {
1579 goto bail;
1580 }
1581 }
1582
1583 seq_items = PySequence_Fast_ITEMS(s_fast);
1584 if (PyList_Append(rval, open_array))
1585 goto bail;
1586 if (s->indent != Py_None) {
1587 /* TODO: DOES NOT RUN */
1588 indent_level += 1;
1589 /*
1590 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1591 separator = _item_separator + newline_indent
1592 buf += newline_indent
1593 */
1594 }
1595 for (i = 0; i < num_items; i++) {
1596 PyObject *obj = seq_items[i];
1597 if (i) {
1598 if (PyList_Append(rval, s->item_separator))
1599 goto bail;
1600 }
1601 if (encoder_listencode_obj(s, rval, obj, indent_level))
1602 goto bail;
1603 }
1604 if (ident != NULL) {
1605 if (PyDict_DelItem(s->markers, ident))
1606 goto bail;
1607 Py_CLEAR(ident);
1608 }
1609 if (s->indent != Py_None) {
1610 /* TODO: DOES NOT RUN */
1611 indent_level -= 1;
1612 /*
1613 yield '\n' + (' ' * (_indent * _current_indent_level))
1614 */
1615 }
1616 if (PyList_Append(rval, close_array))
1617 goto bail;
1618 Py_DECREF(s_fast);
1619 return 0;
1620
1621bail:
1622 Py_XDECREF(ident);
1623 Py_DECREF(s_fast);
1624 return -1;
1625}
1626
1627static void
1628encoder_dealloc(PyObject *self)
1629{
1630 /* Deallocate Encoder */
1631 encoder_clear(self);
1632 Py_TYPE(self)->tp_free(self);
1633}
1634
1635static int
1636encoder_traverse(PyObject *self, visitproc visit, void *arg)
1637{
1638 PyEncoderObject *s;
1639 assert(PyEncoder_Check(self));
1640 s = (PyEncoderObject *)self;
1641 Py_VISIT(s->markers);
1642 Py_VISIT(s->defaultfn);
1643 Py_VISIT(s->encoder);
1644 Py_VISIT(s->indent);
1645 Py_VISIT(s->key_separator);
1646 Py_VISIT(s->item_separator);
1647 Py_VISIT(s->sort_keys);
1648 Py_VISIT(s->skipkeys);
1649 return 0;
1650}
1651
1652static int
1653encoder_clear(PyObject *self)
1654{
1655 /* Deallocate Encoder */
1656 PyEncoderObject *s;
1657 assert(PyEncoder_Check(self));
1658 s = (PyEncoderObject *)self;
1659 Py_CLEAR(s->markers);
1660 Py_CLEAR(s->defaultfn);
1661 Py_CLEAR(s->encoder);
1662 Py_CLEAR(s->indent);
1663 Py_CLEAR(s->key_separator);
1664 Py_CLEAR(s->item_separator);
1665 Py_CLEAR(s->sort_keys);
1666 Py_CLEAR(s->skipkeys);
1667 return 0;
1668}
1669
1670PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
1671
1672static
1673PyTypeObject PyEncoderType = {
1674 PyVarObject_HEAD_INIT(NULL, 0)
1675 "_json.Encoder", /* tp_name */
1676 sizeof(PyEncoderObject), /* tp_basicsize */
1677 0, /* tp_itemsize */
1678 encoder_dealloc, /* tp_dealloc */
1679 0, /* tp_print */
1680 0, /* tp_getattr */
1681 0, /* tp_setattr */
1682 0, /* tp_compare */
1683 0, /* tp_repr */
1684 0, /* tp_as_number */
1685 0, /* tp_as_sequence */
1686 0, /* tp_as_mapping */
1687 0, /* tp_hash */
1688 encoder_call, /* tp_call */
1689 0, /* tp_str */
1690 0, /* tp_getattro */
1691 0, /* tp_setattro */
1692 0, /* tp_as_buffer */
1693 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1694 encoder_doc, /* tp_doc */
1695 encoder_traverse, /* tp_traverse */
1696 encoder_clear, /* tp_clear */
1697 0, /* tp_richcompare */
1698 0, /* tp_weaklistoffset */
1699 0, /* tp_iter */
1700 0, /* tp_iternext */
1701 0, /* tp_methods */
1702 encoder_members, /* tp_members */
1703 0, /* tp_getset */
1704 0, /* tp_base */
1705 0, /* tp_dict */
1706 0, /* tp_descr_get */
1707 0, /* tp_descr_set */
1708 0, /* tp_dictoffset */
1709 encoder_init, /* tp_init */
1710 0, /* tp_alloc */
1711 encoder_new, /* tp_new */
1712 0, /* tp_free */
1713};
1714
1715static PyMethodDef speedups_methods[] = {
1716 {"encode_basestring_ascii",
1717 (PyCFunction)py_encode_basestring_ascii,
1718 METH_O,
1719 pydoc_encode_basestring_ascii},
1720 {"scanstring",
1721 (PyCFunction)py_scanstring,
1722 METH_VARARGS,
1723 pydoc_scanstring},
Christian Heimes90540002008-05-08 14:29:10 +00001724 {NULL, NULL, 0, NULL}
1725};
1726
1727PyDoc_STRVAR(module_doc,
1728"json speedups\n");
1729
Martin v. Löwis1a214512008-06-11 05:26:20 +00001730static struct PyModuleDef jsonmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001731 PyModuleDef_HEAD_INIT,
1732 "_json",
1733 module_doc,
1734 -1,
1735 speedups_methods,
1736 NULL,
1737 NULL,
1738 NULL,
1739 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001740};
1741
1742PyObject*
1743PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +00001744{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001745 PyObject *m = PyModule_Create(&jsonmodule);
1746 if (!m)
1747 return NULL;
1748 PyScannerType.tp_new = PyType_GenericNew;
1749 if (PyType_Ready(&PyScannerType) < 0)
1750 goto fail;
1751 PyEncoderType.tp_new = PyType_GenericNew;
1752 if (PyType_Ready(&PyEncoderType) < 0)
1753 goto fail;
1754 Py_INCREF((PyObject*)&PyScannerType);
1755 if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1756 Py_DECREF((PyObject*)&PyScannerType);
1757 goto fail;
1758 }
1759 Py_INCREF((PyObject*)&PyEncoderType);
1760 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1761 Py_DECREF((PyObject*)&PyEncoderType);
1762 goto fail;
1763 }
1764 return m;
1765 fail:
1766 Py_DECREF(m);
1767 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +00001768}