blob: 2e1659eb98f23f71bbcc094f598ad583bccb2faf [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++] = '"';
Christian Heimesf402e922013-01-03 09:21:55 +0100213#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200214 assert(_PyUnicode_CheckConsistency(rval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100215#endif
Christian Heimes90540002008-05-08 14:29:10 +0000216 return rval;
217}
218
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000219static void
Christian Heimes90540002008-05-08 14:29:10 +0000220raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
221{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000222 /* Use the Python function json.decoder.errmsg to raise a nice
223 looking ValueError exception */
Christian Heimes90540002008-05-08 14:29:10 +0000224 static PyObject *errmsg_fn = NULL;
225 PyObject *pymsg;
226 if (errmsg_fn == NULL) {
227 PyObject *decoder = PyImport_ImportModule("json.decoder");
228 if (decoder == NULL)
229 return;
230 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000231 Py_DECREF(decoder);
Christian Heimes90540002008-05-08 14:29:10 +0000232 if (errmsg_fn == NULL)
233 return;
Christian Heimes90540002008-05-08 14:29:10 +0000234 }
Antoine Pitroucbb02842012-12-01 19:34:16 +0100235 pymsg = PyObject_CallFunction(errmsg_fn, "(zOn)", msg, s, end);
Benjamin Petersona13d4752008-10-16 21:17:24 +0000236 if (pymsg) {
237 PyErr_SetObject(PyExc_ValueError, pymsg);
238 Py_DECREF(pymsg);
239 }
Christian Heimes90540002008-05-08 14:29:10 +0000240}
241
Ezio Melotti37623ab2013-01-03 08:44:15 +0200242static void
243raise_stop_iteration(Py_ssize_t idx)
244{
245 PyObject *value = PyLong_FromSsize_t(idx);
246 if (value != NULL) {
247 PyErr_SetObject(PyExc_StopIteration, value);
248 Py_DECREF(value);
249 }
250}
251
Christian Heimes90540002008-05-08 14:29:10 +0000252static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000253_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
254 /* return (rval, idx) tuple, stealing reference to rval */
255 PyObject *tpl;
256 PyObject *pyidx;
257 /*
258 steal a reference to rval, returns (rval, idx)
259 */
260 if (rval == NULL) {
Christian Heimes90540002008-05-08 14:29:10 +0000261 return NULL;
262 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000263 pyidx = PyLong_FromSsize_t(idx);
264 if (pyidx == NULL) {
265 Py_DECREF(rval);
266 return NULL;
267 }
268 tpl = PyTuple_New(2);
269 if (tpl == NULL) {
270 Py_DECREF(pyidx);
271 Py_DECREF(rval);
272 return NULL;
273 }
274 PyTuple_SET_ITEM(tpl, 0, rval);
275 PyTuple_SET_ITEM(tpl, 1, pyidx);
276 return tpl;
Christian Heimes90540002008-05-08 14:29:10 +0000277}
278
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000279#define APPEND_OLD_CHUNK \
280 if (chunk != NULL) { \
281 if (chunks == NULL) { \
282 chunks = PyList_New(0); \
283 if (chunks == NULL) { \
284 goto bail; \
285 } \
286 } \
287 if (PyList_Append(chunks, chunk)) { \
288 Py_DECREF(chunk); \
289 goto bail; \
290 } \
291 Py_CLEAR(chunk); \
292 }
293
Christian Heimes90540002008-05-08 14:29:10 +0000294static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000295scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000296{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000297 /* Read the JSON string from PyUnicode pystr.
298 end is the index of the first character after the quote.
299 if strict is zero then literal control characters are allowed
300 *next_end_ptr is a return-by-reference index of the character
301 after the end quote
Christian Heimes90540002008-05-08 14:29:10 +0000302
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000303 Return value is a new PyUnicode
304 */
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000305 PyObject *rval = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200306 Py_ssize_t len;
Christian Heimes90540002008-05-08 14:29:10 +0000307 Py_ssize_t begin = end - 1;
Brett Cannonb94767f2011-02-22 20:15:44 +0000308 Py_ssize_t next /* = begin */;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200309 const void *buf;
310 int kind;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000311 PyObject *chunks = NULL;
312 PyObject *chunk = NULL;
313
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200314 if (PyUnicode_READY(pystr) == -1)
315 return 0;
316
317 len = PyUnicode_GET_LENGTH(pystr);
318 buf = PyUnicode_DATA(pystr);
319 kind = PyUnicode_KIND(pystr);
320
Ezio Melotti37623ab2013-01-03 08:44:15 +0200321 if (end < 0 || len < end) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000322 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
323 goto bail;
324 }
Christian Heimes90540002008-05-08 14:29:10 +0000325 while (1) {
326 /* Find the end of the string or the next escape */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200327 Py_UCS4 c = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000328 for (next = end; next < len; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200329 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000330 if (c == '"' || c == '\\') {
331 break;
332 }
333 else if (strict && c <= 0x1f) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000334 raise_errmsg("Invalid control character at", pystr, next);
Christian Heimes90540002008-05-08 14:29:10 +0000335 goto bail;
336 }
337 }
338 if (!(c == '"' || c == '\\')) {
339 raise_errmsg("Unterminated string starting at", pystr, begin);
340 goto bail;
341 }
342 /* Pick up this chunk if it's not zero length */
343 if (next != end) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000344 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200345 chunk = PyUnicode_FromKindAndData(
346 kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200347 (char*)buf + kind * end,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200348 next - end);
Christian Heimes90540002008-05-08 14:29:10 +0000349 if (chunk == NULL) {
350 goto bail;
351 }
Christian Heimes90540002008-05-08 14:29:10 +0000352 }
353 next++;
354 if (c == '"') {
355 end = next;
356 break;
357 }
358 if (next == len) {
359 raise_errmsg("Unterminated string starting at", pystr, begin);
360 goto bail;
361 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200362 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000363 if (c != 'u') {
364 /* Non-unicode backslash escapes */
365 end = next + 1;
366 switch (c) {
367 case '"': break;
368 case '\\': break;
369 case '/': break;
370 case 'b': c = '\b'; break;
371 case 'f': c = '\f'; break;
372 case 'n': c = '\n'; break;
373 case 'r': c = '\r'; break;
374 case 't': c = '\t'; break;
375 default: c = 0;
376 }
377 if (c == 0) {
378 raise_errmsg("Invalid \\escape", pystr, end - 2);
379 goto bail;
380 }
381 }
382 else {
383 c = 0;
384 next++;
385 end = next + 4;
386 if (end >= len) {
387 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
388 goto bail;
389 }
390 /* Decode 4 hex digits */
391 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200392 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000393 c <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000394 switch (digit) {
395 case '0': case '1': case '2': case '3': case '4':
396 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000397 c |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000398 case 'a': case 'b': case 'c': case 'd': case 'e':
399 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000400 c |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000401 case 'A': case 'B': case 'C': case 'D': case 'E':
402 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000403 c |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000404 default:
405 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
406 goto bail;
407 }
408 }
Christian Heimes90540002008-05-08 14:29:10 +0000409 /* Surrogate pair */
Victor Stinner76df43d2012-10-30 01:42:39 +0100410 if (Py_UNICODE_IS_HIGH_SURROGATE(c)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200411 Py_UCS4 c2 = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000412 if (end + 6 >= len) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000413 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
414 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000415 }
Victor Stinnerd9c06312011-10-11 21:56:19 +0200416 if (PyUnicode_READ(kind, buf, next++) != '\\' ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200417 PyUnicode_READ(kind, buf, next++) != 'u') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000418 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
419 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000420 }
421 end += 6;
422 /* Decode 4 hex digits */
423 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200424 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Antoine Pitrou5b0e9e82010-10-09 15:24:28 +0000425 c2 <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000426 switch (digit) {
427 case '0': case '1': case '2': case '3': case '4':
428 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000429 c2 |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000430 case 'a': case 'b': case 'c': case 'd': case 'e':
431 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000432 c2 |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000433 case 'A': case 'B': case 'C': case 'D': case 'E':
434 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000435 c2 |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000436 default:
437 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
438 goto bail;
439 }
440 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100441 if (!Py_UNICODE_IS_LOW_SURROGATE(c2)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000442 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
443 goto bail;
444 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100445 c = Py_UNICODE_JOIN_SURROGATES(c, c2);
Christian Heimes90540002008-05-08 14:29:10 +0000446 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100447 else if (Py_UNICODE_IS_LOW_SURROGATE(c)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000448 raise_errmsg("Unpaired low surrogate", pystr, end - 5);
449 goto bail;
450 }
Christian Heimes90540002008-05-08 14:29:10 +0000451 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000452 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200453 chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1);
Christian Heimes90540002008-05-08 14:29:10 +0000454 if (chunk == NULL) {
455 goto bail;
456 }
Christian Heimes90540002008-05-08 14:29:10 +0000457 }
458
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000459 if (chunks == NULL) {
460 if (chunk != NULL)
461 rval = chunk;
462 else
463 rval = PyUnicode_FromStringAndSize("", 0);
Christian Heimes90540002008-05-08 14:29:10 +0000464 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000465 else {
466 APPEND_OLD_CHUNK
467 rval = join_list_unicode(chunks);
468 if (rval == NULL) {
469 goto bail;
470 }
471 Py_CLEAR(chunks);
472 }
473
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000474 *next_end_ptr = end;
475 return rval;
Christian Heimes90540002008-05-08 14:29:10 +0000476bail:
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000477 *next_end_ptr = -1;
Christian Heimes90540002008-05-08 14:29:10 +0000478 Py_XDECREF(chunks);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000479 Py_XDECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000480 return NULL;
481}
482
483PyDoc_STRVAR(pydoc_scanstring,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000484 "scanstring(string, end, strict=True) -> (string, end)\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000485 "\n"
486 "Scan the string s for a JSON string. End is the index of the\n"
487 "character in s after the quote that started the JSON string.\n"
488 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
489 "on attempt to decode an invalid string. If strict is False then literal\n"
490 "control characters are allowed in the string.\n"
491 "\n"
492 "Returns a tuple of the decoded string and the index of the character in s\n"
493 "after the end quote."
494);
Christian Heimes90540002008-05-08 14:29:10 +0000495
496static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000497py_scanstring(PyObject* self UNUSED, PyObject *args)
Christian Heimes90540002008-05-08 14:29:10 +0000498{
499 PyObject *pystr;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000500 PyObject *rval;
Christian Heimes90540002008-05-08 14:29:10 +0000501 Py_ssize_t end;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000502 Py_ssize_t next_end = -1;
503 int strict = 1;
Antoine Pitroucbb02842012-12-01 19:34:16 +0100504 if (!PyArg_ParseTuple(args, "On|i:scanstring", &pystr, &end, &strict)) {
Christian Heimes90540002008-05-08 14:29:10 +0000505 return NULL;
506 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000507 if (PyUnicode_Check(pystr)) {
508 rval = scanstring_unicode(pystr, end, strict, &next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000509 }
510 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 PyErr_Format(PyExc_TypeError,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000512 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000513 Py_TYPE(pystr)->tp_name);
514 return NULL;
515 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000516 return _build_rval_index_tuple(rval, next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000517}
518
519PyDoc_STRVAR(pydoc_encode_basestring_ascii,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000520 "encode_basestring_ascii(string) -> string\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000521 "\n"
522 "Return an ASCII-only JSON representation of a Python string"
523);
Christian Heimes90540002008-05-08 14:29:10 +0000524
525static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000526py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
Christian Heimes90540002008-05-08 14:29:10 +0000527{
528 PyObject *rval;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000529 /* Return an ASCII-only JSON representation of a Python string */
Christian Heimes90540002008-05-08 14:29:10 +0000530 /* METH_O */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000531 if (PyUnicode_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000532 rval = ascii_escape_unicode(pystr);
533 }
534 else {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000535 PyErr_Format(PyExc_TypeError,
536 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000537 Py_TYPE(pystr)->tp_name);
538 return NULL;
539 }
Christian Heimes90540002008-05-08 14:29:10 +0000540 return rval;
541}
542
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000543static void
544scanner_dealloc(PyObject *self)
545{
546 /* Deallocate scanner object */
547 scanner_clear(self);
548 Py_TYPE(self)->tp_free(self);
549}
550
551static int
552scanner_traverse(PyObject *self, visitproc visit, void *arg)
553{
554 PyScannerObject *s;
555 assert(PyScanner_Check(self));
556 s = (PyScannerObject *)self;
557 Py_VISIT(s->strict);
558 Py_VISIT(s->object_hook);
559 Py_VISIT(s->object_pairs_hook);
560 Py_VISIT(s->parse_float);
561 Py_VISIT(s->parse_int);
562 Py_VISIT(s->parse_constant);
563 return 0;
564}
565
566static int
567scanner_clear(PyObject *self)
568{
569 PyScannerObject *s;
570 assert(PyScanner_Check(self));
571 s = (PyScannerObject *)self;
572 Py_CLEAR(s->strict);
573 Py_CLEAR(s->object_hook);
574 Py_CLEAR(s->object_pairs_hook);
575 Py_CLEAR(s->parse_float);
576 Py_CLEAR(s->parse_int);
577 Py_CLEAR(s->parse_constant);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000578 Py_CLEAR(s->memo);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000579 return 0;
580}
581
582static PyObject *
583_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
584 /* Read a JSON object from PyUnicode pystr.
585 idx is the index of the first character after the opening curly brace.
586 *next_idx_ptr is a return-by-reference index to the first character after
587 the closing curly brace.
588
589 Returns a new PyObject (usually a dict, but object_hook can change that)
590 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200591 void *str;
592 int kind;
593 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000594 PyObject *val = NULL;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000595 PyObject *rval = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000596 PyObject *key = NULL;
597 int strict = PyObject_IsTrue(s->strict);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000598 int has_pairs_hook = (s->object_pairs_hook != Py_None);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000599 Py_ssize_t next_idx;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000600
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200601 if (PyUnicode_READY(pystr) == -1)
602 return NULL;
603
604 str = PyUnicode_DATA(pystr);
605 kind = PyUnicode_KIND(pystr);
606 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
607
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000608 if (has_pairs_hook)
609 rval = PyList_New(0);
610 else
611 rval = PyDict_New();
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000612 if (rval == NULL)
613 return NULL;
614
615 /* skip whitespace after { */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200616 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000617
618 /* only loop if the object is non-empty */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200619 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') {
620 while (1) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000621 PyObject *memokey;
622
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000623 /* read key */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200624 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '"') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200625 raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000626 goto bail;
627 }
628 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
629 if (key == NULL)
630 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000631 memokey = PyDict_GetItem(s->memo, key);
632 if (memokey != NULL) {
633 Py_INCREF(memokey);
634 Py_DECREF(key);
635 key = memokey;
636 }
637 else {
638 if (PyDict_SetItem(s->memo, key, key) < 0)
639 goto bail;
640 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000641 idx = next_idx;
642
643 /* skip whitespace between key and : delimiter, read :, skip whitespace */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200644 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
645 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200646 raise_errmsg("Expecting ':' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000647 goto bail;
648 }
649 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200650 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000651
652 /* read any JSON term */
653 val = scan_once_unicode(s, pystr, idx, &next_idx);
654 if (val == NULL)
655 goto bail;
656
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000657 if (has_pairs_hook) {
658 PyObject *item = PyTuple_Pack(2, key, val);
659 if (item == NULL)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000660 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000661 Py_CLEAR(key);
662 Py_CLEAR(val);
663 if (PyList_Append(rval, item) == -1) {
664 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000665 goto bail;
666 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000667 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000668 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000669 else {
670 if (PyDict_SetItem(rval, key, val) < 0)
671 goto bail;
672 Py_CLEAR(key);
673 Py_CLEAR(val);
674 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000675 idx = next_idx;
676
677 /* skip whitespace before } or , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200678 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000679
680 /* bail if the object is closed or we didn't get the , delimiter */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200681 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) == '}')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000682 break;
Ezio Melotti37623ab2013-01-03 08:44:15 +0200683 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200684 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000685 goto bail;
686 }
687 idx++;
688
689 /* skip whitespace after , delimiter */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200690 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000691 }
692 }
693
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000694 *next_idx_ptr = idx + 1;
695
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000696 if (has_pairs_hook) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000697 val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000698 Py_DECREF(rval);
699 return val;
700 }
701
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000702 /* if object_hook is not None: rval = object_hook(rval) */
703 if (s->object_hook != Py_None) {
704 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000705 Py_DECREF(rval);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000706 return val;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000707 }
708 return rval;
709bail:
710 Py_XDECREF(key);
711 Py_XDECREF(val);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000712 Py_XDECREF(rval);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000713 return NULL;
714}
715
716static PyObject *
717_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
718 /* Read a JSON array from PyString pystr.
719 idx is the index of the first character after the opening brace.
720 *next_idx_ptr is a return-by-reference index to the first character after
721 the closing brace.
722
723 Returns a new PyList
724 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200725 void *str;
726 int kind;
727 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000728 PyObject *val = NULL;
729 PyObject *rval = PyList_New(0);
730 Py_ssize_t next_idx;
731 if (rval == NULL)
732 return NULL;
733
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200734 if (PyUnicode_READY(pystr) == -1)
735 return NULL;
736
737 str = PyUnicode_DATA(pystr);
738 kind = PyUnicode_KIND(pystr);
739 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
740
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000741 /* skip whitespace after [ */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200742 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000743
744 /* only loop if the array is non-empty */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200745 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
746 while (1) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000747
748 /* read any JSON term */
749 val = scan_once_unicode(s, pystr, idx, &next_idx);
750 if (val == NULL)
751 goto bail;
752
753 if (PyList_Append(rval, val) == -1)
754 goto bail;
755
756 Py_CLEAR(val);
757 idx = next_idx;
758
759 /* skip whitespace between term and , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200760 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000761
762 /* bail if the array is closed or we didn't get the , delimiter */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200763 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) == ']')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000764 break;
Ezio Melotti37623ab2013-01-03 08:44:15 +0200765 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200766 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000767 goto bail;
768 }
769 idx++;
770
771 /* skip whitespace after , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200772 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000773 }
774 }
775
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200776 /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */
777 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
Ezio Melotti37623ab2013-01-03 08:44:15 +0200778 raise_errmsg("Expecting value", pystr, end_idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000779 goto bail;
780 }
781 *next_idx_ptr = idx + 1;
782 return rval;
783bail:
784 Py_XDECREF(val);
785 Py_DECREF(rval);
786 return NULL;
787}
788
789static PyObject *
790_parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
791 /* Read a JSON constant from PyString pystr.
792 constant is the constant string that was found
793 ("NaN", "Infinity", "-Infinity").
794 idx is the index of the first character of the constant
795 *next_idx_ptr is a return-by-reference index to the first character after
796 the constant.
797
798 Returns the result of parse_constant
799 */
800 PyObject *cstr;
801 PyObject *rval;
802 /* constant is "NaN", "Infinity", or "-Infinity" */
803 cstr = PyUnicode_InternFromString(constant);
804 if (cstr == NULL)
805 return NULL;
806
807 /* rval = parse_constant(constant) */
808 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
Victor Stinnerc4f281e2011-10-11 22:11:42 +0200809 idx += PyUnicode_GET_LENGTH(cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000810 Py_DECREF(cstr);
811 *next_idx_ptr = idx;
812 return rval;
813}
814
815static PyObject *
816_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
817 /* Read a JSON number from PyUnicode pystr.
818 idx is the index of the first character of the number
819 *next_idx_ptr is a return-by-reference index to the first character after
820 the number.
821
822 Returns a new PyObject representation of that number:
823 PyInt, PyLong, or PyFloat.
824 May return other types if parse_int or parse_float are set
825 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200826 void *str;
827 int kind;
828 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000829 Py_ssize_t idx = start;
830 int is_float = 0;
831 PyObject *rval;
Antoine Pitrouf6454512011-04-25 19:16:06 +0200832 PyObject *numstr = NULL;
833 PyObject *custom_func;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000834
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200835 if (PyUnicode_READY(pystr) == -1)
836 return NULL;
837
838 str = PyUnicode_DATA(pystr);
839 kind = PyUnicode_KIND(pystr);
840 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
841
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000842 /* 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 +0200843 if (PyUnicode_READ(kind, str, idx) == '-') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000844 idx++;
845 if (idx > end_idx) {
Ezio Melotti37623ab2013-01-03 08:44:15 +0200846 raise_stop_iteration(start);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000847 return NULL;
848 }
849 }
850
851 /* 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 +0200852 if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000853 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200854 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000855 }
856 /* if it starts with 0 we only expect one integer digit */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200857 else if (PyUnicode_READ(kind, str, idx) == '0') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000858 idx++;
859 }
860 /* no integer digits, error */
861 else {
Ezio Melotti37623ab2013-01-03 08:44:15 +0200862 raise_stop_iteration(start);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000863 return NULL;
864 }
865
866 /* if the next char is '.' followed by a digit then read all float digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200867 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 +0000868 is_float = 1;
869 idx += 2;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200870 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000871 }
872
873 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200874 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000875 Py_ssize_t e_start = idx;
876 idx++;
877
878 /* read an exponent sign if present */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200879 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000880
881 /* read all digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200882 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000883
884 /* if we got a digit, then parse as float. if not, backtrack */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200885 if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000886 is_float = 1;
887 }
888 else {
889 idx = e_start;
890 }
891 }
892
Antoine Pitrouf6454512011-04-25 19:16:06 +0200893 if (is_float && s->parse_float != (PyObject *)&PyFloat_Type)
894 custom_func = s->parse_float;
895 else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type)
896 custom_func = s->parse_int;
897 else
898 custom_func = NULL;
899
900 if (custom_func) {
901 /* copy the section we determined to be a number */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200902 numstr = PyUnicode_FromKindAndData(kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200903 (char*)str + kind * start,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200904 idx - start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200905 if (numstr == NULL)
906 return NULL;
907 rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000908 }
909 else {
Antoine Pitrouf6454512011-04-25 19:16:06 +0200910 Py_ssize_t i, n;
911 char *buf;
912 /* Straight conversion to ASCII, to avoid costly conversion of
913 decimal unicode digits (which cannot appear here) */
914 n = idx - start;
915 numstr = PyBytes_FromStringAndSize(NULL, n);
916 if (numstr == NULL)
917 return NULL;
918 buf = PyBytes_AS_STRING(numstr);
919 for (i = 0; i < n; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200920 buf[i] = (char) PyUnicode_READ(kind, str, i + start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200921 }
922 if (is_float)
923 rval = PyFloat_FromString(numstr);
924 else
925 rval = PyLong_FromString(buf, NULL, 10);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000926 }
927 Py_DECREF(numstr);
928 *next_idx_ptr = idx;
929 return rval;
930}
931
932static PyObject *
933scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
934{
935 /* Read one JSON term (of any kind) from PyUnicode pystr.
936 idx is the index of the first character of the term
937 *next_idx_ptr is a return-by-reference index to the first character after
938 the number.
939
940 Returns a new PyObject representation of the term.
941 */
Ezio Melotti362b9512011-05-07 17:58:09 +0300942 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200943 void *str;
944 int kind;
945 Py_ssize_t length;
946
947 if (PyUnicode_READY(pystr) == -1)
948 return NULL;
949
950 str = PyUnicode_DATA(pystr);
951 kind = PyUnicode_KIND(pystr);
952 length = PyUnicode_GET_LENGTH(pystr);
953
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000954 if (idx >= length) {
Ezio Melotti37623ab2013-01-03 08:44:15 +0200955 raise_stop_iteration(idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000956 return NULL;
957 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200958
959 switch (PyUnicode_READ(kind, str, idx)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000960 case '"':
961 /* string */
962 return scanstring_unicode(pystr, idx + 1,
963 PyObject_IsTrue(s->strict),
964 next_idx_ptr);
965 case '{':
966 /* object */
Ezio Melotti362b9512011-05-07 17:58:09 +0300967 if (Py_EnterRecursiveCall(" while decoding a JSON object "
968 "from a unicode string"))
969 return NULL;
970 res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
971 Py_LeaveRecursiveCall();
972 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000973 case '[':
974 /* array */
Ezio Melotti362b9512011-05-07 17:58:09 +0300975 if (Py_EnterRecursiveCall(" while decoding a JSON array "
976 "from a unicode string"))
977 return NULL;
978 res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
979 Py_LeaveRecursiveCall();
980 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000981 case 'n':
982 /* null */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200983 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 +0000984 Py_INCREF(Py_None);
985 *next_idx_ptr = idx + 4;
986 return Py_None;
987 }
988 break;
989 case 't':
990 /* true */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200991 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 +0000992 Py_INCREF(Py_True);
993 *next_idx_ptr = idx + 4;
994 return Py_True;
995 }
996 break;
997 case 'f':
998 /* false */
Victor Stinnerd9c06312011-10-11 21:56:19 +0200999 if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
1000 PyUnicode_READ(kind, str, idx + 2) == 'l' &&
1001 PyUnicode_READ(kind, str, idx + 3) == 's' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001002 PyUnicode_READ(kind, str, idx + 4) == 'e') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001003 Py_INCREF(Py_False);
1004 *next_idx_ptr = idx + 5;
1005 return Py_False;
1006 }
1007 break;
1008 case 'N':
1009 /* NaN */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001010 if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001011 PyUnicode_READ(kind, str, idx + 2) == 'N') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001012 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1013 }
1014 break;
1015 case 'I':
1016 /* Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001017 if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
1018 PyUnicode_READ(kind, str, idx + 2) == 'f' &&
1019 PyUnicode_READ(kind, str, idx + 3) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001020 PyUnicode_READ(kind, str, idx + 4) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001021 PyUnicode_READ(kind, str, idx + 5) == 'i' &&
1022 PyUnicode_READ(kind, str, idx + 6) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001023 PyUnicode_READ(kind, str, idx + 7) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001024 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1025 }
1026 break;
1027 case '-':
1028 /* -Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001029 if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001030 PyUnicode_READ(kind, str, idx + 2) == 'n' &&
1031 PyUnicode_READ(kind, str, idx + 3) == 'f' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001032 PyUnicode_READ(kind, str, idx + 4) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001033 PyUnicode_READ(kind, str, idx + 5) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001034 PyUnicode_READ(kind, str, idx + 6) == 'i' &&
1035 PyUnicode_READ(kind, str, idx + 7) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001036 PyUnicode_READ(kind, str, idx + 8) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001037 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1038 }
1039 break;
1040 }
1041 /* Didn't find a string, object, array, or named constant. Look for a number. */
1042 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
1043}
1044
1045static PyObject *
1046scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
1047{
1048 /* Python callable interface to scan_once_{str,unicode} */
1049 PyObject *pystr;
1050 PyObject *rval;
1051 Py_ssize_t idx;
1052 Py_ssize_t next_idx = -1;
1053 static char *kwlist[] = {"string", "idx", NULL};
1054 PyScannerObject *s;
1055 assert(PyScanner_Check(self));
1056 s = (PyScannerObject *)self;
Antoine Pitroucbb02842012-12-01 19:34:16 +01001057 if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:scan_once", kwlist, &pystr, &idx))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001058 return NULL;
1059
1060 if (PyUnicode_Check(pystr)) {
1061 rval = scan_once_unicode(s, pystr, idx, &next_idx);
1062 }
1063 else {
1064 PyErr_Format(PyExc_TypeError,
1065 "first argument must be a string, not %.80s",
1066 Py_TYPE(pystr)->tp_name);
1067 return NULL;
1068 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001069 PyDict_Clear(s->memo);
1070 if (rval == NULL)
1071 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001072 return _build_rval_index_tuple(rval, next_idx);
1073}
1074
1075static PyObject *
1076scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1077{
1078 PyScannerObject *s;
1079 s = (PyScannerObject *)type->tp_alloc(type, 0);
1080 if (s != NULL) {
1081 s->strict = NULL;
1082 s->object_hook = NULL;
1083 s->object_pairs_hook = NULL;
1084 s->parse_float = NULL;
1085 s->parse_int = NULL;
1086 s->parse_constant = NULL;
1087 }
1088 return (PyObject *)s;
1089}
1090
1091static int
1092scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1093{
1094 /* Initialize Scanner object */
1095 PyObject *ctx;
1096 static char *kwlist[] = {"context", NULL};
1097 PyScannerObject *s;
1098
1099 assert(PyScanner_Check(self));
1100 s = (PyScannerObject *)self;
1101
1102 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1103 return -1;
1104
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001105 if (s->memo == NULL) {
1106 s->memo = PyDict_New();
1107 if (s->memo == NULL)
1108 goto bail;
1109 }
1110
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001111 /* All of these will fail "gracefully" so we don't need to verify them */
1112 s->strict = PyObject_GetAttrString(ctx, "strict");
1113 if (s->strict == NULL)
1114 goto bail;
1115 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1116 if (s->object_hook == NULL)
1117 goto bail;
1118 s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1119 if (s->object_pairs_hook == NULL)
1120 goto bail;
1121 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1122 if (s->parse_float == NULL)
1123 goto bail;
1124 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1125 if (s->parse_int == NULL)
1126 goto bail;
1127 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1128 if (s->parse_constant == NULL)
1129 goto bail;
1130
1131 return 0;
1132
1133bail:
1134 Py_CLEAR(s->strict);
1135 Py_CLEAR(s->object_hook);
1136 Py_CLEAR(s->object_pairs_hook);
1137 Py_CLEAR(s->parse_float);
1138 Py_CLEAR(s->parse_int);
1139 Py_CLEAR(s->parse_constant);
1140 return -1;
1141}
1142
1143PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1144
1145static
1146PyTypeObject PyScannerType = {
1147 PyVarObject_HEAD_INIT(NULL, 0)
1148 "_json.Scanner", /* tp_name */
1149 sizeof(PyScannerObject), /* tp_basicsize */
1150 0, /* tp_itemsize */
1151 scanner_dealloc, /* tp_dealloc */
1152 0, /* tp_print */
1153 0, /* tp_getattr */
1154 0, /* tp_setattr */
1155 0, /* tp_compare */
1156 0, /* tp_repr */
1157 0, /* tp_as_number */
1158 0, /* tp_as_sequence */
1159 0, /* tp_as_mapping */
1160 0, /* tp_hash */
1161 scanner_call, /* tp_call */
1162 0, /* tp_str */
1163 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1164 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1165 0, /* tp_as_buffer */
1166 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1167 scanner_doc, /* tp_doc */
1168 scanner_traverse, /* tp_traverse */
1169 scanner_clear, /* tp_clear */
1170 0, /* tp_richcompare */
1171 0, /* tp_weaklistoffset */
1172 0, /* tp_iter */
1173 0, /* tp_iternext */
1174 0, /* tp_methods */
1175 scanner_members, /* tp_members */
1176 0, /* tp_getset */
1177 0, /* tp_base */
1178 0, /* tp_dict */
1179 0, /* tp_descr_get */
1180 0, /* tp_descr_set */
1181 0, /* tp_dictoffset */
1182 scanner_init, /* tp_init */
1183 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1184 scanner_new, /* tp_new */
1185 0,/* PyObject_GC_Del, */ /* tp_free */
1186};
1187
1188static PyObject *
1189encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1190{
1191 PyEncoderObject *s;
1192 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1193 if (s != NULL) {
1194 s->markers = NULL;
1195 s->defaultfn = NULL;
1196 s->encoder = NULL;
1197 s->indent = NULL;
1198 s->key_separator = NULL;
1199 s->item_separator = NULL;
1200 s->sort_keys = NULL;
1201 s->skipkeys = NULL;
1202 }
1203 return (PyObject *)s;
1204}
1205
1206static int
1207encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1208{
1209 /* initialize Encoder object */
1210 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1211
1212 PyEncoderObject *s;
Antoine Pitrou781eba72009-12-08 15:57:31 +00001213 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1214 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001215
1216 assert(PyEncoder_Check(self));
1217 s = (PyEncoderObject *)self;
1218
1219 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
Antoine Pitrou781eba72009-12-08 15:57:31 +00001220 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1221 &sort_keys, &skipkeys, &allow_nan))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001222 return -1;
1223
Antoine Pitrou781eba72009-12-08 15:57:31 +00001224 s->markers = markers;
1225 s->defaultfn = defaultfn;
1226 s->encoder = encoder;
1227 s->indent = indent;
1228 s->key_separator = key_separator;
1229 s->item_separator = item_separator;
1230 s->sort_keys = sort_keys;
1231 s->skipkeys = skipkeys;
1232 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1233 s->allow_nan = PyObject_IsTrue(allow_nan);
1234
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001235 Py_INCREF(s->markers);
1236 Py_INCREF(s->defaultfn);
1237 Py_INCREF(s->encoder);
1238 Py_INCREF(s->indent);
1239 Py_INCREF(s->key_separator);
1240 Py_INCREF(s->item_separator);
1241 Py_INCREF(s->sort_keys);
1242 Py_INCREF(s->skipkeys);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001243 return 0;
1244}
1245
1246static PyObject *
1247encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1248{
1249 /* Python callable interface to encode_listencode_obj */
1250 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1251 PyObject *obj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001252 Py_ssize_t indent_level;
1253 PyEncoderObject *s;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001254 _PyAccu acc;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001255
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001256 assert(PyEncoder_Check(self));
1257 s = (PyEncoderObject *)self;
Antoine Pitroucbb02842012-12-01 19:34:16 +01001258 if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:_iterencode", kwlist,
1259 &obj, &indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001260 return NULL;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001261 if (_PyAccu_Init(&acc))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001262 return NULL;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001263 if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001264 _PyAccu_Destroy(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001265 return NULL;
1266 }
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001267 return _PyAccu_FinishAsList(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001268}
1269
1270static PyObject *
1271_encoded_const(PyObject *obj)
1272{
1273 /* Return the JSON string representation of None, True, False */
1274 if (obj == Py_None) {
1275 static PyObject *s_null = NULL;
1276 if (s_null == NULL) {
1277 s_null = PyUnicode_InternFromString("null");
1278 }
1279 Py_INCREF(s_null);
1280 return s_null;
1281 }
1282 else if (obj == Py_True) {
1283 static PyObject *s_true = NULL;
1284 if (s_true == NULL) {
1285 s_true = PyUnicode_InternFromString("true");
1286 }
1287 Py_INCREF(s_true);
1288 return s_true;
1289 }
1290 else if (obj == Py_False) {
1291 static PyObject *s_false = NULL;
1292 if (s_false == NULL) {
1293 s_false = PyUnicode_InternFromString("false");
1294 }
1295 Py_INCREF(s_false);
1296 return s_false;
1297 }
1298 else {
1299 PyErr_SetString(PyExc_ValueError, "not a const");
1300 return NULL;
1301 }
1302}
1303
1304static PyObject *
1305encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1306{
1307 /* Return the JSON representation of a PyFloat */
1308 double i = PyFloat_AS_DOUBLE(obj);
1309 if (!Py_IS_FINITE(i)) {
1310 if (!s->allow_nan) {
1311 PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant");
1312 return NULL;
1313 }
1314 if (i > 0) {
1315 return PyUnicode_FromString("Infinity");
1316 }
1317 else if (i < 0) {
1318 return PyUnicode_FromString("-Infinity");
1319 }
1320 else {
1321 return PyUnicode_FromString("NaN");
1322 }
1323 }
1324 /* Use a better float format here? */
1325 return PyObject_Repr(obj);
1326}
1327
1328static PyObject *
1329encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1330{
1331 /* Return the JSON representation of a string */
1332 if (s->fast_encode)
1333 return py_encode_basestring_ascii(NULL, obj);
1334 else
1335 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1336}
1337
1338static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001339_steal_accumulate(_PyAccu *acc, PyObject *stolen)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001340{
1341 /* Append stolen and then decrement its reference count */
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001342 int rval = _PyAccu_Accumulate(acc, stolen);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001343 Py_DECREF(stolen);
1344 return rval;
1345}
1346
1347static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001348encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001349 PyObject *obj, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001350{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001351 /* Encode Python object obj to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001352 PyObject *newobj;
1353 int rv;
1354
1355 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1356 PyObject *cstr = _encoded_const(obj);
1357 if (cstr == NULL)
1358 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001359 return _steal_accumulate(acc, cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001360 }
1361 else if (PyUnicode_Check(obj))
1362 {
1363 PyObject *encoded = encoder_encode_string(s, obj);
1364 if (encoded == NULL)
1365 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001366 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001367 }
1368 else if (PyLong_Check(obj)) {
1369 PyObject *encoded = PyObject_Str(obj);
1370 if (encoded == NULL)
1371 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001372 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001373 }
1374 else if (PyFloat_Check(obj)) {
1375 PyObject *encoded = encoder_encode_float(s, obj);
1376 if (encoded == NULL)
1377 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001378 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001379 }
1380 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001381 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1382 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001383 rv = encoder_listencode_list(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001384 Py_LeaveRecursiveCall();
1385 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001386 }
1387 else if (PyDict_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001388 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1389 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001390 rv = encoder_listencode_dict(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001391 Py_LeaveRecursiveCall();
1392 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001393 }
1394 else {
1395 PyObject *ident = NULL;
1396 if (s->markers != Py_None) {
1397 int has_key;
1398 ident = PyLong_FromVoidPtr(obj);
1399 if (ident == NULL)
1400 return -1;
1401 has_key = PyDict_Contains(s->markers, ident);
1402 if (has_key) {
1403 if (has_key != -1)
1404 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1405 Py_DECREF(ident);
1406 return -1;
1407 }
1408 if (PyDict_SetItem(s->markers, ident, obj)) {
1409 Py_DECREF(ident);
1410 return -1;
1411 }
1412 }
1413 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1414 if (newobj == NULL) {
1415 Py_XDECREF(ident);
1416 return -1;
1417 }
Ezio Melotti13672652011-05-11 01:02:56 +03001418
1419 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1420 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001421 rv = encoder_listencode_obj(s, acc, newobj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001422 Py_LeaveRecursiveCall();
1423
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001424 Py_DECREF(newobj);
1425 if (rv) {
1426 Py_XDECREF(ident);
1427 return -1;
1428 }
1429 if (ident != NULL) {
1430 if (PyDict_DelItem(s->markers, ident)) {
1431 Py_XDECREF(ident);
1432 return -1;
1433 }
1434 Py_XDECREF(ident);
1435 }
1436 return rv;
1437 }
1438}
1439
1440static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001441encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001442 PyObject *dct, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001443{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001444 /* Encode Python dict dct a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001445 static PyObject *open_dict = NULL;
1446 static PyObject *close_dict = NULL;
1447 static PyObject *empty_dict = NULL;
1448 PyObject *kstr = NULL;
1449 PyObject *ident = NULL;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001450 PyObject *it = NULL;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001451 PyObject *items;
1452 PyObject *item = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001453 int skipkeys;
1454 Py_ssize_t idx;
1455
1456 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
1457 open_dict = PyUnicode_InternFromString("{");
1458 close_dict = PyUnicode_InternFromString("}");
1459 empty_dict = PyUnicode_InternFromString("{}");
1460 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
1461 return -1;
1462 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001463 if (Py_SIZE(dct) == 0)
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001464 return _PyAccu_Accumulate(acc, empty_dict);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001465
1466 if (s->markers != Py_None) {
1467 int has_key;
1468 ident = PyLong_FromVoidPtr(dct);
1469 if (ident == NULL)
1470 goto bail;
1471 has_key = PyDict_Contains(s->markers, ident);
1472 if (has_key) {
1473 if (has_key != -1)
1474 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1475 goto bail;
1476 }
1477 if (PyDict_SetItem(s->markers, ident, dct)) {
1478 goto bail;
1479 }
1480 }
1481
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001482 if (_PyAccu_Accumulate(acc, open_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001483 goto bail;
1484
1485 if (s->indent != Py_None) {
1486 /* TODO: DOES NOT RUN */
1487 indent_level += 1;
1488 /*
1489 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1490 separator = _item_separator + newline_indent
1491 buf += newline_indent
1492 */
1493 }
1494
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001495 if (PyObject_IsTrue(s->sort_keys)) {
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001496 /* First sort the keys then replace them with (key, value) tuples. */
1497 Py_ssize_t i, nitems;
1498 items = PyMapping_Keys(dct);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 if (items == NULL)
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001500 goto bail;
1501 if (!PyList_Check(items)) {
1502 PyErr_SetString(PyExc_ValueError, "keys must return list");
1503 goto bail;
1504 }
1505 if (PyList_Sort(items) < 0)
1506 goto bail;
1507 nitems = PyList_GET_SIZE(items);
1508 for (i = 0; i < nitems; i++) {
1509 PyObject *key, *value;
1510 key = PyList_GET_ITEM(items, i);
1511 value = PyDict_GetItem(dct, key);
1512 item = PyTuple_Pack(2, key, value);
1513 if (item == NULL)
1514 goto bail;
1515 PyList_SET_ITEM(items, i, item);
1516 Py_DECREF(key);
1517 }
1518 }
1519 else {
1520 items = PyMapping_Items(dct);
1521 }
1522 if (items == NULL)
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001523 goto bail;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001524 it = PyObject_GetIter(items);
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001525 Py_DECREF(items);
1526 if (it == NULL)
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001527 goto bail;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001528 skipkeys = PyObject_IsTrue(s->skipkeys);
1529 idx = 0;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001530 while ((item = PyIter_Next(it)) != NULL) {
1531 PyObject *encoded, *key, *value;
1532 if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
1533 PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
1534 goto bail;
1535 }
1536 key = PyTuple_GET_ITEM(item, 0);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001537 if (PyUnicode_Check(key)) {
1538 Py_INCREF(key);
1539 kstr = key;
1540 }
1541 else if (PyFloat_Check(key)) {
1542 kstr = encoder_encode_float(s, key);
1543 if (kstr == NULL)
1544 goto bail;
1545 }
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001546 else if (key == Py_True || key == Py_False || key == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 /* This must come before the PyLong_Check because
1548 True and False are also 1 and 0.*/
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001549 kstr = _encoded_const(key);
1550 if (kstr == NULL)
1551 goto bail;
1552 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001553 else if (PyLong_Check(key)) {
1554 kstr = PyObject_Str(key);
1555 if (kstr == NULL)
1556 goto bail;
1557 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001558 else if (skipkeys) {
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001559 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001560 continue;
1561 }
1562 else {
1563 /* TODO: include repr of key */
Doug Hellmann1c524752010-07-21 12:29:04 +00001564 PyErr_SetString(PyExc_TypeError, "keys must be a string");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001565 goto bail;
1566 }
1567
1568 if (idx) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001569 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001570 goto bail;
1571 }
1572
1573 encoded = encoder_encode_string(s, kstr);
1574 Py_CLEAR(kstr);
1575 if (encoded == NULL)
1576 goto bail;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001577 if (_PyAccu_Accumulate(acc, encoded)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001578 Py_DECREF(encoded);
1579 goto bail;
1580 }
1581 Py_DECREF(encoded);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001582 if (_PyAccu_Accumulate(acc, s->key_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001583 goto bail;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001584
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001585 value = PyTuple_GET_ITEM(item, 1);
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001586 if (encoder_listencode_obj(s, acc, value, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001587 goto bail;
1588 idx += 1;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001589 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001590 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001591 if (PyErr_Occurred())
1592 goto bail;
1593 Py_CLEAR(it);
1594
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001595 if (ident != NULL) {
1596 if (PyDict_DelItem(s->markers, ident))
1597 goto bail;
1598 Py_CLEAR(ident);
1599 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001600 /* TODO DOES NOT RUN; dead code
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001601 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001602 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001603
1604 yield '\n' + (' ' * (_indent * _current_indent_level))
1605 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001606 if (_PyAccu_Accumulate(acc, close_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001607 goto bail;
1608 return 0;
1609
1610bail:
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001611 Py_XDECREF(it);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001612 Py_XDECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001613 Py_XDECREF(kstr);
1614 Py_XDECREF(ident);
1615 return -1;
1616}
1617
1618
1619static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001620encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001621 PyObject *seq, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001622{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001623 /* Encode Python list seq to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001624 static PyObject *open_array = NULL;
1625 static PyObject *close_array = NULL;
1626 static PyObject *empty_array = NULL;
1627 PyObject *ident = NULL;
1628 PyObject *s_fast = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001629 Py_ssize_t i;
1630
1631 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
1632 open_array = PyUnicode_InternFromString("[");
1633 close_array = PyUnicode_InternFromString("]");
1634 empty_array = PyUnicode_InternFromString("[]");
1635 if (open_array == NULL || close_array == NULL || empty_array == NULL)
1636 return -1;
1637 }
1638 ident = NULL;
1639 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
1640 if (s_fast == NULL)
1641 return -1;
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001642 if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001643 Py_DECREF(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001644 return _PyAccu_Accumulate(acc, empty_array);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001645 }
1646
1647 if (s->markers != Py_None) {
1648 int has_key;
1649 ident = PyLong_FromVoidPtr(seq);
1650 if (ident == NULL)
1651 goto bail;
1652 has_key = PyDict_Contains(s->markers, ident);
1653 if (has_key) {
1654 if (has_key != -1)
1655 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1656 goto bail;
1657 }
1658 if (PyDict_SetItem(s->markers, ident, seq)) {
1659 goto bail;
1660 }
1661 }
1662
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001663 if (_PyAccu_Accumulate(acc, open_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001664 goto bail;
1665 if (s->indent != Py_None) {
1666 /* TODO: DOES NOT RUN */
1667 indent_level += 1;
1668 /*
1669 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1670 separator = _item_separator + newline_indent
1671 buf += newline_indent
1672 */
1673 }
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001674 for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
1675 PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001676 if (i) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001677 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001678 goto bail;
1679 }
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001680 if (encoder_listencode_obj(s, acc, obj, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001681 goto bail;
1682 }
1683 if (ident != NULL) {
1684 if (PyDict_DelItem(s->markers, ident))
1685 goto bail;
1686 Py_CLEAR(ident);
1687 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001688
1689 /* TODO: DOES NOT RUN
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001690 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001691 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001692
1693 yield '\n' + (' ' * (_indent * _current_indent_level))
1694 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001695 if (_PyAccu_Accumulate(acc, close_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001696 goto bail;
1697 Py_DECREF(s_fast);
1698 return 0;
1699
1700bail:
1701 Py_XDECREF(ident);
1702 Py_DECREF(s_fast);
1703 return -1;
1704}
1705
1706static void
1707encoder_dealloc(PyObject *self)
1708{
1709 /* Deallocate Encoder */
1710 encoder_clear(self);
1711 Py_TYPE(self)->tp_free(self);
1712}
1713
1714static int
1715encoder_traverse(PyObject *self, visitproc visit, void *arg)
1716{
1717 PyEncoderObject *s;
1718 assert(PyEncoder_Check(self));
1719 s = (PyEncoderObject *)self;
1720 Py_VISIT(s->markers);
1721 Py_VISIT(s->defaultfn);
1722 Py_VISIT(s->encoder);
1723 Py_VISIT(s->indent);
1724 Py_VISIT(s->key_separator);
1725 Py_VISIT(s->item_separator);
1726 Py_VISIT(s->sort_keys);
1727 Py_VISIT(s->skipkeys);
1728 return 0;
1729}
1730
1731static int
1732encoder_clear(PyObject *self)
1733{
1734 /* Deallocate Encoder */
1735 PyEncoderObject *s;
1736 assert(PyEncoder_Check(self));
1737 s = (PyEncoderObject *)self;
1738 Py_CLEAR(s->markers);
1739 Py_CLEAR(s->defaultfn);
1740 Py_CLEAR(s->encoder);
1741 Py_CLEAR(s->indent);
1742 Py_CLEAR(s->key_separator);
1743 Py_CLEAR(s->item_separator);
1744 Py_CLEAR(s->sort_keys);
1745 Py_CLEAR(s->skipkeys);
1746 return 0;
1747}
1748
1749PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
1750
1751static
1752PyTypeObject PyEncoderType = {
1753 PyVarObject_HEAD_INIT(NULL, 0)
1754 "_json.Encoder", /* tp_name */
1755 sizeof(PyEncoderObject), /* tp_basicsize */
1756 0, /* tp_itemsize */
1757 encoder_dealloc, /* tp_dealloc */
1758 0, /* tp_print */
1759 0, /* tp_getattr */
1760 0, /* tp_setattr */
1761 0, /* tp_compare */
1762 0, /* tp_repr */
1763 0, /* tp_as_number */
1764 0, /* tp_as_sequence */
1765 0, /* tp_as_mapping */
1766 0, /* tp_hash */
1767 encoder_call, /* tp_call */
1768 0, /* tp_str */
1769 0, /* tp_getattro */
1770 0, /* tp_setattro */
1771 0, /* tp_as_buffer */
1772 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1773 encoder_doc, /* tp_doc */
1774 encoder_traverse, /* tp_traverse */
1775 encoder_clear, /* tp_clear */
1776 0, /* tp_richcompare */
1777 0, /* tp_weaklistoffset */
1778 0, /* tp_iter */
1779 0, /* tp_iternext */
1780 0, /* tp_methods */
1781 encoder_members, /* tp_members */
1782 0, /* tp_getset */
1783 0, /* tp_base */
1784 0, /* tp_dict */
1785 0, /* tp_descr_get */
1786 0, /* tp_descr_set */
1787 0, /* tp_dictoffset */
1788 encoder_init, /* tp_init */
1789 0, /* tp_alloc */
1790 encoder_new, /* tp_new */
1791 0, /* tp_free */
1792};
1793
1794static PyMethodDef speedups_methods[] = {
1795 {"encode_basestring_ascii",
1796 (PyCFunction)py_encode_basestring_ascii,
1797 METH_O,
1798 pydoc_encode_basestring_ascii},
1799 {"scanstring",
1800 (PyCFunction)py_scanstring,
1801 METH_VARARGS,
1802 pydoc_scanstring},
Christian Heimes90540002008-05-08 14:29:10 +00001803 {NULL, NULL, 0, NULL}
1804};
1805
1806PyDoc_STRVAR(module_doc,
1807"json speedups\n");
1808
Martin v. Löwis1a214512008-06-11 05:26:20 +00001809static struct PyModuleDef jsonmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 PyModuleDef_HEAD_INIT,
1811 "_json",
1812 module_doc,
1813 -1,
1814 speedups_methods,
1815 NULL,
1816 NULL,
1817 NULL,
1818 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001819};
1820
1821PyObject*
1822PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +00001823{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001824 PyObject *m = PyModule_Create(&jsonmodule);
1825 if (!m)
1826 return NULL;
1827 PyScannerType.tp_new = PyType_GenericNew;
1828 if (PyType_Ready(&PyScannerType) < 0)
1829 goto fail;
1830 PyEncoderType.tp_new = PyType_GenericNew;
1831 if (PyType_Ready(&PyEncoderType) < 0)
1832 goto fail;
1833 Py_INCREF((PyObject*)&PyScannerType);
1834 if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1835 Py_DECREF((PyObject*)&PyScannerType);
1836 goto fail;
1837 }
1838 Py_INCREF((PyObject*)&PyEncoderType);
1839 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1840 Py_DECREF((PyObject*)&PyEncoderType);
1841 goto fail;
1842 }
1843 return m;
1844 fail:
1845 Py_DECREF(m);
1846 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +00001847}