blob: db45c28fe4033fd875b53675bd3531b17439faa9 [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 */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000436 if ((c & 0xfc00) == 0xd800) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200437 Py_UCS4 c2 = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000438 if (end + 6 >= len) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000439 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
440 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000441 }
Victor Stinnerd9c06312011-10-11 21:56:19 +0200442 if (PyUnicode_READ(kind, buf, next++) != '\\' ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200443 PyUnicode_READ(kind, buf, next++) != 'u') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000444 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
445 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000446 }
447 end += 6;
448 /* Decode 4 hex digits */
449 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200450 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Antoine Pitrou5b0e9e82010-10-09 15:24:28 +0000451 c2 <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000452 switch (digit) {
453 case '0': case '1': case '2': case '3': case '4':
454 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000455 c2 |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000456 case 'a': case 'b': case 'c': case 'd': case 'e':
457 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000458 c2 |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000459 case 'A': case 'B': case 'C': case 'D': case 'E':
460 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000461 c2 |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000462 default:
463 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
464 goto bail;
465 }
466 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000467 if ((c2 & 0xfc00) != 0xdc00) {
468 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
469 goto bail;
470 }
Christian Heimes90540002008-05-08 14:29:10 +0000471 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
472 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000473 else if ((c & 0xfc00) == 0xdc00) {
474 raise_errmsg("Unpaired low surrogate", pystr, end - 5);
475 goto bail;
476 }
Christian Heimes90540002008-05-08 14:29:10 +0000477 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000478 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200479 chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1);
Christian Heimes90540002008-05-08 14:29:10 +0000480 if (chunk == NULL) {
481 goto bail;
482 }
Christian Heimes90540002008-05-08 14:29:10 +0000483 }
484
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000485 if (chunks == NULL) {
486 if (chunk != NULL)
487 rval = chunk;
488 else
489 rval = PyUnicode_FromStringAndSize("", 0);
Christian Heimes90540002008-05-08 14:29:10 +0000490 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000491 else {
492 APPEND_OLD_CHUNK
493 rval = join_list_unicode(chunks);
494 if (rval == NULL) {
495 goto bail;
496 }
497 Py_CLEAR(chunks);
498 }
499
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000500 *next_end_ptr = end;
501 return rval;
Christian Heimes90540002008-05-08 14:29:10 +0000502bail:
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000503 *next_end_ptr = -1;
Christian Heimes90540002008-05-08 14:29:10 +0000504 Py_XDECREF(chunks);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000505 Py_XDECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000506 return NULL;
507}
508
509PyDoc_STRVAR(pydoc_scanstring,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000510 "scanstring(string, end, strict=True) -> (string, end)\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000511 "\n"
512 "Scan the string s for a JSON string. End is the index of the\n"
513 "character in s after the quote that started the JSON string.\n"
514 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
515 "on attempt to decode an invalid string. If strict is False then literal\n"
516 "control characters are allowed in the string.\n"
517 "\n"
518 "Returns a tuple of the decoded string and the index of the character in s\n"
519 "after the end quote."
520);
Christian Heimes90540002008-05-08 14:29:10 +0000521
522static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000523py_scanstring(PyObject* self UNUSED, PyObject *args)
Christian Heimes90540002008-05-08 14:29:10 +0000524{
525 PyObject *pystr;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000526 PyObject *rval;
Christian Heimes90540002008-05-08 14:29:10 +0000527 Py_ssize_t end;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000528 Py_ssize_t next_end = -1;
529 int strict = 1;
530 if (!PyArg_ParseTuple(args, "OO&|i:scanstring", &pystr, _convertPyInt_AsSsize_t, &end, &strict)) {
Christian Heimes90540002008-05-08 14:29:10 +0000531 return NULL;
532 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000533 if (PyUnicode_Check(pystr)) {
534 rval = scanstring_unicode(pystr, end, strict, &next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000535 }
536 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 PyErr_Format(PyExc_TypeError,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000538 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000539 Py_TYPE(pystr)->tp_name);
540 return NULL;
541 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000542 return _build_rval_index_tuple(rval, next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000543}
544
545PyDoc_STRVAR(pydoc_encode_basestring_ascii,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000546 "encode_basestring_ascii(string) -> string\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000547 "\n"
548 "Return an ASCII-only JSON representation of a Python string"
549);
Christian Heimes90540002008-05-08 14:29:10 +0000550
551static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000552py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
Christian Heimes90540002008-05-08 14:29:10 +0000553{
554 PyObject *rval;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000555 /* Return an ASCII-only JSON representation of a Python string */
Christian Heimes90540002008-05-08 14:29:10 +0000556 /* METH_O */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000557 if (PyUnicode_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000558 rval = ascii_escape_unicode(pystr);
559 }
560 else {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000561 PyErr_Format(PyExc_TypeError,
562 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000563 Py_TYPE(pystr)->tp_name);
564 return NULL;
565 }
Christian Heimes90540002008-05-08 14:29:10 +0000566 return rval;
567}
568
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000569static void
570scanner_dealloc(PyObject *self)
571{
572 /* Deallocate scanner object */
573 scanner_clear(self);
574 Py_TYPE(self)->tp_free(self);
575}
576
577static int
578scanner_traverse(PyObject *self, visitproc visit, void *arg)
579{
580 PyScannerObject *s;
581 assert(PyScanner_Check(self));
582 s = (PyScannerObject *)self;
583 Py_VISIT(s->strict);
584 Py_VISIT(s->object_hook);
585 Py_VISIT(s->object_pairs_hook);
586 Py_VISIT(s->parse_float);
587 Py_VISIT(s->parse_int);
588 Py_VISIT(s->parse_constant);
589 return 0;
590}
591
592static int
593scanner_clear(PyObject *self)
594{
595 PyScannerObject *s;
596 assert(PyScanner_Check(self));
597 s = (PyScannerObject *)self;
598 Py_CLEAR(s->strict);
599 Py_CLEAR(s->object_hook);
600 Py_CLEAR(s->object_pairs_hook);
601 Py_CLEAR(s->parse_float);
602 Py_CLEAR(s->parse_int);
603 Py_CLEAR(s->parse_constant);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000604 Py_CLEAR(s->memo);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000605 return 0;
606}
607
608static PyObject *
609_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
610 /* Read a JSON object from PyUnicode pystr.
611 idx is the index of the first character after the opening curly brace.
612 *next_idx_ptr is a return-by-reference index to the first character after
613 the closing curly brace.
614
615 Returns a new PyObject (usually a dict, but object_hook can change that)
616 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200617 void *str;
618 int kind;
619 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000620 PyObject *val = NULL;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000621 PyObject *rval = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000622 PyObject *key = NULL;
623 int strict = PyObject_IsTrue(s->strict);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000624 int has_pairs_hook = (s->object_pairs_hook != Py_None);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000625 Py_ssize_t next_idx;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000626
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200627 if (PyUnicode_READY(pystr) == -1)
628 return NULL;
629
630 str = PyUnicode_DATA(pystr);
631 kind = PyUnicode_KIND(pystr);
632 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
633
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000634 if (has_pairs_hook)
635 rval = PyList_New(0);
636 else
637 rval = PyDict_New();
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000638 if (rval == NULL)
639 return NULL;
640
641 /* skip whitespace after { */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200642 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000643
644 /* only loop if the object is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200645 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000646 while (idx <= end_idx) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000647 PyObject *memokey;
648
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000649 /* read key */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200650 if (PyUnicode_READ(kind, str, idx) != '"') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200651 raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000652 goto bail;
653 }
654 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
655 if (key == NULL)
656 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000657 memokey = PyDict_GetItem(s->memo, key);
658 if (memokey != NULL) {
659 Py_INCREF(memokey);
660 Py_DECREF(key);
661 key = memokey;
662 }
663 else {
664 if (PyDict_SetItem(s->memo, key, key) < 0)
665 goto bail;
666 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000667 idx = next_idx;
668
669 /* skip whitespace between key and : delimiter, read :, skip whitespace */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200670 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
671 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200672 raise_errmsg("Expecting ':' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000673 goto bail;
674 }
675 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200676 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000677
678 /* read any JSON term */
679 val = scan_once_unicode(s, pystr, idx, &next_idx);
680 if (val == NULL)
681 goto bail;
682
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000683 if (has_pairs_hook) {
684 PyObject *item = PyTuple_Pack(2, key, val);
685 if (item == NULL)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000686 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000687 Py_CLEAR(key);
688 Py_CLEAR(val);
689 if (PyList_Append(rval, item) == -1) {
690 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000691 goto bail;
692 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000693 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000694 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000695 else {
696 if (PyDict_SetItem(rval, key, val) < 0)
697 goto bail;
698 Py_CLEAR(key);
699 Py_CLEAR(val);
700 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000701 idx = next_idx;
702
703 /* skip whitespace before } or , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200704 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000705
706 /* bail if the object is closed or we didn't get the , delimiter */
707 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200708 if (PyUnicode_READ(kind, str, idx) == '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000709 break;
710 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200711 else if (PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200712 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000713 goto bail;
714 }
715 idx++;
716
717 /* skip whitespace after , delimiter */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200718 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000719 }
720 }
721
722 /* verify that idx < end_idx, str[idx] should be '}' */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200723 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000724 raise_errmsg("Expecting object", pystr, end_idx);
725 goto bail;
726 }
727
728 *next_idx_ptr = idx + 1;
729
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000730 if (has_pairs_hook) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000731 val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000732 Py_DECREF(rval);
733 return val;
734 }
735
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000736 /* if object_hook is not None: rval = object_hook(rval) */
737 if (s->object_hook != Py_None) {
738 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000739 Py_DECREF(rval);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000740 return val;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000741 }
742 return rval;
743bail:
744 Py_XDECREF(key);
745 Py_XDECREF(val);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000746 Py_XDECREF(rval);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000747 return NULL;
748}
749
750static PyObject *
751_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
752 /* Read a JSON array from PyString pystr.
753 idx is the index of the first character after the opening brace.
754 *next_idx_ptr is a return-by-reference index to the first character after
755 the closing brace.
756
757 Returns a new PyList
758 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200759 void *str;
760 int kind;
761 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000762 PyObject *val = NULL;
763 PyObject *rval = PyList_New(0);
764 Py_ssize_t next_idx;
765 if (rval == NULL)
766 return NULL;
767
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200768 if (PyUnicode_READY(pystr) == -1)
769 return NULL;
770
771 str = PyUnicode_DATA(pystr);
772 kind = PyUnicode_KIND(pystr);
773 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
774
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000775 /* skip whitespace after [ */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200776 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000777
778 /* only loop if the array is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200779 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000780 while (idx <= end_idx) {
781
782 /* read any JSON term */
783 val = scan_once_unicode(s, pystr, idx, &next_idx);
784 if (val == NULL)
785 goto bail;
786
787 if (PyList_Append(rval, val) == -1)
788 goto bail;
789
790 Py_CLEAR(val);
791 idx = next_idx;
792
793 /* skip whitespace between term and , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200794 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000795
796 /* bail if the array is closed or we didn't get the , delimiter */
797 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200798 if (PyUnicode_READ(kind, str, idx) == ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000799 break;
800 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200801 else if (PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200802 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000803 goto bail;
804 }
805 idx++;
806
807 /* skip whitespace after , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200808 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000809 }
810 }
811
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200812 /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */
813 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000814 raise_errmsg("Expecting object", pystr, end_idx);
815 goto bail;
816 }
817 *next_idx_ptr = idx + 1;
818 return rval;
819bail:
820 Py_XDECREF(val);
821 Py_DECREF(rval);
822 return NULL;
823}
824
825static PyObject *
826_parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
827 /* Read a JSON constant from PyString pystr.
828 constant is the constant string that was found
829 ("NaN", "Infinity", "-Infinity").
830 idx is the index of the first character of the constant
831 *next_idx_ptr is a return-by-reference index to the first character after
832 the constant.
833
834 Returns the result of parse_constant
835 */
836 PyObject *cstr;
837 PyObject *rval;
838 /* constant is "NaN", "Infinity", or "-Infinity" */
839 cstr = PyUnicode_InternFromString(constant);
840 if (cstr == NULL)
841 return NULL;
842
843 /* rval = parse_constant(constant) */
844 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
Victor Stinnerc4f281e2011-10-11 22:11:42 +0200845 idx += PyUnicode_GET_LENGTH(cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000846 Py_DECREF(cstr);
847 *next_idx_ptr = idx;
848 return rval;
849}
850
851static PyObject *
852_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
853 /* Read a JSON number from PyUnicode pystr.
854 idx is the index of the first character of the number
855 *next_idx_ptr is a return-by-reference index to the first character after
856 the number.
857
858 Returns a new PyObject representation of that number:
859 PyInt, PyLong, or PyFloat.
860 May return other types if parse_int or parse_float are set
861 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200862 void *str;
863 int kind;
864 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000865 Py_ssize_t idx = start;
866 int is_float = 0;
867 PyObject *rval;
Antoine Pitrouf6454512011-04-25 19:16:06 +0200868 PyObject *numstr = NULL;
869 PyObject *custom_func;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000870
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200871 if (PyUnicode_READY(pystr) == -1)
872 return NULL;
873
874 str = PyUnicode_DATA(pystr);
875 kind = PyUnicode_KIND(pystr);
876 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
877
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000878 /* 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 +0200879 if (PyUnicode_READ(kind, str, idx) == '-') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000880 idx++;
881 if (idx > end_idx) {
882 PyErr_SetNone(PyExc_StopIteration);
883 return NULL;
884 }
885 }
886
887 /* 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 +0200888 if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000889 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200890 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000891 }
892 /* if it starts with 0 we only expect one integer digit */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200893 else if (PyUnicode_READ(kind, str, idx) == '0') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000894 idx++;
895 }
896 /* no integer digits, error */
897 else {
898 PyErr_SetNone(PyExc_StopIteration);
899 return NULL;
900 }
901
902 /* if the next char is '.' followed by a digit then read all float digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200903 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 +0000904 is_float = 1;
905 idx += 2;
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
909 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200910 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000911 Py_ssize_t e_start = idx;
912 idx++;
913
914 /* read an exponent sign if present */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200915 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000916
917 /* read all digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200918 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000919
920 /* if we got a digit, then parse as float. if not, backtrack */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200921 if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000922 is_float = 1;
923 }
924 else {
925 idx = e_start;
926 }
927 }
928
Antoine Pitrouf6454512011-04-25 19:16:06 +0200929 if (is_float && s->parse_float != (PyObject *)&PyFloat_Type)
930 custom_func = s->parse_float;
931 else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type)
932 custom_func = s->parse_int;
933 else
934 custom_func = NULL;
935
936 if (custom_func) {
937 /* copy the section we determined to be a number */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200938 numstr = PyUnicode_FromKindAndData(kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200939 (char*)str + kind * start,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200940 idx - start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200941 if (numstr == NULL)
942 return NULL;
943 rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000944 }
945 else {
Antoine Pitrouf6454512011-04-25 19:16:06 +0200946 Py_ssize_t i, n;
947 char *buf;
948 /* Straight conversion to ASCII, to avoid costly conversion of
949 decimal unicode digits (which cannot appear here) */
950 n = idx - start;
951 numstr = PyBytes_FromStringAndSize(NULL, n);
952 if (numstr == NULL)
953 return NULL;
954 buf = PyBytes_AS_STRING(numstr);
955 for (i = 0; i < n; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200956 buf[i] = (char) PyUnicode_READ(kind, str, i + start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200957 }
958 if (is_float)
959 rval = PyFloat_FromString(numstr);
960 else
961 rval = PyLong_FromString(buf, NULL, 10);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000962 }
963 Py_DECREF(numstr);
964 *next_idx_ptr = idx;
965 return rval;
966}
967
968static PyObject *
969scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
970{
971 /* Read one JSON term (of any kind) from PyUnicode pystr.
972 idx is the index of the first character of the term
973 *next_idx_ptr is a return-by-reference index to the first character after
974 the number.
975
976 Returns a new PyObject representation of the term.
977 */
Ezio Melotti362b9512011-05-07 17:58:09 +0300978 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200979 void *str;
980 int kind;
981 Py_ssize_t length;
982
983 if (PyUnicode_READY(pystr) == -1)
984 return NULL;
985
986 str = PyUnicode_DATA(pystr);
987 kind = PyUnicode_KIND(pystr);
988 length = PyUnicode_GET_LENGTH(pystr);
989
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000990 if (idx >= length) {
991 PyErr_SetNone(PyExc_StopIteration);
992 return NULL;
993 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200994
995 switch (PyUnicode_READ(kind, str, idx)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000996 case '"':
997 /* string */
998 return scanstring_unicode(pystr, idx + 1,
999 PyObject_IsTrue(s->strict),
1000 next_idx_ptr);
1001 case '{':
1002 /* object */
Ezio Melotti362b9512011-05-07 17:58:09 +03001003 if (Py_EnterRecursiveCall(" while decoding a JSON object "
1004 "from a unicode string"))
1005 return NULL;
1006 res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
1007 Py_LeaveRecursiveCall();
1008 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001009 case '[':
1010 /* array */
Ezio Melotti362b9512011-05-07 17:58:09 +03001011 if (Py_EnterRecursiveCall(" while decoding a JSON array "
1012 "from a unicode string"))
1013 return NULL;
1014 res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
1015 Py_LeaveRecursiveCall();
1016 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001017 case 'n':
1018 /* null */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001019 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 +00001020 Py_INCREF(Py_None);
1021 *next_idx_ptr = idx + 4;
1022 return Py_None;
1023 }
1024 break;
1025 case 't':
1026 /* true */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001027 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 +00001028 Py_INCREF(Py_True);
1029 *next_idx_ptr = idx + 4;
1030 return Py_True;
1031 }
1032 break;
1033 case 'f':
1034 /* false */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001035 if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
1036 PyUnicode_READ(kind, str, idx + 2) == 'l' &&
1037 PyUnicode_READ(kind, str, idx + 3) == 's' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001038 PyUnicode_READ(kind, str, idx + 4) == 'e') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001039 Py_INCREF(Py_False);
1040 *next_idx_ptr = idx + 5;
1041 return Py_False;
1042 }
1043 break;
1044 case 'N':
1045 /* NaN */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001046 if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001047 PyUnicode_READ(kind, str, idx + 2) == 'N') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001048 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1049 }
1050 break;
1051 case 'I':
1052 /* Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001053 if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
1054 PyUnicode_READ(kind, str, idx + 2) == 'f' &&
1055 PyUnicode_READ(kind, str, idx + 3) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001056 PyUnicode_READ(kind, str, idx + 4) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001057 PyUnicode_READ(kind, str, idx + 5) == 'i' &&
1058 PyUnicode_READ(kind, str, idx + 6) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001059 PyUnicode_READ(kind, str, idx + 7) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001060 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1061 }
1062 break;
1063 case '-':
1064 /* -Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001065 if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001066 PyUnicode_READ(kind, str, idx + 2) == 'n' &&
1067 PyUnicode_READ(kind, str, idx + 3) == 'f' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001068 PyUnicode_READ(kind, str, idx + 4) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001069 PyUnicode_READ(kind, str, idx + 5) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001070 PyUnicode_READ(kind, str, idx + 6) == 'i' &&
1071 PyUnicode_READ(kind, str, idx + 7) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001072 PyUnicode_READ(kind, str, idx + 8) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001073 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1074 }
1075 break;
1076 }
1077 /* Didn't find a string, object, array, or named constant. Look for a number. */
1078 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
1079}
1080
1081static PyObject *
1082scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
1083{
1084 /* Python callable interface to scan_once_{str,unicode} */
1085 PyObject *pystr;
1086 PyObject *rval;
1087 Py_ssize_t idx;
1088 Py_ssize_t next_idx = -1;
1089 static char *kwlist[] = {"string", "idx", NULL};
1090 PyScannerObject *s;
1091 assert(PyScanner_Check(self));
1092 s = (PyScannerObject *)self;
1093 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:scan_once", kwlist, &pystr, _convertPyInt_AsSsize_t, &idx))
1094 return NULL;
1095
1096 if (PyUnicode_Check(pystr)) {
1097 rval = scan_once_unicode(s, pystr, idx, &next_idx);
1098 }
1099 else {
1100 PyErr_Format(PyExc_TypeError,
1101 "first argument must be a string, not %.80s",
1102 Py_TYPE(pystr)->tp_name);
1103 return NULL;
1104 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001105 PyDict_Clear(s->memo);
1106 if (rval == NULL)
1107 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001108 return _build_rval_index_tuple(rval, next_idx);
1109}
1110
1111static PyObject *
1112scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1113{
1114 PyScannerObject *s;
1115 s = (PyScannerObject *)type->tp_alloc(type, 0);
1116 if (s != NULL) {
1117 s->strict = NULL;
1118 s->object_hook = NULL;
1119 s->object_pairs_hook = NULL;
1120 s->parse_float = NULL;
1121 s->parse_int = NULL;
1122 s->parse_constant = NULL;
1123 }
1124 return (PyObject *)s;
1125}
1126
1127static int
1128scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1129{
1130 /* Initialize Scanner object */
1131 PyObject *ctx;
1132 static char *kwlist[] = {"context", NULL};
1133 PyScannerObject *s;
1134
1135 assert(PyScanner_Check(self));
1136 s = (PyScannerObject *)self;
1137
1138 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1139 return -1;
1140
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001141 if (s->memo == NULL) {
1142 s->memo = PyDict_New();
1143 if (s->memo == NULL)
1144 goto bail;
1145 }
1146
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001147 /* All of these will fail "gracefully" so we don't need to verify them */
1148 s->strict = PyObject_GetAttrString(ctx, "strict");
1149 if (s->strict == NULL)
1150 goto bail;
1151 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1152 if (s->object_hook == NULL)
1153 goto bail;
1154 s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1155 if (s->object_pairs_hook == NULL)
1156 goto bail;
1157 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1158 if (s->parse_float == NULL)
1159 goto bail;
1160 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1161 if (s->parse_int == NULL)
1162 goto bail;
1163 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1164 if (s->parse_constant == NULL)
1165 goto bail;
1166
1167 return 0;
1168
1169bail:
1170 Py_CLEAR(s->strict);
1171 Py_CLEAR(s->object_hook);
1172 Py_CLEAR(s->object_pairs_hook);
1173 Py_CLEAR(s->parse_float);
1174 Py_CLEAR(s->parse_int);
1175 Py_CLEAR(s->parse_constant);
1176 return -1;
1177}
1178
1179PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1180
1181static
1182PyTypeObject PyScannerType = {
1183 PyVarObject_HEAD_INIT(NULL, 0)
1184 "_json.Scanner", /* tp_name */
1185 sizeof(PyScannerObject), /* tp_basicsize */
1186 0, /* tp_itemsize */
1187 scanner_dealloc, /* tp_dealloc */
1188 0, /* tp_print */
1189 0, /* tp_getattr */
1190 0, /* tp_setattr */
1191 0, /* tp_compare */
1192 0, /* tp_repr */
1193 0, /* tp_as_number */
1194 0, /* tp_as_sequence */
1195 0, /* tp_as_mapping */
1196 0, /* tp_hash */
1197 scanner_call, /* tp_call */
1198 0, /* tp_str */
1199 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1200 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1201 0, /* tp_as_buffer */
1202 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1203 scanner_doc, /* tp_doc */
1204 scanner_traverse, /* tp_traverse */
1205 scanner_clear, /* tp_clear */
1206 0, /* tp_richcompare */
1207 0, /* tp_weaklistoffset */
1208 0, /* tp_iter */
1209 0, /* tp_iternext */
1210 0, /* tp_methods */
1211 scanner_members, /* tp_members */
1212 0, /* tp_getset */
1213 0, /* tp_base */
1214 0, /* tp_dict */
1215 0, /* tp_descr_get */
1216 0, /* tp_descr_set */
1217 0, /* tp_dictoffset */
1218 scanner_init, /* tp_init */
1219 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1220 scanner_new, /* tp_new */
1221 0,/* PyObject_GC_Del, */ /* tp_free */
1222};
1223
1224static PyObject *
1225encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1226{
1227 PyEncoderObject *s;
1228 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1229 if (s != NULL) {
1230 s->markers = NULL;
1231 s->defaultfn = NULL;
1232 s->encoder = NULL;
1233 s->indent = NULL;
1234 s->key_separator = NULL;
1235 s->item_separator = NULL;
1236 s->sort_keys = NULL;
1237 s->skipkeys = NULL;
1238 }
1239 return (PyObject *)s;
1240}
1241
1242static int
1243encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1244{
1245 /* initialize Encoder object */
1246 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1247
1248 PyEncoderObject *s;
Antoine Pitrou781eba72009-12-08 15:57:31 +00001249 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1250 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001251
1252 assert(PyEncoder_Check(self));
1253 s = (PyEncoderObject *)self;
1254
1255 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
Antoine Pitrou781eba72009-12-08 15:57:31 +00001256 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1257 &sort_keys, &skipkeys, &allow_nan))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001258 return -1;
1259
Antoine Pitrou781eba72009-12-08 15:57:31 +00001260 s->markers = markers;
1261 s->defaultfn = defaultfn;
1262 s->encoder = encoder;
1263 s->indent = indent;
1264 s->key_separator = key_separator;
1265 s->item_separator = item_separator;
1266 s->sort_keys = sort_keys;
1267 s->skipkeys = skipkeys;
1268 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1269 s->allow_nan = PyObject_IsTrue(allow_nan);
1270
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001271 Py_INCREF(s->markers);
1272 Py_INCREF(s->defaultfn);
1273 Py_INCREF(s->encoder);
1274 Py_INCREF(s->indent);
1275 Py_INCREF(s->key_separator);
1276 Py_INCREF(s->item_separator);
1277 Py_INCREF(s->sort_keys);
1278 Py_INCREF(s->skipkeys);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001279 return 0;
1280}
1281
1282static PyObject *
1283encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1284{
1285 /* Python callable interface to encode_listencode_obj */
1286 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1287 PyObject *obj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001288 Py_ssize_t indent_level;
1289 PyEncoderObject *s;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001290 _PyAccu acc;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001291
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001292 assert(PyEncoder_Check(self));
1293 s = (PyEncoderObject *)self;
1294 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:_iterencode", kwlist,
1295 &obj, _convertPyInt_AsSsize_t, &indent_level))
1296 return NULL;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001297 if (_PyAccu_Init(&acc))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001298 return NULL;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001299 if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001300 _PyAccu_Destroy(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001301 return NULL;
1302 }
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001303 return _PyAccu_FinishAsList(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001304}
1305
1306static PyObject *
1307_encoded_const(PyObject *obj)
1308{
1309 /* Return the JSON string representation of None, True, False */
1310 if (obj == Py_None) {
1311 static PyObject *s_null = NULL;
1312 if (s_null == NULL) {
1313 s_null = PyUnicode_InternFromString("null");
1314 }
1315 Py_INCREF(s_null);
1316 return s_null;
1317 }
1318 else if (obj == Py_True) {
1319 static PyObject *s_true = NULL;
1320 if (s_true == NULL) {
1321 s_true = PyUnicode_InternFromString("true");
1322 }
1323 Py_INCREF(s_true);
1324 return s_true;
1325 }
1326 else if (obj == Py_False) {
1327 static PyObject *s_false = NULL;
1328 if (s_false == NULL) {
1329 s_false = PyUnicode_InternFromString("false");
1330 }
1331 Py_INCREF(s_false);
1332 return s_false;
1333 }
1334 else {
1335 PyErr_SetString(PyExc_ValueError, "not a const");
1336 return NULL;
1337 }
1338}
1339
1340static PyObject *
1341encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1342{
1343 /* Return the JSON representation of a PyFloat */
1344 double i = PyFloat_AS_DOUBLE(obj);
1345 if (!Py_IS_FINITE(i)) {
1346 if (!s->allow_nan) {
1347 PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant");
1348 return NULL;
1349 }
1350 if (i > 0) {
1351 return PyUnicode_FromString("Infinity");
1352 }
1353 else if (i < 0) {
1354 return PyUnicode_FromString("-Infinity");
1355 }
1356 else {
1357 return PyUnicode_FromString("NaN");
1358 }
1359 }
1360 /* Use a better float format here? */
1361 return PyObject_Repr(obj);
1362}
1363
1364static PyObject *
1365encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1366{
1367 /* Return the JSON representation of a string */
1368 if (s->fast_encode)
1369 return py_encode_basestring_ascii(NULL, obj);
1370 else
1371 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1372}
1373
1374static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001375_steal_accumulate(_PyAccu *acc, PyObject *stolen)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001376{
1377 /* Append stolen and then decrement its reference count */
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001378 int rval = _PyAccu_Accumulate(acc, stolen);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001379 Py_DECREF(stolen);
1380 return rval;
1381}
1382
1383static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001384encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001385 PyObject *obj, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001386{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001387 /* Encode Python object obj to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001388 PyObject *newobj;
1389 int rv;
1390
1391 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1392 PyObject *cstr = _encoded_const(obj);
1393 if (cstr == NULL)
1394 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001395 return _steal_accumulate(acc, cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001396 }
1397 else if (PyUnicode_Check(obj))
1398 {
1399 PyObject *encoded = encoder_encode_string(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 (PyLong_Check(obj)) {
1405 PyObject *encoded = PyObject_Str(obj);
1406 if (encoded == NULL)
1407 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001408 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001409 }
1410 else if (PyFloat_Check(obj)) {
1411 PyObject *encoded = encoder_encode_float(s, obj);
1412 if (encoded == NULL)
1413 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001414 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001415 }
1416 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001417 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1418 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001419 rv = encoder_listencode_list(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001420 Py_LeaveRecursiveCall();
1421 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001422 }
1423 else if (PyDict_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001424 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1425 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001426 rv = encoder_listencode_dict(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001427 Py_LeaveRecursiveCall();
1428 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001429 }
1430 else {
1431 PyObject *ident = NULL;
1432 if (s->markers != Py_None) {
1433 int has_key;
1434 ident = PyLong_FromVoidPtr(obj);
1435 if (ident == NULL)
1436 return -1;
1437 has_key = PyDict_Contains(s->markers, ident);
1438 if (has_key) {
1439 if (has_key != -1)
1440 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1441 Py_DECREF(ident);
1442 return -1;
1443 }
1444 if (PyDict_SetItem(s->markers, ident, obj)) {
1445 Py_DECREF(ident);
1446 return -1;
1447 }
1448 }
1449 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1450 if (newobj == NULL) {
1451 Py_XDECREF(ident);
1452 return -1;
1453 }
Ezio Melotti13672652011-05-11 01:02:56 +03001454
1455 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1456 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001457 rv = encoder_listencode_obj(s, acc, newobj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001458 Py_LeaveRecursiveCall();
1459
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001460 Py_DECREF(newobj);
1461 if (rv) {
1462 Py_XDECREF(ident);
1463 return -1;
1464 }
1465 if (ident != NULL) {
1466 if (PyDict_DelItem(s->markers, ident)) {
1467 Py_XDECREF(ident);
1468 return -1;
1469 }
1470 Py_XDECREF(ident);
1471 }
1472 return rv;
1473 }
1474}
1475
1476static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001477encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001478 PyObject *dct, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001479{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001480 /* Encode Python dict dct a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001481 static PyObject *open_dict = NULL;
1482 static PyObject *close_dict = NULL;
1483 static PyObject *empty_dict = NULL;
1484 PyObject *kstr = NULL;
1485 PyObject *ident = NULL;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001486 PyObject *it = NULL;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001487 PyObject *items;
1488 PyObject *item = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001489 int skipkeys;
1490 Py_ssize_t idx;
1491
1492 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
1493 open_dict = PyUnicode_InternFromString("{");
1494 close_dict = PyUnicode_InternFromString("}");
1495 empty_dict = PyUnicode_InternFromString("{}");
1496 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
1497 return -1;
1498 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001499 if (Py_SIZE(dct) == 0)
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001500 return _PyAccu_Accumulate(acc, empty_dict);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001501
1502 if (s->markers != Py_None) {
1503 int has_key;
1504 ident = PyLong_FromVoidPtr(dct);
1505 if (ident == NULL)
1506 goto bail;
1507 has_key = PyDict_Contains(s->markers, ident);
1508 if (has_key) {
1509 if (has_key != -1)
1510 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1511 goto bail;
1512 }
1513 if (PyDict_SetItem(s->markers, ident, dct)) {
1514 goto bail;
1515 }
1516 }
1517
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001518 if (_PyAccu_Accumulate(acc, open_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001519 goto bail;
1520
1521 if (s->indent != Py_None) {
1522 /* TODO: DOES NOT RUN */
1523 indent_level += 1;
1524 /*
1525 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1526 separator = _item_separator + newline_indent
1527 buf += newline_indent
1528 */
1529 }
1530
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001531 if (PyObject_IsTrue(s->sort_keys)) {
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001532 /* First sort the keys then replace them with (key, value) tuples. */
1533 Py_ssize_t i, nitems;
1534 items = PyMapping_Keys(dct);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 if (items == NULL)
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001536 goto bail;
1537 if (!PyList_Check(items)) {
1538 PyErr_SetString(PyExc_ValueError, "keys must return list");
1539 goto bail;
1540 }
1541 if (PyList_Sort(items) < 0)
1542 goto bail;
1543 nitems = PyList_GET_SIZE(items);
1544 for (i = 0; i < nitems; i++) {
1545 PyObject *key, *value;
1546 key = PyList_GET_ITEM(items, i);
1547 value = PyDict_GetItem(dct, key);
1548 item = PyTuple_Pack(2, key, value);
1549 if (item == NULL)
1550 goto bail;
1551 PyList_SET_ITEM(items, i, item);
1552 Py_DECREF(key);
1553 }
1554 }
1555 else {
1556 items = PyMapping_Items(dct);
1557 }
1558 if (items == NULL)
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001559 goto bail;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001560 it = PyObject_GetIter(items);
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001561 Py_DECREF(items);
1562 if (it == NULL)
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001563 goto bail;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001564 skipkeys = PyObject_IsTrue(s->skipkeys);
1565 idx = 0;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001566 while ((item = PyIter_Next(it)) != NULL) {
1567 PyObject *encoded, *key, *value;
1568 if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
1569 PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
1570 goto bail;
1571 }
1572 key = PyTuple_GET_ITEM(item, 0);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001573 if (PyUnicode_Check(key)) {
1574 Py_INCREF(key);
1575 kstr = key;
1576 }
1577 else if (PyFloat_Check(key)) {
1578 kstr = encoder_encode_float(s, key);
1579 if (kstr == NULL)
1580 goto bail;
1581 }
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001582 else if (key == Py_True || key == Py_False || key == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 /* This must come before the PyLong_Check because
1584 True and False are also 1 and 0.*/
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001585 kstr = _encoded_const(key);
1586 if (kstr == NULL)
1587 goto bail;
1588 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001589 else if (PyLong_Check(key)) {
1590 kstr = PyObject_Str(key);
1591 if (kstr == NULL)
1592 goto bail;
1593 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001594 else if (skipkeys) {
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001595 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001596 continue;
1597 }
1598 else {
1599 /* TODO: include repr of key */
Doug Hellmann1c524752010-07-21 12:29:04 +00001600 PyErr_SetString(PyExc_TypeError, "keys must be a string");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001601 goto bail;
1602 }
1603
1604 if (idx) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001605 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001606 goto bail;
1607 }
1608
1609 encoded = encoder_encode_string(s, kstr);
1610 Py_CLEAR(kstr);
1611 if (encoded == NULL)
1612 goto bail;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001613 if (_PyAccu_Accumulate(acc, encoded)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001614 Py_DECREF(encoded);
1615 goto bail;
1616 }
1617 Py_DECREF(encoded);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001618 if (_PyAccu_Accumulate(acc, s->key_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001619 goto bail;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001620
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001621 value = PyTuple_GET_ITEM(item, 1);
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001622 if (encoder_listencode_obj(s, acc, value, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001623 goto bail;
1624 idx += 1;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001625 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001626 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001627 if (PyErr_Occurred())
1628 goto bail;
1629 Py_CLEAR(it);
1630
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001631 if (ident != NULL) {
1632 if (PyDict_DelItem(s->markers, ident))
1633 goto bail;
1634 Py_CLEAR(ident);
1635 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001636 /* TODO DOES NOT RUN; dead code
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001637 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001638 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001639
1640 yield '\n' + (' ' * (_indent * _current_indent_level))
1641 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001642 if (_PyAccu_Accumulate(acc, close_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001643 goto bail;
1644 return 0;
1645
1646bail:
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001647 Py_XDECREF(it);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001648 Py_XDECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001649 Py_XDECREF(kstr);
1650 Py_XDECREF(ident);
1651 return -1;
1652}
1653
1654
1655static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001656encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001657 PyObject *seq, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001658{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001659 /* Encode Python list seq to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001660 static PyObject *open_array = NULL;
1661 static PyObject *close_array = NULL;
1662 static PyObject *empty_array = NULL;
1663 PyObject *ident = NULL;
1664 PyObject *s_fast = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001665 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;
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001678 if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001679 Py_DECREF(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001680 return _PyAccu_Accumulate(acc, empty_array);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001681 }
1682
1683 if (s->markers != Py_None) {
1684 int has_key;
1685 ident = PyLong_FromVoidPtr(seq);
1686 if (ident == NULL)
1687 goto bail;
1688 has_key = PyDict_Contains(s->markers, ident);
1689 if (has_key) {
1690 if (has_key != -1)
1691 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1692 goto bail;
1693 }
1694 if (PyDict_SetItem(s->markers, ident, seq)) {
1695 goto bail;
1696 }
1697 }
1698
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001699 if (_PyAccu_Accumulate(acc, open_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001700 goto bail;
1701 if (s->indent != Py_None) {
1702 /* TODO: DOES NOT RUN */
1703 indent_level += 1;
1704 /*
1705 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1706 separator = _item_separator + newline_indent
1707 buf += newline_indent
1708 */
1709 }
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001710 for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
1711 PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001712 if (i) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001713 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001714 goto bail;
1715 }
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001716 if (encoder_listencode_obj(s, acc, obj, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001717 goto bail;
1718 }
1719 if (ident != NULL) {
1720 if (PyDict_DelItem(s->markers, ident))
1721 goto bail;
1722 Py_CLEAR(ident);
1723 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001724
1725 /* TODO: DOES NOT RUN
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001726 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001727 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001728
1729 yield '\n' + (' ' * (_indent * _current_indent_level))
1730 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001731 if (_PyAccu_Accumulate(acc, close_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001732 goto bail;
1733 Py_DECREF(s_fast);
1734 return 0;
1735
1736bail:
1737 Py_XDECREF(ident);
1738 Py_DECREF(s_fast);
1739 return -1;
1740}
1741
1742static void
1743encoder_dealloc(PyObject *self)
1744{
1745 /* Deallocate Encoder */
1746 encoder_clear(self);
1747 Py_TYPE(self)->tp_free(self);
1748}
1749
1750static int
1751encoder_traverse(PyObject *self, visitproc visit, void *arg)
1752{
1753 PyEncoderObject *s;
1754 assert(PyEncoder_Check(self));
1755 s = (PyEncoderObject *)self;
1756 Py_VISIT(s->markers);
1757 Py_VISIT(s->defaultfn);
1758 Py_VISIT(s->encoder);
1759 Py_VISIT(s->indent);
1760 Py_VISIT(s->key_separator);
1761 Py_VISIT(s->item_separator);
1762 Py_VISIT(s->sort_keys);
1763 Py_VISIT(s->skipkeys);
1764 return 0;
1765}
1766
1767static int
1768encoder_clear(PyObject *self)
1769{
1770 /* Deallocate Encoder */
1771 PyEncoderObject *s;
1772 assert(PyEncoder_Check(self));
1773 s = (PyEncoderObject *)self;
1774 Py_CLEAR(s->markers);
1775 Py_CLEAR(s->defaultfn);
1776 Py_CLEAR(s->encoder);
1777 Py_CLEAR(s->indent);
1778 Py_CLEAR(s->key_separator);
1779 Py_CLEAR(s->item_separator);
1780 Py_CLEAR(s->sort_keys);
1781 Py_CLEAR(s->skipkeys);
1782 return 0;
1783}
1784
1785PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
1786
1787static
1788PyTypeObject PyEncoderType = {
1789 PyVarObject_HEAD_INIT(NULL, 0)
1790 "_json.Encoder", /* tp_name */
1791 sizeof(PyEncoderObject), /* tp_basicsize */
1792 0, /* tp_itemsize */
1793 encoder_dealloc, /* tp_dealloc */
1794 0, /* tp_print */
1795 0, /* tp_getattr */
1796 0, /* tp_setattr */
1797 0, /* tp_compare */
1798 0, /* tp_repr */
1799 0, /* tp_as_number */
1800 0, /* tp_as_sequence */
1801 0, /* tp_as_mapping */
1802 0, /* tp_hash */
1803 encoder_call, /* tp_call */
1804 0, /* tp_str */
1805 0, /* tp_getattro */
1806 0, /* tp_setattro */
1807 0, /* tp_as_buffer */
1808 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1809 encoder_doc, /* tp_doc */
1810 encoder_traverse, /* tp_traverse */
1811 encoder_clear, /* tp_clear */
1812 0, /* tp_richcompare */
1813 0, /* tp_weaklistoffset */
1814 0, /* tp_iter */
1815 0, /* tp_iternext */
1816 0, /* tp_methods */
1817 encoder_members, /* tp_members */
1818 0, /* tp_getset */
1819 0, /* tp_base */
1820 0, /* tp_dict */
1821 0, /* tp_descr_get */
1822 0, /* tp_descr_set */
1823 0, /* tp_dictoffset */
1824 encoder_init, /* tp_init */
1825 0, /* tp_alloc */
1826 encoder_new, /* tp_new */
1827 0, /* tp_free */
1828};
1829
1830static PyMethodDef speedups_methods[] = {
1831 {"encode_basestring_ascii",
1832 (PyCFunction)py_encode_basestring_ascii,
1833 METH_O,
1834 pydoc_encode_basestring_ascii},
1835 {"scanstring",
1836 (PyCFunction)py_scanstring,
1837 METH_VARARGS,
1838 pydoc_scanstring},
Christian Heimes90540002008-05-08 14:29:10 +00001839 {NULL, NULL, 0, NULL}
1840};
1841
1842PyDoc_STRVAR(module_doc,
1843"json speedups\n");
1844
Martin v. Löwis1a214512008-06-11 05:26:20 +00001845static struct PyModuleDef jsonmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 PyModuleDef_HEAD_INIT,
1847 "_json",
1848 module_doc,
1849 -1,
1850 speedups_methods,
1851 NULL,
1852 NULL,
1853 NULL,
1854 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001855};
1856
1857PyObject*
1858PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +00001859{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001860 PyObject *m = PyModule_Create(&jsonmodule);
1861 if (!m)
1862 return NULL;
1863 PyScannerType.tp_new = PyType_GenericNew;
1864 if (PyType_Ready(&PyScannerType) < 0)
1865 goto fail;
1866 PyEncoderType.tp_new = PyType_GenericNew;
1867 if (PyType_Ready(&PyEncoderType) < 0)
1868 goto fail;
1869 Py_INCREF((PyObject*)&PyScannerType);
1870 if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1871 Py_DECREF((PyObject*)&PyScannerType);
1872 goto fail;
1873 }
1874 Py_INCREF((PyObject*)&PyEncoderType);
1875 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1876 Py_DECREF((PyObject*)&PyEncoderType);
1877 goto fail;
1878 }
1879 return m;
1880 fail:
1881 Py_DECREF(m);
1882 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +00001883}