blob: 9b6bbd398fa9859d49f677421c6a4198f98f5817 [file] [log] [blame]
Christian Heimes90540002008-05-08 14:29:10 +00001#include "Python.h"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00002#include "structmember.h"
Antoine Pitroud0acb412012-03-22 14:42:18 +01003#include "accu.h"
4
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00005#ifdef __GNUC__
6#define UNUSED __attribute__((__unused__))
7#else
8#define UNUSED
9#endif
10
11#define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType)
12#define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType)
13#define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType)
14#define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType)
15
16static PyTypeObject PyScannerType;
17static PyTypeObject PyEncoderType;
18
19typedef struct _PyScannerObject {
20 PyObject_HEAD
21 PyObject *strict;
22 PyObject *object_hook;
23 PyObject *object_pairs_hook;
24 PyObject *parse_float;
25 PyObject *parse_int;
26 PyObject *parse_constant;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +000027 PyObject *memo;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000028} PyScannerObject;
29
30static PyMemberDef scanner_members[] = {
31 {"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"},
32 {"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"},
33 {"object_pairs_hook", T_OBJECT, offsetof(PyScannerObject, object_pairs_hook), READONLY},
34 {"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"},
35 {"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "parse_int"},
36 {"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READONLY, "parse_constant"},
37 {NULL}
38};
39
40typedef struct _PyEncoderObject {
41 PyObject_HEAD
42 PyObject *markers;
43 PyObject *defaultfn;
44 PyObject *encoder;
45 PyObject *indent;
46 PyObject *key_separator;
47 PyObject *item_separator;
48 PyObject *sort_keys;
49 PyObject *skipkeys;
50 int fast_encode;
51 int allow_nan;
52} PyEncoderObject;
53
54static PyMemberDef encoder_members[] = {
55 {"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers"},
56 {"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "default"},
57 {"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder"},
58 {"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"},
59 {"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READONLY, "key_separator"},
60 {"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READONLY, "item_separator"},
61 {"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sort_keys"},
62 {"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipkeys"},
63 {NULL}
64};
65
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +020066static PyObject *
67join_list_unicode(PyObject *lst)
68{
69 /* return u''.join(lst) */
70 static PyObject *sep = NULL;
71 if (sep == NULL) {
72 sep = PyUnicode_FromStringAndSize("", 0);
73 if (sep == NULL)
74 return NULL;
75 }
76 return PyUnicode_Join(sep, lst);
77}
78
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +020079/* Forward decls */
80
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000081static PyObject *
82ascii_escape_unicode(PyObject *pystr);
83static PyObject *
84py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr);
85void init_json(void);
86static PyObject *
87scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr);
88static PyObject *
89_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx);
90static PyObject *
91scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
92static int
93scanner_init(PyObject *self, PyObject *args, PyObject *kwds);
94static void
95scanner_dealloc(PyObject *self);
96static int
97scanner_clear(PyObject *self);
98static PyObject *
99encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
100static int
101encoder_init(PyObject *self, PyObject *args, PyObject *kwds);
102static void
103encoder_dealloc(PyObject *self);
104static int
105encoder_clear(PyObject *self);
106static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200107encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc, PyObject *seq, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000108static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200109encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, PyObject *obj, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000110static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200111encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, PyObject *dct, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000112static PyObject *
Hirokazu Yamamotofecf5d12009-05-02 15:55:19 +0000113_encoded_const(PyObject *obj);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000114static void
115raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
116static PyObject *
117encoder_encode_string(PyEncoderObject *s, PyObject *obj);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000118static 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 +0000124static Py_ssize_t
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200125ascii_escape_unichar(Py_UCS4 c, unsigned char *output, Py_ssize_t chars)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000126{
127 /* Escape unicode code point c to ASCII escape sequences
128 in char *output. output must have at least 12 bytes unused to
129 accommodate an escaped surrogate pair "\uXXXX\uXXXX" */
Christian Heimes90540002008-05-08 14:29:10 +0000130 output[chars++] = '\\';
131 switch (c) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000132 case '\\': output[chars++] = c; break;
133 case '"': output[chars++] = c; break;
Christian Heimes90540002008-05-08 14:29:10 +0000134 case '\b': output[chars++] = 'b'; break;
135 case '\f': output[chars++] = 'f'; break;
136 case '\n': output[chars++] = 'n'; break;
137 case '\r': output[chars++] = 'r'; break;
138 case '\t': output[chars++] = 't'; break;
139 default:
Christian Heimes90540002008-05-08 14:29:10 +0000140 if (c >= 0x10000) {
141 /* UTF-16 surrogate pair */
Victor Stinner76df43d2012-10-30 01:42:39 +0100142 Py_UCS4 v = Py_UNICODE_HIGH_SURROGATE(c);
Christian Heimes90540002008-05-08 14:29:10 +0000143 output[chars++] = 'u';
Victor Stinner76df43d2012-10-30 01:42:39 +0100144 output[chars++] = Py_hexdigits[(v >> 12) & 0xf];
145 output[chars++] = Py_hexdigits[(v >> 8) & 0xf];
146 output[chars++] = Py_hexdigits[(v >> 4) & 0xf];
147 output[chars++] = Py_hexdigits[(v ) & 0xf];
148 c = Py_UNICODE_LOW_SURROGATE(c);
Christian Heimes90540002008-05-08 14:29:10 +0000149 output[chars++] = '\\';
150 }
Christian Heimes90540002008-05-08 14:29:10 +0000151 output[chars++] = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200152 output[chars++] = Py_hexdigits[(c >> 12) & 0xf];
153 output[chars++] = Py_hexdigits[(c >> 8) & 0xf];
154 output[chars++] = Py_hexdigits[(c >> 4) & 0xf];
155 output[chars++] = Py_hexdigits[(c ) & 0xf];
Christian Heimes90540002008-05-08 14:29:10 +0000156 }
157 return chars;
158}
159
160static PyObject *
161ascii_escape_unicode(PyObject *pystr)
162{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000163 /* Take a PyUnicode pystr and return a new ASCII-only escaped PyUnicode */
Christian Heimes90540002008-05-08 14:29:10 +0000164 Py_ssize_t i;
165 Py_ssize_t input_chars;
166 Py_ssize_t output_size;
167 Py_ssize_t chars;
168 PyObject *rval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200169 void *input;
170 unsigned char *output;
171 int kind;
Christian Heimes90540002008-05-08 14:29:10 +0000172
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200173 if (PyUnicode_READY(pystr) == -1)
174 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000175
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200176 input_chars = PyUnicode_GET_LENGTH(pystr);
177 input = PyUnicode_DATA(pystr);
178 kind = PyUnicode_KIND(pystr);
179
180 /* Compute the output size */
181 for (i = 0, output_size = 2; i < input_chars; i++) {
182 Py_UCS4 c = PyUnicode_READ(kind, input, i);
183 if (S_CHAR(c))
184 output_size++;
185 else {
186 switch(c) {
Victor Stinnerd9c06312011-10-11 21:56:19 +0200187 case '\\': case '"': case '\b': case '\f':
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200188 case '\n': case '\r': case '\t':
189 output_size += 2; break;
190 default:
191 output_size += c >= 0x10000 ? 12 : 6;
192 }
193 }
194 }
195
196 rval = PyUnicode_New(output_size, 127);
Christian Heimes90540002008-05-08 14:29:10 +0000197 if (rval == NULL) {
198 return NULL;
199 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200200 output = PyUnicode_1BYTE_DATA(rval);
Christian Heimes90540002008-05-08 14:29:10 +0000201 chars = 0;
202 output[chars++] = '"';
203 for (i = 0; i < input_chars; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200204 Py_UCS4 c = PyUnicode_READ(kind, input, i);
Christian Heimes90540002008-05-08 14:29:10 +0000205 if (S_CHAR(c)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000206 output[chars++] = c;
Christian Heimes90540002008-05-08 14:29:10 +0000207 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000208 else {
209 chars = ascii_escape_unichar(c, output, chars);
Christian Heimes90540002008-05-08 14:29:10 +0000210 }
Christian Heimes90540002008-05-08 14:29:10 +0000211 }
212 output[chars++] = '"';
Victor Stinner8f825062012-04-27 13:55:39 +0200213 assert(_PyUnicode_CheckConsistency(rval, 1));
Christian Heimes90540002008-05-08 14:29:10 +0000214 return rval;
215}
216
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000217static void
Christian Heimes90540002008-05-08 14:29:10 +0000218raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
219{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000220 /* Use the Python function json.decoder.errmsg to raise a nice
221 looking ValueError exception */
Christian Heimes90540002008-05-08 14:29:10 +0000222 static PyObject *errmsg_fn = NULL;
223 PyObject *pymsg;
224 if (errmsg_fn == NULL) {
225 PyObject *decoder = PyImport_ImportModule("json.decoder");
226 if (decoder == NULL)
227 return;
228 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000229 Py_DECREF(decoder);
Christian Heimes90540002008-05-08 14:29:10 +0000230 if (errmsg_fn == NULL)
231 return;
Christian Heimes90540002008-05-08 14:29:10 +0000232 }
Antoine Pitroucbb02842012-12-01 19:34:16 +0100233 pymsg = PyObject_CallFunction(errmsg_fn, "(zOn)", msg, s, end);
Benjamin Petersona13d4752008-10-16 21:17:24 +0000234 if (pymsg) {
235 PyErr_SetObject(PyExc_ValueError, pymsg);
236 Py_DECREF(pymsg);
237 }
Christian Heimes90540002008-05-08 14:29:10 +0000238}
239
240static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000241_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
242 /* return (rval, idx) tuple, stealing reference to rval */
243 PyObject *tpl;
244 PyObject *pyidx;
245 /*
246 steal a reference to rval, returns (rval, idx)
247 */
248 if (rval == NULL) {
Christian Heimes90540002008-05-08 14:29:10 +0000249 return NULL;
250 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000251 pyidx = PyLong_FromSsize_t(idx);
252 if (pyidx == NULL) {
253 Py_DECREF(rval);
254 return NULL;
255 }
256 tpl = PyTuple_New(2);
257 if (tpl == NULL) {
258 Py_DECREF(pyidx);
259 Py_DECREF(rval);
260 return NULL;
261 }
262 PyTuple_SET_ITEM(tpl, 0, rval);
263 PyTuple_SET_ITEM(tpl, 1, pyidx);
264 return tpl;
Christian Heimes90540002008-05-08 14:29:10 +0000265}
266
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000267#define APPEND_OLD_CHUNK \
268 if (chunk != NULL) { \
269 if (chunks == NULL) { \
270 chunks = PyList_New(0); \
271 if (chunks == NULL) { \
272 goto bail; \
273 } \
274 } \
275 if (PyList_Append(chunks, chunk)) { \
276 Py_DECREF(chunk); \
277 goto bail; \
278 } \
279 Py_CLEAR(chunk); \
280 }
281
Christian Heimes90540002008-05-08 14:29:10 +0000282static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000283scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000284{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000285 /* Read the JSON string from PyUnicode pystr.
286 end is the index of the first character after the quote.
287 if strict is zero then literal control characters are allowed
288 *next_end_ptr is a return-by-reference index of the character
289 after the end quote
Christian Heimes90540002008-05-08 14:29:10 +0000290
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000291 Return value is a new PyUnicode
292 */
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000293 PyObject *rval = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200294 Py_ssize_t len;
Christian Heimes90540002008-05-08 14:29:10 +0000295 Py_ssize_t begin = end - 1;
Brett Cannonb94767f2011-02-22 20:15:44 +0000296 Py_ssize_t next /* = begin */;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200297 const void *buf;
298 int kind;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000299 PyObject *chunks = NULL;
300 PyObject *chunk = NULL;
301
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200302 if (PyUnicode_READY(pystr) == -1)
303 return 0;
304
305 len = PyUnicode_GET_LENGTH(pystr);
306 buf = PyUnicode_DATA(pystr);
307 kind = PyUnicode_KIND(pystr);
308
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000309 if (end < 0 || len <= end) {
310 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
311 goto bail;
312 }
Christian Heimes90540002008-05-08 14:29:10 +0000313 while (1) {
314 /* Find the end of the string or the next escape */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200315 Py_UCS4 c = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000316 for (next = end; next < len; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200317 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000318 if (c == '"' || c == '\\') {
319 break;
320 }
321 else if (strict && c <= 0x1f) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000322 raise_errmsg("Invalid control character at", pystr, next);
Christian Heimes90540002008-05-08 14:29:10 +0000323 goto bail;
324 }
325 }
326 if (!(c == '"' || c == '\\')) {
327 raise_errmsg("Unterminated string starting at", pystr, begin);
328 goto bail;
329 }
330 /* Pick up this chunk if it's not zero length */
331 if (next != end) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000332 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200333 chunk = PyUnicode_FromKindAndData(
334 kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200335 (char*)buf + kind * end,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200336 next - end);
Christian Heimes90540002008-05-08 14:29:10 +0000337 if (chunk == NULL) {
338 goto bail;
339 }
Christian Heimes90540002008-05-08 14:29:10 +0000340 }
341 next++;
342 if (c == '"') {
343 end = next;
344 break;
345 }
346 if (next == len) {
347 raise_errmsg("Unterminated string starting at", pystr, begin);
348 goto bail;
349 }
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 != 'u') {
352 /* Non-unicode backslash escapes */
353 end = next + 1;
354 switch (c) {
355 case '"': break;
356 case '\\': break;
357 case '/': break;
358 case 'b': c = '\b'; break;
359 case 'f': c = '\f'; break;
360 case 'n': c = '\n'; break;
361 case 'r': c = '\r'; break;
362 case 't': c = '\t'; break;
363 default: c = 0;
364 }
365 if (c == 0) {
366 raise_errmsg("Invalid \\escape", pystr, end - 2);
367 goto bail;
368 }
369 }
370 else {
371 c = 0;
372 next++;
373 end = next + 4;
374 if (end >= len) {
375 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
376 goto bail;
377 }
378 /* Decode 4 hex digits */
379 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200380 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000381 c <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000382 switch (digit) {
383 case '0': case '1': case '2': case '3': case '4':
384 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000385 c |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000386 case 'a': case 'b': case 'c': case 'd': case 'e':
387 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000388 c |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000389 case 'A': case 'B': case 'C': case 'D': case 'E':
390 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000391 c |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000392 default:
393 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
394 goto bail;
395 }
396 }
Christian Heimes90540002008-05-08 14:29:10 +0000397 /* Surrogate pair */
Victor Stinner76df43d2012-10-30 01:42:39 +0100398 if (Py_UNICODE_IS_HIGH_SURROGATE(c)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200399 Py_UCS4 c2 = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000400 if (end + 6 >= len) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000401 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
402 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000403 }
Victor Stinnerd9c06312011-10-11 21:56:19 +0200404 if (PyUnicode_READ(kind, buf, next++) != '\\' ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200405 PyUnicode_READ(kind, buf, next++) != 'u') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000406 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
407 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000408 }
409 end += 6;
410 /* Decode 4 hex digits */
411 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200412 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Antoine Pitrou5b0e9e82010-10-09 15:24:28 +0000413 c2 <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000414 switch (digit) {
415 case '0': case '1': case '2': case '3': case '4':
416 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000417 c2 |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000418 case 'a': case 'b': case 'c': case 'd': case 'e':
419 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000420 c2 |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000421 case 'A': case 'B': case 'C': case 'D': case 'E':
422 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000423 c2 |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000424 default:
425 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
426 goto bail;
427 }
428 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100429 if (!Py_UNICODE_IS_LOW_SURROGATE(c2)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000430 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
431 goto bail;
432 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100433 c = Py_UNICODE_JOIN_SURROGATES(c, c2);
Christian Heimes90540002008-05-08 14:29:10 +0000434 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100435 else if (Py_UNICODE_IS_LOW_SURROGATE(c)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000436 raise_errmsg("Unpaired low surrogate", pystr, end - 5);
437 goto bail;
438 }
Christian Heimes90540002008-05-08 14:29:10 +0000439 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000440 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200441 chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1);
Christian Heimes90540002008-05-08 14:29:10 +0000442 if (chunk == NULL) {
443 goto bail;
444 }
Christian Heimes90540002008-05-08 14:29:10 +0000445 }
446
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000447 if (chunks == NULL) {
448 if (chunk != NULL)
449 rval = chunk;
450 else
451 rval = PyUnicode_FromStringAndSize("", 0);
Christian Heimes90540002008-05-08 14:29:10 +0000452 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000453 else {
454 APPEND_OLD_CHUNK
455 rval = join_list_unicode(chunks);
456 if (rval == NULL) {
457 goto bail;
458 }
459 Py_CLEAR(chunks);
460 }
461
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000462 *next_end_ptr = end;
463 return rval;
Christian Heimes90540002008-05-08 14:29:10 +0000464bail:
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000465 *next_end_ptr = -1;
Christian Heimes90540002008-05-08 14:29:10 +0000466 Py_XDECREF(chunks);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000467 Py_XDECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000468 return NULL;
469}
470
471PyDoc_STRVAR(pydoc_scanstring,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000472 "scanstring(string, end, strict=True) -> (string, end)\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000473 "\n"
474 "Scan the string s for a JSON string. End is the index of the\n"
475 "character in s after the quote that started the JSON string.\n"
476 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
477 "on attempt to decode an invalid string. If strict is False then literal\n"
478 "control characters are allowed in the string.\n"
479 "\n"
480 "Returns a tuple of the decoded string and the index of the character in s\n"
481 "after the end quote."
482);
Christian Heimes90540002008-05-08 14:29:10 +0000483
484static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000485py_scanstring(PyObject* self UNUSED, PyObject *args)
Christian Heimes90540002008-05-08 14:29:10 +0000486{
487 PyObject *pystr;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000488 PyObject *rval;
Christian Heimes90540002008-05-08 14:29:10 +0000489 Py_ssize_t end;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000490 Py_ssize_t next_end = -1;
491 int strict = 1;
Antoine Pitroucbb02842012-12-01 19:34:16 +0100492 if (!PyArg_ParseTuple(args, "On|i:scanstring", &pystr, &end, &strict)) {
Christian Heimes90540002008-05-08 14:29:10 +0000493 return NULL;
494 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000495 if (PyUnicode_Check(pystr)) {
496 rval = scanstring_unicode(pystr, end, strict, &next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000497 }
498 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyErr_Format(PyExc_TypeError,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000500 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000501 Py_TYPE(pystr)->tp_name);
502 return NULL;
503 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000504 return _build_rval_index_tuple(rval, next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000505}
506
507PyDoc_STRVAR(pydoc_encode_basestring_ascii,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000508 "encode_basestring_ascii(string) -> string\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000509 "\n"
510 "Return an ASCII-only JSON representation of a Python string"
511);
Christian Heimes90540002008-05-08 14:29:10 +0000512
513static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000514py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
Christian Heimes90540002008-05-08 14:29:10 +0000515{
516 PyObject *rval;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000517 /* Return an ASCII-only JSON representation of a Python string */
Christian Heimes90540002008-05-08 14:29:10 +0000518 /* METH_O */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000519 if (PyUnicode_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000520 rval = ascii_escape_unicode(pystr);
521 }
522 else {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000523 PyErr_Format(PyExc_TypeError,
524 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000525 Py_TYPE(pystr)->tp_name);
526 return NULL;
527 }
Christian Heimes90540002008-05-08 14:29:10 +0000528 return rval;
529}
530
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000531static void
532scanner_dealloc(PyObject *self)
533{
534 /* Deallocate scanner object */
535 scanner_clear(self);
536 Py_TYPE(self)->tp_free(self);
537}
538
539static int
540scanner_traverse(PyObject *self, visitproc visit, void *arg)
541{
542 PyScannerObject *s;
543 assert(PyScanner_Check(self));
544 s = (PyScannerObject *)self;
545 Py_VISIT(s->strict);
546 Py_VISIT(s->object_hook);
547 Py_VISIT(s->object_pairs_hook);
548 Py_VISIT(s->parse_float);
549 Py_VISIT(s->parse_int);
550 Py_VISIT(s->parse_constant);
551 return 0;
552}
553
554static int
555scanner_clear(PyObject *self)
556{
557 PyScannerObject *s;
558 assert(PyScanner_Check(self));
559 s = (PyScannerObject *)self;
560 Py_CLEAR(s->strict);
561 Py_CLEAR(s->object_hook);
562 Py_CLEAR(s->object_pairs_hook);
563 Py_CLEAR(s->parse_float);
564 Py_CLEAR(s->parse_int);
565 Py_CLEAR(s->parse_constant);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000566 Py_CLEAR(s->memo);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000567 return 0;
568}
569
570static PyObject *
571_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
572 /* Read a JSON object from PyUnicode pystr.
573 idx is the index of the first character after the opening curly brace.
574 *next_idx_ptr is a return-by-reference index to the first character after
575 the closing curly brace.
576
577 Returns a new PyObject (usually a dict, but object_hook can change that)
578 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200579 void *str;
580 int kind;
581 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000582 PyObject *val = NULL;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000583 PyObject *rval = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000584 PyObject *key = NULL;
585 int strict = PyObject_IsTrue(s->strict);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000586 int has_pairs_hook = (s->object_pairs_hook != Py_None);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000587 Py_ssize_t next_idx;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000588
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200589 if (PyUnicode_READY(pystr) == -1)
590 return NULL;
591
592 str = PyUnicode_DATA(pystr);
593 kind = PyUnicode_KIND(pystr);
594 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
595
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000596 if (has_pairs_hook)
597 rval = PyList_New(0);
598 else
599 rval = PyDict_New();
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000600 if (rval == NULL)
601 return NULL;
602
603 /* skip whitespace after { */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200604 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000605
606 /* only loop if the object is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200607 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000608 while (idx <= end_idx) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000609 PyObject *memokey;
610
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000611 /* read key */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200612 if (PyUnicode_READ(kind, str, idx) != '"') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200613 raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000614 goto bail;
615 }
616 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
617 if (key == NULL)
618 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000619 memokey = PyDict_GetItem(s->memo, key);
620 if (memokey != NULL) {
621 Py_INCREF(memokey);
622 Py_DECREF(key);
623 key = memokey;
624 }
625 else {
626 if (PyDict_SetItem(s->memo, key, key) < 0)
627 goto bail;
628 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000629 idx = next_idx;
630
631 /* skip whitespace between key and : delimiter, read :, skip whitespace */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200632 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
633 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200634 raise_errmsg("Expecting ':' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000635 goto bail;
636 }
637 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200638 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000639
640 /* read any JSON term */
641 val = scan_once_unicode(s, pystr, idx, &next_idx);
642 if (val == NULL)
643 goto bail;
644
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000645 if (has_pairs_hook) {
646 PyObject *item = PyTuple_Pack(2, key, val);
647 if (item == NULL)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000648 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000649 Py_CLEAR(key);
650 Py_CLEAR(val);
651 if (PyList_Append(rval, item) == -1) {
652 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000653 goto bail;
654 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000655 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000656 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000657 else {
658 if (PyDict_SetItem(rval, key, val) < 0)
659 goto bail;
660 Py_CLEAR(key);
661 Py_CLEAR(val);
662 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000663 idx = next_idx;
664
665 /* skip whitespace before } or , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200666 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000667
668 /* bail if the object is closed or we didn't get the , delimiter */
669 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200670 if (PyUnicode_READ(kind, str, idx) == '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000671 break;
672 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200673 else if (PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200674 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000675 goto bail;
676 }
677 idx++;
678
679 /* skip whitespace after , delimiter */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200680 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000681 }
682 }
683
684 /* verify that idx < end_idx, str[idx] should be '}' */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200685 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000686 raise_errmsg("Expecting object", pystr, end_idx);
687 goto bail;
688 }
689
690 *next_idx_ptr = idx + 1;
691
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000692 if (has_pairs_hook) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000693 val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000694 Py_DECREF(rval);
695 return val;
696 }
697
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000698 /* if object_hook is not None: rval = object_hook(rval) */
699 if (s->object_hook != Py_None) {
700 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000701 Py_DECREF(rval);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000702 return val;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000703 }
704 return rval;
705bail:
706 Py_XDECREF(key);
707 Py_XDECREF(val);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000708 Py_XDECREF(rval);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000709 return NULL;
710}
711
712static PyObject *
713_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
714 /* Read a JSON array from PyString pystr.
715 idx is the index of the first character after the opening brace.
716 *next_idx_ptr is a return-by-reference index to the first character after
717 the closing brace.
718
719 Returns a new PyList
720 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200721 void *str;
722 int kind;
723 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000724 PyObject *val = NULL;
725 PyObject *rval = PyList_New(0);
726 Py_ssize_t next_idx;
727 if (rval == NULL)
728 return NULL;
729
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200730 if (PyUnicode_READY(pystr) == -1)
731 return NULL;
732
733 str = PyUnicode_DATA(pystr);
734 kind = PyUnicode_KIND(pystr);
735 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
736
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000737 /* skip whitespace after [ */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200738 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000739
740 /* only loop if the array is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200741 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000742 while (idx <= end_idx) {
743
744 /* read any JSON term */
745 val = scan_once_unicode(s, pystr, idx, &next_idx);
746 if (val == NULL)
747 goto bail;
748
749 if (PyList_Append(rval, val) == -1)
750 goto bail;
751
752 Py_CLEAR(val);
753 idx = next_idx;
754
755 /* skip whitespace between term and , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200756 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000757
758 /* bail if the array is closed or we didn't get the , delimiter */
759 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200760 if (PyUnicode_READ(kind, str, idx) == ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000761 break;
762 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200763 else if (PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200764 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000765 goto bail;
766 }
767 idx++;
768
769 /* skip whitespace after , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200770 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000771 }
772 }
773
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200774 /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */
775 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000776 raise_errmsg("Expecting object", pystr, end_idx);
777 goto bail;
778 }
779 *next_idx_ptr = idx + 1;
780 return rval;
781bail:
782 Py_XDECREF(val);
783 Py_DECREF(rval);
784 return NULL;
785}
786
787static PyObject *
788_parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
789 /* Read a JSON constant from PyString pystr.
790 constant is the constant string that was found
791 ("NaN", "Infinity", "-Infinity").
792 idx is the index of the first character of the constant
793 *next_idx_ptr is a return-by-reference index to the first character after
794 the constant.
795
796 Returns the result of parse_constant
797 */
798 PyObject *cstr;
799 PyObject *rval;
800 /* constant is "NaN", "Infinity", or "-Infinity" */
801 cstr = PyUnicode_InternFromString(constant);
802 if (cstr == NULL)
803 return NULL;
804
805 /* rval = parse_constant(constant) */
806 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
Victor Stinnerc4f281e2011-10-11 22:11:42 +0200807 idx += PyUnicode_GET_LENGTH(cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000808 Py_DECREF(cstr);
809 *next_idx_ptr = idx;
810 return rval;
811}
812
813static PyObject *
814_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
815 /* Read a JSON number from PyUnicode pystr.
816 idx is the index of the first character of the number
817 *next_idx_ptr is a return-by-reference index to the first character after
818 the number.
819
820 Returns a new PyObject representation of that number:
821 PyInt, PyLong, or PyFloat.
822 May return other types if parse_int or parse_float are set
823 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200824 void *str;
825 int kind;
826 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000827 Py_ssize_t idx = start;
828 int is_float = 0;
829 PyObject *rval;
Antoine Pitrouf6454512011-04-25 19:16:06 +0200830 PyObject *numstr = NULL;
831 PyObject *custom_func;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000832
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200833 if (PyUnicode_READY(pystr) == -1)
834 return NULL;
835
836 str = PyUnicode_DATA(pystr);
837 kind = PyUnicode_KIND(pystr);
838 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
839
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000840 /* 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 +0200841 if (PyUnicode_READ(kind, str, idx) == '-') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000842 idx++;
843 if (idx > end_idx) {
844 PyErr_SetNone(PyExc_StopIteration);
845 return NULL;
846 }
847 }
848
849 /* 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 +0200850 if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000851 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200852 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000853 }
854 /* if it starts with 0 we only expect one integer digit */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200855 else if (PyUnicode_READ(kind, str, idx) == '0') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000856 idx++;
857 }
858 /* no integer digits, error */
859 else {
860 PyErr_SetNone(PyExc_StopIteration);
861 return NULL;
862 }
863
864 /* if the next char is '.' followed by a digit then read all float digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200865 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 +0000866 is_float = 1;
867 idx += 2;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200868 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000869 }
870
871 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200872 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000873 Py_ssize_t e_start = idx;
874 idx++;
875
876 /* read an exponent sign if present */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200877 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000878
879 /* read all digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200880 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000881
882 /* if we got a digit, then parse as float. if not, backtrack */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200883 if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000884 is_float = 1;
885 }
886 else {
887 idx = e_start;
888 }
889 }
890
Antoine Pitrouf6454512011-04-25 19:16:06 +0200891 if (is_float && s->parse_float != (PyObject *)&PyFloat_Type)
892 custom_func = s->parse_float;
893 else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type)
894 custom_func = s->parse_int;
895 else
896 custom_func = NULL;
897
898 if (custom_func) {
899 /* copy the section we determined to be a number */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200900 numstr = PyUnicode_FromKindAndData(kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200901 (char*)str + kind * start,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200902 idx - start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200903 if (numstr == NULL)
904 return NULL;
905 rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000906 }
907 else {
Antoine Pitrouf6454512011-04-25 19:16:06 +0200908 Py_ssize_t i, n;
909 char *buf;
910 /* Straight conversion to ASCII, to avoid costly conversion of
911 decimal unicode digits (which cannot appear here) */
912 n = idx - start;
913 numstr = PyBytes_FromStringAndSize(NULL, n);
914 if (numstr == NULL)
915 return NULL;
916 buf = PyBytes_AS_STRING(numstr);
917 for (i = 0; i < n; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200918 buf[i] = (char) PyUnicode_READ(kind, str, i + start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200919 }
920 if (is_float)
921 rval = PyFloat_FromString(numstr);
922 else
923 rval = PyLong_FromString(buf, NULL, 10);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000924 }
925 Py_DECREF(numstr);
926 *next_idx_ptr = idx;
927 return rval;
928}
929
930static PyObject *
931scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
932{
933 /* Read one JSON term (of any kind) from PyUnicode pystr.
934 idx is the index of the first character of the term
935 *next_idx_ptr is a return-by-reference index to the first character after
936 the number.
937
938 Returns a new PyObject representation of the term.
939 */
Ezio Melotti362b9512011-05-07 17:58:09 +0300940 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200941 void *str;
942 int kind;
943 Py_ssize_t length;
944
945 if (PyUnicode_READY(pystr) == -1)
946 return NULL;
947
948 str = PyUnicode_DATA(pystr);
949 kind = PyUnicode_KIND(pystr);
950 length = PyUnicode_GET_LENGTH(pystr);
951
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000952 if (idx >= length) {
953 PyErr_SetNone(PyExc_StopIteration);
954 return NULL;
955 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200956
957 switch (PyUnicode_READ(kind, str, idx)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000958 case '"':
959 /* string */
960 return scanstring_unicode(pystr, idx + 1,
961 PyObject_IsTrue(s->strict),
962 next_idx_ptr);
963 case '{':
964 /* object */
Ezio Melotti362b9512011-05-07 17:58:09 +0300965 if (Py_EnterRecursiveCall(" while decoding a JSON object "
966 "from a unicode string"))
967 return NULL;
968 res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
969 Py_LeaveRecursiveCall();
970 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000971 case '[':
972 /* array */
Ezio Melotti362b9512011-05-07 17:58:09 +0300973 if (Py_EnterRecursiveCall(" while decoding a JSON array "
974 "from a unicode string"))
975 return NULL;
976 res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
977 Py_LeaveRecursiveCall();
978 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000979 case 'n':
980 /* null */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200981 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 +0000982 Py_INCREF(Py_None);
983 *next_idx_ptr = idx + 4;
984 return Py_None;
985 }
986 break;
987 case 't':
988 /* true */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200989 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 +0000990 Py_INCREF(Py_True);
991 *next_idx_ptr = idx + 4;
992 return Py_True;
993 }
994 break;
995 case 'f':
996 /* false */
Victor Stinnerd9c06312011-10-11 21:56:19 +0200997 if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
998 PyUnicode_READ(kind, str, idx + 2) == 'l' &&
999 PyUnicode_READ(kind, str, idx + 3) == 's' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001000 PyUnicode_READ(kind, str, idx + 4) == 'e') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001001 Py_INCREF(Py_False);
1002 *next_idx_ptr = idx + 5;
1003 return Py_False;
1004 }
1005 break;
1006 case 'N':
1007 /* NaN */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001008 if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001009 PyUnicode_READ(kind, str, idx + 2) == 'N') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001010 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1011 }
1012 break;
1013 case 'I':
1014 /* Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001015 if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
1016 PyUnicode_READ(kind, str, idx + 2) == 'f' &&
1017 PyUnicode_READ(kind, str, idx + 3) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001018 PyUnicode_READ(kind, str, idx + 4) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001019 PyUnicode_READ(kind, str, idx + 5) == 'i' &&
1020 PyUnicode_READ(kind, str, idx + 6) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001021 PyUnicode_READ(kind, str, idx + 7) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001022 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1023 }
1024 break;
1025 case '-':
1026 /* -Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001027 if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001028 PyUnicode_READ(kind, str, idx + 2) == 'n' &&
1029 PyUnicode_READ(kind, str, idx + 3) == 'f' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001030 PyUnicode_READ(kind, str, idx + 4) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001031 PyUnicode_READ(kind, str, idx + 5) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001032 PyUnicode_READ(kind, str, idx + 6) == 'i' &&
1033 PyUnicode_READ(kind, str, idx + 7) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001034 PyUnicode_READ(kind, str, idx + 8) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001035 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1036 }
1037 break;
1038 }
1039 /* Didn't find a string, object, array, or named constant. Look for a number. */
1040 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
1041}
1042
1043static PyObject *
1044scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
1045{
1046 /* Python callable interface to scan_once_{str,unicode} */
1047 PyObject *pystr;
1048 PyObject *rval;
1049 Py_ssize_t idx;
1050 Py_ssize_t next_idx = -1;
1051 static char *kwlist[] = {"string", "idx", NULL};
1052 PyScannerObject *s;
1053 assert(PyScanner_Check(self));
1054 s = (PyScannerObject *)self;
Antoine Pitroucbb02842012-12-01 19:34:16 +01001055 if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:scan_once", kwlist, &pystr, &idx))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001056 return NULL;
1057
1058 if (PyUnicode_Check(pystr)) {
1059 rval = scan_once_unicode(s, pystr, idx, &next_idx);
1060 }
1061 else {
1062 PyErr_Format(PyExc_TypeError,
1063 "first argument must be a string, not %.80s",
1064 Py_TYPE(pystr)->tp_name);
1065 return NULL;
1066 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001067 PyDict_Clear(s->memo);
1068 if (rval == NULL)
1069 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001070 return _build_rval_index_tuple(rval, next_idx);
1071}
1072
1073static PyObject *
1074scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1075{
1076 PyScannerObject *s;
1077 s = (PyScannerObject *)type->tp_alloc(type, 0);
1078 if (s != NULL) {
1079 s->strict = NULL;
1080 s->object_hook = NULL;
1081 s->object_pairs_hook = NULL;
1082 s->parse_float = NULL;
1083 s->parse_int = NULL;
1084 s->parse_constant = NULL;
1085 }
1086 return (PyObject *)s;
1087}
1088
1089static int
1090scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1091{
1092 /* Initialize Scanner object */
1093 PyObject *ctx;
1094 static char *kwlist[] = {"context", NULL};
1095 PyScannerObject *s;
1096
1097 assert(PyScanner_Check(self));
1098 s = (PyScannerObject *)self;
1099
1100 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1101 return -1;
1102
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001103 if (s->memo == NULL) {
1104 s->memo = PyDict_New();
1105 if (s->memo == NULL)
1106 goto bail;
1107 }
1108
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001109 /* All of these will fail "gracefully" so we don't need to verify them */
1110 s->strict = PyObject_GetAttrString(ctx, "strict");
1111 if (s->strict == NULL)
1112 goto bail;
1113 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1114 if (s->object_hook == NULL)
1115 goto bail;
1116 s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1117 if (s->object_pairs_hook == NULL)
1118 goto bail;
1119 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1120 if (s->parse_float == NULL)
1121 goto bail;
1122 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1123 if (s->parse_int == NULL)
1124 goto bail;
1125 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1126 if (s->parse_constant == NULL)
1127 goto bail;
1128
1129 return 0;
1130
1131bail:
1132 Py_CLEAR(s->strict);
1133 Py_CLEAR(s->object_hook);
1134 Py_CLEAR(s->object_pairs_hook);
1135 Py_CLEAR(s->parse_float);
1136 Py_CLEAR(s->parse_int);
1137 Py_CLEAR(s->parse_constant);
1138 return -1;
1139}
1140
1141PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1142
1143static
1144PyTypeObject PyScannerType = {
1145 PyVarObject_HEAD_INIT(NULL, 0)
1146 "_json.Scanner", /* tp_name */
1147 sizeof(PyScannerObject), /* tp_basicsize */
1148 0, /* tp_itemsize */
1149 scanner_dealloc, /* tp_dealloc */
1150 0, /* tp_print */
1151 0, /* tp_getattr */
1152 0, /* tp_setattr */
1153 0, /* tp_compare */
1154 0, /* tp_repr */
1155 0, /* tp_as_number */
1156 0, /* tp_as_sequence */
1157 0, /* tp_as_mapping */
1158 0, /* tp_hash */
1159 scanner_call, /* tp_call */
1160 0, /* tp_str */
1161 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1162 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1163 0, /* tp_as_buffer */
1164 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1165 scanner_doc, /* tp_doc */
1166 scanner_traverse, /* tp_traverse */
1167 scanner_clear, /* tp_clear */
1168 0, /* tp_richcompare */
1169 0, /* tp_weaklistoffset */
1170 0, /* tp_iter */
1171 0, /* tp_iternext */
1172 0, /* tp_methods */
1173 scanner_members, /* tp_members */
1174 0, /* tp_getset */
1175 0, /* tp_base */
1176 0, /* tp_dict */
1177 0, /* tp_descr_get */
1178 0, /* tp_descr_set */
1179 0, /* tp_dictoffset */
1180 scanner_init, /* tp_init */
1181 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1182 scanner_new, /* tp_new */
1183 0,/* PyObject_GC_Del, */ /* tp_free */
1184};
1185
1186static PyObject *
1187encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1188{
1189 PyEncoderObject *s;
1190 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1191 if (s != NULL) {
1192 s->markers = NULL;
1193 s->defaultfn = NULL;
1194 s->encoder = NULL;
1195 s->indent = NULL;
1196 s->key_separator = NULL;
1197 s->item_separator = NULL;
1198 s->sort_keys = NULL;
1199 s->skipkeys = NULL;
1200 }
1201 return (PyObject *)s;
1202}
1203
1204static int
1205encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1206{
1207 /* initialize Encoder object */
1208 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1209
1210 PyEncoderObject *s;
Antoine Pitrou781eba72009-12-08 15:57:31 +00001211 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1212 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001213
1214 assert(PyEncoder_Check(self));
1215 s = (PyEncoderObject *)self;
1216
1217 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
Antoine Pitrou781eba72009-12-08 15:57:31 +00001218 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1219 &sort_keys, &skipkeys, &allow_nan))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001220 return -1;
1221
Antoine Pitrou781eba72009-12-08 15:57:31 +00001222 s->markers = markers;
1223 s->defaultfn = defaultfn;
1224 s->encoder = encoder;
1225 s->indent = indent;
1226 s->key_separator = key_separator;
1227 s->item_separator = item_separator;
1228 s->sort_keys = sort_keys;
1229 s->skipkeys = skipkeys;
1230 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1231 s->allow_nan = PyObject_IsTrue(allow_nan);
1232
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001233 Py_INCREF(s->markers);
1234 Py_INCREF(s->defaultfn);
1235 Py_INCREF(s->encoder);
1236 Py_INCREF(s->indent);
1237 Py_INCREF(s->key_separator);
1238 Py_INCREF(s->item_separator);
1239 Py_INCREF(s->sort_keys);
1240 Py_INCREF(s->skipkeys);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001241 return 0;
1242}
1243
1244static PyObject *
1245encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1246{
1247 /* Python callable interface to encode_listencode_obj */
1248 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1249 PyObject *obj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001250 Py_ssize_t indent_level;
1251 PyEncoderObject *s;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001252 _PyAccu acc;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001253
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001254 assert(PyEncoder_Check(self));
1255 s = (PyEncoderObject *)self;
Antoine Pitroucbb02842012-12-01 19:34:16 +01001256 if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:_iterencode", kwlist,
1257 &obj, &indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001258 return NULL;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001259 if (_PyAccu_Init(&acc))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001260 return NULL;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001261 if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001262 _PyAccu_Destroy(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001263 return NULL;
1264 }
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001265 return _PyAccu_FinishAsList(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001266}
1267
1268static PyObject *
1269_encoded_const(PyObject *obj)
1270{
1271 /* Return the JSON string representation of None, True, False */
1272 if (obj == Py_None) {
1273 static PyObject *s_null = NULL;
1274 if (s_null == NULL) {
1275 s_null = PyUnicode_InternFromString("null");
1276 }
1277 Py_INCREF(s_null);
1278 return s_null;
1279 }
1280 else if (obj == Py_True) {
1281 static PyObject *s_true = NULL;
1282 if (s_true == NULL) {
1283 s_true = PyUnicode_InternFromString("true");
1284 }
1285 Py_INCREF(s_true);
1286 return s_true;
1287 }
1288 else if (obj == Py_False) {
1289 static PyObject *s_false = NULL;
1290 if (s_false == NULL) {
1291 s_false = PyUnicode_InternFromString("false");
1292 }
1293 Py_INCREF(s_false);
1294 return s_false;
1295 }
1296 else {
1297 PyErr_SetString(PyExc_ValueError, "not a const");
1298 return NULL;
1299 }
1300}
1301
1302static PyObject *
1303encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1304{
1305 /* Return the JSON representation of a PyFloat */
1306 double i = PyFloat_AS_DOUBLE(obj);
1307 if (!Py_IS_FINITE(i)) {
1308 if (!s->allow_nan) {
1309 PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant");
1310 return NULL;
1311 }
1312 if (i > 0) {
1313 return PyUnicode_FromString("Infinity");
1314 }
1315 else if (i < 0) {
1316 return PyUnicode_FromString("-Infinity");
1317 }
1318 else {
1319 return PyUnicode_FromString("NaN");
1320 }
1321 }
1322 /* Use a better float format here? */
1323 return PyObject_Repr(obj);
1324}
1325
1326static PyObject *
1327encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1328{
1329 /* Return the JSON representation of a string */
1330 if (s->fast_encode)
1331 return py_encode_basestring_ascii(NULL, obj);
1332 else
1333 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1334}
1335
1336static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001337_steal_accumulate(_PyAccu *acc, PyObject *stolen)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001338{
1339 /* Append stolen and then decrement its reference count */
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001340 int rval = _PyAccu_Accumulate(acc, stolen);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001341 Py_DECREF(stolen);
1342 return rval;
1343}
1344
1345static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001346encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001347 PyObject *obj, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001348{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001349 /* Encode Python object obj to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001350 PyObject *newobj;
1351 int rv;
1352
1353 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1354 PyObject *cstr = _encoded_const(obj);
1355 if (cstr == NULL)
1356 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001357 return _steal_accumulate(acc, cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001358 }
1359 else if (PyUnicode_Check(obj))
1360 {
1361 PyObject *encoded = encoder_encode_string(s, obj);
1362 if (encoded == NULL)
1363 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001364 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001365 }
1366 else if (PyLong_Check(obj)) {
1367 PyObject *encoded = PyObject_Str(obj);
1368 if (encoded == NULL)
1369 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001370 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001371 }
1372 else if (PyFloat_Check(obj)) {
1373 PyObject *encoded = encoder_encode_float(s, obj);
1374 if (encoded == NULL)
1375 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001376 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001377 }
1378 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001379 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1380 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001381 rv = encoder_listencode_list(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001382 Py_LeaveRecursiveCall();
1383 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001384 }
1385 else if (PyDict_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001386 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1387 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001388 rv = encoder_listencode_dict(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001389 Py_LeaveRecursiveCall();
1390 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001391 }
1392 else {
1393 PyObject *ident = NULL;
1394 if (s->markers != Py_None) {
1395 int has_key;
1396 ident = PyLong_FromVoidPtr(obj);
1397 if (ident == NULL)
1398 return -1;
1399 has_key = PyDict_Contains(s->markers, ident);
1400 if (has_key) {
1401 if (has_key != -1)
1402 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1403 Py_DECREF(ident);
1404 return -1;
1405 }
1406 if (PyDict_SetItem(s->markers, ident, obj)) {
1407 Py_DECREF(ident);
1408 return -1;
1409 }
1410 }
1411 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1412 if (newobj == NULL) {
1413 Py_XDECREF(ident);
1414 return -1;
1415 }
Ezio Melotti13672652011-05-11 01:02:56 +03001416
1417 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1418 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001419 rv = encoder_listencode_obj(s, acc, newobj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001420 Py_LeaveRecursiveCall();
1421
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001422 Py_DECREF(newobj);
1423 if (rv) {
1424 Py_XDECREF(ident);
1425 return -1;
1426 }
1427 if (ident != NULL) {
1428 if (PyDict_DelItem(s->markers, ident)) {
1429 Py_XDECREF(ident);
1430 return -1;
1431 }
1432 Py_XDECREF(ident);
1433 }
1434 return rv;
1435 }
1436}
1437
1438static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001439encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001440 PyObject *dct, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001441{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001442 /* Encode Python dict dct a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001443 static PyObject *open_dict = NULL;
1444 static PyObject *close_dict = NULL;
1445 static PyObject *empty_dict = NULL;
1446 PyObject *kstr = NULL;
1447 PyObject *ident = NULL;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001448 PyObject *it = NULL;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001449 PyObject *items;
1450 PyObject *item = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001451 int skipkeys;
1452 Py_ssize_t idx;
1453
1454 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
1455 open_dict = PyUnicode_InternFromString("{");
1456 close_dict = PyUnicode_InternFromString("}");
1457 empty_dict = PyUnicode_InternFromString("{}");
1458 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
1459 return -1;
1460 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001461 if (Py_SIZE(dct) == 0)
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001462 return _PyAccu_Accumulate(acc, empty_dict);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001463
1464 if (s->markers != Py_None) {
1465 int has_key;
1466 ident = PyLong_FromVoidPtr(dct);
1467 if (ident == NULL)
1468 goto bail;
1469 has_key = PyDict_Contains(s->markers, ident);
1470 if (has_key) {
1471 if (has_key != -1)
1472 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1473 goto bail;
1474 }
1475 if (PyDict_SetItem(s->markers, ident, dct)) {
1476 goto bail;
1477 }
1478 }
1479
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001480 if (_PyAccu_Accumulate(acc, open_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001481 goto bail;
1482
1483 if (s->indent != Py_None) {
1484 /* TODO: DOES NOT RUN */
1485 indent_level += 1;
1486 /*
1487 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1488 separator = _item_separator + newline_indent
1489 buf += newline_indent
1490 */
1491 }
1492
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001493 if (PyObject_IsTrue(s->sort_keys)) {
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001494 /* First sort the keys then replace them with (key, value) tuples. */
1495 Py_ssize_t i, nitems;
1496 items = PyMapping_Keys(dct);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 if (items == NULL)
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001498 goto bail;
1499 if (!PyList_Check(items)) {
1500 PyErr_SetString(PyExc_ValueError, "keys must return list");
1501 goto bail;
1502 }
1503 if (PyList_Sort(items) < 0)
1504 goto bail;
1505 nitems = PyList_GET_SIZE(items);
1506 for (i = 0; i < nitems; i++) {
1507 PyObject *key, *value;
1508 key = PyList_GET_ITEM(items, i);
1509 value = PyDict_GetItem(dct, key);
1510 item = PyTuple_Pack(2, key, value);
1511 if (item == NULL)
1512 goto bail;
1513 PyList_SET_ITEM(items, i, item);
1514 Py_DECREF(key);
1515 }
1516 }
1517 else {
1518 items = PyMapping_Items(dct);
1519 }
1520 if (items == NULL)
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001521 goto bail;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001522 it = PyObject_GetIter(items);
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001523 Py_DECREF(items);
1524 if (it == NULL)
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001525 goto bail;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001526 skipkeys = PyObject_IsTrue(s->skipkeys);
1527 idx = 0;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001528 while ((item = PyIter_Next(it)) != NULL) {
1529 PyObject *encoded, *key, *value;
1530 if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
1531 PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
1532 goto bail;
1533 }
1534 key = PyTuple_GET_ITEM(item, 0);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001535 if (PyUnicode_Check(key)) {
1536 Py_INCREF(key);
1537 kstr = key;
1538 }
1539 else if (PyFloat_Check(key)) {
1540 kstr = encoder_encode_float(s, key);
1541 if (kstr == NULL)
1542 goto bail;
1543 }
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001544 else if (key == Py_True || key == Py_False || key == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 /* This must come before the PyLong_Check because
1546 True and False are also 1 and 0.*/
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001547 kstr = _encoded_const(key);
1548 if (kstr == NULL)
1549 goto bail;
1550 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001551 else if (PyLong_Check(key)) {
1552 kstr = PyObject_Str(key);
1553 if (kstr == NULL)
1554 goto bail;
1555 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001556 else if (skipkeys) {
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001557 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001558 continue;
1559 }
1560 else {
1561 /* TODO: include repr of key */
Doug Hellmann1c524752010-07-21 12:29:04 +00001562 PyErr_SetString(PyExc_TypeError, "keys must be a string");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001563 goto bail;
1564 }
1565
1566 if (idx) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001567 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001568 goto bail;
1569 }
1570
1571 encoded = encoder_encode_string(s, kstr);
1572 Py_CLEAR(kstr);
1573 if (encoded == NULL)
1574 goto bail;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001575 if (_PyAccu_Accumulate(acc, encoded)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001576 Py_DECREF(encoded);
1577 goto bail;
1578 }
1579 Py_DECREF(encoded);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001580 if (_PyAccu_Accumulate(acc, s->key_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001581 goto bail;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001582
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001583 value = PyTuple_GET_ITEM(item, 1);
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001584 if (encoder_listencode_obj(s, acc, value, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001585 goto bail;
1586 idx += 1;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001587 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001588 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001589 if (PyErr_Occurred())
1590 goto bail;
1591 Py_CLEAR(it);
1592
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001593 if (ident != NULL) {
1594 if (PyDict_DelItem(s->markers, ident))
1595 goto bail;
1596 Py_CLEAR(ident);
1597 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001598 /* TODO DOES NOT RUN; dead code
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001599 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001600 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001601
1602 yield '\n' + (' ' * (_indent * _current_indent_level))
1603 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001604 if (_PyAccu_Accumulate(acc, close_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001605 goto bail;
1606 return 0;
1607
1608bail:
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001609 Py_XDECREF(it);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001610 Py_XDECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001611 Py_XDECREF(kstr);
1612 Py_XDECREF(ident);
1613 return -1;
1614}
1615
1616
1617static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001618encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001619 PyObject *seq, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001620{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001621 /* Encode Python list seq to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001622 static PyObject *open_array = NULL;
1623 static PyObject *close_array = NULL;
1624 static PyObject *empty_array = NULL;
1625 PyObject *ident = NULL;
1626 PyObject *s_fast = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001627 Py_ssize_t i;
1628
1629 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
1630 open_array = PyUnicode_InternFromString("[");
1631 close_array = PyUnicode_InternFromString("]");
1632 empty_array = PyUnicode_InternFromString("[]");
1633 if (open_array == NULL || close_array == NULL || empty_array == NULL)
1634 return -1;
1635 }
1636 ident = NULL;
1637 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
1638 if (s_fast == NULL)
1639 return -1;
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001640 if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001641 Py_DECREF(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001642 return _PyAccu_Accumulate(acc, empty_array);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001643 }
1644
1645 if (s->markers != Py_None) {
1646 int has_key;
1647 ident = PyLong_FromVoidPtr(seq);
1648 if (ident == NULL)
1649 goto bail;
1650 has_key = PyDict_Contains(s->markers, ident);
1651 if (has_key) {
1652 if (has_key != -1)
1653 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1654 goto bail;
1655 }
1656 if (PyDict_SetItem(s->markers, ident, seq)) {
1657 goto bail;
1658 }
1659 }
1660
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001661 if (_PyAccu_Accumulate(acc, open_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001662 goto bail;
1663 if (s->indent != Py_None) {
1664 /* TODO: DOES NOT RUN */
1665 indent_level += 1;
1666 /*
1667 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1668 separator = _item_separator + newline_indent
1669 buf += newline_indent
1670 */
1671 }
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001672 for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
1673 PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001674 if (i) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001675 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001676 goto bail;
1677 }
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001678 if (encoder_listencode_obj(s, acc, obj, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001679 goto bail;
1680 }
1681 if (ident != NULL) {
1682 if (PyDict_DelItem(s->markers, ident))
1683 goto bail;
1684 Py_CLEAR(ident);
1685 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001686
1687 /* TODO: DOES NOT RUN
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001688 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001689 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001690
1691 yield '\n' + (' ' * (_indent * _current_indent_level))
1692 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001693 if (_PyAccu_Accumulate(acc, close_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001694 goto bail;
1695 Py_DECREF(s_fast);
1696 return 0;
1697
1698bail:
1699 Py_XDECREF(ident);
1700 Py_DECREF(s_fast);
1701 return -1;
1702}
1703
1704static void
1705encoder_dealloc(PyObject *self)
1706{
1707 /* Deallocate Encoder */
1708 encoder_clear(self);
1709 Py_TYPE(self)->tp_free(self);
1710}
1711
1712static int
1713encoder_traverse(PyObject *self, visitproc visit, void *arg)
1714{
1715 PyEncoderObject *s;
1716 assert(PyEncoder_Check(self));
1717 s = (PyEncoderObject *)self;
1718 Py_VISIT(s->markers);
1719 Py_VISIT(s->defaultfn);
1720 Py_VISIT(s->encoder);
1721 Py_VISIT(s->indent);
1722 Py_VISIT(s->key_separator);
1723 Py_VISIT(s->item_separator);
1724 Py_VISIT(s->sort_keys);
1725 Py_VISIT(s->skipkeys);
1726 return 0;
1727}
1728
1729static int
1730encoder_clear(PyObject *self)
1731{
1732 /* Deallocate Encoder */
1733 PyEncoderObject *s;
1734 assert(PyEncoder_Check(self));
1735 s = (PyEncoderObject *)self;
1736 Py_CLEAR(s->markers);
1737 Py_CLEAR(s->defaultfn);
1738 Py_CLEAR(s->encoder);
1739 Py_CLEAR(s->indent);
1740 Py_CLEAR(s->key_separator);
1741 Py_CLEAR(s->item_separator);
1742 Py_CLEAR(s->sort_keys);
1743 Py_CLEAR(s->skipkeys);
1744 return 0;
1745}
1746
1747PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
1748
1749static
1750PyTypeObject PyEncoderType = {
1751 PyVarObject_HEAD_INIT(NULL, 0)
1752 "_json.Encoder", /* tp_name */
1753 sizeof(PyEncoderObject), /* tp_basicsize */
1754 0, /* tp_itemsize */
1755 encoder_dealloc, /* tp_dealloc */
1756 0, /* tp_print */
1757 0, /* tp_getattr */
1758 0, /* tp_setattr */
1759 0, /* tp_compare */
1760 0, /* tp_repr */
1761 0, /* tp_as_number */
1762 0, /* tp_as_sequence */
1763 0, /* tp_as_mapping */
1764 0, /* tp_hash */
1765 encoder_call, /* tp_call */
1766 0, /* tp_str */
1767 0, /* tp_getattro */
1768 0, /* tp_setattro */
1769 0, /* tp_as_buffer */
1770 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1771 encoder_doc, /* tp_doc */
1772 encoder_traverse, /* tp_traverse */
1773 encoder_clear, /* tp_clear */
1774 0, /* tp_richcompare */
1775 0, /* tp_weaklistoffset */
1776 0, /* tp_iter */
1777 0, /* tp_iternext */
1778 0, /* tp_methods */
1779 encoder_members, /* tp_members */
1780 0, /* tp_getset */
1781 0, /* tp_base */
1782 0, /* tp_dict */
1783 0, /* tp_descr_get */
1784 0, /* tp_descr_set */
1785 0, /* tp_dictoffset */
1786 encoder_init, /* tp_init */
1787 0, /* tp_alloc */
1788 encoder_new, /* tp_new */
1789 0, /* tp_free */
1790};
1791
1792static PyMethodDef speedups_methods[] = {
1793 {"encode_basestring_ascii",
1794 (PyCFunction)py_encode_basestring_ascii,
1795 METH_O,
1796 pydoc_encode_basestring_ascii},
1797 {"scanstring",
1798 (PyCFunction)py_scanstring,
1799 METH_VARARGS,
1800 pydoc_scanstring},
Christian Heimes90540002008-05-08 14:29:10 +00001801 {NULL, NULL, 0, NULL}
1802};
1803
1804PyDoc_STRVAR(module_doc,
1805"json speedups\n");
1806
Martin v. Löwis1a214512008-06-11 05:26:20 +00001807static struct PyModuleDef jsonmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 PyModuleDef_HEAD_INIT,
1809 "_json",
1810 module_doc,
1811 -1,
1812 speedups_methods,
1813 NULL,
1814 NULL,
1815 NULL,
1816 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001817};
1818
1819PyObject*
1820PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +00001821{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001822 PyObject *m = PyModule_Create(&jsonmodule);
1823 if (!m)
1824 return NULL;
1825 PyScannerType.tp_new = PyType_GenericNew;
1826 if (PyType_Ready(&PyScannerType) < 0)
1827 goto fail;
1828 PyEncoderType.tp_new = PyType_GenericNew;
1829 if (PyType_Ready(&PyEncoderType) < 0)
1830 goto fail;
1831 Py_INCREF((PyObject*)&PyScannerType);
1832 if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1833 Py_DECREF((PyObject*)&PyScannerType);
1834 goto fail;
1835 }
1836 Py_INCREF((PyObject*)&PyEncoderType);
1837 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1838 Py_DECREF((PyObject*)&PyEncoderType);
1839 goto fail;
1840 }
1841 return m;
1842 fail:
1843 Py_DECREF(m);
1844 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +00001845}