blob: e49d1b2f41ab03b58910f4f7d1bec24d0197cfd1 [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;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +000039 PyObject *memo;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000040} PyScannerObject;
41
42static PyMemberDef scanner_members[] = {
43 {"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"},
44 {"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"},
45 {"object_pairs_hook", T_OBJECT, offsetof(PyScannerObject, object_pairs_hook), READONLY},
46 {"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"},
47 {"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "parse_int"},
48 {"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READONLY, "parse_constant"},
49 {NULL}
50};
51
52typedef struct _PyEncoderObject {
53 PyObject_HEAD
54 PyObject *markers;
55 PyObject *defaultfn;
56 PyObject *encoder;
57 PyObject *indent;
58 PyObject *key_separator;
59 PyObject *item_separator;
60 PyObject *sort_keys;
61 PyObject *skipkeys;
62 int fast_encode;
63 int allow_nan;
64} PyEncoderObject;
65
66static PyMemberDef encoder_members[] = {
67 {"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers"},
68 {"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "default"},
69 {"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder"},
70 {"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"},
71 {"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READONLY, "key_separator"},
72 {"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READONLY, "item_separator"},
73 {"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sort_keys"},
74 {"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipkeys"},
75 {NULL}
76};
77
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +020078static PyObject *
79join_list_unicode(PyObject *lst)
80{
81 /* return u''.join(lst) */
82 static PyObject *sep = NULL;
83 if (sep == NULL) {
84 sep = PyUnicode_FromStringAndSize("", 0);
85 if (sep == NULL)
86 return NULL;
87 }
88 return PyUnicode_Join(sep, lst);
89}
90
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +020091/* Forward decls */
92
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000093static PyObject *
94ascii_escape_unicode(PyObject *pystr);
95static PyObject *
96py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr);
97void init_json(void);
98static PyObject *
99scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr);
100static PyObject *
101_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx);
102static PyObject *
103scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
104static int
105scanner_init(PyObject *self, PyObject *args, PyObject *kwds);
106static void
107scanner_dealloc(PyObject *self);
108static int
109scanner_clear(PyObject *self);
110static PyObject *
111encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
112static int
113encoder_init(PyObject *self, PyObject *args, PyObject *kwds);
114static void
115encoder_dealloc(PyObject *self);
116static int
117encoder_clear(PyObject *self);
118static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200119encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc, PyObject *seq, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000120static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200121encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, PyObject *obj, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000122static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200123encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, PyObject *dct, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000124static PyObject *
Hirokazu Yamamotofecf5d12009-05-02 15:55:19 +0000125_encoded_const(PyObject *obj);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000126static void
127raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
128static PyObject *
129encoder_encode_string(PyEncoderObject *s, PyObject *obj);
130static int
131_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr);
132static PyObject *
133_convertPyInt_FromSsize_t(Py_ssize_t *size_ptr);
134static PyObject *
135encoder_encode_float(PyEncoderObject *s, PyObject *obj);
136
Christian Heimes90540002008-05-08 14:29:10 +0000137#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000138#define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
Christian Heimes90540002008-05-08 14:29:10 +0000139
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000140static int
141_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000142{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000143 /* PyObject to Py_ssize_t converter */
144 *size_ptr = PyLong_AsSsize_t(o);
Georg Brandl59682052009-05-05 07:52:05 +0000145 if (*size_ptr == -1 && PyErr_Occurred())
146 return 0;
147 return 1;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000148}
149
150static PyObject *
151_convertPyInt_FromSsize_t(Py_ssize_t *size_ptr)
152{
153 /* Py_ssize_t to PyObject converter */
154 return PyLong_FromSsize_t(*size_ptr);
155}
156
157static Py_ssize_t
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200158ascii_escape_unichar(Py_UCS4 c, unsigned char *output, Py_ssize_t chars)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000159{
160 /* Escape unicode code point c to ASCII escape sequences
161 in char *output. output must have at least 12 bytes unused to
162 accommodate an escaped surrogate pair "\uXXXX\uXXXX" */
Christian Heimes90540002008-05-08 14:29:10 +0000163 output[chars++] = '\\';
164 switch (c) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000165 case '\\': output[chars++] = c; break;
166 case '"': output[chars++] = c; break;
Christian Heimes90540002008-05-08 14:29:10 +0000167 case '\b': output[chars++] = 'b'; break;
168 case '\f': output[chars++] = 'f'; break;
169 case '\n': output[chars++] = 'n'; break;
170 case '\r': output[chars++] = 'r'; break;
171 case '\t': output[chars++] = 't'; break;
172 default:
Christian Heimes90540002008-05-08 14:29:10 +0000173 if (c >= 0x10000) {
174 /* UTF-16 surrogate pair */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200175 Py_UCS4 v = c - 0x10000;
Christian Heimes90540002008-05-08 14:29:10 +0000176 c = 0xd800 | ((v >> 10) & 0x3ff);
177 output[chars++] = 'u';
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000178 output[chars++] = "0123456789abcdef"[(c >> 12) & 0xf];
179 output[chars++] = "0123456789abcdef"[(c >> 8) & 0xf];
180 output[chars++] = "0123456789abcdef"[(c >> 4) & 0xf];
181 output[chars++] = "0123456789abcdef"[(c ) & 0xf];
Christian Heimes90540002008-05-08 14:29:10 +0000182 c = 0xdc00 | (v & 0x3ff);
183 output[chars++] = '\\';
184 }
Christian Heimes90540002008-05-08 14:29:10 +0000185 output[chars++] = 'u';
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000186 output[chars++] = "0123456789abcdef"[(c >> 12) & 0xf];
187 output[chars++] = "0123456789abcdef"[(c >> 8) & 0xf];
188 output[chars++] = "0123456789abcdef"[(c >> 4) & 0xf];
189 output[chars++] = "0123456789abcdef"[(c ) & 0xf];
Christian Heimes90540002008-05-08 14:29:10 +0000190 }
191 return chars;
192}
193
194static PyObject *
195ascii_escape_unicode(PyObject *pystr)
196{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000197 /* Take a PyUnicode pystr and return a new ASCII-only escaped PyUnicode */
Christian Heimes90540002008-05-08 14:29:10 +0000198 Py_ssize_t i;
199 Py_ssize_t input_chars;
200 Py_ssize_t output_size;
201 Py_ssize_t chars;
202 PyObject *rval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200203 void *input;
204 unsigned char *output;
205 int kind;
Christian Heimes90540002008-05-08 14:29:10 +0000206
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200207 if (PyUnicode_READY(pystr) == -1)
208 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000209
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200210 input_chars = PyUnicode_GET_LENGTH(pystr);
211 input = PyUnicode_DATA(pystr);
212 kind = PyUnicode_KIND(pystr);
213
214 /* Compute the output size */
215 for (i = 0, output_size = 2; i < input_chars; i++) {
216 Py_UCS4 c = PyUnicode_READ(kind, input, i);
217 if (S_CHAR(c))
218 output_size++;
219 else {
220 switch(c) {
221 case '\\': case '"': case '\b': case '\f':
222 case '\n': case '\r': case '\t':
223 output_size += 2; break;
224 default:
225 output_size += c >= 0x10000 ? 12 : 6;
226 }
227 }
228 }
229
230 rval = PyUnicode_New(output_size, 127);
Christian Heimes90540002008-05-08 14:29:10 +0000231 if (rval == NULL) {
232 return NULL;
233 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200234 output = PyUnicode_1BYTE_DATA(rval);
Christian Heimes90540002008-05-08 14:29:10 +0000235 chars = 0;
236 output[chars++] = '"';
237 for (i = 0; i < input_chars; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200238 Py_UCS4 c = PyUnicode_READ(kind, input, i);
Christian Heimes90540002008-05-08 14:29:10 +0000239 if (S_CHAR(c)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000240 output[chars++] = c;
Christian Heimes90540002008-05-08 14:29:10 +0000241 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000242 else {
243 chars = ascii_escape_unichar(c, output, chars);
Christian Heimes90540002008-05-08 14:29:10 +0000244 }
Christian Heimes90540002008-05-08 14:29:10 +0000245 }
246 output[chars++] = '"';
Christian Heimes90540002008-05-08 14:29:10 +0000247 return rval;
248}
249
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000250static void
Christian Heimes90540002008-05-08 14:29:10 +0000251raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
252{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000253 /* Use the Python function json.decoder.errmsg to raise a nice
254 looking ValueError exception */
Christian Heimes90540002008-05-08 14:29:10 +0000255 static PyObject *errmsg_fn = NULL;
256 PyObject *pymsg;
257 if (errmsg_fn == NULL) {
258 PyObject *decoder = PyImport_ImportModule("json.decoder");
259 if (decoder == NULL)
260 return;
261 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000262 Py_DECREF(decoder);
Christian Heimes90540002008-05-08 14:29:10 +0000263 if (errmsg_fn == NULL)
264 return;
Christian Heimes90540002008-05-08 14:29:10 +0000265 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000266 pymsg = PyObject_CallFunction(errmsg_fn, "(zOO&)", msg, s, _convertPyInt_FromSsize_t, &end);
Benjamin Petersona13d4752008-10-16 21:17:24 +0000267 if (pymsg) {
268 PyErr_SetObject(PyExc_ValueError, pymsg);
269 Py_DECREF(pymsg);
270 }
Christian Heimes90540002008-05-08 14:29:10 +0000271}
272
273static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000274_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
275 /* return (rval, idx) tuple, stealing reference to rval */
276 PyObject *tpl;
277 PyObject *pyidx;
278 /*
279 steal a reference to rval, returns (rval, idx)
280 */
281 if (rval == NULL) {
Christian Heimes90540002008-05-08 14:29:10 +0000282 return NULL;
283 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000284 pyidx = PyLong_FromSsize_t(idx);
285 if (pyidx == NULL) {
286 Py_DECREF(rval);
287 return NULL;
288 }
289 tpl = PyTuple_New(2);
290 if (tpl == NULL) {
291 Py_DECREF(pyidx);
292 Py_DECREF(rval);
293 return NULL;
294 }
295 PyTuple_SET_ITEM(tpl, 0, rval);
296 PyTuple_SET_ITEM(tpl, 1, pyidx);
297 return tpl;
Christian Heimes90540002008-05-08 14:29:10 +0000298}
299
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000300#define APPEND_OLD_CHUNK \
301 if (chunk != NULL) { \
302 if (chunks == NULL) { \
303 chunks = PyList_New(0); \
304 if (chunks == NULL) { \
305 goto bail; \
306 } \
307 } \
308 if (PyList_Append(chunks, chunk)) { \
309 Py_DECREF(chunk); \
310 goto bail; \
311 } \
312 Py_CLEAR(chunk); \
313 }
314
Christian Heimes90540002008-05-08 14:29:10 +0000315static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000316scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000317{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000318 /* Read the JSON string from PyUnicode pystr.
319 end is the index of the first character after the quote.
320 if strict is zero then literal control characters are allowed
321 *next_end_ptr is a return-by-reference index of the character
322 after the end quote
Christian Heimes90540002008-05-08 14:29:10 +0000323
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000324 Return value is a new PyUnicode
325 */
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000326 PyObject *rval = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200327 Py_ssize_t len;
Christian Heimes90540002008-05-08 14:29:10 +0000328 Py_ssize_t begin = end - 1;
Brett Cannonb94767f2011-02-22 20:15:44 +0000329 Py_ssize_t next /* = begin */;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200330 const void *buf;
331 int kind;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000332 PyObject *chunks = NULL;
333 PyObject *chunk = NULL;
334
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200335 if (PyUnicode_READY(pystr) == -1)
336 return 0;
337
338 len = PyUnicode_GET_LENGTH(pystr);
339 buf = PyUnicode_DATA(pystr);
340 kind = PyUnicode_KIND(pystr);
341
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000342 if (end < 0 || len <= end) {
343 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
344 goto bail;
345 }
Christian Heimes90540002008-05-08 14:29:10 +0000346 while (1) {
347 /* Find the end of the string or the next escape */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200348 Py_UCS4 c = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000349 for (next = end; next < len; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200350 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000351 if (c == '"' || c == '\\') {
352 break;
353 }
354 else if (strict && c <= 0x1f) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000355 raise_errmsg("Invalid control character at", pystr, next);
Christian Heimes90540002008-05-08 14:29:10 +0000356 goto bail;
357 }
358 }
359 if (!(c == '"' || c == '\\')) {
360 raise_errmsg("Unterminated string starting at", pystr, begin);
361 goto bail;
362 }
363 /* Pick up this chunk if it's not zero length */
364 if (next != end) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000365 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200366 chunk = PyUnicode_FromKindAndData(
367 kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200368 (char*)buf + kind * end,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200369 next - end);
Christian Heimes90540002008-05-08 14:29:10 +0000370 if (chunk == NULL) {
371 goto bail;
372 }
Christian Heimes90540002008-05-08 14:29:10 +0000373 }
374 next++;
375 if (c == '"') {
376 end = next;
377 break;
378 }
379 if (next == len) {
380 raise_errmsg("Unterminated string starting at", pystr, begin);
381 goto bail;
382 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200383 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000384 if (c != 'u') {
385 /* Non-unicode backslash escapes */
386 end = next + 1;
387 switch (c) {
388 case '"': break;
389 case '\\': break;
390 case '/': break;
391 case 'b': c = '\b'; break;
392 case 'f': c = '\f'; break;
393 case 'n': c = '\n'; break;
394 case 'r': c = '\r'; break;
395 case 't': c = '\t'; break;
396 default: c = 0;
397 }
398 if (c == 0) {
399 raise_errmsg("Invalid \\escape", pystr, end - 2);
400 goto bail;
401 }
402 }
403 else {
404 c = 0;
405 next++;
406 end = next + 4;
407 if (end >= len) {
408 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
409 goto bail;
410 }
411 /* Decode 4 hex digits */
412 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200413 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000414 c <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000415 switch (digit) {
416 case '0': case '1': case '2': case '3': case '4':
417 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000418 c |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000419 case 'a': case 'b': case 'c': case 'd': case 'e':
420 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000421 c |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000422 case 'A': case 'B': case 'C': case 'D': case 'E':
423 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000424 c |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000425 default:
426 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
427 goto bail;
428 }
429 }
Christian Heimes90540002008-05-08 14:29:10 +0000430 /* Surrogate pair */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000431 if ((c & 0xfc00) == 0xd800) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200432 Py_UCS4 c2 = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000433 if (end + 6 >= len) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000434 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
435 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000436 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200437 if (PyUnicode_READ(kind, buf, next++) != '\\' ||
438 PyUnicode_READ(kind, buf, next++) != 'u') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000439 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
440 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000441 }
442 end += 6;
443 /* Decode 4 hex digits */
444 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200445 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Antoine Pitrou5b0e9e82010-10-09 15:24:28 +0000446 c2 <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000447 switch (digit) {
448 case '0': case '1': case '2': case '3': case '4':
449 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000450 c2 |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000451 case 'a': case 'b': case 'c': case 'd': case 'e':
452 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000453 c2 |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000454 case 'A': case 'B': case 'C': case 'D': case 'E':
455 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000456 c2 |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000457 default:
458 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
459 goto bail;
460 }
461 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000462 if ((c2 & 0xfc00) != 0xdc00) {
463 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
464 goto bail;
465 }
Christian Heimes90540002008-05-08 14:29:10 +0000466 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
467 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000468 else if ((c & 0xfc00) == 0xdc00) {
469 raise_errmsg("Unpaired low surrogate", pystr, end - 5);
470 goto bail;
471 }
Christian Heimes90540002008-05-08 14:29:10 +0000472 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000473 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200474 chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1);
Christian Heimes90540002008-05-08 14:29:10 +0000475 if (chunk == NULL) {
476 goto bail;
477 }
Christian Heimes90540002008-05-08 14:29:10 +0000478 }
479
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000480 if (chunks == NULL) {
481 if (chunk != NULL)
482 rval = chunk;
483 else
484 rval = PyUnicode_FromStringAndSize("", 0);
Christian Heimes90540002008-05-08 14:29:10 +0000485 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000486 else {
487 APPEND_OLD_CHUNK
488 rval = join_list_unicode(chunks);
489 if (rval == NULL) {
490 goto bail;
491 }
492 Py_CLEAR(chunks);
493 }
494
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000495 *next_end_ptr = end;
496 return rval;
Christian Heimes90540002008-05-08 14:29:10 +0000497bail:
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000498 *next_end_ptr = -1;
Christian Heimes90540002008-05-08 14:29:10 +0000499 Py_XDECREF(chunks);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000500 Py_XDECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000501 return NULL;
502}
503
504PyDoc_STRVAR(pydoc_scanstring,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000505 "scanstring(string, end, strict=True) -> (string, end)\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000506 "\n"
507 "Scan the string s for a JSON string. End is the index of the\n"
508 "character in s after the quote that started the JSON string.\n"
509 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
510 "on attempt to decode an invalid string. If strict is False then literal\n"
511 "control characters are allowed in the string.\n"
512 "\n"
513 "Returns a tuple of the decoded string and the index of the character in s\n"
514 "after the end quote."
515);
Christian Heimes90540002008-05-08 14:29:10 +0000516
517static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000518py_scanstring(PyObject* self UNUSED, PyObject *args)
Christian Heimes90540002008-05-08 14:29:10 +0000519{
520 PyObject *pystr;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000521 PyObject *rval;
Christian Heimes90540002008-05-08 14:29:10 +0000522 Py_ssize_t end;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000523 Py_ssize_t next_end = -1;
524 int strict = 1;
525 if (!PyArg_ParseTuple(args, "OO&|i:scanstring", &pystr, _convertPyInt_AsSsize_t, &end, &strict)) {
Christian Heimes90540002008-05-08 14:29:10 +0000526 return NULL;
527 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000528 if (PyUnicode_Check(pystr)) {
529 rval = scanstring_unicode(pystr, end, strict, &next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000530 }
531 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 PyErr_Format(PyExc_TypeError,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000533 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000534 Py_TYPE(pystr)->tp_name);
535 return NULL;
536 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000537 return _build_rval_index_tuple(rval, next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000538}
539
540PyDoc_STRVAR(pydoc_encode_basestring_ascii,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000541 "encode_basestring_ascii(string) -> string\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000542 "\n"
543 "Return an ASCII-only JSON representation of a Python string"
544);
Christian Heimes90540002008-05-08 14:29:10 +0000545
546static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000547py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
Christian Heimes90540002008-05-08 14:29:10 +0000548{
549 PyObject *rval;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000550 /* Return an ASCII-only JSON representation of a Python string */
Christian Heimes90540002008-05-08 14:29:10 +0000551 /* METH_O */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000552 if (PyUnicode_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000553 rval = ascii_escape_unicode(pystr);
554 }
555 else {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000556 PyErr_Format(PyExc_TypeError,
557 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000558 Py_TYPE(pystr)->tp_name);
559 return NULL;
560 }
Christian Heimes90540002008-05-08 14:29:10 +0000561 return rval;
562}
563
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000564static void
565scanner_dealloc(PyObject *self)
566{
567 /* Deallocate scanner object */
568 scanner_clear(self);
569 Py_TYPE(self)->tp_free(self);
570}
571
572static int
573scanner_traverse(PyObject *self, visitproc visit, void *arg)
574{
575 PyScannerObject *s;
576 assert(PyScanner_Check(self));
577 s = (PyScannerObject *)self;
578 Py_VISIT(s->strict);
579 Py_VISIT(s->object_hook);
580 Py_VISIT(s->object_pairs_hook);
581 Py_VISIT(s->parse_float);
582 Py_VISIT(s->parse_int);
583 Py_VISIT(s->parse_constant);
584 return 0;
585}
586
587static int
588scanner_clear(PyObject *self)
589{
590 PyScannerObject *s;
591 assert(PyScanner_Check(self));
592 s = (PyScannerObject *)self;
593 Py_CLEAR(s->strict);
594 Py_CLEAR(s->object_hook);
595 Py_CLEAR(s->object_pairs_hook);
596 Py_CLEAR(s->parse_float);
597 Py_CLEAR(s->parse_int);
598 Py_CLEAR(s->parse_constant);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000599 Py_CLEAR(s->memo);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000600 return 0;
601}
602
603static PyObject *
604_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
605 /* Read a JSON object from PyUnicode pystr.
606 idx is the index of the first character after the opening curly brace.
607 *next_idx_ptr is a return-by-reference index to the first character after
608 the closing curly brace.
609
610 Returns a new PyObject (usually a dict, but object_hook can change that)
611 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200612 void *str;
613 int kind;
614 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000615 PyObject *val = NULL;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000616 PyObject *rval = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000617 PyObject *key = NULL;
618 int strict = PyObject_IsTrue(s->strict);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000619 int has_pairs_hook = (s->object_pairs_hook != Py_None);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000620 Py_ssize_t next_idx;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000621
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200622 if (PyUnicode_READY(pystr) == -1)
623 return NULL;
624
625 str = PyUnicode_DATA(pystr);
626 kind = PyUnicode_KIND(pystr);
627 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
628
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000629 if (has_pairs_hook)
630 rval = PyList_New(0);
631 else
632 rval = PyDict_New();
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000633 if (rval == NULL)
634 return NULL;
635
636 /* skip whitespace after { */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200637 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000638
639 /* only loop if the object is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200640 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000641 while (idx <= end_idx) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000642 PyObject *memokey;
643
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000644 /* read key */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200645 if (PyUnicode_READ(kind, str, idx) != '"') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000646 raise_errmsg("Expecting property name", pystr, idx);
647 goto bail;
648 }
649 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
650 if (key == NULL)
651 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000652 memokey = PyDict_GetItem(s->memo, key);
653 if (memokey != NULL) {
654 Py_INCREF(memokey);
655 Py_DECREF(key);
656 key = memokey;
657 }
658 else {
659 if (PyDict_SetItem(s->memo, key, key) < 0)
660 goto bail;
661 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000662 idx = next_idx;
663
664 /* skip whitespace between key and : delimiter, read :, skip whitespace */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200665 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
666 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000667 raise_errmsg("Expecting : delimiter", pystr, idx);
668 goto bail;
669 }
670 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200671 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000672
673 /* read any JSON term */
674 val = scan_once_unicode(s, pystr, idx, &next_idx);
675 if (val == NULL)
676 goto bail;
677
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000678 if (has_pairs_hook) {
679 PyObject *item = PyTuple_Pack(2, key, val);
680 if (item == NULL)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000681 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000682 Py_CLEAR(key);
683 Py_CLEAR(val);
684 if (PyList_Append(rval, item) == -1) {
685 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000686 goto bail;
687 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000688 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000689 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000690 else {
691 if (PyDict_SetItem(rval, key, val) < 0)
692 goto bail;
693 Py_CLEAR(key);
694 Py_CLEAR(val);
695 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000696 idx = next_idx;
697
698 /* skip whitespace before } or , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200699 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000700
701 /* bail if the object is closed or we didn't get the , delimiter */
702 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200703 if (PyUnicode_READ(kind, str, idx) == '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000704 break;
705 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200706 else if (PyUnicode_READ(kind, str, idx) != ',') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000707 raise_errmsg("Expecting , delimiter", pystr, idx);
708 goto bail;
709 }
710 idx++;
711
712 /* skip whitespace after , delimiter */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200713 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000714 }
715 }
716
717 /* verify that idx < end_idx, str[idx] should be '}' */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200718 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000719 raise_errmsg("Expecting object", pystr, end_idx);
720 goto bail;
721 }
722
723 *next_idx_ptr = idx + 1;
724
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000725 if (has_pairs_hook) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000726 val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000727 Py_DECREF(rval);
728 return val;
729 }
730
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000731 /* if object_hook is not None: rval = object_hook(rval) */
732 if (s->object_hook != Py_None) {
733 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000734 Py_DECREF(rval);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000735 return val;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000736 }
737 return rval;
738bail:
739 Py_XDECREF(key);
740 Py_XDECREF(val);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000741 Py_XDECREF(rval);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000742 return NULL;
743}
744
745static PyObject *
746_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
747 /* Read a JSON array from PyString pystr.
748 idx is the index of the first character after the opening brace.
749 *next_idx_ptr is a return-by-reference index to the first character after
750 the closing brace.
751
752 Returns a new PyList
753 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200754 void *str;
755 int kind;
756 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000757 PyObject *val = NULL;
758 PyObject *rval = PyList_New(0);
759 Py_ssize_t next_idx;
760 if (rval == NULL)
761 return NULL;
762
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200763 if (PyUnicode_READY(pystr) == -1)
764 return NULL;
765
766 str = PyUnicode_DATA(pystr);
767 kind = PyUnicode_KIND(pystr);
768 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
769
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000770 /* skip whitespace after [ */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200771 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000772
773 /* only loop if the array is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200774 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000775 while (idx <= end_idx) {
776
777 /* read any JSON term */
778 val = scan_once_unicode(s, pystr, idx, &next_idx);
779 if (val == NULL)
780 goto bail;
781
782 if (PyList_Append(rval, val) == -1)
783 goto bail;
784
785 Py_CLEAR(val);
786 idx = next_idx;
787
788 /* skip whitespace between term and , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200789 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000790
791 /* bail if the array is closed or we didn't get the , delimiter */
792 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200793 if (PyUnicode_READ(kind, str, idx) == ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000794 break;
795 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200796 else if (PyUnicode_READ(kind, str, idx) != ',') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000797 raise_errmsg("Expecting , delimiter", pystr, idx);
798 goto bail;
799 }
800 idx++;
801
802 /* skip whitespace after , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200803 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000804 }
805 }
806
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200807 /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */
808 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000809 raise_errmsg("Expecting object", pystr, end_idx);
810 goto bail;
811 }
812 *next_idx_ptr = idx + 1;
813 return rval;
814bail:
815 Py_XDECREF(val);
816 Py_DECREF(rval);
817 return NULL;
818}
819
820static PyObject *
821_parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
822 /* Read a JSON constant from PyString pystr.
823 constant is the constant string that was found
824 ("NaN", "Infinity", "-Infinity").
825 idx is the index of the first character of the constant
826 *next_idx_ptr is a return-by-reference index to the first character after
827 the constant.
828
829 Returns the result of parse_constant
830 */
831 PyObject *cstr;
832 PyObject *rval;
833 /* constant is "NaN", "Infinity", or "-Infinity" */
834 cstr = PyUnicode_InternFromString(constant);
835 if (cstr == NULL)
836 return NULL;
837
838 /* rval = parse_constant(constant) */
839 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
840 idx += PyUnicode_GET_SIZE(cstr);
841 Py_DECREF(cstr);
842 *next_idx_ptr = idx;
843 return rval;
844}
845
846static PyObject *
847_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
848 /* Read a JSON number from PyUnicode pystr.
849 idx is the index of the first character of the number
850 *next_idx_ptr is a return-by-reference index to the first character after
851 the number.
852
853 Returns a new PyObject representation of that number:
854 PyInt, PyLong, or PyFloat.
855 May return other types if parse_int or parse_float are set
856 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200857 void *str;
858 int kind;
859 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000860 Py_ssize_t idx = start;
861 int is_float = 0;
862 PyObject *rval;
Antoine Pitrouf6454512011-04-25 19:16:06 +0200863 PyObject *numstr = NULL;
864 PyObject *custom_func;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000865
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200866 if (PyUnicode_READY(pystr) == -1)
867 return NULL;
868
869 str = PyUnicode_DATA(pystr);
870 kind = PyUnicode_KIND(pystr);
871 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
872
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000873 /* read a sign if it's there, make sure it's not the end of the string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200874 if (PyUnicode_READ(kind, str, idx) == '-') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000875 idx++;
876 if (idx > end_idx) {
877 PyErr_SetNone(PyExc_StopIteration);
878 return NULL;
879 }
880 }
881
882 /* read as many integer digits as we find as long as it doesn't start with 0 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200883 if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000884 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200885 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000886 }
887 /* if it starts with 0 we only expect one integer digit */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200888 else if (PyUnicode_READ(kind, str, idx) == '0') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000889 idx++;
890 }
891 /* no integer digits, error */
892 else {
893 PyErr_SetNone(PyExc_StopIteration);
894 return NULL;
895 }
896
897 /* if the next char is '.' followed by a digit then read all float digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200898 if (idx < end_idx && PyUnicode_READ(kind, str, idx) == '.' && PyUnicode_READ(kind, str, idx + 1) >= '0' && PyUnicode_READ(kind, str, idx + 1) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000899 is_float = 1;
900 idx += 2;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200901 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000902 }
903
904 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200905 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000906 Py_ssize_t e_start = idx;
907 idx++;
908
909 /* read an exponent sign if present */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200910 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000911
912 /* read all digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200913 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000914
915 /* if we got a digit, then parse as float. if not, backtrack */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200916 if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000917 is_float = 1;
918 }
919 else {
920 idx = e_start;
921 }
922 }
923
Antoine Pitrouf6454512011-04-25 19:16:06 +0200924 if (is_float && s->parse_float != (PyObject *)&PyFloat_Type)
925 custom_func = s->parse_float;
926 else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type)
927 custom_func = s->parse_int;
928 else
929 custom_func = NULL;
930
931 if (custom_func) {
932 /* copy the section we determined to be a number */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200933 numstr = PyUnicode_FromKindAndData(kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200934 (char*)str + kind * start,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200935 idx - start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200936 if (numstr == NULL)
937 return NULL;
938 rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000939 }
940 else {
Antoine Pitrouf6454512011-04-25 19:16:06 +0200941 Py_ssize_t i, n;
942 char *buf;
943 /* Straight conversion to ASCII, to avoid costly conversion of
944 decimal unicode digits (which cannot appear here) */
945 n = idx - start;
946 numstr = PyBytes_FromStringAndSize(NULL, n);
947 if (numstr == NULL)
948 return NULL;
949 buf = PyBytes_AS_STRING(numstr);
950 for (i = 0; i < n; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200951 buf[i] = (char) PyUnicode_READ(kind, str, i + start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200952 }
953 if (is_float)
954 rval = PyFloat_FromString(numstr);
955 else
956 rval = PyLong_FromString(buf, NULL, 10);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000957 }
958 Py_DECREF(numstr);
959 *next_idx_ptr = idx;
960 return rval;
961}
962
963static PyObject *
964scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
965{
966 /* Read one JSON term (of any kind) from PyUnicode pystr.
967 idx is the index of the first character of the term
968 *next_idx_ptr is a return-by-reference index to the first character after
969 the number.
970
971 Returns a new PyObject representation of the term.
972 */
Ezio Melotti362b9512011-05-07 17:58:09 +0300973 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200974 void *str;
975 int kind;
976 Py_ssize_t length;
977
978 if (PyUnicode_READY(pystr) == -1)
979 return NULL;
980
981 str = PyUnicode_DATA(pystr);
982 kind = PyUnicode_KIND(pystr);
983 length = PyUnicode_GET_LENGTH(pystr);
984
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000985 if (idx >= length) {
986 PyErr_SetNone(PyExc_StopIteration);
987 return NULL;
988 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200989
990 switch (PyUnicode_READ(kind, str, idx)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000991 case '"':
992 /* string */
993 return scanstring_unicode(pystr, idx + 1,
994 PyObject_IsTrue(s->strict),
995 next_idx_ptr);
996 case '{':
997 /* object */
Ezio Melotti362b9512011-05-07 17:58:09 +0300998 if (Py_EnterRecursiveCall(" while decoding a JSON object "
999 "from a unicode string"))
1000 return NULL;
1001 res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
1002 Py_LeaveRecursiveCall();
1003 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001004 case '[':
1005 /* array */
Ezio Melotti362b9512011-05-07 17:58:09 +03001006 if (Py_EnterRecursiveCall(" while decoding a JSON array "
1007 "from a unicode string"))
1008 return NULL;
1009 res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
1010 Py_LeaveRecursiveCall();
1011 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001012 case 'n':
1013 /* null */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001014 if ((idx + 3 < length) && PyUnicode_READ(kind, str, idx + 1) == 'u' && PyUnicode_READ(kind, str, idx + 2) == 'l' && PyUnicode_READ(kind, str, idx + 3) == 'l') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001015 Py_INCREF(Py_None);
1016 *next_idx_ptr = idx + 4;
1017 return Py_None;
1018 }
1019 break;
1020 case 't':
1021 /* true */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001022 if ((idx + 3 < length) && PyUnicode_READ(kind, str, idx + 1) == 'r' && PyUnicode_READ(kind, str, idx + 2) == 'u' && PyUnicode_READ(kind, str, idx + 3) == 'e') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001023 Py_INCREF(Py_True);
1024 *next_idx_ptr = idx + 4;
1025 return Py_True;
1026 }
1027 break;
1028 case 'f':
1029 /* false */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001030 if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
1031 PyUnicode_READ(kind, str, idx + 2) == 'l' &&
1032 PyUnicode_READ(kind, str, idx + 3) == 's' &&
1033 PyUnicode_READ(kind, str, idx + 4) == 'e') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001034 Py_INCREF(Py_False);
1035 *next_idx_ptr = idx + 5;
1036 return Py_False;
1037 }
1038 break;
1039 case 'N':
1040 /* NaN */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001041 if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
1042 PyUnicode_READ(kind, str, idx + 2) == 'N') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001043 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1044 }
1045 break;
1046 case 'I':
1047 /* Infinity */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001048 if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
1049 PyUnicode_READ(kind, str, idx + 2) == 'f' &&
1050 PyUnicode_READ(kind, str, idx + 3) == 'i' &&
1051 PyUnicode_READ(kind, str, idx + 4) == 'n' &&
1052 PyUnicode_READ(kind, str, idx + 5) == 'i' &&
1053 PyUnicode_READ(kind, str, idx + 6) == 't' &&
1054 PyUnicode_READ(kind, str, idx + 7) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001055 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1056 }
1057 break;
1058 case '-':
1059 /* -Infinity */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001060 if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
1061 PyUnicode_READ(kind, str, idx + 2) == 'n' &&
1062 PyUnicode_READ(kind, str, idx + 3) == 'f' &&
1063 PyUnicode_READ(kind, str, idx + 4) == 'i' &&
1064 PyUnicode_READ(kind, str, idx + 5) == 'n' &&
1065 PyUnicode_READ(kind, str, idx + 6) == 'i' &&
1066 PyUnicode_READ(kind, str, idx + 7) == 't' &&
1067 PyUnicode_READ(kind, str, idx + 8) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001068 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1069 }
1070 break;
1071 }
1072 /* Didn't find a string, object, array, or named constant. Look for a number. */
1073 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
1074}
1075
1076static PyObject *
1077scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
1078{
1079 /* Python callable interface to scan_once_{str,unicode} */
1080 PyObject *pystr;
1081 PyObject *rval;
1082 Py_ssize_t idx;
1083 Py_ssize_t next_idx = -1;
1084 static char *kwlist[] = {"string", "idx", NULL};
1085 PyScannerObject *s;
1086 assert(PyScanner_Check(self));
1087 s = (PyScannerObject *)self;
1088 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:scan_once", kwlist, &pystr, _convertPyInt_AsSsize_t, &idx))
1089 return NULL;
1090
1091 if (PyUnicode_Check(pystr)) {
1092 rval = scan_once_unicode(s, pystr, idx, &next_idx);
1093 }
1094 else {
1095 PyErr_Format(PyExc_TypeError,
1096 "first argument must be a string, not %.80s",
1097 Py_TYPE(pystr)->tp_name);
1098 return NULL;
1099 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001100 PyDict_Clear(s->memo);
1101 if (rval == NULL)
1102 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001103 return _build_rval_index_tuple(rval, next_idx);
1104}
1105
1106static PyObject *
1107scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1108{
1109 PyScannerObject *s;
1110 s = (PyScannerObject *)type->tp_alloc(type, 0);
1111 if (s != NULL) {
1112 s->strict = NULL;
1113 s->object_hook = NULL;
1114 s->object_pairs_hook = NULL;
1115 s->parse_float = NULL;
1116 s->parse_int = NULL;
1117 s->parse_constant = NULL;
1118 }
1119 return (PyObject *)s;
1120}
1121
1122static int
1123scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1124{
1125 /* Initialize Scanner object */
1126 PyObject *ctx;
1127 static char *kwlist[] = {"context", NULL};
1128 PyScannerObject *s;
1129
1130 assert(PyScanner_Check(self));
1131 s = (PyScannerObject *)self;
1132
1133 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1134 return -1;
1135
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001136 if (s->memo == NULL) {
1137 s->memo = PyDict_New();
1138 if (s->memo == NULL)
1139 goto bail;
1140 }
1141
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001142 /* All of these will fail "gracefully" so we don't need to verify them */
1143 s->strict = PyObject_GetAttrString(ctx, "strict");
1144 if (s->strict == NULL)
1145 goto bail;
1146 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1147 if (s->object_hook == NULL)
1148 goto bail;
1149 s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1150 if (s->object_pairs_hook == NULL)
1151 goto bail;
1152 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1153 if (s->parse_float == NULL)
1154 goto bail;
1155 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1156 if (s->parse_int == NULL)
1157 goto bail;
1158 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1159 if (s->parse_constant == NULL)
1160 goto bail;
1161
1162 return 0;
1163
1164bail:
1165 Py_CLEAR(s->strict);
1166 Py_CLEAR(s->object_hook);
1167 Py_CLEAR(s->object_pairs_hook);
1168 Py_CLEAR(s->parse_float);
1169 Py_CLEAR(s->parse_int);
1170 Py_CLEAR(s->parse_constant);
1171 return -1;
1172}
1173
1174PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1175
1176static
1177PyTypeObject PyScannerType = {
1178 PyVarObject_HEAD_INIT(NULL, 0)
1179 "_json.Scanner", /* tp_name */
1180 sizeof(PyScannerObject), /* tp_basicsize */
1181 0, /* tp_itemsize */
1182 scanner_dealloc, /* tp_dealloc */
1183 0, /* tp_print */
1184 0, /* tp_getattr */
1185 0, /* tp_setattr */
1186 0, /* tp_compare */
1187 0, /* tp_repr */
1188 0, /* tp_as_number */
1189 0, /* tp_as_sequence */
1190 0, /* tp_as_mapping */
1191 0, /* tp_hash */
1192 scanner_call, /* tp_call */
1193 0, /* tp_str */
1194 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1195 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1196 0, /* tp_as_buffer */
1197 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1198 scanner_doc, /* tp_doc */
1199 scanner_traverse, /* tp_traverse */
1200 scanner_clear, /* tp_clear */
1201 0, /* tp_richcompare */
1202 0, /* tp_weaklistoffset */
1203 0, /* tp_iter */
1204 0, /* tp_iternext */
1205 0, /* tp_methods */
1206 scanner_members, /* tp_members */
1207 0, /* tp_getset */
1208 0, /* tp_base */
1209 0, /* tp_dict */
1210 0, /* tp_descr_get */
1211 0, /* tp_descr_set */
1212 0, /* tp_dictoffset */
1213 scanner_init, /* tp_init */
1214 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1215 scanner_new, /* tp_new */
1216 0,/* PyObject_GC_Del, */ /* tp_free */
1217};
1218
1219static PyObject *
1220encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1221{
1222 PyEncoderObject *s;
1223 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1224 if (s != NULL) {
1225 s->markers = NULL;
1226 s->defaultfn = NULL;
1227 s->encoder = NULL;
1228 s->indent = NULL;
1229 s->key_separator = NULL;
1230 s->item_separator = NULL;
1231 s->sort_keys = NULL;
1232 s->skipkeys = NULL;
1233 }
1234 return (PyObject *)s;
1235}
1236
1237static int
1238encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1239{
1240 /* initialize Encoder object */
1241 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1242
1243 PyEncoderObject *s;
Antoine Pitrou781eba72009-12-08 15:57:31 +00001244 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1245 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001246
1247 assert(PyEncoder_Check(self));
1248 s = (PyEncoderObject *)self;
1249
1250 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
Antoine Pitrou781eba72009-12-08 15:57:31 +00001251 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1252 &sort_keys, &skipkeys, &allow_nan))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001253 return -1;
1254
Antoine Pitrou781eba72009-12-08 15:57:31 +00001255 s->markers = markers;
1256 s->defaultfn = defaultfn;
1257 s->encoder = encoder;
1258 s->indent = indent;
1259 s->key_separator = key_separator;
1260 s->item_separator = item_separator;
1261 s->sort_keys = sort_keys;
1262 s->skipkeys = skipkeys;
1263 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1264 s->allow_nan = PyObject_IsTrue(allow_nan);
1265
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001266 Py_INCREF(s->markers);
1267 Py_INCREF(s->defaultfn);
1268 Py_INCREF(s->encoder);
1269 Py_INCREF(s->indent);
1270 Py_INCREF(s->key_separator);
1271 Py_INCREF(s->item_separator);
1272 Py_INCREF(s->sort_keys);
1273 Py_INCREF(s->skipkeys);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001274 return 0;
1275}
1276
1277static PyObject *
1278encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1279{
1280 /* Python callable interface to encode_listencode_obj */
1281 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1282 PyObject *obj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001283 Py_ssize_t indent_level;
1284 PyEncoderObject *s;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001285 _PyAccu acc;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001286
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001287 assert(PyEncoder_Check(self));
1288 s = (PyEncoderObject *)self;
1289 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:_iterencode", kwlist,
1290 &obj, _convertPyInt_AsSsize_t, &indent_level))
1291 return NULL;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001292 if (_PyAccu_Init(&acc))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001293 return NULL;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001294 if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001295 _PyAccu_Destroy(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001296 return NULL;
1297 }
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001298 return _PyAccu_FinishAsList(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001299}
1300
1301static PyObject *
1302_encoded_const(PyObject *obj)
1303{
1304 /* Return the JSON string representation of None, True, False */
1305 if (obj == Py_None) {
1306 static PyObject *s_null = NULL;
1307 if (s_null == NULL) {
1308 s_null = PyUnicode_InternFromString("null");
1309 }
1310 Py_INCREF(s_null);
1311 return s_null;
1312 }
1313 else if (obj == Py_True) {
1314 static PyObject *s_true = NULL;
1315 if (s_true == NULL) {
1316 s_true = PyUnicode_InternFromString("true");
1317 }
1318 Py_INCREF(s_true);
1319 return s_true;
1320 }
1321 else if (obj == Py_False) {
1322 static PyObject *s_false = NULL;
1323 if (s_false == NULL) {
1324 s_false = PyUnicode_InternFromString("false");
1325 }
1326 Py_INCREF(s_false);
1327 return s_false;
1328 }
1329 else {
1330 PyErr_SetString(PyExc_ValueError, "not a const");
1331 return NULL;
1332 }
1333}
1334
1335static PyObject *
1336encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1337{
1338 /* Return the JSON representation of a PyFloat */
1339 double i = PyFloat_AS_DOUBLE(obj);
1340 if (!Py_IS_FINITE(i)) {
1341 if (!s->allow_nan) {
1342 PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant");
1343 return NULL;
1344 }
1345 if (i > 0) {
1346 return PyUnicode_FromString("Infinity");
1347 }
1348 else if (i < 0) {
1349 return PyUnicode_FromString("-Infinity");
1350 }
1351 else {
1352 return PyUnicode_FromString("NaN");
1353 }
1354 }
1355 /* Use a better float format here? */
1356 return PyObject_Repr(obj);
1357}
1358
1359static PyObject *
1360encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1361{
1362 /* Return the JSON representation of a string */
1363 if (s->fast_encode)
1364 return py_encode_basestring_ascii(NULL, obj);
1365 else
1366 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1367}
1368
1369static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001370_steal_accumulate(_PyAccu *acc, PyObject *stolen)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001371{
1372 /* Append stolen and then decrement its reference count */
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001373 int rval = _PyAccu_Accumulate(acc, stolen);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001374 Py_DECREF(stolen);
1375 return rval;
1376}
1377
1378static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001379encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001380 PyObject *obj, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001381{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001382 /* Encode Python object obj to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001383 PyObject *newobj;
1384 int rv;
1385
1386 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1387 PyObject *cstr = _encoded_const(obj);
1388 if (cstr == NULL)
1389 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001390 return _steal_accumulate(acc, cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001391 }
1392 else if (PyUnicode_Check(obj))
1393 {
1394 PyObject *encoded = encoder_encode_string(s, obj);
1395 if (encoded == NULL)
1396 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001397 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001398 }
1399 else if (PyLong_Check(obj)) {
1400 PyObject *encoded = PyObject_Str(obj);
1401 if (encoded == NULL)
1402 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001403 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001404 }
1405 else if (PyFloat_Check(obj)) {
1406 PyObject *encoded = encoder_encode_float(s, obj);
1407 if (encoded == NULL)
1408 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001409 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001410 }
1411 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001412 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1413 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001414 rv = encoder_listencode_list(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001415 Py_LeaveRecursiveCall();
1416 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001417 }
1418 else if (PyDict_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001419 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1420 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001421 rv = encoder_listencode_dict(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001422 Py_LeaveRecursiveCall();
1423 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001424 }
1425 else {
1426 PyObject *ident = NULL;
1427 if (s->markers != Py_None) {
1428 int has_key;
1429 ident = PyLong_FromVoidPtr(obj);
1430 if (ident == NULL)
1431 return -1;
1432 has_key = PyDict_Contains(s->markers, ident);
1433 if (has_key) {
1434 if (has_key != -1)
1435 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1436 Py_DECREF(ident);
1437 return -1;
1438 }
1439 if (PyDict_SetItem(s->markers, ident, obj)) {
1440 Py_DECREF(ident);
1441 return -1;
1442 }
1443 }
1444 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1445 if (newobj == NULL) {
1446 Py_XDECREF(ident);
1447 return -1;
1448 }
Ezio Melotti13672652011-05-11 01:02:56 +03001449
1450 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1451 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001452 rv = encoder_listencode_obj(s, acc, newobj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001453 Py_LeaveRecursiveCall();
1454
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001455 Py_DECREF(newobj);
1456 if (rv) {
1457 Py_XDECREF(ident);
1458 return -1;
1459 }
1460 if (ident != NULL) {
1461 if (PyDict_DelItem(s->markers, ident)) {
1462 Py_XDECREF(ident);
1463 return -1;
1464 }
1465 Py_XDECREF(ident);
1466 }
1467 return rv;
1468 }
1469}
1470
1471static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001472encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001473 PyObject *dct, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001474{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001475 /* Encode Python dict dct a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001476 static PyObject *open_dict = NULL;
1477 static PyObject *close_dict = NULL;
1478 static PyObject *empty_dict = NULL;
1479 PyObject *kstr = NULL;
1480 PyObject *ident = NULL;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001481 PyObject *it = NULL;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001482 PyObject *items;
1483 PyObject *item = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001484 int skipkeys;
1485 Py_ssize_t idx;
1486
1487 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
1488 open_dict = PyUnicode_InternFromString("{");
1489 close_dict = PyUnicode_InternFromString("}");
1490 empty_dict = PyUnicode_InternFromString("{}");
1491 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
1492 return -1;
1493 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001494 if (Py_SIZE(dct) == 0)
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001495 return _PyAccu_Accumulate(acc, empty_dict);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001496
1497 if (s->markers != Py_None) {
1498 int has_key;
1499 ident = PyLong_FromVoidPtr(dct);
1500 if (ident == NULL)
1501 goto bail;
1502 has_key = PyDict_Contains(s->markers, ident);
1503 if (has_key) {
1504 if (has_key != -1)
1505 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1506 goto bail;
1507 }
1508 if (PyDict_SetItem(s->markers, ident, dct)) {
1509 goto bail;
1510 }
1511 }
1512
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001513 if (_PyAccu_Accumulate(acc, open_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001514 goto bail;
1515
1516 if (s->indent != Py_None) {
1517 /* TODO: DOES NOT RUN */
1518 indent_level += 1;
1519 /*
1520 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1521 separator = _item_separator + newline_indent
1522 buf += newline_indent
1523 */
1524 }
1525
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001526 if (PyObject_IsTrue(s->sort_keys)) {
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001527 /* First sort the keys then replace them with (key, value) tuples. */
1528 Py_ssize_t i, nitems;
1529 items = PyMapping_Keys(dct);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 if (items == NULL)
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001531 goto bail;
1532 if (!PyList_Check(items)) {
1533 PyErr_SetString(PyExc_ValueError, "keys must return list");
1534 goto bail;
1535 }
1536 if (PyList_Sort(items) < 0)
1537 goto bail;
1538 nitems = PyList_GET_SIZE(items);
1539 for (i = 0; i < nitems; i++) {
1540 PyObject *key, *value;
1541 key = PyList_GET_ITEM(items, i);
1542 value = PyDict_GetItem(dct, key);
1543 item = PyTuple_Pack(2, key, value);
1544 if (item == NULL)
1545 goto bail;
1546 PyList_SET_ITEM(items, i, item);
1547 Py_DECREF(key);
1548 }
1549 }
1550 else {
1551 items = PyMapping_Items(dct);
1552 }
1553 if (items == NULL)
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001554 goto bail;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001555 it = PyObject_GetIter(items);
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001556 Py_DECREF(items);
1557 if (it == NULL)
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001558 goto bail;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001559 skipkeys = PyObject_IsTrue(s->skipkeys);
1560 idx = 0;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001561 while ((item = PyIter_Next(it)) != NULL) {
1562 PyObject *encoded, *key, *value;
1563 if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
1564 PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
1565 goto bail;
1566 }
1567 key = PyTuple_GET_ITEM(item, 0);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001568 if (PyUnicode_Check(key)) {
1569 Py_INCREF(key);
1570 kstr = key;
1571 }
1572 else if (PyFloat_Check(key)) {
1573 kstr = encoder_encode_float(s, key);
1574 if (kstr == NULL)
1575 goto bail;
1576 }
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001577 else if (key == Py_True || key == Py_False || key == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 /* This must come before the PyLong_Check because
1579 True and False are also 1 and 0.*/
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001580 kstr = _encoded_const(key);
1581 if (kstr == NULL)
1582 goto bail;
1583 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001584 else if (PyLong_Check(key)) {
1585 kstr = PyObject_Str(key);
1586 if (kstr == NULL)
1587 goto bail;
1588 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001589 else if (skipkeys) {
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001590 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001591 continue;
1592 }
1593 else {
1594 /* TODO: include repr of key */
Doug Hellmann1c524752010-07-21 12:29:04 +00001595 PyErr_SetString(PyExc_TypeError, "keys must be a string");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001596 goto bail;
1597 }
1598
1599 if (idx) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001600 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001601 goto bail;
1602 }
1603
1604 encoded = encoder_encode_string(s, kstr);
1605 Py_CLEAR(kstr);
1606 if (encoded == NULL)
1607 goto bail;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001608 if (_PyAccu_Accumulate(acc, encoded)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001609 Py_DECREF(encoded);
1610 goto bail;
1611 }
1612 Py_DECREF(encoded);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001613 if (_PyAccu_Accumulate(acc, s->key_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001614 goto bail;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001615
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001616 value = PyTuple_GET_ITEM(item, 1);
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001617 if (encoder_listencode_obj(s, acc, value, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001618 goto bail;
1619 idx += 1;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001620 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001621 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001622 if (PyErr_Occurred())
1623 goto bail;
1624 Py_CLEAR(it);
1625
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001626 if (ident != NULL) {
1627 if (PyDict_DelItem(s->markers, ident))
1628 goto bail;
1629 Py_CLEAR(ident);
1630 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001631 /* TODO DOES NOT RUN; dead code
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001632 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001633 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001634
1635 yield '\n' + (' ' * (_indent * _current_indent_level))
1636 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001637 if (_PyAccu_Accumulate(acc, close_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001638 goto bail;
1639 return 0;
1640
1641bail:
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001642 Py_XDECREF(it);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001643 Py_XDECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001644 Py_XDECREF(kstr);
1645 Py_XDECREF(ident);
1646 return -1;
1647}
1648
1649
1650static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001651encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001652 PyObject *seq, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001653{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001654 /* Encode Python list seq to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001655 static PyObject *open_array = NULL;
1656 static PyObject *close_array = NULL;
1657 static PyObject *empty_array = NULL;
1658 PyObject *ident = NULL;
1659 PyObject *s_fast = NULL;
1660 Py_ssize_t num_items;
1661 PyObject **seq_items;
1662 Py_ssize_t i;
1663
1664 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
1665 open_array = PyUnicode_InternFromString("[");
1666 close_array = PyUnicode_InternFromString("]");
1667 empty_array = PyUnicode_InternFromString("[]");
1668 if (open_array == NULL || close_array == NULL || empty_array == NULL)
1669 return -1;
1670 }
1671 ident = NULL;
1672 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
1673 if (s_fast == NULL)
1674 return -1;
1675 num_items = PySequence_Fast_GET_SIZE(s_fast);
1676 if (num_items == 0) {
1677 Py_DECREF(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001678 return _PyAccu_Accumulate(acc, empty_array);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001679 }
1680
1681 if (s->markers != Py_None) {
1682 int has_key;
1683 ident = PyLong_FromVoidPtr(seq);
1684 if (ident == NULL)
1685 goto bail;
1686 has_key = PyDict_Contains(s->markers, ident);
1687 if (has_key) {
1688 if (has_key != -1)
1689 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1690 goto bail;
1691 }
1692 if (PyDict_SetItem(s->markers, ident, seq)) {
1693 goto bail;
1694 }
1695 }
1696
1697 seq_items = PySequence_Fast_ITEMS(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001698 if (_PyAccu_Accumulate(acc, open_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001699 goto bail;
1700 if (s->indent != Py_None) {
1701 /* TODO: DOES NOT RUN */
1702 indent_level += 1;
1703 /*
1704 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1705 separator = _item_separator + newline_indent
1706 buf += newline_indent
1707 */
1708 }
1709 for (i = 0; i < num_items; i++) {
1710 PyObject *obj = seq_items[i];
1711 if (i) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001712 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001713 goto bail;
1714 }
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001715 if (encoder_listencode_obj(s, acc, obj, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001716 goto bail;
1717 }
1718 if (ident != NULL) {
1719 if (PyDict_DelItem(s->markers, ident))
1720 goto bail;
1721 Py_CLEAR(ident);
1722 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001723
1724 /* TODO: DOES NOT RUN
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001725 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001726 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001727
1728 yield '\n' + (' ' * (_indent * _current_indent_level))
1729 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001730 if (_PyAccu_Accumulate(acc, close_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001731 goto bail;
1732 Py_DECREF(s_fast);
1733 return 0;
1734
1735bail:
1736 Py_XDECREF(ident);
1737 Py_DECREF(s_fast);
1738 return -1;
1739}
1740
1741static void
1742encoder_dealloc(PyObject *self)
1743{
1744 /* Deallocate Encoder */
1745 encoder_clear(self);
1746 Py_TYPE(self)->tp_free(self);
1747}
1748
1749static int
1750encoder_traverse(PyObject *self, visitproc visit, void *arg)
1751{
1752 PyEncoderObject *s;
1753 assert(PyEncoder_Check(self));
1754 s = (PyEncoderObject *)self;
1755 Py_VISIT(s->markers);
1756 Py_VISIT(s->defaultfn);
1757 Py_VISIT(s->encoder);
1758 Py_VISIT(s->indent);
1759 Py_VISIT(s->key_separator);
1760 Py_VISIT(s->item_separator);
1761 Py_VISIT(s->sort_keys);
1762 Py_VISIT(s->skipkeys);
1763 return 0;
1764}
1765
1766static int
1767encoder_clear(PyObject *self)
1768{
1769 /* Deallocate Encoder */
1770 PyEncoderObject *s;
1771 assert(PyEncoder_Check(self));
1772 s = (PyEncoderObject *)self;
1773 Py_CLEAR(s->markers);
1774 Py_CLEAR(s->defaultfn);
1775 Py_CLEAR(s->encoder);
1776 Py_CLEAR(s->indent);
1777 Py_CLEAR(s->key_separator);
1778 Py_CLEAR(s->item_separator);
1779 Py_CLEAR(s->sort_keys);
1780 Py_CLEAR(s->skipkeys);
1781 return 0;
1782}
1783
1784PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
1785
1786static
1787PyTypeObject PyEncoderType = {
1788 PyVarObject_HEAD_INIT(NULL, 0)
1789 "_json.Encoder", /* tp_name */
1790 sizeof(PyEncoderObject), /* tp_basicsize */
1791 0, /* tp_itemsize */
1792 encoder_dealloc, /* tp_dealloc */
1793 0, /* tp_print */
1794 0, /* tp_getattr */
1795 0, /* tp_setattr */
1796 0, /* tp_compare */
1797 0, /* tp_repr */
1798 0, /* tp_as_number */
1799 0, /* tp_as_sequence */
1800 0, /* tp_as_mapping */
1801 0, /* tp_hash */
1802 encoder_call, /* tp_call */
1803 0, /* tp_str */
1804 0, /* tp_getattro */
1805 0, /* tp_setattro */
1806 0, /* tp_as_buffer */
1807 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1808 encoder_doc, /* tp_doc */
1809 encoder_traverse, /* tp_traverse */
1810 encoder_clear, /* tp_clear */
1811 0, /* tp_richcompare */
1812 0, /* tp_weaklistoffset */
1813 0, /* tp_iter */
1814 0, /* tp_iternext */
1815 0, /* tp_methods */
1816 encoder_members, /* tp_members */
1817 0, /* tp_getset */
1818 0, /* tp_base */
1819 0, /* tp_dict */
1820 0, /* tp_descr_get */
1821 0, /* tp_descr_set */
1822 0, /* tp_dictoffset */
1823 encoder_init, /* tp_init */
1824 0, /* tp_alloc */
1825 encoder_new, /* tp_new */
1826 0, /* tp_free */
1827};
1828
1829static PyMethodDef speedups_methods[] = {
1830 {"encode_basestring_ascii",
1831 (PyCFunction)py_encode_basestring_ascii,
1832 METH_O,
1833 pydoc_encode_basestring_ascii},
1834 {"scanstring",
1835 (PyCFunction)py_scanstring,
1836 METH_VARARGS,
1837 pydoc_scanstring},
Christian Heimes90540002008-05-08 14:29:10 +00001838 {NULL, NULL, 0, NULL}
1839};
1840
1841PyDoc_STRVAR(module_doc,
1842"json speedups\n");
1843
Martin v. Löwis1a214512008-06-11 05:26:20 +00001844static struct PyModuleDef jsonmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 PyModuleDef_HEAD_INIT,
1846 "_json",
1847 module_doc,
1848 -1,
1849 speedups_methods,
1850 NULL,
1851 NULL,
1852 NULL,
1853 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001854};
1855
1856PyObject*
1857PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +00001858{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001859 PyObject *m = PyModule_Create(&jsonmodule);
1860 if (!m)
1861 return NULL;
1862 PyScannerType.tp_new = PyType_GenericNew;
1863 if (PyType_Ready(&PyScannerType) < 0)
1864 goto fail;
1865 PyEncoderType.tp_new = PyType_GenericNew;
1866 if (PyType_Ready(&PyEncoderType) < 0)
1867 goto fail;
1868 Py_INCREF((PyObject*)&PyScannerType);
1869 if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1870 Py_DECREF((PyObject*)&PyScannerType);
1871 goto fail;
1872 }
1873 Py_INCREF((PyObject*)&PyEncoderType);
1874 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1875 Py_DECREF((PyObject*)&PyEncoderType);
1876 goto fail;
1877 }
1878 return m;
1879 fail:
1880 Py_DECREF(m);
1881 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +00001882}