blob: 916668028911cfbe0d30fca5e925b4a5506ace08 [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++] = '"';
Christian Heimesf402e922013-01-03 09:21:55 +0100249#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200250 assert(_PyUnicode_CheckConsistency(rval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100251#endif
Christian Heimes90540002008-05-08 14:29:10 +0000252 return rval;
253}
254
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000255static void
Christian Heimes90540002008-05-08 14:29:10 +0000256raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
257{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000258 /* Use the Python function json.decoder.errmsg to raise a nice
259 looking ValueError exception */
Christian Heimes90540002008-05-08 14:29:10 +0000260 static PyObject *errmsg_fn = NULL;
261 PyObject *pymsg;
262 if (errmsg_fn == NULL) {
263 PyObject *decoder = PyImport_ImportModule("json.decoder");
264 if (decoder == NULL)
265 return;
266 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000267 Py_DECREF(decoder);
Christian Heimes90540002008-05-08 14:29:10 +0000268 if (errmsg_fn == NULL)
269 return;
Christian Heimes90540002008-05-08 14:29:10 +0000270 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000271 pymsg = PyObject_CallFunction(errmsg_fn, "(zOO&)", msg, s, _convertPyInt_FromSsize_t, &end);
Benjamin Petersona13d4752008-10-16 21:17:24 +0000272 if (pymsg) {
273 PyErr_SetObject(PyExc_ValueError, pymsg);
274 Py_DECREF(pymsg);
275 }
Christian Heimes90540002008-05-08 14:29:10 +0000276}
277
278static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000279_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
280 /* return (rval, idx) tuple, stealing reference to rval */
281 PyObject *tpl;
282 PyObject *pyidx;
283 /*
284 steal a reference to rval, returns (rval, idx)
285 */
286 if (rval == NULL) {
Christian Heimes90540002008-05-08 14:29:10 +0000287 return NULL;
288 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000289 pyidx = PyLong_FromSsize_t(idx);
290 if (pyidx == NULL) {
291 Py_DECREF(rval);
292 return NULL;
293 }
294 tpl = PyTuple_New(2);
295 if (tpl == NULL) {
296 Py_DECREF(pyidx);
297 Py_DECREF(rval);
298 return NULL;
299 }
300 PyTuple_SET_ITEM(tpl, 0, rval);
301 PyTuple_SET_ITEM(tpl, 1, pyidx);
302 return tpl;
Christian Heimes90540002008-05-08 14:29:10 +0000303}
304
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000305#define APPEND_OLD_CHUNK \
306 if (chunk != NULL) { \
307 if (chunks == NULL) { \
308 chunks = PyList_New(0); \
309 if (chunks == NULL) { \
310 goto bail; \
311 } \
312 } \
313 if (PyList_Append(chunks, chunk)) { \
314 Py_DECREF(chunk); \
315 goto bail; \
316 } \
317 Py_CLEAR(chunk); \
318 }
319
Christian Heimes90540002008-05-08 14:29:10 +0000320static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000321scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000322{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000323 /* Read the JSON string from PyUnicode pystr.
324 end is the index of the first character after the quote.
325 if strict is zero then literal control characters are allowed
326 *next_end_ptr is a return-by-reference index of the character
327 after the end quote
Christian Heimes90540002008-05-08 14:29:10 +0000328
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000329 Return value is a new PyUnicode
330 */
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000331 PyObject *rval = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200332 Py_ssize_t len;
Christian Heimes90540002008-05-08 14:29:10 +0000333 Py_ssize_t begin = end - 1;
Brett Cannonb94767f2011-02-22 20:15:44 +0000334 Py_ssize_t next /* = begin */;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200335 const void *buf;
336 int kind;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000337 PyObject *chunks = NULL;
338 PyObject *chunk = NULL;
339
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200340 if (PyUnicode_READY(pystr) == -1)
341 return 0;
342
343 len = PyUnicode_GET_LENGTH(pystr);
344 buf = PyUnicode_DATA(pystr);
345 kind = PyUnicode_KIND(pystr);
346
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000347 if (end < 0 || len <= end) {
348 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
349 goto bail;
350 }
Christian Heimes90540002008-05-08 14:29:10 +0000351 while (1) {
352 /* Find the end of the string or the next escape */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200353 Py_UCS4 c = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000354 for (next = end; next < len; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200355 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000356 if (c == '"' || c == '\\') {
357 break;
358 }
359 else if (strict && c <= 0x1f) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000360 raise_errmsg("Invalid control character at", pystr, next);
Christian Heimes90540002008-05-08 14:29:10 +0000361 goto bail;
362 }
363 }
364 if (!(c == '"' || c == '\\')) {
365 raise_errmsg("Unterminated string starting at", pystr, begin);
366 goto bail;
367 }
368 /* Pick up this chunk if it's not zero length */
369 if (next != end) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000370 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200371 chunk = PyUnicode_FromKindAndData(
372 kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200373 (char*)buf + kind * end,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200374 next - end);
Christian Heimes90540002008-05-08 14:29:10 +0000375 if (chunk == NULL) {
376 goto bail;
377 }
Christian Heimes90540002008-05-08 14:29:10 +0000378 }
379 next++;
380 if (c == '"') {
381 end = next;
382 break;
383 }
384 if (next == len) {
385 raise_errmsg("Unterminated string starting at", pystr, begin);
386 goto bail;
387 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200388 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000389 if (c != 'u') {
390 /* Non-unicode backslash escapes */
391 end = next + 1;
392 switch (c) {
393 case '"': break;
394 case '\\': break;
395 case '/': break;
396 case 'b': c = '\b'; break;
397 case 'f': c = '\f'; break;
398 case 'n': c = '\n'; break;
399 case 'r': c = '\r'; break;
400 case 't': c = '\t'; break;
401 default: c = 0;
402 }
403 if (c == 0) {
404 raise_errmsg("Invalid \\escape", pystr, end - 2);
405 goto bail;
406 }
407 }
408 else {
409 c = 0;
410 next++;
411 end = next + 4;
412 if (end >= len) {
413 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
414 goto bail;
415 }
416 /* Decode 4 hex digits */
417 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200418 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000419 c <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000420 switch (digit) {
421 case '0': case '1': case '2': case '3': case '4':
422 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000423 c |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000424 case 'a': case 'b': case 'c': case 'd': case 'e':
425 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000426 c |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000427 case 'A': case 'B': case 'C': case 'D': case 'E':
428 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000429 c |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000430 default:
431 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
432 goto bail;
433 }
434 }
Christian Heimes90540002008-05-08 14:29:10 +0000435 /* Surrogate pair */
Serhiy Storchakac93329b2013-11-26 21:25:28 +0200436 if (Py_UNICODE_IS_HIGH_SURROGATE(c) && end + 6 < len &&
437 PyUnicode_READ(kind, buf, next++) == '\\' &&
438 PyUnicode_READ(kind, buf, next++) == 'u') {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200439 Py_UCS4 c2 = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000440 end += 6;
441 /* Decode 4 hex digits */
442 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200443 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Antoine Pitrou5b0e9e82010-10-09 15:24:28 +0000444 c2 <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000445 switch (digit) {
446 case '0': case '1': case '2': case '3': case '4':
447 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000448 c2 |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000449 case 'a': case 'b': case 'c': case 'd': case 'e':
450 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000451 c2 |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000452 case 'A': case 'B': case 'C': case 'D': case 'E':
453 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000454 c2 |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000455 default:
456 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
457 goto bail;
458 }
459 }
Serhiy Storchakac93329b2013-11-26 21:25:28 +0200460 if (Py_UNICODE_IS_LOW_SURROGATE(c2))
461 c = Py_UNICODE_JOIN_SURROGATES(c, c2);
462 else
463 end -= 6;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000464 }
Christian Heimes90540002008-05-08 14:29:10 +0000465 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000466 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200467 chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1);
Christian Heimes90540002008-05-08 14:29:10 +0000468 if (chunk == NULL) {
469 goto bail;
470 }
Christian Heimes90540002008-05-08 14:29:10 +0000471 }
472
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000473 if (chunks == NULL) {
474 if (chunk != NULL)
475 rval = chunk;
476 else
477 rval = PyUnicode_FromStringAndSize("", 0);
Christian Heimes90540002008-05-08 14:29:10 +0000478 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000479 else {
480 APPEND_OLD_CHUNK
481 rval = join_list_unicode(chunks);
482 if (rval == NULL) {
483 goto bail;
484 }
485 Py_CLEAR(chunks);
486 }
487
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000488 *next_end_ptr = end;
489 return rval;
Christian Heimes90540002008-05-08 14:29:10 +0000490bail:
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000491 *next_end_ptr = -1;
Christian Heimes90540002008-05-08 14:29:10 +0000492 Py_XDECREF(chunks);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000493 Py_XDECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000494 return NULL;
495}
496
497PyDoc_STRVAR(pydoc_scanstring,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000498 "scanstring(string, end, strict=True) -> (string, end)\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000499 "\n"
500 "Scan the string s for a JSON string. End is the index of the\n"
501 "character in s after the quote that started the JSON string.\n"
502 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
503 "on attempt to decode an invalid string. If strict is False then literal\n"
504 "control characters are allowed in the string.\n"
505 "\n"
506 "Returns a tuple of the decoded string and the index of the character in s\n"
507 "after the end quote."
508);
Christian Heimes90540002008-05-08 14:29:10 +0000509
510static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000511py_scanstring(PyObject* self UNUSED, PyObject *args)
Christian Heimes90540002008-05-08 14:29:10 +0000512{
513 PyObject *pystr;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000514 PyObject *rval;
Christian Heimes90540002008-05-08 14:29:10 +0000515 Py_ssize_t end;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000516 Py_ssize_t next_end = -1;
517 int strict = 1;
518 if (!PyArg_ParseTuple(args, "OO&|i:scanstring", &pystr, _convertPyInt_AsSsize_t, &end, &strict)) {
Christian Heimes90540002008-05-08 14:29:10 +0000519 return NULL;
520 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000521 if (PyUnicode_Check(pystr)) {
522 rval = scanstring_unicode(pystr, end, strict, &next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000523 }
524 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 PyErr_Format(PyExc_TypeError,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000526 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000527 Py_TYPE(pystr)->tp_name);
528 return NULL;
529 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000530 return _build_rval_index_tuple(rval, next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000531}
532
533PyDoc_STRVAR(pydoc_encode_basestring_ascii,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000534 "encode_basestring_ascii(string) -> string\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000535 "\n"
536 "Return an ASCII-only JSON representation of a Python string"
537);
Christian Heimes90540002008-05-08 14:29:10 +0000538
539static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000540py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
Christian Heimes90540002008-05-08 14:29:10 +0000541{
542 PyObject *rval;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000543 /* Return an ASCII-only JSON representation of a Python string */
Christian Heimes90540002008-05-08 14:29:10 +0000544 /* METH_O */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000545 if (PyUnicode_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000546 rval = ascii_escape_unicode(pystr);
547 }
548 else {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000549 PyErr_Format(PyExc_TypeError,
550 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000551 Py_TYPE(pystr)->tp_name);
552 return NULL;
553 }
Christian Heimes90540002008-05-08 14:29:10 +0000554 return rval;
555}
556
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000557static void
558scanner_dealloc(PyObject *self)
559{
560 /* Deallocate scanner object */
561 scanner_clear(self);
562 Py_TYPE(self)->tp_free(self);
563}
564
565static int
566scanner_traverse(PyObject *self, visitproc visit, void *arg)
567{
568 PyScannerObject *s;
569 assert(PyScanner_Check(self));
570 s = (PyScannerObject *)self;
571 Py_VISIT(s->strict);
572 Py_VISIT(s->object_hook);
573 Py_VISIT(s->object_pairs_hook);
574 Py_VISIT(s->parse_float);
575 Py_VISIT(s->parse_int);
576 Py_VISIT(s->parse_constant);
577 return 0;
578}
579
580static int
581scanner_clear(PyObject *self)
582{
583 PyScannerObject *s;
584 assert(PyScanner_Check(self));
585 s = (PyScannerObject *)self;
586 Py_CLEAR(s->strict);
587 Py_CLEAR(s->object_hook);
588 Py_CLEAR(s->object_pairs_hook);
589 Py_CLEAR(s->parse_float);
590 Py_CLEAR(s->parse_int);
591 Py_CLEAR(s->parse_constant);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000592 Py_CLEAR(s->memo);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000593 return 0;
594}
595
596static PyObject *
597_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
598 /* Read a JSON object from PyUnicode pystr.
599 idx is the index of the first character after the opening curly brace.
600 *next_idx_ptr is a return-by-reference index to the first character after
601 the closing curly brace.
602
603 Returns a new PyObject (usually a dict, but object_hook can change that)
604 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200605 void *str;
606 int kind;
607 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000608 PyObject *val = NULL;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000609 PyObject *rval = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000610 PyObject *key = NULL;
611 int strict = PyObject_IsTrue(s->strict);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000612 int has_pairs_hook = (s->object_pairs_hook != Py_None);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000613 Py_ssize_t next_idx;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000614
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200615 if (PyUnicode_READY(pystr) == -1)
616 return NULL;
617
618 str = PyUnicode_DATA(pystr);
619 kind = PyUnicode_KIND(pystr);
620 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
621
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000622 if (has_pairs_hook)
623 rval = PyList_New(0);
624 else
625 rval = PyDict_New();
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000626 if (rval == NULL)
627 return NULL;
628
629 /* skip whitespace after { */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200630 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000631
632 /* only loop if the object is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200633 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000634 while (idx <= end_idx) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000635 PyObject *memokey;
636
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000637 /* read key */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200638 if (PyUnicode_READ(kind, str, idx) != '"') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200639 raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000640 goto bail;
641 }
642 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
643 if (key == NULL)
644 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000645 memokey = PyDict_GetItem(s->memo, key);
646 if (memokey != NULL) {
647 Py_INCREF(memokey);
648 Py_DECREF(key);
649 key = memokey;
650 }
651 else {
652 if (PyDict_SetItem(s->memo, key, key) < 0)
653 goto bail;
654 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000655 idx = next_idx;
656
657 /* skip whitespace between key and : delimiter, read :, skip whitespace */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200658 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
659 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200660 raise_errmsg("Expecting ':' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000661 goto bail;
662 }
663 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200664 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000665
666 /* read any JSON term */
667 val = scan_once_unicode(s, pystr, idx, &next_idx);
668 if (val == NULL)
669 goto bail;
670
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000671 if (has_pairs_hook) {
672 PyObject *item = PyTuple_Pack(2, key, val);
673 if (item == NULL)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000674 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000675 Py_CLEAR(key);
676 Py_CLEAR(val);
677 if (PyList_Append(rval, item) == -1) {
678 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000679 goto bail;
680 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000681 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000682 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000683 else {
684 if (PyDict_SetItem(rval, key, val) < 0)
685 goto bail;
686 Py_CLEAR(key);
687 Py_CLEAR(val);
688 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000689 idx = next_idx;
690
691 /* skip whitespace before } or , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200692 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000693
694 /* bail if the object is closed or we didn't get the , delimiter */
695 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200696 if (PyUnicode_READ(kind, str, idx) == '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000697 break;
698 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200699 else if (PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200700 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000701 goto bail;
702 }
703 idx++;
704
705 /* skip whitespace after , delimiter */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200706 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000707 }
708 }
709
710 /* verify that idx < end_idx, str[idx] should be '}' */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200711 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000712 raise_errmsg("Expecting object", pystr, end_idx);
713 goto bail;
714 }
715
716 *next_idx_ptr = idx + 1;
717
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000718 if (has_pairs_hook) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000719 val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000720 Py_DECREF(rval);
721 return val;
722 }
723
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000724 /* if object_hook is not None: rval = object_hook(rval) */
725 if (s->object_hook != Py_None) {
726 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000727 Py_DECREF(rval);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000728 return val;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000729 }
730 return rval;
731bail:
732 Py_XDECREF(key);
733 Py_XDECREF(val);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000734 Py_XDECREF(rval);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000735 return NULL;
736}
737
738static PyObject *
739_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
740 /* Read a JSON array from PyString pystr.
741 idx is the index of the first character after the opening brace.
742 *next_idx_ptr is a return-by-reference index to the first character after
743 the closing brace.
744
745 Returns a new PyList
746 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200747 void *str;
748 int kind;
749 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000750 PyObject *val = NULL;
751 PyObject *rval = PyList_New(0);
752 Py_ssize_t next_idx;
753 if (rval == NULL)
754 return NULL;
755
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200756 if (PyUnicode_READY(pystr) == -1)
757 return NULL;
758
759 str = PyUnicode_DATA(pystr);
760 kind = PyUnicode_KIND(pystr);
761 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
762
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000763 /* skip whitespace after [ */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200764 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000765
766 /* only loop if the array is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200767 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000768 while (idx <= end_idx) {
769
770 /* read any JSON term */
771 val = scan_once_unicode(s, pystr, idx, &next_idx);
772 if (val == NULL)
773 goto bail;
774
775 if (PyList_Append(rval, val) == -1)
776 goto bail;
777
778 Py_CLEAR(val);
779 idx = next_idx;
780
781 /* skip whitespace between term and , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200782 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000783
784 /* bail if the array is closed or we didn't get the , delimiter */
785 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200786 if (PyUnicode_READ(kind, str, idx) == ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000787 break;
788 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200789 else if (PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200790 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000791 goto bail;
792 }
793 idx++;
794
795 /* skip whitespace after , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200796 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000797 }
798 }
799
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200800 /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */
801 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000802 raise_errmsg("Expecting object", pystr, end_idx);
803 goto bail;
804 }
805 *next_idx_ptr = idx + 1;
806 return rval;
807bail:
808 Py_XDECREF(val);
809 Py_DECREF(rval);
810 return NULL;
811}
812
813static PyObject *
814_parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
815 /* Read a JSON constant from PyString pystr.
816 constant is the constant string that was found
817 ("NaN", "Infinity", "-Infinity").
818 idx is the index of the first character of the constant
819 *next_idx_ptr is a return-by-reference index to the first character after
820 the constant.
821
822 Returns the result of parse_constant
823 */
824 PyObject *cstr;
825 PyObject *rval;
826 /* constant is "NaN", "Infinity", or "-Infinity" */
827 cstr = PyUnicode_InternFromString(constant);
828 if (cstr == NULL)
829 return NULL;
830
831 /* rval = parse_constant(constant) */
832 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
Victor Stinnerc4f281e2011-10-11 22:11:42 +0200833 idx += PyUnicode_GET_LENGTH(cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000834 Py_DECREF(cstr);
835 *next_idx_ptr = idx;
836 return rval;
837}
838
839static PyObject *
840_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
841 /* Read a JSON number from PyUnicode pystr.
842 idx is the index of the first character of the number
843 *next_idx_ptr is a return-by-reference index to the first character after
844 the number.
845
846 Returns a new PyObject representation of that number:
847 PyInt, PyLong, or PyFloat.
848 May return other types if parse_int or parse_float are set
849 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200850 void *str;
851 int kind;
852 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000853 Py_ssize_t idx = start;
854 int is_float = 0;
855 PyObject *rval;
Antoine Pitrouf6454512011-04-25 19:16:06 +0200856 PyObject *numstr = NULL;
857 PyObject *custom_func;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000858
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200859 if (PyUnicode_READY(pystr) == -1)
860 return NULL;
861
862 str = PyUnicode_DATA(pystr);
863 kind = PyUnicode_KIND(pystr);
864 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
865
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000866 /* 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 +0200867 if (PyUnicode_READ(kind, str, idx) == '-') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000868 idx++;
869 if (idx > end_idx) {
870 PyErr_SetNone(PyExc_StopIteration);
871 return NULL;
872 }
873 }
874
875 /* 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 +0200876 if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000877 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200878 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000879 }
880 /* if it starts with 0 we only expect one integer digit */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200881 else if (PyUnicode_READ(kind, str, idx) == '0') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000882 idx++;
883 }
884 /* no integer digits, error */
885 else {
886 PyErr_SetNone(PyExc_StopIteration);
887 return NULL;
888 }
889
890 /* if the next char is '.' followed by a digit then read all float digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200891 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 +0000892 is_float = 1;
893 idx += 2;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200894 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000895 }
896
897 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200898 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000899 Py_ssize_t e_start = idx;
900 idx++;
901
902 /* read an exponent sign if present */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200903 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000904
905 /* read all digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200906 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000907
908 /* if we got a digit, then parse as float. if not, backtrack */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200909 if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000910 is_float = 1;
911 }
912 else {
913 idx = e_start;
914 }
915 }
916
Antoine Pitrouf6454512011-04-25 19:16:06 +0200917 if (is_float && s->parse_float != (PyObject *)&PyFloat_Type)
918 custom_func = s->parse_float;
919 else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type)
920 custom_func = s->parse_int;
921 else
922 custom_func = NULL;
923
924 if (custom_func) {
925 /* copy the section we determined to be a number */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200926 numstr = PyUnicode_FromKindAndData(kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200927 (char*)str + kind * start,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200928 idx - start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200929 if (numstr == NULL)
930 return NULL;
931 rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000932 }
933 else {
Antoine Pitrouf6454512011-04-25 19:16:06 +0200934 Py_ssize_t i, n;
935 char *buf;
936 /* Straight conversion to ASCII, to avoid costly conversion of
937 decimal unicode digits (which cannot appear here) */
938 n = idx - start;
939 numstr = PyBytes_FromStringAndSize(NULL, n);
940 if (numstr == NULL)
941 return NULL;
942 buf = PyBytes_AS_STRING(numstr);
943 for (i = 0; i < n; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200944 buf[i] = (char) PyUnicode_READ(kind, str, i + start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200945 }
946 if (is_float)
947 rval = PyFloat_FromString(numstr);
948 else
949 rval = PyLong_FromString(buf, NULL, 10);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000950 }
951 Py_DECREF(numstr);
952 *next_idx_ptr = idx;
953 return rval;
954}
955
956static PyObject *
957scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
958{
959 /* Read one JSON term (of any kind) from PyUnicode pystr.
960 idx is the index of the first character of the term
961 *next_idx_ptr is a return-by-reference index to the first character after
962 the number.
963
964 Returns a new PyObject representation of the term.
965 */
Ezio Melotti362b9512011-05-07 17:58:09 +0300966 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200967 void *str;
968 int kind;
969 Py_ssize_t length;
970
971 if (PyUnicode_READY(pystr) == -1)
972 return NULL;
973
974 str = PyUnicode_DATA(pystr);
975 kind = PyUnicode_KIND(pystr);
976 length = PyUnicode_GET_LENGTH(pystr);
977
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000978 if (idx >= length) {
979 PyErr_SetNone(PyExc_StopIteration);
980 return NULL;
981 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200982
983 switch (PyUnicode_READ(kind, str, idx)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000984 case '"':
985 /* string */
986 return scanstring_unicode(pystr, idx + 1,
987 PyObject_IsTrue(s->strict),
988 next_idx_ptr);
989 case '{':
990 /* object */
Ezio Melotti362b9512011-05-07 17:58:09 +0300991 if (Py_EnterRecursiveCall(" while decoding a JSON object "
992 "from a unicode string"))
993 return NULL;
994 res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
995 Py_LeaveRecursiveCall();
996 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000997 case '[':
998 /* array */
Ezio Melotti362b9512011-05-07 17:58:09 +0300999 if (Py_EnterRecursiveCall(" while decoding a JSON array "
1000 "from a unicode string"))
1001 return NULL;
1002 res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
1003 Py_LeaveRecursiveCall();
1004 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001005 case 'n':
1006 /* null */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001007 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 +00001008 Py_INCREF(Py_None);
1009 *next_idx_ptr = idx + 4;
1010 return Py_None;
1011 }
1012 break;
1013 case 't':
1014 /* true */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001015 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 +00001016 Py_INCREF(Py_True);
1017 *next_idx_ptr = idx + 4;
1018 return Py_True;
1019 }
1020 break;
1021 case 'f':
1022 /* false */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001023 if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
1024 PyUnicode_READ(kind, str, idx + 2) == 'l' &&
1025 PyUnicode_READ(kind, str, idx + 3) == 's' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001026 PyUnicode_READ(kind, str, idx + 4) == 'e') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001027 Py_INCREF(Py_False);
1028 *next_idx_ptr = idx + 5;
1029 return Py_False;
1030 }
1031 break;
1032 case 'N':
1033 /* NaN */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001034 if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001035 PyUnicode_READ(kind, str, idx + 2) == 'N') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001036 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1037 }
1038 break;
1039 case 'I':
1040 /* Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001041 if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
1042 PyUnicode_READ(kind, str, idx + 2) == 'f' &&
1043 PyUnicode_READ(kind, str, idx + 3) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001044 PyUnicode_READ(kind, str, idx + 4) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001045 PyUnicode_READ(kind, str, idx + 5) == 'i' &&
1046 PyUnicode_READ(kind, str, idx + 6) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001047 PyUnicode_READ(kind, str, idx + 7) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001048 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1049 }
1050 break;
1051 case '-':
1052 /* -Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001053 if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001054 PyUnicode_READ(kind, str, idx + 2) == 'n' &&
1055 PyUnicode_READ(kind, str, idx + 3) == 'f' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001056 PyUnicode_READ(kind, str, idx + 4) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001057 PyUnicode_READ(kind, str, idx + 5) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001058 PyUnicode_READ(kind, str, idx + 6) == 'i' &&
1059 PyUnicode_READ(kind, str, idx + 7) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001060 PyUnicode_READ(kind, str, idx + 8) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001061 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1062 }
1063 break;
1064 }
1065 /* Didn't find a string, object, array, or named constant. Look for a number. */
1066 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
1067}
1068
1069static PyObject *
1070scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
1071{
1072 /* Python callable interface to scan_once_{str,unicode} */
1073 PyObject *pystr;
1074 PyObject *rval;
1075 Py_ssize_t idx;
1076 Py_ssize_t next_idx = -1;
1077 static char *kwlist[] = {"string", "idx", NULL};
1078 PyScannerObject *s;
1079 assert(PyScanner_Check(self));
1080 s = (PyScannerObject *)self;
1081 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:scan_once", kwlist, &pystr, _convertPyInt_AsSsize_t, &idx))
1082 return NULL;
1083
1084 if (PyUnicode_Check(pystr)) {
1085 rval = scan_once_unicode(s, pystr, idx, &next_idx);
1086 }
1087 else {
1088 PyErr_Format(PyExc_TypeError,
1089 "first argument must be a string, not %.80s",
1090 Py_TYPE(pystr)->tp_name);
1091 return NULL;
1092 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001093 PyDict_Clear(s->memo);
1094 if (rval == NULL)
1095 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001096 return _build_rval_index_tuple(rval, next_idx);
1097}
1098
1099static PyObject *
1100scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1101{
1102 PyScannerObject *s;
1103 s = (PyScannerObject *)type->tp_alloc(type, 0);
1104 if (s != NULL) {
1105 s->strict = NULL;
1106 s->object_hook = NULL;
1107 s->object_pairs_hook = NULL;
1108 s->parse_float = NULL;
1109 s->parse_int = NULL;
1110 s->parse_constant = NULL;
1111 }
1112 return (PyObject *)s;
1113}
1114
1115static int
1116scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1117{
1118 /* Initialize Scanner object */
1119 PyObject *ctx;
1120 static char *kwlist[] = {"context", NULL};
1121 PyScannerObject *s;
1122
1123 assert(PyScanner_Check(self));
1124 s = (PyScannerObject *)self;
1125
1126 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1127 return -1;
1128
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001129 if (s->memo == NULL) {
1130 s->memo = PyDict_New();
1131 if (s->memo == NULL)
1132 goto bail;
1133 }
1134
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001135 /* All of these will fail "gracefully" so we don't need to verify them */
1136 s->strict = PyObject_GetAttrString(ctx, "strict");
1137 if (s->strict == NULL)
1138 goto bail;
1139 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1140 if (s->object_hook == NULL)
1141 goto bail;
1142 s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1143 if (s->object_pairs_hook == NULL)
1144 goto bail;
1145 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1146 if (s->parse_float == NULL)
1147 goto bail;
1148 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1149 if (s->parse_int == NULL)
1150 goto bail;
1151 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1152 if (s->parse_constant == NULL)
1153 goto bail;
1154
1155 return 0;
1156
1157bail:
1158 Py_CLEAR(s->strict);
1159 Py_CLEAR(s->object_hook);
1160 Py_CLEAR(s->object_pairs_hook);
1161 Py_CLEAR(s->parse_float);
1162 Py_CLEAR(s->parse_int);
1163 Py_CLEAR(s->parse_constant);
1164 return -1;
1165}
1166
1167PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1168
1169static
1170PyTypeObject PyScannerType = {
1171 PyVarObject_HEAD_INIT(NULL, 0)
1172 "_json.Scanner", /* tp_name */
1173 sizeof(PyScannerObject), /* tp_basicsize */
1174 0, /* tp_itemsize */
1175 scanner_dealloc, /* tp_dealloc */
1176 0, /* tp_print */
1177 0, /* tp_getattr */
1178 0, /* tp_setattr */
1179 0, /* tp_compare */
1180 0, /* tp_repr */
1181 0, /* tp_as_number */
1182 0, /* tp_as_sequence */
1183 0, /* tp_as_mapping */
1184 0, /* tp_hash */
1185 scanner_call, /* tp_call */
1186 0, /* tp_str */
1187 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1188 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1189 0, /* tp_as_buffer */
1190 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1191 scanner_doc, /* tp_doc */
1192 scanner_traverse, /* tp_traverse */
1193 scanner_clear, /* tp_clear */
1194 0, /* tp_richcompare */
1195 0, /* tp_weaklistoffset */
1196 0, /* tp_iter */
1197 0, /* tp_iternext */
1198 0, /* tp_methods */
1199 scanner_members, /* tp_members */
1200 0, /* tp_getset */
1201 0, /* tp_base */
1202 0, /* tp_dict */
1203 0, /* tp_descr_get */
1204 0, /* tp_descr_set */
1205 0, /* tp_dictoffset */
1206 scanner_init, /* tp_init */
1207 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1208 scanner_new, /* tp_new */
1209 0,/* PyObject_GC_Del, */ /* tp_free */
1210};
1211
1212static PyObject *
1213encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1214{
1215 PyEncoderObject *s;
1216 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1217 if (s != NULL) {
1218 s->markers = NULL;
1219 s->defaultfn = NULL;
1220 s->encoder = NULL;
1221 s->indent = NULL;
1222 s->key_separator = NULL;
1223 s->item_separator = NULL;
1224 s->sort_keys = NULL;
1225 s->skipkeys = NULL;
1226 }
1227 return (PyObject *)s;
1228}
1229
1230static int
1231encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1232{
1233 /* initialize Encoder object */
1234 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1235
1236 PyEncoderObject *s;
Antoine Pitrou781eba72009-12-08 15:57:31 +00001237 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1238 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001239
1240 assert(PyEncoder_Check(self));
1241 s = (PyEncoderObject *)self;
1242
1243 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
Antoine Pitrou781eba72009-12-08 15:57:31 +00001244 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1245 &sort_keys, &skipkeys, &allow_nan))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001246 return -1;
1247
Antoine Pitrou781eba72009-12-08 15:57:31 +00001248 s->markers = markers;
1249 s->defaultfn = defaultfn;
1250 s->encoder = encoder;
1251 s->indent = indent;
1252 s->key_separator = key_separator;
1253 s->item_separator = item_separator;
1254 s->sort_keys = sort_keys;
1255 s->skipkeys = skipkeys;
1256 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1257 s->allow_nan = PyObject_IsTrue(allow_nan);
1258
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001259 Py_INCREF(s->markers);
1260 Py_INCREF(s->defaultfn);
1261 Py_INCREF(s->encoder);
1262 Py_INCREF(s->indent);
1263 Py_INCREF(s->key_separator);
1264 Py_INCREF(s->item_separator);
1265 Py_INCREF(s->sort_keys);
1266 Py_INCREF(s->skipkeys);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001267 return 0;
1268}
1269
1270static PyObject *
1271encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1272{
1273 /* Python callable interface to encode_listencode_obj */
1274 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1275 PyObject *obj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001276 Py_ssize_t indent_level;
1277 PyEncoderObject *s;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001278 _PyAccu acc;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001279
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001280 assert(PyEncoder_Check(self));
1281 s = (PyEncoderObject *)self;
1282 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:_iterencode", kwlist,
1283 &obj, _convertPyInt_AsSsize_t, &indent_level))
1284 return NULL;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001285 if (_PyAccu_Init(&acc))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001286 return NULL;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001287 if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001288 _PyAccu_Destroy(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001289 return NULL;
1290 }
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001291 return _PyAccu_FinishAsList(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001292}
1293
1294static PyObject *
1295_encoded_const(PyObject *obj)
1296{
1297 /* Return the JSON string representation of None, True, False */
1298 if (obj == Py_None) {
1299 static PyObject *s_null = NULL;
1300 if (s_null == NULL) {
1301 s_null = PyUnicode_InternFromString("null");
1302 }
1303 Py_INCREF(s_null);
1304 return s_null;
1305 }
1306 else if (obj == Py_True) {
1307 static PyObject *s_true = NULL;
1308 if (s_true == NULL) {
1309 s_true = PyUnicode_InternFromString("true");
1310 }
1311 Py_INCREF(s_true);
1312 return s_true;
1313 }
1314 else if (obj == Py_False) {
1315 static PyObject *s_false = NULL;
1316 if (s_false == NULL) {
1317 s_false = PyUnicode_InternFromString("false");
1318 }
1319 Py_INCREF(s_false);
1320 return s_false;
1321 }
1322 else {
1323 PyErr_SetString(PyExc_ValueError, "not a const");
1324 return NULL;
1325 }
1326}
1327
1328static PyObject *
1329encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1330{
1331 /* Return the JSON representation of a PyFloat */
1332 double i = PyFloat_AS_DOUBLE(obj);
1333 if (!Py_IS_FINITE(i)) {
1334 if (!s->allow_nan) {
1335 PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant");
1336 return NULL;
1337 }
1338 if (i > 0) {
1339 return PyUnicode_FromString("Infinity");
1340 }
1341 else if (i < 0) {
1342 return PyUnicode_FromString("-Infinity");
1343 }
1344 else {
1345 return PyUnicode_FromString("NaN");
1346 }
1347 }
1348 /* Use a better float format here? */
1349 return PyObject_Repr(obj);
1350}
1351
1352static PyObject *
1353encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1354{
1355 /* Return the JSON representation of a string */
1356 if (s->fast_encode)
1357 return py_encode_basestring_ascii(NULL, obj);
1358 else
1359 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1360}
1361
1362static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001363_steal_accumulate(_PyAccu *acc, PyObject *stolen)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001364{
1365 /* Append stolen and then decrement its reference count */
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001366 int rval = _PyAccu_Accumulate(acc, stolen);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001367 Py_DECREF(stolen);
1368 return rval;
1369}
1370
1371static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001372encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001373 PyObject *obj, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001374{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001375 /* Encode Python object obj to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001376 PyObject *newobj;
1377 int rv;
1378
1379 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1380 PyObject *cstr = _encoded_const(obj);
1381 if (cstr == NULL)
1382 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001383 return _steal_accumulate(acc, cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001384 }
1385 else if (PyUnicode_Check(obj))
1386 {
1387 PyObject *encoded = encoder_encode_string(s, obj);
1388 if (encoded == NULL)
1389 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001390 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001391 }
1392 else if (PyLong_Check(obj)) {
1393 PyObject *encoded = PyObject_Str(obj);
1394 if (encoded == NULL)
1395 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001396 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001397 }
1398 else if (PyFloat_Check(obj)) {
1399 PyObject *encoded = encoder_encode_float(s, obj);
1400 if (encoded == NULL)
1401 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001402 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001403 }
1404 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001405 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1406 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001407 rv = encoder_listencode_list(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001408 Py_LeaveRecursiveCall();
1409 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001410 }
1411 else if (PyDict_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001412 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1413 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001414 rv = encoder_listencode_dict(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001415 Py_LeaveRecursiveCall();
1416 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001417 }
1418 else {
1419 PyObject *ident = NULL;
1420 if (s->markers != Py_None) {
1421 int has_key;
1422 ident = PyLong_FromVoidPtr(obj);
1423 if (ident == NULL)
1424 return -1;
1425 has_key = PyDict_Contains(s->markers, ident);
1426 if (has_key) {
1427 if (has_key != -1)
1428 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1429 Py_DECREF(ident);
1430 return -1;
1431 }
1432 if (PyDict_SetItem(s->markers, ident, obj)) {
1433 Py_DECREF(ident);
1434 return -1;
1435 }
1436 }
1437 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1438 if (newobj == NULL) {
1439 Py_XDECREF(ident);
1440 return -1;
1441 }
Ezio Melotti13672652011-05-11 01:02:56 +03001442
1443 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1444 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001445 rv = encoder_listencode_obj(s, acc, newobj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001446 Py_LeaveRecursiveCall();
1447
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001448 Py_DECREF(newobj);
1449 if (rv) {
1450 Py_XDECREF(ident);
1451 return -1;
1452 }
1453 if (ident != NULL) {
1454 if (PyDict_DelItem(s->markers, ident)) {
1455 Py_XDECREF(ident);
1456 return -1;
1457 }
1458 Py_XDECREF(ident);
1459 }
1460 return rv;
1461 }
1462}
1463
1464static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001465encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001466 PyObject *dct, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001467{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001468 /* Encode Python dict dct a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001469 static PyObject *open_dict = NULL;
1470 static PyObject *close_dict = NULL;
1471 static PyObject *empty_dict = NULL;
1472 PyObject *kstr = NULL;
1473 PyObject *ident = NULL;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001474 PyObject *it = NULL;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001475 PyObject *items;
1476 PyObject *item = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001477 int skipkeys;
1478 Py_ssize_t idx;
1479
1480 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
1481 open_dict = PyUnicode_InternFromString("{");
1482 close_dict = PyUnicode_InternFromString("}");
1483 empty_dict = PyUnicode_InternFromString("{}");
1484 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
1485 return -1;
1486 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001487 if (Py_SIZE(dct) == 0)
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001488 return _PyAccu_Accumulate(acc, empty_dict);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001489
1490 if (s->markers != Py_None) {
1491 int has_key;
1492 ident = PyLong_FromVoidPtr(dct);
1493 if (ident == NULL)
1494 goto bail;
1495 has_key = PyDict_Contains(s->markers, ident);
1496 if (has_key) {
1497 if (has_key != -1)
1498 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1499 goto bail;
1500 }
1501 if (PyDict_SetItem(s->markers, ident, dct)) {
1502 goto bail;
1503 }
1504 }
1505
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001506 if (_PyAccu_Accumulate(acc, open_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001507 goto bail;
1508
1509 if (s->indent != Py_None) {
1510 /* TODO: DOES NOT RUN */
1511 indent_level += 1;
1512 /*
1513 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1514 separator = _item_separator + newline_indent
1515 buf += newline_indent
1516 */
1517 }
1518
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001519 if (PyObject_IsTrue(s->sort_keys)) {
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001520 /* First sort the keys then replace them with (key, value) tuples. */
1521 Py_ssize_t i, nitems;
1522 items = PyMapping_Keys(dct);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 if (items == NULL)
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001524 goto bail;
1525 if (!PyList_Check(items)) {
1526 PyErr_SetString(PyExc_ValueError, "keys must return list");
1527 goto bail;
1528 }
1529 if (PyList_Sort(items) < 0)
1530 goto bail;
1531 nitems = PyList_GET_SIZE(items);
1532 for (i = 0; i < nitems; i++) {
1533 PyObject *key, *value;
1534 key = PyList_GET_ITEM(items, i);
1535 value = PyDict_GetItem(dct, key);
1536 item = PyTuple_Pack(2, key, value);
1537 if (item == NULL)
1538 goto bail;
1539 PyList_SET_ITEM(items, i, item);
1540 Py_DECREF(key);
1541 }
1542 }
1543 else {
1544 items = PyMapping_Items(dct);
1545 }
1546 if (items == NULL)
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001547 goto bail;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001548 it = PyObject_GetIter(items);
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001549 Py_DECREF(items);
1550 if (it == NULL)
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001551 goto bail;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001552 skipkeys = PyObject_IsTrue(s->skipkeys);
1553 idx = 0;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001554 while ((item = PyIter_Next(it)) != NULL) {
1555 PyObject *encoded, *key, *value;
1556 if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
1557 PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
1558 goto bail;
1559 }
1560 key = PyTuple_GET_ITEM(item, 0);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001561 if (PyUnicode_Check(key)) {
1562 Py_INCREF(key);
1563 kstr = key;
1564 }
1565 else if (PyFloat_Check(key)) {
1566 kstr = encoder_encode_float(s, key);
1567 if (kstr == NULL)
1568 goto bail;
1569 }
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001570 else if (key == Py_True || key == Py_False || key == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 /* This must come before the PyLong_Check because
1572 True and False are also 1 and 0.*/
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001573 kstr = _encoded_const(key);
1574 if (kstr == NULL)
1575 goto bail;
1576 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001577 else if (PyLong_Check(key)) {
1578 kstr = PyObject_Str(key);
1579 if (kstr == NULL)
1580 goto bail;
1581 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001582 else if (skipkeys) {
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001583 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001584 continue;
1585 }
1586 else {
1587 /* TODO: include repr of key */
Doug Hellmann1c524752010-07-21 12:29:04 +00001588 PyErr_SetString(PyExc_TypeError, "keys must be a string");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001589 goto bail;
1590 }
1591
1592 if (idx) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001593 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001594 goto bail;
1595 }
1596
1597 encoded = encoder_encode_string(s, kstr);
1598 Py_CLEAR(kstr);
1599 if (encoded == NULL)
1600 goto bail;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001601 if (_PyAccu_Accumulate(acc, encoded)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001602 Py_DECREF(encoded);
1603 goto bail;
1604 }
1605 Py_DECREF(encoded);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001606 if (_PyAccu_Accumulate(acc, s->key_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001607 goto bail;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001608
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001609 value = PyTuple_GET_ITEM(item, 1);
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001610 if (encoder_listencode_obj(s, acc, value, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001611 goto bail;
1612 idx += 1;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001613 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001614 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001615 if (PyErr_Occurred())
1616 goto bail;
1617 Py_CLEAR(it);
1618
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001619 if (ident != NULL) {
1620 if (PyDict_DelItem(s->markers, ident))
1621 goto bail;
1622 Py_CLEAR(ident);
1623 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001624 /* TODO DOES NOT RUN; dead code
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001625 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001626 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001627
1628 yield '\n' + (' ' * (_indent * _current_indent_level))
1629 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001630 if (_PyAccu_Accumulate(acc, close_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001631 goto bail;
1632 return 0;
1633
1634bail:
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001635 Py_XDECREF(it);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001636 Py_XDECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001637 Py_XDECREF(kstr);
1638 Py_XDECREF(ident);
1639 return -1;
1640}
1641
1642
1643static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001644encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001645 PyObject *seq, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001646{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001647 /* Encode Python list seq to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001648 static PyObject *open_array = NULL;
1649 static PyObject *close_array = NULL;
1650 static PyObject *empty_array = NULL;
1651 PyObject *ident = NULL;
1652 PyObject *s_fast = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001653 Py_ssize_t i;
1654
1655 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
1656 open_array = PyUnicode_InternFromString("[");
1657 close_array = PyUnicode_InternFromString("]");
1658 empty_array = PyUnicode_InternFromString("[]");
1659 if (open_array == NULL || close_array == NULL || empty_array == NULL)
1660 return -1;
1661 }
1662 ident = NULL;
1663 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
1664 if (s_fast == NULL)
1665 return -1;
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001666 if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001667 Py_DECREF(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001668 return _PyAccu_Accumulate(acc, empty_array);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001669 }
1670
1671 if (s->markers != Py_None) {
1672 int has_key;
1673 ident = PyLong_FromVoidPtr(seq);
1674 if (ident == NULL)
1675 goto bail;
1676 has_key = PyDict_Contains(s->markers, ident);
1677 if (has_key) {
1678 if (has_key != -1)
1679 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1680 goto bail;
1681 }
1682 if (PyDict_SetItem(s->markers, ident, seq)) {
1683 goto bail;
1684 }
1685 }
1686
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001687 if (_PyAccu_Accumulate(acc, open_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001688 goto bail;
1689 if (s->indent != Py_None) {
1690 /* TODO: DOES NOT RUN */
1691 indent_level += 1;
1692 /*
1693 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1694 separator = _item_separator + newline_indent
1695 buf += newline_indent
1696 */
1697 }
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001698 for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
1699 PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001700 if (i) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001701 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001702 goto bail;
1703 }
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001704 if (encoder_listencode_obj(s, acc, obj, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001705 goto bail;
1706 }
1707 if (ident != NULL) {
1708 if (PyDict_DelItem(s->markers, ident))
1709 goto bail;
1710 Py_CLEAR(ident);
1711 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001712
1713 /* TODO: DOES NOT RUN
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001714 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001715 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001716
1717 yield '\n' + (' ' * (_indent * _current_indent_level))
1718 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001719 if (_PyAccu_Accumulate(acc, close_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001720 goto bail;
1721 Py_DECREF(s_fast);
1722 return 0;
1723
1724bail:
1725 Py_XDECREF(ident);
1726 Py_DECREF(s_fast);
1727 return -1;
1728}
1729
1730static void
1731encoder_dealloc(PyObject *self)
1732{
1733 /* Deallocate Encoder */
1734 encoder_clear(self);
1735 Py_TYPE(self)->tp_free(self);
1736}
1737
1738static int
1739encoder_traverse(PyObject *self, visitproc visit, void *arg)
1740{
1741 PyEncoderObject *s;
1742 assert(PyEncoder_Check(self));
1743 s = (PyEncoderObject *)self;
1744 Py_VISIT(s->markers);
1745 Py_VISIT(s->defaultfn);
1746 Py_VISIT(s->encoder);
1747 Py_VISIT(s->indent);
1748 Py_VISIT(s->key_separator);
1749 Py_VISIT(s->item_separator);
1750 Py_VISIT(s->sort_keys);
1751 Py_VISIT(s->skipkeys);
1752 return 0;
1753}
1754
1755static int
1756encoder_clear(PyObject *self)
1757{
1758 /* Deallocate Encoder */
1759 PyEncoderObject *s;
1760 assert(PyEncoder_Check(self));
1761 s = (PyEncoderObject *)self;
1762 Py_CLEAR(s->markers);
1763 Py_CLEAR(s->defaultfn);
1764 Py_CLEAR(s->encoder);
1765 Py_CLEAR(s->indent);
1766 Py_CLEAR(s->key_separator);
1767 Py_CLEAR(s->item_separator);
1768 Py_CLEAR(s->sort_keys);
1769 Py_CLEAR(s->skipkeys);
1770 return 0;
1771}
1772
1773PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
1774
1775static
1776PyTypeObject PyEncoderType = {
1777 PyVarObject_HEAD_INIT(NULL, 0)
1778 "_json.Encoder", /* tp_name */
1779 sizeof(PyEncoderObject), /* tp_basicsize */
1780 0, /* tp_itemsize */
1781 encoder_dealloc, /* tp_dealloc */
1782 0, /* tp_print */
1783 0, /* tp_getattr */
1784 0, /* tp_setattr */
1785 0, /* tp_compare */
1786 0, /* tp_repr */
1787 0, /* tp_as_number */
1788 0, /* tp_as_sequence */
1789 0, /* tp_as_mapping */
1790 0, /* tp_hash */
1791 encoder_call, /* tp_call */
1792 0, /* tp_str */
1793 0, /* tp_getattro */
1794 0, /* tp_setattro */
1795 0, /* tp_as_buffer */
1796 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1797 encoder_doc, /* tp_doc */
1798 encoder_traverse, /* tp_traverse */
1799 encoder_clear, /* tp_clear */
1800 0, /* tp_richcompare */
1801 0, /* tp_weaklistoffset */
1802 0, /* tp_iter */
1803 0, /* tp_iternext */
1804 0, /* tp_methods */
1805 encoder_members, /* tp_members */
1806 0, /* tp_getset */
1807 0, /* tp_base */
1808 0, /* tp_dict */
1809 0, /* tp_descr_get */
1810 0, /* tp_descr_set */
1811 0, /* tp_dictoffset */
1812 encoder_init, /* tp_init */
1813 0, /* tp_alloc */
1814 encoder_new, /* tp_new */
1815 0, /* tp_free */
1816};
1817
1818static PyMethodDef speedups_methods[] = {
1819 {"encode_basestring_ascii",
1820 (PyCFunction)py_encode_basestring_ascii,
1821 METH_O,
1822 pydoc_encode_basestring_ascii},
1823 {"scanstring",
1824 (PyCFunction)py_scanstring,
1825 METH_VARARGS,
1826 pydoc_scanstring},
Christian Heimes90540002008-05-08 14:29:10 +00001827 {NULL, NULL, 0, NULL}
1828};
1829
1830PyDoc_STRVAR(module_doc,
1831"json speedups\n");
1832
Martin v. Löwis1a214512008-06-11 05:26:20 +00001833static struct PyModuleDef jsonmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 PyModuleDef_HEAD_INIT,
1835 "_json",
1836 module_doc,
1837 -1,
1838 speedups_methods,
1839 NULL,
1840 NULL,
1841 NULL,
1842 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001843};
1844
1845PyObject*
1846PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +00001847{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001848 PyObject *m = PyModule_Create(&jsonmodule);
1849 if (!m)
1850 return NULL;
1851 PyScannerType.tp_new = PyType_GenericNew;
1852 if (PyType_Ready(&PyScannerType) < 0)
1853 goto fail;
1854 PyEncoderType.tp_new = PyType_GenericNew;
1855 if (PyType_Ready(&PyEncoderType) < 0)
1856 goto fail;
1857 Py_INCREF((PyObject*)&PyScannerType);
1858 if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1859 Py_DECREF((PyObject*)&PyScannerType);
1860 goto fail;
1861 }
1862 Py_INCREF((PyObject*)&PyEncoderType);
1863 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1864 Py_DECREF((PyObject*)&PyEncoderType);
1865 goto fail;
1866 }
1867 return m;
1868 fail:
1869 Py_DECREF(m);
1870 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +00001871}