blob: 40c2ced5028f6702900419fd7d7e8161bea5a30e [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#if PY_VERSION_HEX < 0x02060000 && !defined(Py_TYPE)
6#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
7#endif
8#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
9typedef int Py_ssize_t;
10#define PY_SSIZE_T_MAX INT_MAX
11#define PY_SSIZE_T_MIN INT_MIN
12#define PyInt_FromSsize_t PyInt_FromLong
13#define PyInt_AsSsize_t PyInt_AsLong
14#endif
15#ifndef Py_IS_FINITE
16#define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X))
17#endif
Christian Heimes90540002008-05-08 14:29:10 +000018
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000019#ifdef __GNUC__
20#define UNUSED __attribute__((__unused__))
21#else
22#define UNUSED
23#endif
24
25#define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType)
26#define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType)
27#define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType)
28#define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType)
29
30static PyTypeObject PyScannerType;
31static PyTypeObject PyEncoderType;
32
33typedef struct _PyScannerObject {
34 PyObject_HEAD
35 PyObject *strict;
36 PyObject *object_hook;
37 PyObject *object_pairs_hook;
38 PyObject *parse_float;
39 PyObject *parse_int;
40 PyObject *parse_constant;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +000041 PyObject *memo;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000042} PyScannerObject;
43
44static PyMemberDef scanner_members[] = {
45 {"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"},
46 {"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"},
47 {"object_pairs_hook", T_OBJECT, offsetof(PyScannerObject, object_pairs_hook), READONLY},
48 {"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"},
49 {"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "parse_int"},
50 {"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READONLY, "parse_constant"},
51 {NULL}
52};
53
54typedef struct _PyEncoderObject {
55 PyObject_HEAD
56 PyObject *markers;
57 PyObject *defaultfn;
58 PyObject *encoder;
59 PyObject *indent;
60 PyObject *key_separator;
61 PyObject *item_separator;
62 PyObject *sort_keys;
63 PyObject *skipkeys;
64 int fast_encode;
65 int allow_nan;
66} PyEncoderObject;
67
68static PyMemberDef encoder_members[] = {
69 {"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers"},
70 {"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "default"},
71 {"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder"},
72 {"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"},
73 {"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READONLY, "key_separator"},
74 {"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READONLY, "item_separator"},
75 {"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sort_keys"},
76 {"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipkeys"},
77 {NULL}
78};
79
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +020080static PyObject *
81join_list_unicode(PyObject *lst)
82{
83 /* return u''.join(lst) */
84 static PyObject *sep = NULL;
85 if (sep == NULL) {
86 sep = PyUnicode_FromStringAndSize("", 0);
87 if (sep == NULL)
88 return NULL;
89 }
90 return PyUnicode_Join(sep, lst);
91}
92
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +020093/* Forward decls */
94
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000095static PyObject *
96ascii_escape_unicode(PyObject *pystr);
97static PyObject *
98py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr);
99void init_json(void);
100static PyObject *
101scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr);
102static PyObject *
103_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx);
104static PyObject *
105scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
106static int
107scanner_init(PyObject *self, PyObject *args, PyObject *kwds);
108static void
109scanner_dealloc(PyObject *self);
110static int
111scanner_clear(PyObject *self);
112static PyObject *
113encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
114static int
115encoder_init(PyObject *self, PyObject *args, PyObject *kwds);
116static void
117encoder_dealloc(PyObject *self);
118static int
119encoder_clear(PyObject *self);
120static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200121encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc, PyObject *seq, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000122static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200123encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, PyObject *obj, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000124static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200125encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, PyObject *dct, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000126static PyObject *
Hirokazu Yamamotofecf5d12009-05-02 15:55:19 +0000127_encoded_const(PyObject *obj);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000128static void
129raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
130static PyObject *
131encoder_encode_string(PyEncoderObject *s, PyObject *obj);
132static int
133_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr);
134static PyObject *
135_convertPyInt_FromSsize_t(Py_ssize_t *size_ptr);
136static PyObject *
137encoder_encode_float(PyEncoderObject *s, PyObject *obj);
138
Christian Heimes90540002008-05-08 14:29:10 +0000139#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000140#define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
Christian Heimes90540002008-05-08 14:29:10 +0000141
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000142static int
143_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000144{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000145 /* PyObject to Py_ssize_t converter */
146 *size_ptr = PyLong_AsSsize_t(o);
Georg Brandl59682052009-05-05 07:52:05 +0000147 if (*size_ptr == -1 && PyErr_Occurred())
148 return 0;
149 return 1;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000150}
151
152static PyObject *
153_convertPyInt_FromSsize_t(Py_ssize_t *size_ptr)
154{
155 /* Py_ssize_t to PyObject converter */
156 return PyLong_FromSsize_t(*size_ptr);
157}
158
159static Py_ssize_t
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200160ascii_escape_unichar(Py_UCS4 c, unsigned char *output, Py_ssize_t chars)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000161{
162 /* Escape unicode code point c to ASCII escape sequences
163 in char *output. output must have at least 12 bytes unused to
164 accommodate an escaped surrogate pair "\uXXXX\uXXXX" */
Christian Heimes90540002008-05-08 14:29:10 +0000165 output[chars++] = '\\';
166 switch (c) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000167 case '\\': output[chars++] = c; break;
168 case '"': output[chars++] = c; break;
Christian Heimes90540002008-05-08 14:29:10 +0000169 case '\b': output[chars++] = 'b'; break;
170 case '\f': output[chars++] = 'f'; break;
171 case '\n': output[chars++] = 'n'; break;
172 case '\r': output[chars++] = 'r'; break;
173 case '\t': output[chars++] = 't'; break;
174 default:
Christian Heimes90540002008-05-08 14:29:10 +0000175 if (c >= 0x10000) {
176 /* UTF-16 surrogate pair */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200177 Py_UCS4 v = c - 0x10000;
Christian Heimes90540002008-05-08 14:29:10 +0000178 c = 0xd800 | ((v >> 10) & 0x3ff);
179 output[chars++] = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200180 output[chars++] = Py_hexdigits[(c >> 12) & 0xf];
181 output[chars++] = Py_hexdigits[(c >> 8) & 0xf];
182 output[chars++] = Py_hexdigits[(c >> 4) & 0xf];
183 output[chars++] = Py_hexdigits[(c ) & 0xf];
Christian Heimes90540002008-05-08 14:29:10 +0000184 c = 0xdc00 | (v & 0x3ff);
185 output[chars++] = '\\';
186 }
Christian Heimes90540002008-05-08 14:29:10 +0000187 output[chars++] = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200188 output[chars++] = Py_hexdigits[(c >> 12) & 0xf];
189 output[chars++] = Py_hexdigits[(c >> 8) & 0xf];
190 output[chars++] = Py_hexdigits[(c >> 4) & 0xf];
191 output[chars++] = Py_hexdigits[(c ) & 0xf];
Christian Heimes90540002008-05-08 14:29:10 +0000192 }
193 return chars;
194}
195
196static PyObject *
197ascii_escape_unicode(PyObject *pystr)
198{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000199 /* Take a PyUnicode pystr and return a new ASCII-only escaped PyUnicode */
Christian Heimes90540002008-05-08 14:29:10 +0000200 Py_ssize_t i;
201 Py_ssize_t input_chars;
202 Py_ssize_t output_size;
203 Py_ssize_t chars;
204 PyObject *rval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200205 void *input;
206 unsigned char *output;
207 int kind;
Christian Heimes90540002008-05-08 14:29:10 +0000208
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200209 if (PyUnicode_READY(pystr) == -1)
210 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000211
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200212 input_chars = PyUnicode_GET_LENGTH(pystr);
213 input = PyUnicode_DATA(pystr);
214 kind = PyUnicode_KIND(pystr);
215
216 /* Compute the output size */
217 for (i = 0, output_size = 2; i < input_chars; i++) {
218 Py_UCS4 c = PyUnicode_READ(kind, input, i);
219 if (S_CHAR(c))
220 output_size++;
221 else {
222 switch(c) {
Victor Stinnerd9c06312011-10-11 21:56:19 +0200223 case '\\': case '"': case '\b': case '\f':
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200224 case '\n': case '\r': case '\t':
225 output_size += 2; break;
226 default:
227 output_size += c >= 0x10000 ? 12 : 6;
228 }
229 }
230 }
231
232 rval = PyUnicode_New(output_size, 127);
Christian Heimes90540002008-05-08 14:29:10 +0000233 if (rval == NULL) {
234 return NULL;
235 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200236 output = PyUnicode_1BYTE_DATA(rval);
Christian Heimes90540002008-05-08 14:29:10 +0000237 chars = 0;
238 output[chars++] = '"';
239 for (i = 0; i < input_chars; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200240 Py_UCS4 c = PyUnicode_READ(kind, input, i);
Christian Heimes90540002008-05-08 14:29:10 +0000241 if (S_CHAR(c)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000242 output[chars++] = c;
Christian Heimes90540002008-05-08 14:29:10 +0000243 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000244 else {
245 chars = ascii_escape_unichar(c, output, chars);
Christian Heimes90540002008-05-08 14:29:10 +0000246 }
Christian Heimes90540002008-05-08 14:29:10 +0000247 }
248 output[chars++] = '"';
Victor Stinner8f825062012-04-27 13:55:39 +0200249 assert(_PyUnicode_CheckConsistency(rval, 1));
Christian Heimes90540002008-05-08 14:29:10 +0000250 return rval;
251}
252
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000253static void
Christian Heimes90540002008-05-08 14:29:10 +0000254raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
255{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000256 /* Use the Python function json.decoder.errmsg to raise a nice
257 looking ValueError exception */
Christian Heimes90540002008-05-08 14:29:10 +0000258 static PyObject *errmsg_fn = NULL;
259 PyObject *pymsg;
260 if (errmsg_fn == NULL) {
261 PyObject *decoder = PyImport_ImportModule("json.decoder");
262 if (decoder == NULL)
263 return;
264 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000265 Py_DECREF(decoder);
Christian Heimes90540002008-05-08 14:29:10 +0000266 if (errmsg_fn == NULL)
267 return;
Christian Heimes90540002008-05-08 14:29:10 +0000268 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000269 pymsg = PyObject_CallFunction(errmsg_fn, "(zOO&)", msg, s, _convertPyInt_FromSsize_t, &end);
Benjamin Petersona13d4752008-10-16 21:17:24 +0000270 if (pymsg) {
271 PyErr_SetObject(PyExc_ValueError, pymsg);
272 Py_DECREF(pymsg);
273 }
Christian Heimes90540002008-05-08 14:29:10 +0000274}
275
276static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000277_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
278 /* return (rval, idx) tuple, stealing reference to rval */
279 PyObject *tpl;
280 PyObject *pyidx;
281 /*
282 steal a reference to rval, returns (rval, idx)
283 */
284 if (rval == NULL) {
Christian Heimes90540002008-05-08 14:29:10 +0000285 return NULL;
286 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000287 pyidx = PyLong_FromSsize_t(idx);
288 if (pyidx == NULL) {
289 Py_DECREF(rval);
290 return NULL;
291 }
292 tpl = PyTuple_New(2);
293 if (tpl == NULL) {
294 Py_DECREF(pyidx);
295 Py_DECREF(rval);
296 return NULL;
297 }
298 PyTuple_SET_ITEM(tpl, 0, rval);
299 PyTuple_SET_ITEM(tpl, 1, pyidx);
300 return tpl;
Christian Heimes90540002008-05-08 14:29:10 +0000301}
302
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000303#define APPEND_OLD_CHUNK \
304 if (chunk != NULL) { \
305 if (chunks == NULL) { \
306 chunks = PyList_New(0); \
307 if (chunks == NULL) { \
308 goto bail; \
309 } \
310 } \
311 if (PyList_Append(chunks, chunk)) { \
312 Py_DECREF(chunk); \
313 goto bail; \
314 } \
315 Py_CLEAR(chunk); \
316 }
317
Christian Heimes90540002008-05-08 14:29:10 +0000318static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000319scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000320{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000321 /* Read the JSON string from PyUnicode pystr.
322 end is the index of the first character after the quote.
323 if strict is zero then literal control characters are allowed
324 *next_end_ptr is a return-by-reference index of the character
325 after the end quote
Christian Heimes90540002008-05-08 14:29:10 +0000326
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000327 Return value is a new PyUnicode
328 */
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000329 PyObject *rval = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200330 Py_ssize_t len;
Christian Heimes90540002008-05-08 14:29:10 +0000331 Py_ssize_t begin = end - 1;
Brett Cannonb94767f2011-02-22 20:15:44 +0000332 Py_ssize_t next /* = begin */;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200333 const void *buf;
334 int kind;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000335 PyObject *chunks = NULL;
336 PyObject *chunk = NULL;
337
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200338 if (PyUnicode_READY(pystr) == -1)
339 return 0;
340
341 len = PyUnicode_GET_LENGTH(pystr);
342 buf = PyUnicode_DATA(pystr);
343 kind = PyUnicode_KIND(pystr);
344
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000345 if (end < 0 || len <= end) {
346 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
347 goto bail;
348 }
Christian Heimes90540002008-05-08 14:29:10 +0000349 while (1) {
350 /* Find the end of the string or the next escape */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200351 Py_UCS4 c = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000352 for (next = end; next < len; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200353 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000354 if (c == '"' || c == '\\') {
355 break;
356 }
357 else if (strict && c <= 0x1f) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000358 raise_errmsg("Invalid control character at", pystr, next);
Christian Heimes90540002008-05-08 14:29:10 +0000359 goto bail;
360 }
361 }
362 if (!(c == '"' || c == '\\')) {
363 raise_errmsg("Unterminated string starting at", pystr, begin);
364 goto bail;
365 }
366 /* Pick up this chunk if it's not zero length */
367 if (next != end) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000368 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200369 chunk = PyUnicode_FromKindAndData(
370 kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200371 (char*)buf + kind * end,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200372 next - end);
Christian Heimes90540002008-05-08 14:29:10 +0000373 if (chunk == NULL) {
374 goto bail;
375 }
Christian Heimes90540002008-05-08 14:29:10 +0000376 }
377 next++;
378 if (c == '"') {
379 end = next;
380 break;
381 }
382 if (next == len) {
383 raise_errmsg("Unterminated string starting at", pystr, begin);
384 goto bail;
385 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200386 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000387 if (c != 'u') {
388 /* Non-unicode backslash escapes */
389 end = next + 1;
390 switch (c) {
391 case '"': break;
392 case '\\': break;
393 case '/': break;
394 case 'b': c = '\b'; break;
395 case 'f': c = '\f'; break;
396 case 'n': c = '\n'; break;
397 case 'r': c = '\r'; break;
398 case 't': c = '\t'; break;
399 default: c = 0;
400 }
401 if (c == 0) {
402 raise_errmsg("Invalid \\escape", pystr, end - 2);
403 goto bail;
404 }
405 }
406 else {
407 c = 0;
408 next++;
409 end = next + 4;
410 if (end >= len) {
411 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
412 goto bail;
413 }
414 /* Decode 4 hex digits */
415 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200416 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000417 c <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000418 switch (digit) {
419 case '0': case '1': case '2': case '3': case '4':
420 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000421 c |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000422 case 'a': case 'b': case 'c': case 'd': case 'e':
423 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000424 c |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000425 case 'A': case 'B': case 'C': case 'D': case 'E':
426 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000427 c |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000428 default:
429 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
430 goto bail;
431 }
432 }
Christian Heimes90540002008-05-08 14:29:10 +0000433 /* Surrogate pair */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000434 if ((c & 0xfc00) == 0xd800) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200435 Py_UCS4 c2 = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000436 if (end + 6 >= len) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000437 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
438 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000439 }
Victor Stinnerd9c06312011-10-11 21:56:19 +0200440 if (PyUnicode_READ(kind, buf, next++) != '\\' ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200441 PyUnicode_READ(kind, buf, next++) != 'u') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000442 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
443 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000444 }
445 end += 6;
446 /* Decode 4 hex digits */
447 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200448 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Antoine Pitrou5b0e9e82010-10-09 15:24:28 +0000449 c2 <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000450 switch (digit) {
451 case '0': case '1': case '2': case '3': case '4':
452 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000453 c2 |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000454 case 'a': case 'b': case 'c': case 'd': case 'e':
455 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000456 c2 |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000457 case 'A': case 'B': case 'C': case 'D': case 'E':
458 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000459 c2 |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000460 default:
461 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
462 goto bail;
463 }
464 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000465 if ((c2 & 0xfc00) != 0xdc00) {
466 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
467 goto bail;
468 }
Christian Heimes90540002008-05-08 14:29:10 +0000469 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
470 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000471 else if ((c & 0xfc00) == 0xdc00) {
472 raise_errmsg("Unpaired low surrogate", pystr, end - 5);
473 goto bail;
474 }
Christian Heimes90540002008-05-08 14:29:10 +0000475 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000476 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200477 chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1);
Christian Heimes90540002008-05-08 14:29:10 +0000478 if (chunk == NULL) {
479 goto bail;
480 }
Christian Heimes90540002008-05-08 14:29:10 +0000481 }
482
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000483 if (chunks == NULL) {
484 if (chunk != NULL)
485 rval = chunk;
486 else
487 rval = PyUnicode_FromStringAndSize("", 0);
Christian Heimes90540002008-05-08 14:29:10 +0000488 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000489 else {
490 APPEND_OLD_CHUNK
491 rval = join_list_unicode(chunks);
492 if (rval == NULL) {
493 goto bail;
494 }
495 Py_CLEAR(chunks);
496 }
497
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000498 *next_end_ptr = end;
499 return rval;
Christian Heimes90540002008-05-08 14:29:10 +0000500bail:
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000501 *next_end_ptr = -1;
Christian Heimes90540002008-05-08 14:29:10 +0000502 Py_XDECREF(chunks);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000503 Py_XDECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000504 return NULL;
505}
506
507PyDoc_STRVAR(pydoc_scanstring,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000508 "scanstring(string, end, strict=True) -> (string, end)\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000509 "\n"
510 "Scan the string s for a JSON string. End is the index of the\n"
511 "character in s after the quote that started the JSON string.\n"
512 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
513 "on attempt to decode an invalid string. If strict is False then literal\n"
514 "control characters are allowed in the string.\n"
515 "\n"
516 "Returns a tuple of the decoded string and the index of the character in s\n"
517 "after the end quote."
518);
Christian Heimes90540002008-05-08 14:29:10 +0000519
520static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000521py_scanstring(PyObject* self UNUSED, PyObject *args)
Christian Heimes90540002008-05-08 14:29:10 +0000522{
523 PyObject *pystr;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000524 PyObject *rval;
Christian Heimes90540002008-05-08 14:29:10 +0000525 Py_ssize_t end;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000526 Py_ssize_t next_end = -1;
527 int strict = 1;
528 if (!PyArg_ParseTuple(args, "OO&|i:scanstring", &pystr, _convertPyInt_AsSsize_t, &end, &strict)) {
Christian Heimes90540002008-05-08 14:29:10 +0000529 return NULL;
530 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000531 if (PyUnicode_Check(pystr)) {
532 rval = scanstring_unicode(pystr, end, strict, &next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000533 }
534 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 PyErr_Format(PyExc_TypeError,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000536 "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 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000540 return _build_rval_index_tuple(rval, next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000541}
542
543PyDoc_STRVAR(pydoc_encode_basestring_ascii,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000544 "encode_basestring_ascii(string) -> string\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000545 "\n"
546 "Return an ASCII-only JSON representation of a Python string"
547);
Christian Heimes90540002008-05-08 14:29:10 +0000548
549static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000550py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
Christian Heimes90540002008-05-08 14:29:10 +0000551{
552 PyObject *rval;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000553 /* Return an ASCII-only JSON representation of a Python string */
Christian Heimes90540002008-05-08 14:29:10 +0000554 /* METH_O */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000555 if (PyUnicode_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000556 rval = ascii_escape_unicode(pystr);
557 }
558 else {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000559 PyErr_Format(PyExc_TypeError,
560 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000561 Py_TYPE(pystr)->tp_name);
562 return NULL;
563 }
Christian Heimes90540002008-05-08 14:29:10 +0000564 return rval;
565}
566
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000567static void
568scanner_dealloc(PyObject *self)
569{
570 /* Deallocate scanner object */
571 scanner_clear(self);
572 Py_TYPE(self)->tp_free(self);
573}
574
575static int
576scanner_traverse(PyObject *self, visitproc visit, void *arg)
577{
578 PyScannerObject *s;
579 assert(PyScanner_Check(self));
580 s = (PyScannerObject *)self;
581 Py_VISIT(s->strict);
582 Py_VISIT(s->object_hook);
583 Py_VISIT(s->object_pairs_hook);
584 Py_VISIT(s->parse_float);
585 Py_VISIT(s->parse_int);
586 Py_VISIT(s->parse_constant);
587 return 0;
588}
589
590static int
591scanner_clear(PyObject *self)
592{
593 PyScannerObject *s;
594 assert(PyScanner_Check(self));
595 s = (PyScannerObject *)self;
596 Py_CLEAR(s->strict);
597 Py_CLEAR(s->object_hook);
598 Py_CLEAR(s->object_pairs_hook);
599 Py_CLEAR(s->parse_float);
600 Py_CLEAR(s->parse_int);
601 Py_CLEAR(s->parse_constant);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000602 Py_CLEAR(s->memo);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000603 return 0;
604}
605
606static PyObject *
607_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
608 /* Read a JSON object from PyUnicode pystr.
609 idx is the index of the first character after the opening curly brace.
610 *next_idx_ptr is a return-by-reference index to the first character after
611 the closing curly brace.
612
613 Returns a new PyObject (usually a dict, but object_hook can change that)
614 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200615 void *str;
616 int kind;
617 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000618 PyObject *val = NULL;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000619 PyObject *rval = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000620 PyObject *key = NULL;
621 int strict = PyObject_IsTrue(s->strict);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000622 int has_pairs_hook = (s->object_pairs_hook != Py_None);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000623 Py_ssize_t next_idx;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000624
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200625 if (PyUnicode_READY(pystr) == -1)
626 return NULL;
627
628 str = PyUnicode_DATA(pystr);
629 kind = PyUnicode_KIND(pystr);
630 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
631
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000632 if (has_pairs_hook)
633 rval = PyList_New(0);
634 else
635 rval = PyDict_New();
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000636 if (rval == NULL)
637 return NULL;
638
639 /* skip whitespace after { */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200640 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000641
642 /* only loop if the object is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200643 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000644 while (idx <= end_idx) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000645 PyObject *memokey;
646
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000647 /* read key */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200648 if (PyUnicode_READ(kind, str, idx) != '"') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000649 raise_errmsg("Expecting property name", pystr, idx);
650 goto bail;
651 }
652 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
653 if (key == NULL)
654 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000655 memokey = PyDict_GetItem(s->memo, key);
656 if (memokey != NULL) {
657 Py_INCREF(memokey);
658 Py_DECREF(key);
659 key = memokey;
660 }
661 else {
662 if (PyDict_SetItem(s->memo, key, key) < 0)
663 goto bail;
664 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000665 idx = next_idx;
666
667 /* skip whitespace between key and : delimiter, read :, skip whitespace */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200668 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
669 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000670 raise_errmsg("Expecting : delimiter", pystr, idx);
671 goto bail;
672 }
673 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200674 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000675
676 /* read any JSON term */
677 val = scan_once_unicode(s, pystr, idx, &next_idx);
678 if (val == NULL)
679 goto bail;
680
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000681 if (has_pairs_hook) {
682 PyObject *item = PyTuple_Pack(2, key, val);
683 if (item == NULL)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000684 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000685 Py_CLEAR(key);
686 Py_CLEAR(val);
687 if (PyList_Append(rval, item) == -1) {
688 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000689 goto bail;
690 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000691 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000692 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000693 else {
694 if (PyDict_SetItem(rval, key, val) < 0)
695 goto bail;
696 Py_CLEAR(key);
697 Py_CLEAR(val);
698 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000699 idx = next_idx;
700
701 /* skip whitespace before } or , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200702 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000703
704 /* bail if the object is closed or we didn't get the , delimiter */
705 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200706 if (PyUnicode_READ(kind, str, idx) == '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000707 break;
708 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200709 else if (PyUnicode_READ(kind, str, idx) != ',') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000710 raise_errmsg("Expecting , delimiter", pystr, idx);
711 goto bail;
712 }
713 idx++;
714
715 /* skip whitespace after , delimiter */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200716 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000717 }
718 }
719
720 /* verify that idx < end_idx, str[idx] should be '}' */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200721 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000722 raise_errmsg("Expecting object", pystr, end_idx);
723 goto bail;
724 }
725
726 *next_idx_ptr = idx + 1;
727
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000728 if (has_pairs_hook) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000729 val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000730 Py_DECREF(rval);
731 return val;
732 }
733
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000734 /* if object_hook is not None: rval = object_hook(rval) */
735 if (s->object_hook != Py_None) {
736 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000737 Py_DECREF(rval);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000738 return val;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000739 }
740 return rval;
741bail:
742 Py_XDECREF(key);
743 Py_XDECREF(val);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000744 Py_XDECREF(rval);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000745 return NULL;
746}
747
748static PyObject *
749_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
750 /* Read a JSON array from PyString pystr.
751 idx is the index of the first character after the opening brace.
752 *next_idx_ptr is a return-by-reference index to the first character after
753 the closing brace.
754
755 Returns a new PyList
756 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200757 void *str;
758 int kind;
759 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000760 PyObject *val = NULL;
761 PyObject *rval = PyList_New(0);
762 Py_ssize_t next_idx;
763 if (rval == NULL)
764 return NULL;
765
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200766 if (PyUnicode_READY(pystr) == -1)
767 return NULL;
768
769 str = PyUnicode_DATA(pystr);
770 kind = PyUnicode_KIND(pystr);
771 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
772
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000773 /* skip whitespace after [ */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200774 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000775
776 /* only loop if the array is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200777 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000778 while (idx <= end_idx) {
779
780 /* read any JSON term */
781 val = scan_once_unicode(s, pystr, idx, &next_idx);
782 if (val == NULL)
783 goto bail;
784
785 if (PyList_Append(rval, val) == -1)
786 goto bail;
787
788 Py_CLEAR(val);
789 idx = next_idx;
790
791 /* skip whitespace between term and , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200792 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000793
794 /* bail if the array is closed or we didn't get the , delimiter */
795 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200796 if (PyUnicode_READ(kind, str, idx) == ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000797 break;
798 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200799 else if (PyUnicode_READ(kind, str, idx) != ',') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000800 raise_errmsg("Expecting , delimiter", pystr, idx);
801 goto bail;
802 }
803 idx++;
804
805 /* skip whitespace after , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200806 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000807 }
808 }
809
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200810 /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */
811 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000812 raise_errmsg("Expecting object", pystr, end_idx);
813 goto bail;
814 }
815 *next_idx_ptr = idx + 1;
816 return rval;
817bail:
818 Py_XDECREF(val);
819 Py_DECREF(rval);
820 return NULL;
821}
822
823static PyObject *
824_parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
825 /* Read a JSON constant from PyString pystr.
826 constant is the constant string that was found
827 ("NaN", "Infinity", "-Infinity").
828 idx is the index of the first character of the constant
829 *next_idx_ptr is a return-by-reference index to the first character after
830 the constant.
831
832 Returns the result of parse_constant
833 */
834 PyObject *cstr;
835 PyObject *rval;
836 /* constant is "NaN", "Infinity", or "-Infinity" */
837 cstr = PyUnicode_InternFromString(constant);
838 if (cstr == NULL)
839 return NULL;
840
841 /* rval = parse_constant(constant) */
842 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
Victor Stinnerc4f281e2011-10-11 22:11:42 +0200843 idx += PyUnicode_GET_LENGTH(cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000844 Py_DECREF(cstr);
845 *next_idx_ptr = idx;
846 return rval;
847}
848
849static PyObject *
850_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
851 /* Read a JSON number from PyUnicode pystr.
852 idx is the index of the first character of the number
853 *next_idx_ptr is a return-by-reference index to the first character after
854 the number.
855
856 Returns a new PyObject representation of that number:
857 PyInt, PyLong, or PyFloat.
858 May return other types if parse_int or parse_float are set
859 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200860 void *str;
861 int kind;
862 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000863 Py_ssize_t idx = start;
864 int is_float = 0;
865 PyObject *rval;
Antoine Pitrouf6454512011-04-25 19:16:06 +0200866 PyObject *numstr = NULL;
867 PyObject *custom_func;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000868
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200869 if (PyUnicode_READY(pystr) == -1)
870 return NULL;
871
872 str = PyUnicode_DATA(pystr);
873 kind = PyUnicode_KIND(pystr);
874 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
875
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000876 /* 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 +0200877 if (PyUnicode_READ(kind, str, idx) == '-') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000878 idx++;
879 if (idx > end_idx) {
880 PyErr_SetNone(PyExc_StopIteration);
881 return NULL;
882 }
883 }
884
885 /* 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 +0200886 if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000887 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200888 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000889 }
890 /* if it starts with 0 we only expect one integer digit */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200891 else if (PyUnicode_READ(kind, str, idx) == '0') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000892 idx++;
893 }
894 /* no integer digits, error */
895 else {
896 PyErr_SetNone(PyExc_StopIteration);
897 return NULL;
898 }
899
900 /* if the next char is '.' followed by a digit then read all float digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200901 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 +0000902 is_float = 1;
903 idx += 2;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200904 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000905 }
906
907 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200908 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000909 Py_ssize_t e_start = idx;
910 idx++;
911
912 /* read an exponent sign if present */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200913 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000914
915 /* read all digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200916 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000917
918 /* if we got a digit, then parse as float. if not, backtrack */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200919 if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000920 is_float = 1;
921 }
922 else {
923 idx = e_start;
924 }
925 }
926
Antoine Pitrouf6454512011-04-25 19:16:06 +0200927 if (is_float && s->parse_float != (PyObject *)&PyFloat_Type)
928 custom_func = s->parse_float;
929 else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type)
930 custom_func = s->parse_int;
931 else
932 custom_func = NULL;
933
934 if (custom_func) {
935 /* copy the section we determined to be a number */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200936 numstr = PyUnicode_FromKindAndData(kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200937 (char*)str + kind * start,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200938 idx - start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200939 if (numstr == NULL)
940 return NULL;
941 rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000942 }
943 else {
Antoine Pitrouf6454512011-04-25 19:16:06 +0200944 Py_ssize_t i, n;
945 char *buf;
946 /* Straight conversion to ASCII, to avoid costly conversion of
947 decimal unicode digits (which cannot appear here) */
948 n = idx - start;
949 numstr = PyBytes_FromStringAndSize(NULL, n);
950 if (numstr == NULL)
951 return NULL;
952 buf = PyBytes_AS_STRING(numstr);
953 for (i = 0; i < n; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200954 buf[i] = (char) PyUnicode_READ(kind, str, i + start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200955 }
956 if (is_float)
957 rval = PyFloat_FromString(numstr);
958 else
959 rval = PyLong_FromString(buf, NULL, 10);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000960 }
961 Py_DECREF(numstr);
962 *next_idx_ptr = idx;
963 return rval;
964}
965
966static PyObject *
967scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
968{
969 /* Read one JSON term (of any kind) from PyUnicode pystr.
970 idx is the index of the first character of the term
971 *next_idx_ptr is a return-by-reference index to the first character after
972 the number.
973
974 Returns a new PyObject representation of the term.
975 */
Ezio Melotti362b9512011-05-07 17:58:09 +0300976 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200977 void *str;
978 int kind;
979 Py_ssize_t length;
980
981 if (PyUnicode_READY(pystr) == -1)
982 return NULL;
983
984 str = PyUnicode_DATA(pystr);
985 kind = PyUnicode_KIND(pystr);
986 length = PyUnicode_GET_LENGTH(pystr);
987
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000988 if (idx >= length) {
989 PyErr_SetNone(PyExc_StopIteration);
990 return NULL;
991 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200992
993 switch (PyUnicode_READ(kind, str, idx)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000994 case '"':
995 /* string */
996 return scanstring_unicode(pystr, idx + 1,
997 PyObject_IsTrue(s->strict),
998 next_idx_ptr);
999 case '{':
1000 /* object */
Ezio Melotti362b9512011-05-07 17:58:09 +03001001 if (Py_EnterRecursiveCall(" while decoding a JSON object "
1002 "from a unicode string"))
1003 return NULL;
1004 res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
1005 Py_LeaveRecursiveCall();
1006 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001007 case '[':
1008 /* array */
Ezio Melotti362b9512011-05-07 17:58:09 +03001009 if (Py_EnterRecursiveCall(" while decoding a JSON array "
1010 "from a unicode string"))
1011 return NULL;
1012 res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
1013 Py_LeaveRecursiveCall();
1014 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001015 case 'n':
1016 /* null */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001017 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 +00001018 Py_INCREF(Py_None);
1019 *next_idx_ptr = idx + 4;
1020 return Py_None;
1021 }
1022 break;
1023 case 't':
1024 /* true */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001025 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 +00001026 Py_INCREF(Py_True);
1027 *next_idx_ptr = idx + 4;
1028 return Py_True;
1029 }
1030 break;
1031 case 'f':
1032 /* false */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001033 if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
1034 PyUnicode_READ(kind, str, idx + 2) == 'l' &&
1035 PyUnicode_READ(kind, str, idx + 3) == 's' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001036 PyUnicode_READ(kind, str, idx + 4) == 'e') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001037 Py_INCREF(Py_False);
1038 *next_idx_ptr = idx + 5;
1039 return Py_False;
1040 }
1041 break;
1042 case 'N':
1043 /* NaN */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001044 if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001045 PyUnicode_READ(kind, str, idx + 2) == 'N') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001046 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1047 }
1048 break;
1049 case 'I':
1050 /* Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001051 if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
1052 PyUnicode_READ(kind, str, idx + 2) == 'f' &&
1053 PyUnicode_READ(kind, str, idx + 3) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001054 PyUnicode_READ(kind, str, idx + 4) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001055 PyUnicode_READ(kind, str, idx + 5) == 'i' &&
1056 PyUnicode_READ(kind, str, idx + 6) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001057 PyUnicode_READ(kind, str, idx + 7) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001058 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1059 }
1060 break;
1061 case '-':
1062 /* -Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001063 if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001064 PyUnicode_READ(kind, str, idx + 2) == 'n' &&
1065 PyUnicode_READ(kind, str, idx + 3) == 'f' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001066 PyUnicode_READ(kind, str, idx + 4) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001067 PyUnicode_READ(kind, str, idx + 5) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001068 PyUnicode_READ(kind, str, idx + 6) == 'i' &&
1069 PyUnicode_READ(kind, str, idx + 7) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001070 PyUnicode_READ(kind, str, idx + 8) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001071 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1072 }
1073 break;
1074 }
1075 /* Didn't find a string, object, array, or named constant. Look for a number. */
1076 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
1077}
1078
1079static PyObject *
1080scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
1081{
1082 /* Python callable interface to scan_once_{str,unicode} */
1083 PyObject *pystr;
1084 PyObject *rval;
1085 Py_ssize_t idx;
1086 Py_ssize_t next_idx = -1;
1087 static char *kwlist[] = {"string", "idx", NULL};
1088 PyScannerObject *s;
1089 assert(PyScanner_Check(self));
1090 s = (PyScannerObject *)self;
1091 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:scan_once", kwlist, &pystr, _convertPyInt_AsSsize_t, &idx))
1092 return NULL;
1093
1094 if (PyUnicode_Check(pystr)) {
1095 rval = scan_once_unicode(s, pystr, idx, &next_idx);
1096 }
1097 else {
1098 PyErr_Format(PyExc_TypeError,
1099 "first argument must be a string, not %.80s",
1100 Py_TYPE(pystr)->tp_name);
1101 return NULL;
1102 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001103 PyDict_Clear(s->memo);
1104 if (rval == NULL)
1105 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001106 return _build_rval_index_tuple(rval, next_idx);
1107}
1108
1109static PyObject *
1110scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1111{
1112 PyScannerObject *s;
1113 s = (PyScannerObject *)type->tp_alloc(type, 0);
1114 if (s != NULL) {
1115 s->strict = NULL;
1116 s->object_hook = NULL;
1117 s->object_pairs_hook = NULL;
1118 s->parse_float = NULL;
1119 s->parse_int = NULL;
1120 s->parse_constant = NULL;
1121 }
1122 return (PyObject *)s;
1123}
1124
1125static int
1126scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1127{
1128 /* Initialize Scanner object */
1129 PyObject *ctx;
1130 static char *kwlist[] = {"context", NULL};
1131 PyScannerObject *s;
1132
1133 assert(PyScanner_Check(self));
1134 s = (PyScannerObject *)self;
1135
1136 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1137 return -1;
1138
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001139 if (s->memo == NULL) {
1140 s->memo = PyDict_New();
1141 if (s->memo == NULL)
1142 goto bail;
1143 }
1144
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001145 /* All of these will fail "gracefully" so we don't need to verify them */
1146 s->strict = PyObject_GetAttrString(ctx, "strict");
1147 if (s->strict == NULL)
1148 goto bail;
1149 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1150 if (s->object_hook == NULL)
1151 goto bail;
1152 s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1153 if (s->object_pairs_hook == NULL)
1154 goto bail;
1155 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1156 if (s->parse_float == NULL)
1157 goto bail;
1158 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1159 if (s->parse_int == NULL)
1160 goto bail;
1161 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1162 if (s->parse_constant == NULL)
1163 goto bail;
1164
1165 return 0;
1166
1167bail:
1168 Py_CLEAR(s->strict);
1169 Py_CLEAR(s->object_hook);
1170 Py_CLEAR(s->object_pairs_hook);
1171 Py_CLEAR(s->parse_float);
1172 Py_CLEAR(s->parse_int);
1173 Py_CLEAR(s->parse_constant);
1174 return -1;
1175}
1176
1177PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1178
1179static
1180PyTypeObject PyScannerType = {
1181 PyVarObject_HEAD_INIT(NULL, 0)
1182 "_json.Scanner", /* tp_name */
1183 sizeof(PyScannerObject), /* tp_basicsize */
1184 0, /* tp_itemsize */
1185 scanner_dealloc, /* tp_dealloc */
1186 0, /* tp_print */
1187 0, /* tp_getattr */
1188 0, /* tp_setattr */
1189 0, /* tp_compare */
1190 0, /* tp_repr */
1191 0, /* tp_as_number */
1192 0, /* tp_as_sequence */
1193 0, /* tp_as_mapping */
1194 0, /* tp_hash */
1195 scanner_call, /* tp_call */
1196 0, /* tp_str */
1197 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1198 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1199 0, /* tp_as_buffer */
1200 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1201 scanner_doc, /* tp_doc */
1202 scanner_traverse, /* tp_traverse */
1203 scanner_clear, /* tp_clear */
1204 0, /* tp_richcompare */
1205 0, /* tp_weaklistoffset */
1206 0, /* tp_iter */
1207 0, /* tp_iternext */
1208 0, /* tp_methods */
1209 scanner_members, /* tp_members */
1210 0, /* tp_getset */
1211 0, /* tp_base */
1212 0, /* tp_dict */
1213 0, /* tp_descr_get */
1214 0, /* tp_descr_set */
1215 0, /* tp_dictoffset */
1216 scanner_init, /* tp_init */
1217 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1218 scanner_new, /* tp_new */
1219 0,/* PyObject_GC_Del, */ /* tp_free */
1220};
1221
1222static PyObject *
1223encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1224{
1225 PyEncoderObject *s;
1226 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1227 if (s != NULL) {
1228 s->markers = NULL;
1229 s->defaultfn = NULL;
1230 s->encoder = NULL;
1231 s->indent = NULL;
1232 s->key_separator = NULL;
1233 s->item_separator = NULL;
1234 s->sort_keys = NULL;
1235 s->skipkeys = NULL;
1236 }
1237 return (PyObject *)s;
1238}
1239
1240static int
1241encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1242{
1243 /* initialize Encoder object */
1244 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1245
1246 PyEncoderObject *s;
Antoine Pitrou781eba72009-12-08 15:57:31 +00001247 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1248 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001249
1250 assert(PyEncoder_Check(self));
1251 s = (PyEncoderObject *)self;
1252
1253 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
Antoine Pitrou781eba72009-12-08 15:57:31 +00001254 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1255 &sort_keys, &skipkeys, &allow_nan))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001256 return -1;
1257
Antoine Pitrou781eba72009-12-08 15:57:31 +00001258 s->markers = markers;
1259 s->defaultfn = defaultfn;
1260 s->encoder = encoder;
1261 s->indent = indent;
1262 s->key_separator = key_separator;
1263 s->item_separator = item_separator;
1264 s->sort_keys = sort_keys;
1265 s->skipkeys = skipkeys;
1266 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1267 s->allow_nan = PyObject_IsTrue(allow_nan);
1268
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001269 Py_INCREF(s->markers);
1270 Py_INCREF(s->defaultfn);
1271 Py_INCREF(s->encoder);
1272 Py_INCREF(s->indent);
1273 Py_INCREF(s->key_separator);
1274 Py_INCREF(s->item_separator);
1275 Py_INCREF(s->sort_keys);
1276 Py_INCREF(s->skipkeys);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001277 return 0;
1278}
1279
1280static PyObject *
1281encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1282{
1283 /* Python callable interface to encode_listencode_obj */
1284 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1285 PyObject *obj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001286 Py_ssize_t indent_level;
1287 PyEncoderObject *s;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001288 _PyAccu acc;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001289
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001290 assert(PyEncoder_Check(self));
1291 s = (PyEncoderObject *)self;
1292 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:_iterencode", kwlist,
1293 &obj, _convertPyInt_AsSsize_t, &indent_level))
1294 return NULL;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001295 if (_PyAccu_Init(&acc))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001296 return NULL;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001297 if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001298 _PyAccu_Destroy(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001299 return NULL;
1300 }
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001301 return _PyAccu_FinishAsList(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001302}
1303
1304static PyObject *
1305_encoded_const(PyObject *obj)
1306{
1307 /* Return the JSON string representation of None, True, False */
1308 if (obj == Py_None) {
1309 static PyObject *s_null = NULL;
1310 if (s_null == NULL) {
1311 s_null = PyUnicode_InternFromString("null");
1312 }
1313 Py_INCREF(s_null);
1314 return s_null;
1315 }
1316 else if (obj == Py_True) {
1317 static PyObject *s_true = NULL;
1318 if (s_true == NULL) {
1319 s_true = PyUnicode_InternFromString("true");
1320 }
1321 Py_INCREF(s_true);
1322 return s_true;
1323 }
1324 else if (obj == Py_False) {
1325 static PyObject *s_false = NULL;
1326 if (s_false == NULL) {
1327 s_false = PyUnicode_InternFromString("false");
1328 }
1329 Py_INCREF(s_false);
1330 return s_false;
1331 }
1332 else {
1333 PyErr_SetString(PyExc_ValueError, "not a const");
1334 return NULL;
1335 }
1336}
1337
1338static PyObject *
1339encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1340{
1341 /* Return the JSON representation of a PyFloat */
1342 double i = PyFloat_AS_DOUBLE(obj);
1343 if (!Py_IS_FINITE(i)) {
1344 if (!s->allow_nan) {
1345 PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant");
1346 return NULL;
1347 }
1348 if (i > 0) {
1349 return PyUnicode_FromString("Infinity");
1350 }
1351 else if (i < 0) {
1352 return PyUnicode_FromString("-Infinity");
1353 }
1354 else {
1355 return PyUnicode_FromString("NaN");
1356 }
1357 }
1358 /* Use a better float format here? */
1359 return PyObject_Repr(obj);
1360}
1361
1362static PyObject *
1363encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1364{
1365 /* Return the JSON representation of a string */
1366 if (s->fast_encode)
1367 return py_encode_basestring_ascii(NULL, obj);
1368 else
1369 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1370}
1371
1372static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001373_steal_accumulate(_PyAccu *acc, PyObject *stolen)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001374{
1375 /* Append stolen and then decrement its reference count */
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001376 int rval = _PyAccu_Accumulate(acc, stolen);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001377 Py_DECREF(stolen);
1378 return rval;
1379}
1380
1381static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001382encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001383 PyObject *obj, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001384{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001385 /* Encode Python object obj to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001386 PyObject *newobj;
1387 int rv;
1388
1389 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1390 PyObject *cstr = _encoded_const(obj);
1391 if (cstr == NULL)
1392 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001393 return _steal_accumulate(acc, cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001394 }
1395 else if (PyUnicode_Check(obj))
1396 {
1397 PyObject *encoded = encoder_encode_string(s, obj);
1398 if (encoded == NULL)
1399 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001400 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001401 }
1402 else if (PyLong_Check(obj)) {
1403 PyObject *encoded = PyObject_Str(obj);
1404 if (encoded == NULL)
1405 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001406 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001407 }
1408 else if (PyFloat_Check(obj)) {
1409 PyObject *encoded = encoder_encode_float(s, obj);
1410 if (encoded == NULL)
1411 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001412 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001413 }
1414 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001415 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1416 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001417 rv = encoder_listencode_list(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001418 Py_LeaveRecursiveCall();
1419 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001420 }
1421 else if (PyDict_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001422 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1423 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001424 rv = encoder_listencode_dict(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001425 Py_LeaveRecursiveCall();
1426 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001427 }
1428 else {
1429 PyObject *ident = NULL;
1430 if (s->markers != Py_None) {
1431 int has_key;
1432 ident = PyLong_FromVoidPtr(obj);
1433 if (ident == NULL)
1434 return -1;
1435 has_key = PyDict_Contains(s->markers, ident);
1436 if (has_key) {
1437 if (has_key != -1)
1438 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1439 Py_DECREF(ident);
1440 return -1;
1441 }
1442 if (PyDict_SetItem(s->markers, ident, obj)) {
1443 Py_DECREF(ident);
1444 return -1;
1445 }
1446 }
1447 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1448 if (newobj == NULL) {
1449 Py_XDECREF(ident);
1450 return -1;
1451 }
Ezio Melotti13672652011-05-11 01:02:56 +03001452
1453 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1454 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001455 rv = encoder_listencode_obj(s, acc, newobj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001456 Py_LeaveRecursiveCall();
1457
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001458 Py_DECREF(newobj);
1459 if (rv) {
1460 Py_XDECREF(ident);
1461 return -1;
1462 }
1463 if (ident != NULL) {
1464 if (PyDict_DelItem(s->markers, ident)) {
1465 Py_XDECREF(ident);
1466 return -1;
1467 }
1468 Py_XDECREF(ident);
1469 }
1470 return rv;
1471 }
1472}
1473
1474static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001475encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001476 PyObject *dct, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001477{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001478 /* Encode Python dict dct a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001479 static PyObject *open_dict = NULL;
1480 static PyObject *close_dict = NULL;
1481 static PyObject *empty_dict = NULL;
1482 PyObject *kstr = NULL;
1483 PyObject *ident = NULL;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001484 PyObject *it = NULL;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001485 PyObject *items;
1486 PyObject *item = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001487 int skipkeys;
1488 Py_ssize_t idx;
1489
1490 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
1491 open_dict = PyUnicode_InternFromString("{");
1492 close_dict = PyUnicode_InternFromString("}");
1493 empty_dict = PyUnicode_InternFromString("{}");
1494 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
1495 return -1;
1496 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001497 if (Py_SIZE(dct) == 0)
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001498 return _PyAccu_Accumulate(acc, empty_dict);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001499
1500 if (s->markers != Py_None) {
1501 int has_key;
1502 ident = PyLong_FromVoidPtr(dct);
1503 if (ident == NULL)
1504 goto bail;
1505 has_key = PyDict_Contains(s->markers, ident);
1506 if (has_key) {
1507 if (has_key != -1)
1508 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1509 goto bail;
1510 }
1511 if (PyDict_SetItem(s->markers, ident, dct)) {
1512 goto bail;
1513 }
1514 }
1515
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001516 if (_PyAccu_Accumulate(acc, open_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001517 goto bail;
1518
1519 if (s->indent != Py_None) {
1520 /* TODO: DOES NOT RUN */
1521 indent_level += 1;
1522 /*
1523 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1524 separator = _item_separator + newline_indent
1525 buf += newline_indent
1526 */
1527 }
1528
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001529 if (PyObject_IsTrue(s->sort_keys)) {
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001530 /* First sort the keys then replace them with (key, value) tuples. */
1531 Py_ssize_t i, nitems;
1532 items = PyMapping_Keys(dct);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 if (items == NULL)
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001534 goto bail;
1535 if (!PyList_Check(items)) {
1536 PyErr_SetString(PyExc_ValueError, "keys must return list");
1537 goto bail;
1538 }
1539 if (PyList_Sort(items) < 0)
1540 goto bail;
1541 nitems = PyList_GET_SIZE(items);
1542 for (i = 0; i < nitems; i++) {
1543 PyObject *key, *value;
1544 key = PyList_GET_ITEM(items, i);
1545 value = PyDict_GetItem(dct, key);
1546 item = PyTuple_Pack(2, key, value);
1547 if (item == NULL)
1548 goto bail;
1549 PyList_SET_ITEM(items, i, item);
1550 Py_DECREF(key);
1551 }
1552 }
1553 else {
1554 items = PyMapping_Items(dct);
1555 }
1556 if (items == NULL)
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001557 goto bail;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001558 it = PyObject_GetIter(items);
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001559 Py_DECREF(items);
1560 if (it == NULL)
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001561 goto bail;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001562 skipkeys = PyObject_IsTrue(s->skipkeys);
1563 idx = 0;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001564 while ((item = PyIter_Next(it)) != NULL) {
1565 PyObject *encoded, *key, *value;
1566 if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
1567 PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
1568 goto bail;
1569 }
1570 key = PyTuple_GET_ITEM(item, 0);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001571 if (PyUnicode_Check(key)) {
1572 Py_INCREF(key);
1573 kstr = key;
1574 }
1575 else if (PyFloat_Check(key)) {
1576 kstr = encoder_encode_float(s, key);
1577 if (kstr == NULL)
1578 goto bail;
1579 }
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001580 else if (key == Py_True || key == Py_False || key == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 /* This must come before the PyLong_Check because
1582 True and False are also 1 and 0.*/
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001583 kstr = _encoded_const(key);
1584 if (kstr == NULL)
1585 goto bail;
1586 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001587 else if (PyLong_Check(key)) {
1588 kstr = PyObject_Str(key);
1589 if (kstr == NULL)
1590 goto bail;
1591 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001592 else if (skipkeys) {
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001593 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001594 continue;
1595 }
1596 else {
1597 /* TODO: include repr of key */
Doug Hellmann1c524752010-07-21 12:29:04 +00001598 PyErr_SetString(PyExc_TypeError, "keys must be a string");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001599 goto bail;
1600 }
1601
1602 if (idx) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001603 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001604 goto bail;
1605 }
1606
1607 encoded = encoder_encode_string(s, kstr);
1608 Py_CLEAR(kstr);
1609 if (encoded == NULL)
1610 goto bail;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001611 if (_PyAccu_Accumulate(acc, encoded)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001612 Py_DECREF(encoded);
1613 goto bail;
1614 }
1615 Py_DECREF(encoded);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001616 if (_PyAccu_Accumulate(acc, s->key_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001617 goto bail;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001618
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001619 value = PyTuple_GET_ITEM(item, 1);
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001620 if (encoder_listencode_obj(s, acc, value, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001621 goto bail;
1622 idx += 1;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001623 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001624 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001625 if (PyErr_Occurred())
1626 goto bail;
1627 Py_CLEAR(it);
1628
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001629 if (ident != NULL) {
1630 if (PyDict_DelItem(s->markers, ident))
1631 goto bail;
1632 Py_CLEAR(ident);
1633 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001634 /* TODO DOES NOT RUN; dead code
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001635 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001636 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001637
1638 yield '\n' + (' ' * (_indent * _current_indent_level))
1639 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001640 if (_PyAccu_Accumulate(acc, close_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001641 goto bail;
1642 return 0;
1643
1644bail:
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001645 Py_XDECREF(it);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001646 Py_XDECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001647 Py_XDECREF(kstr);
1648 Py_XDECREF(ident);
1649 return -1;
1650}
1651
1652
1653static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001654encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001655 PyObject *seq, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001656{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001657 /* Encode Python list seq to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001658 static PyObject *open_array = NULL;
1659 static PyObject *close_array = NULL;
1660 static PyObject *empty_array = NULL;
1661 PyObject *ident = NULL;
1662 PyObject *s_fast = NULL;
1663 Py_ssize_t num_items;
1664 PyObject **seq_items;
1665 Py_ssize_t i;
1666
1667 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
1668 open_array = PyUnicode_InternFromString("[");
1669 close_array = PyUnicode_InternFromString("]");
1670 empty_array = PyUnicode_InternFromString("[]");
1671 if (open_array == NULL || close_array == NULL || empty_array == NULL)
1672 return -1;
1673 }
1674 ident = NULL;
1675 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
1676 if (s_fast == NULL)
1677 return -1;
1678 num_items = PySequence_Fast_GET_SIZE(s_fast);
1679 if (num_items == 0) {
1680 Py_DECREF(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001681 return _PyAccu_Accumulate(acc, empty_array);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001682 }
1683
1684 if (s->markers != Py_None) {
1685 int has_key;
1686 ident = PyLong_FromVoidPtr(seq);
1687 if (ident == NULL)
1688 goto bail;
1689 has_key = PyDict_Contains(s->markers, ident);
1690 if (has_key) {
1691 if (has_key != -1)
1692 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1693 goto bail;
1694 }
1695 if (PyDict_SetItem(s->markers, ident, seq)) {
1696 goto bail;
1697 }
1698 }
1699
1700 seq_items = PySequence_Fast_ITEMS(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001701 if (_PyAccu_Accumulate(acc, open_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001702 goto bail;
1703 if (s->indent != Py_None) {
1704 /* TODO: DOES NOT RUN */
1705 indent_level += 1;
1706 /*
1707 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1708 separator = _item_separator + newline_indent
1709 buf += newline_indent
1710 */
1711 }
1712 for (i = 0; i < num_items; i++) {
1713 PyObject *obj = seq_items[i];
1714 if (i) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001715 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001716 goto bail;
1717 }
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001718 if (encoder_listencode_obj(s, acc, obj, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001719 goto bail;
1720 }
1721 if (ident != NULL) {
1722 if (PyDict_DelItem(s->markers, ident))
1723 goto bail;
1724 Py_CLEAR(ident);
1725 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001726
1727 /* TODO: DOES NOT RUN
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001728 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001729 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001730
1731 yield '\n' + (' ' * (_indent * _current_indent_level))
1732 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001733 if (_PyAccu_Accumulate(acc, close_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001734 goto bail;
1735 Py_DECREF(s_fast);
1736 return 0;
1737
1738bail:
1739 Py_XDECREF(ident);
1740 Py_DECREF(s_fast);
1741 return -1;
1742}
1743
1744static void
1745encoder_dealloc(PyObject *self)
1746{
1747 /* Deallocate Encoder */
1748 encoder_clear(self);
1749 Py_TYPE(self)->tp_free(self);
1750}
1751
1752static int
1753encoder_traverse(PyObject *self, visitproc visit, void *arg)
1754{
1755 PyEncoderObject *s;
1756 assert(PyEncoder_Check(self));
1757 s = (PyEncoderObject *)self;
1758 Py_VISIT(s->markers);
1759 Py_VISIT(s->defaultfn);
1760 Py_VISIT(s->encoder);
1761 Py_VISIT(s->indent);
1762 Py_VISIT(s->key_separator);
1763 Py_VISIT(s->item_separator);
1764 Py_VISIT(s->sort_keys);
1765 Py_VISIT(s->skipkeys);
1766 return 0;
1767}
1768
1769static int
1770encoder_clear(PyObject *self)
1771{
1772 /* Deallocate Encoder */
1773 PyEncoderObject *s;
1774 assert(PyEncoder_Check(self));
1775 s = (PyEncoderObject *)self;
1776 Py_CLEAR(s->markers);
1777 Py_CLEAR(s->defaultfn);
1778 Py_CLEAR(s->encoder);
1779 Py_CLEAR(s->indent);
1780 Py_CLEAR(s->key_separator);
1781 Py_CLEAR(s->item_separator);
1782 Py_CLEAR(s->sort_keys);
1783 Py_CLEAR(s->skipkeys);
1784 return 0;
1785}
1786
1787PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
1788
1789static
1790PyTypeObject PyEncoderType = {
1791 PyVarObject_HEAD_INIT(NULL, 0)
1792 "_json.Encoder", /* tp_name */
1793 sizeof(PyEncoderObject), /* tp_basicsize */
1794 0, /* tp_itemsize */
1795 encoder_dealloc, /* tp_dealloc */
1796 0, /* tp_print */
1797 0, /* tp_getattr */
1798 0, /* tp_setattr */
1799 0, /* tp_compare */
1800 0, /* tp_repr */
1801 0, /* tp_as_number */
1802 0, /* tp_as_sequence */
1803 0, /* tp_as_mapping */
1804 0, /* tp_hash */
1805 encoder_call, /* tp_call */
1806 0, /* tp_str */
1807 0, /* tp_getattro */
1808 0, /* tp_setattro */
1809 0, /* tp_as_buffer */
1810 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1811 encoder_doc, /* tp_doc */
1812 encoder_traverse, /* tp_traverse */
1813 encoder_clear, /* tp_clear */
1814 0, /* tp_richcompare */
1815 0, /* tp_weaklistoffset */
1816 0, /* tp_iter */
1817 0, /* tp_iternext */
1818 0, /* tp_methods */
1819 encoder_members, /* tp_members */
1820 0, /* tp_getset */
1821 0, /* tp_base */
1822 0, /* tp_dict */
1823 0, /* tp_descr_get */
1824 0, /* tp_descr_set */
1825 0, /* tp_dictoffset */
1826 encoder_init, /* tp_init */
1827 0, /* tp_alloc */
1828 encoder_new, /* tp_new */
1829 0, /* tp_free */
1830};
1831
1832static PyMethodDef speedups_methods[] = {
1833 {"encode_basestring_ascii",
1834 (PyCFunction)py_encode_basestring_ascii,
1835 METH_O,
1836 pydoc_encode_basestring_ascii},
1837 {"scanstring",
1838 (PyCFunction)py_scanstring,
1839 METH_VARARGS,
1840 pydoc_scanstring},
Christian Heimes90540002008-05-08 14:29:10 +00001841 {NULL, NULL, 0, NULL}
1842};
1843
1844PyDoc_STRVAR(module_doc,
1845"json speedups\n");
1846
Martin v. Löwis1a214512008-06-11 05:26:20 +00001847static struct PyModuleDef jsonmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 PyModuleDef_HEAD_INIT,
1849 "_json",
1850 module_doc,
1851 -1,
1852 speedups_methods,
1853 NULL,
1854 NULL,
1855 NULL,
1856 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001857};
1858
1859PyObject*
1860PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +00001861{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001862 PyObject *m = PyModule_Create(&jsonmodule);
1863 if (!m)
1864 return NULL;
1865 PyScannerType.tp_new = PyType_GenericNew;
1866 if (PyType_Ready(&PyScannerType) < 0)
1867 goto fail;
1868 PyEncoderType.tp_new = PyType_GenericNew;
1869 if (PyType_Ready(&PyEncoderType) < 0)
1870 goto fail;
1871 Py_INCREF((PyObject*)&PyScannerType);
1872 if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1873 Py_DECREF((PyObject*)&PyScannerType);
1874 goto fail;
1875 }
1876 Py_INCREF((PyObject*)&PyEncoderType);
1877 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1878 Py_DECREF((PyObject*)&PyEncoderType);
1879 goto fail;
1880 }
1881 return m;
1882 fail:
1883 Py_DECREF(m);
1884 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +00001885}