blob: 4bc585dc2ab0d610b8019126e7df388838f9ade2 [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 Peterson6ef2b362014-04-14 11:45:21 -0400978 if (idx < 0) {
Benjamin Peterson9beee042014-04-14 11:46:51 -0400979 PyErr_SetString(PyExc_ValueError, "idx cannot be negative");
Benjamin Peterson6ef2b362014-04-14 11:45:21 -0400980 return NULL;
981 }
982 if (idx >= length) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000983 PyErr_SetNone(PyExc_StopIteration);
984 return NULL;
985 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200986
987 switch (PyUnicode_READ(kind, str, idx)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000988 case '"':
989 /* string */
990 return scanstring_unicode(pystr, idx + 1,
991 PyObject_IsTrue(s->strict),
992 next_idx_ptr);
993 case '{':
994 /* object */
Ezio Melotti362b9512011-05-07 17:58:09 +0300995 if (Py_EnterRecursiveCall(" while decoding a JSON object "
996 "from a unicode string"))
997 return NULL;
998 res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
999 Py_LeaveRecursiveCall();
1000 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001001 case '[':
1002 /* array */
Ezio Melotti362b9512011-05-07 17:58:09 +03001003 if (Py_EnterRecursiveCall(" while decoding a JSON array "
1004 "from a unicode string"))
1005 return NULL;
1006 res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
1007 Py_LeaveRecursiveCall();
1008 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001009 case 'n':
1010 /* null */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001011 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 +00001012 Py_INCREF(Py_None);
1013 *next_idx_ptr = idx + 4;
1014 return Py_None;
1015 }
1016 break;
1017 case 't':
1018 /* true */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001019 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 +00001020 Py_INCREF(Py_True);
1021 *next_idx_ptr = idx + 4;
1022 return Py_True;
1023 }
1024 break;
1025 case 'f':
1026 /* false */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001027 if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
1028 PyUnicode_READ(kind, str, idx + 2) == 'l' &&
1029 PyUnicode_READ(kind, str, idx + 3) == 's' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001030 PyUnicode_READ(kind, str, idx + 4) == 'e') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001031 Py_INCREF(Py_False);
1032 *next_idx_ptr = idx + 5;
1033 return Py_False;
1034 }
1035 break;
1036 case 'N':
1037 /* NaN */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001038 if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001039 PyUnicode_READ(kind, str, idx + 2) == 'N') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001040 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1041 }
1042 break;
1043 case 'I':
1044 /* Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001045 if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
1046 PyUnicode_READ(kind, str, idx + 2) == 'f' &&
1047 PyUnicode_READ(kind, str, idx + 3) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001048 PyUnicode_READ(kind, str, idx + 4) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001049 PyUnicode_READ(kind, str, idx + 5) == 'i' &&
1050 PyUnicode_READ(kind, str, idx + 6) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001051 PyUnicode_READ(kind, str, idx + 7) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001052 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1053 }
1054 break;
1055 case '-':
1056 /* -Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001057 if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001058 PyUnicode_READ(kind, str, idx + 2) == 'n' &&
1059 PyUnicode_READ(kind, str, idx + 3) == 'f' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001060 PyUnicode_READ(kind, str, idx + 4) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001061 PyUnicode_READ(kind, str, idx + 5) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001062 PyUnicode_READ(kind, str, idx + 6) == 'i' &&
1063 PyUnicode_READ(kind, str, idx + 7) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001064 PyUnicode_READ(kind, str, idx + 8) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001065 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1066 }
1067 break;
1068 }
1069 /* Didn't find a string, object, array, or named constant. Look for a number. */
1070 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
1071}
1072
1073static PyObject *
1074scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
1075{
1076 /* Python callable interface to scan_once_{str,unicode} */
1077 PyObject *pystr;
1078 PyObject *rval;
1079 Py_ssize_t idx;
1080 Py_ssize_t next_idx = -1;
1081 static char *kwlist[] = {"string", "idx", NULL};
1082 PyScannerObject *s;
1083 assert(PyScanner_Check(self));
1084 s = (PyScannerObject *)self;
1085 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:scan_once", kwlist, &pystr, _convertPyInt_AsSsize_t, &idx))
1086 return NULL;
1087
1088 if (PyUnicode_Check(pystr)) {
1089 rval = scan_once_unicode(s, pystr, idx, &next_idx);
1090 }
1091 else {
1092 PyErr_Format(PyExc_TypeError,
1093 "first argument must be a string, not %.80s",
1094 Py_TYPE(pystr)->tp_name);
1095 return NULL;
1096 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001097 PyDict_Clear(s->memo);
1098 if (rval == NULL)
1099 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001100 return _build_rval_index_tuple(rval, next_idx);
1101}
1102
1103static PyObject *
1104scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1105{
1106 PyScannerObject *s;
1107 s = (PyScannerObject *)type->tp_alloc(type, 0);
1108 if (s != NULL) {
1109 s->strict = NULL;
1110 s->object_hook = NULL;
1111 s->object_pairs_hook = NULL;
1112 s->parse_float = NULL;
1113 s->parse_int = NULL;
1114 s->parse_constant = NULL;
1115 }
1116 return (PyObject *)s;
1117}
1118
1119static int
1120scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1121{
1122 /* Initialize Scanner object */
1123 PyObject *ctx;
1124 static char *kwlist[] = {"context", NULL};
1125 PyScannerObject *s;
1126
1127 assert(PyScanner_Check(self));
1128 s = (PyScannerObject *)self;
1129
1130 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1131 return -1;
1132
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001133 if (s->memo == NULL) {
1134 s->memo = PyDict_New();
1135 if (s->memo == NULL)
1136 goto bail;
1137 }
1138
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001139 /* All of these will fail "gracefully" so we don't need to verify them */
1140 s->strict = PyObject_GetAttrString(ctx, "strict");
1141 if (s->strict == NULL)
1142 goto bail;
1143 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1144 if (s->object_hook == NULL)
1145 goto bail;
1146 s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1147 if (s->object_pairs_hook == NULL)
1148 goto bail;
1149 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1150 if (s->parse_float == NULL)
1151 goto bail;
1152 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1153 if (s->parse_int == NULL)
1154 goto bail;
1155 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1156 if (s->parse_constant == NULL)
1157 goto bail;
1158
1159 return 0;
1160
1161bail:
1162 Py_CLEAR(s->strict);
1163 Py_CLEAR(s->object_hook);
1164 Py_CLEAR(s->object_pairs_hook);
1165 Py_CLEAR(s->parse_float);
1166 Py_CLEAR(s->parse_int);
1167 Py_CLEAR(s->parse_constant);
1168 return -1;
1169}
1170
1171PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1172
1173static
1174PyTypeObject PyScannerType = {
1175 PyVarObject_HEAD_INIT(NULL, 0)
1176 "_json.Scanner", /* tp_name */
1177 sizeof(PyScannerObject), /* tp_basicsize */
1178 0, /* tp_itemsize */
1179 scanner_dealloc, /* tp_dealloc */
1180 0, /* tp_print */
1181 0, /* tp_getattr */
1182 0, /* tp_setattr */
1183 0, /* tp_compare */
1184 0, /* tp_repr */
1185 0, /* tp_as_number */
1186 0, /* tp_as_sequence */
1187 0, /* tp_as_mapping */
1188 0, /* tp_hash */
1189 scanner_call, /* tp_call */
1190 0, /* tp_str */
1191 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1192 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1193 0, /* tp_as_buffer */
1194 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1195 scanner_doc, /* tp_doc */
1196 scanner_traverse, /* tp_traverse */
1197 scanner_clear, /* tp_clear */
1198 0, /* tp_richcompare */
1199 0, /* tp_weaklistoffset */
1200 0, /* tp_iter */
1201 0, /* tp_iternext */
1202 0, /* tp_methods */
1203 scanner_members, /* tp_members */
1204 0, /* tp_getset */
1205 0, /* tp_base */
1206 0, /* tp_dict */
1207 0, /* tp_descr_get */
1208 0, /* tp_descr_set */
1209 0, /* tp_dictoffset */
1210 scanner_init, /* tp_init */
1211 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1212 scanner_new, /* tp_new */
1213 0,/* PyObject_GC_Del, */ /* tp_free */
1214};
1215
1216static PyObject *
1217encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1218{
1219 PyEncoderObject *s;
1220 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1221 if (s != NULL) {
1222 s->markers = NULL;
1223 s->defaultfn = NULL;
1224 s->encoder = NULL;
1225 s->indent = NULL;
1226 s->key_separator = NULL;
1227 s->item_separator = NULL;
1228 s->sort_keys = NULL;
1229 s->skipkeys = NULL;
1230 }
1231 return (PyObject *)s;
1232}
1233
1234static int
1235encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1236{
1237 /* initialize Encoder object */
1238 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1239
1240 PyEncoderObject *s;
Antoine Pitrou781eba72009-12-08 15:57:31 +00001241 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1242 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001243
1244 assert(PyEncoder_Check(self));
1245 s = (PyEncoderObject *)self;
1246
1247 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
Antoine Pitrou781eba72009-12-08 15:57:31 +00001248 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1249 &sort_keys, &skipkeys, &allow_nan))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001250 return -1;
1251
Antoine Pitrou781eba72009-12-08 15:57:31 +00001252 s->markers = markers;
1253 s->defaultfn = defaultfn;
1254 s->encoder = encoder;
1255 s->indent = indent;
1256 s->key_separator = key_separator;
1257 s->item_separator = item_separator;
1258 s->sort_keys = sort_keys;
1259 s->skipkeys = skipkeys;
1260 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1261 s->allow_nan = PyObject_IsTrue(allow_nan);
1262
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001263 Py_INCREF(s->markers);
1264 Py_INCREF(s->defaultfn);
1265 Py_INCREF(s->encoder);
1266 Py_INCREF(s->indent);
1267 Py_INCREF(s->key_separator);
1268 Py_INCREF(s->item_separator);
1269 Py_INCREF(s->sort_keys);
1270 Py_INCREF(s->skipkeys);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001271 return 0;
1272}
1273
1274static PyObject *
1275encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1276{
1277 /* Python callable interface to encode_listencode_obj */
1278 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1279 PyObject *obj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001280 Py_ssize_t indent_level;
1281 PyEncoderObject *s;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001282 _PyAccu acc;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001283
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001284 assert(PyEncoder_Check(self));
1285 s = (PyEncoderObject *)self;
1286 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:_iterencode", kwlist,
1287 &obj, _convertPyInt_AsSsize_t, &indent_level))
1288 return NULL;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001289 if (_PyAccu_Init(&acc))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001290 return NULL;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001291 if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001292 _PyAccu_Destroy(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001293 return NULL;
1294 }
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001295 return _PyAccu_FinishAsList(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001296}
1297
1298static PyObject *
1299_encoded_const(PyObject *obj)
1300{
1301 /* Return the JSON string representation of None, True, False */
1302 if (obj == Py_None) {
1303 static PyObject *s_null = NULL;
1304 if (s_null == NULL) {
1305 s_null = PyUnicode_InternFromString("null");
1306 }
1307 Py_INCREF(s_null);
1308 return s_null;
1309 }
1310 else if (obj == Py_True) {
1311 static PyObject *s_true = NULL;
1312 if (s_true == NULL) {
1313 s_true = PyUnicode_InternFromString("true");
1314 }
1315 Py_INCREF(s_true);
1316 return s_true;
1317 }
1318 else if (obj == Py_False) {
1319 static PyObject *s_false = NULL;
1320 if (s_false == NULL) {
1321 s_false = PyUnicode_InternFromString("false");
1322 }
1323 Py_INCREF(s_false);
1324 return s_false;
1325 }
1326 else {
1327 PyErr_SetString(PyExc_ValueError, "not a const");
1328 return NULL;
1329 }
1330}
1331
1332static PyObject *
1333encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1334{
1335 /* Return the JSON representation of a PyFloat */
1336 double i = PyFloat_AS_DOUBLE(obj);
1337 if (!Py_IS_FINITE(i)) {
1338 if (!s->allow_nan) {
1339 PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant");
1340 return NULL;
1341 }
1342 if (i > 0) {
1343 return PyUnicode_FromString("Infinity");
1344 }
1345 else if (i < 0) {
1346 return PyUnicode_FromString("-Infinity");
1347 }
1348 else {
1349 return PyUnicode_FromString("NaN");
1350 }
1351 }
1352 /* Use a better float format here? */
1353 return PyObject_Repr(obj);
1354}
1355
1356static PyObject *
1357encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1358{
1359 /* Return the JSON representation of a string */
1360 if (s->fast_encode)
1361 return py_encode_basestring_ascii(NULL, obj);
1362 else
1363 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1364}
1365
1366static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001367_steal_accumulate(_PyAccu *acc, PyObject *stolen)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001368{
1369 /* Append stolen and then decrement its reference count */
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001370 int rval = _PyAccu_Accumulate(acc, stolen);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001371 Py_DECREF(stolen);
1372 return rval;
1373}
1374
1375static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001376encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001377 PyObject *obj, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001378{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001379 /* Encode Python object obj to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001380 PyObject *newobj;
1381 int rv;
1382
1383 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1384 PyObject *cstr = _encoded_const(obj);
1385 if (cstr == NULL)
1386 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001387 return _steal_accumulate(acc, cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001388 }
1389 else if (PyUnicode_Check(obj))
1390 {
1391 PyObject *encoded = encoder_encode_string(s, obj);
1392 if (encoded == NULL)
1393 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001394 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001395 }
1396 else if (PyLong_Check(obj)) {
1397 PyObject *encoded = PyObject_Str(obj);
1398 if (encoded == NULL)
1399 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001400 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001401 }
1402 else if (PyFloat_Check(obj)) {
1403 PyObject *encoded = encoder_encode_float(s, obj);
1404 if (encoded == NULL)
1405 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001406 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001407 }
1408 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001409 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1410 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001411 rv = encoder_listencode_list(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001412 Py_LeaveRecursiveCall();
1413 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001414 }
1415 else if (PyDict_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001416 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1417 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001418 rv = encoder_listencode_dict(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001419 Py_LeaveRecursiveCall();
1420 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001421 }
1422 else {
1423 PyObject *ident = NULL;
1424 if (s->markers != Py_None) {
1425 int has_key;
1426 ident = PyLong_FromVoidPtr(obj);
1427 if (ident == NULL)
1428 return -1;
1429 has_key = PyDict_Contains(s->markers, ident);
1430 if (has_key) {
1431 if (has_key != -1)
1432 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1433 Py_DECREF(ident);
1434 return -1;
1435 }
1436 if (PyDict_SetItem(s->markers, ident, obj)) {
1437 Py_DECREF(ident);
1438 return -1;
1439 }
1440 }
1441 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1442 if (newobj == NULL) {
1443 Py_XDECREF(ident);
1444 return -1;
1445 }
Ezio Melotti13672652011-05-11 01:02:56 +03001446
1447 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1448 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001449 rv = encoder_listencode_obj(s, acc, newobj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001450 Py_LeaveRecursiveCall();
1451
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001452 Py_DECREF(newobj);
1453 if (rv) {
1454 Py_XDECREF(ident);
1455 return -1;
1456 }
1457 if (ident != NULL) {
1458 if (PyDict_DelItem(s->markers, ident)) {
1459 Py_XDECREF(ident);
1460 return -1;
1461 }
1462 Py_XDECREF(ident);
1463 }
1464 return rv;
1465 }
1466}
1467
1468static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001469encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001470 PyObject *dct, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001471{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001472 /* Encode Python dict dct a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001473 static PyObject *open_dict = NULL;
1474 static PyObject *close_dict = NULL;
1475 static PyObject *empty_dict = NULL;
1476 PyObject *kstr = NULL;
1477 PyObject *ident = NULL;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001478 PyObject *it = NULL;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001479 PyObject *items;
1480 PyObject *item = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001481 int skipkeys;
1482 Py_ssize_t idx;
1483
1484 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
1485 open_dict = PyUnicode_InternFromString("{");
1486 close_dict = PyUnicode_InternFromString("}");
1487 empty_dict = PyUnicode_InternFromString("{}");
1488 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
1489 return -1;
1490 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001491 if (Py_SIZE(dct) == 0)
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001492 return _PyAccu_Accumulate(acc, empty_dict);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001493
1494 if (s->markers != Py_None) {
1495 int has_key;
1496 ident = PyLong_FromVoidPtr(dct);
1497 if (ident == NULL)
1498 goto bail;
1499 has_key = PyDict_Contains(s->markers, ident);
1500 if (has_key) {
1501 if (has_key != -1)
1502 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1503 goto bail;
1504 }
1505 if (PyDict_SetItem(s->markers, ident, dct)) {
1506 goto bail;
1507 }
1508 }
1509
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001510 if (_PyAccu_Accumulate(acc, open_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001511 goto bail;
1512
1513 if (s->indent != Py_None) {
1514 /* TODO: DOES NOT RUN */
1515 indent_level += 1;
1516 /*
1517 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1518 separator = _item_separator + newline_indent
1519 buf += newline_indent
1520 */
1521 }
1522
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001523 if (PyObject_IsTrue(s->sort_keys)) {
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001524 /* First sort the keys then replace them with (key, value) tuples. */
1525 Py_ssize_t i, nitems;
1526 items = PyMapping_Keys(dct);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 if (items == NULL)
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001528 goto bail;
1529 if (!PyList_Check(items)) {
1530 PyErr_SetString(PyExc_ValueError, "keys must return list");
1531 goto bail;
1532 }
1533 if (PyList_Sort(items) < 0)
1534 goto bail;
1535 nitems = PyList_GET_SIZE(items);
1536 for (i = 0; i < nitems; i++) {
1537 PyObject *key, *value;
1538 key = PyList_GET_ITEM(items, i);
1539 value = PyDict_GetItem(dct, key);
1540 item = PyTuple_Pack(2, key, value);
1541 if (item == NULL)
1542 goto bail;
1543 PyList_SET_ITEM(items, i, item);
1544 Py_DECREF(key);
1545 }
1546 }
1547 else {
1548 items = PyMapping_Items(dct);
1549 }
1550 if (items == NULL)
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001551 goto bail;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001552 it = PyObject_GetIter(items);
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001553 Py_DECREF(items);
1554 if (it == NULL)
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001555 goto bail;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001556 skipkeys = PyObject_IsTrue(s->skipkeys);
1557 idx = 0;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001558 while ((item = PyIter_Next(it)) != NULL) {
1559 PyObject *encoded, *key, *value;
1560 if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
1561 PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
1562 goto bail;
1563 }
1564 key = PyTuple_GET_ITEM(item, 0);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001565 if (PyUnicode_Check(key)) {
1566 Py_INCREF(key);
1567 kstr = key;
1568 }
1569 else if (PyFloat_Check(key)) {
1570 kstr = encoder_encode_float(s, key);
1571 if (kstr == NULL)
1572 goto bail;
1573 }
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001574 else if (key == Py_True || key == Py_False || key == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 /* This must come before the PyLong_Check because
1576 True and False are also 1 and 0.*/
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001577 kstr = _encoded_const(key);
1578 if (kstr == NULL)
1579 goto bail;
1580 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001581 else if (PyLong_Check(key)) {
1582 kstr = PyObject_Str(key);
1583 if (kstr == NULL)
1584 goto bail;
1585 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001586 else if (skipkeys) {
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001587 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001588 continue;
1589 }
1590 else {
1591 /* TODO: include repr of key */
Doug Hellmann1c524752010-07-21 12:29:04 +00001592 PyErr_SetString(PyExc_TypeError, "keys must be a string");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001593 goto bail;
1594 }
1595
1596 if (idx) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001597 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001598 goto bail;
1599 }
1600
1601 encoded = encoder_encode_string(s, kstr);
1602 Py_CLEAR(kstr);
1603 if (encoded == NULL)
1604 goto bail;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001605 if (_PyAccu_Accumulate(acc, encoded)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001606 Py_DECREF(encoded);
1607 goto bail;
1608 }
1609 Py_DECREF(encoded);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001610 if (_PyAccu_Accumulate(acc, s->key_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001611 goto bail;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001612
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001613 value = PyTuple_GET_ITEM(item, 1);
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001614 if (encoder_listencode_obj(s, acc, value, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001615 goto bail;
1616 idx += 1;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001617 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001618 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001619 if (PyErr_Occurred())
1620 goto bail;
1621 Py_CLEAR(it);
1622
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001623 if (ident != NULL) {
1624 if (PyDict_DelItem(s->markers, ident))
1625 goto bail;
1626 Py_CLEAR(ident);
1627 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001628 /* TODO DOES NOT RUN; dead code
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001629 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001630 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001631
1632 yield '\n' + (' ' * (_indent * _current_indent_level))
1633 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001634 if (_PyAccu_Accumulate(acc, close_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001635 goto bail;
1636 return 0;
1637
1638bail:
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001639 Py_XDECREF(it);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001640 Py_XDECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001641 Py_XDECREF(kstr);
1642 Py_XDECREF(ident);
1643 return -1;
1644}
1645
1646
1647static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001648encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001649 PyObject *seq, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001650{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001651 /* Encode Python list seq to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001652 static PyObject *open_array = NULL;
1653 static PyObject *close_array = NULL;
1654 static PyObject *empty_array = NULL;
1655 PyObject *ident = NULL;
1656 PyObject *s_fast = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001657 Py_ssize_t i;
1658
1659 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
1660 open_array = PyUnicode_InternFromString("[");
1661 close_array = PyUnicode_InternFromString("]");
1662 empty_array = PyUnicode_InternFromString("[]");
1663 if (open_array == NULL || close_array == NULL || empty_array == NULL)
1664 return -1;
1665 }
1666 ident = NULL;
1667 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
1668 if (s_fast == NULL)
1669 return -1;
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001670 if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001671 Py_DECREF(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001672 return _PyAccu_Accumulate(acc, empty_array);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001673 }
1674
1675 if (s->markers != Py_None) {
1676 int has_key;
1677 ident = PyLong_FromVoidPtr(seq);
1678 if (ident == NULL)
1679 goto bail;
1680 has_key = PyDict_Contains(s->markers, ident);
1681 if (has_key) {
1682 if (has_key != -1)
1683 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1684 goto bail;
1685 }
1686 if (PyDict_SetItem(s->markers, ident, seq)) {
1687 goto bail;
1688 }
1689 }
1690
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001691 if (_PyAccu_Accumulate(acc, open_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001692 goto bail;
1693 if (s->indent != Py_None) {
1694 /* TODO: DOES NOT RUN */
1695 indent_level += 1;
1696 /*
1697 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1698 separator = _item_separator + newline_indent
1699 buf += newline_indent
1700 */
1701 }
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001702 for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
1703 PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001704 if (i) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001705 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001706 goto bail;
1707 }
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001708 if (encoder_listencode_obj(s, acc, obj, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001709 goto bail;
1710 }
1711 if (ident != NULL) {
1712 if (PyDict_DelItem(s->markers, ident))
1713 goto bail;
1714 Py_CLEAR(ident);
1715 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001716
1717 /* TODO: DOES NOT RUN
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001718 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001719 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001720
1721 yield '\n' + (' ' * (_indent * _current_indent_level))
1722 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001723 if (_PyAccu_Accumulate(acc, close_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001724 goto bail;
1725 Py_DECREF(s_fast);
1726 return 0;
1727
1728bail:
1729 Py_XDECREF(ident);
1730 Py_DECREF(s_fast);
1731 return -1;
1732}
1733
1734static void
1735encoder_dealloc(PyObject *self)
1736{
1737 /* Deallocate Encoder */
1738 encoder_clear(self);
1739 Py_TYPE(self)->tp_free(self);
1740}
1741
1742static int
1743encoder_traverse(PyObject *self, visitproc visit, void *arg)
1744{
1745 PyEncoderObject *s;
1746 assert(PyEncoder_Check(self));
1747 s = (PyEncoderObject *)self;
1748 Py_VISIT(s->markers);
1749 Py_VISIT(s->defaultfn);
1750 Py_VISIT(s->encoder);
1751 Py_VISIT(s->indent);
1752 Py_VISIT(s->key_separator);
1753 Py_VISIT(s->item_separator);
1754 Py_VISIT(s->sort_keys);
1755 Py_VISIT(s->skipkeys);
1756 return 0;
1757}
1758
1759static int
1760encoder_clear(PyObject *self)
1761{
1762 /* Deallocate Encoder */
1763 PyEncoderObject *s;
1764 assert(PyEncoder_Check(self));
1765 s = (PyEncoderObject *)self;
1766 Py_CLEAR(s->markers);
1767 Py_CLEAR(s->defaultfn);
1768 Py_CLEAR(s->encoder);
1769 Py_CLEAR(s->indent);
1770 Py_CLEAR(s->key_separator);
1771 Py_CLEAR(s->item_separator);
1772 Py_CLEAR(s->sort_keys);
1773 Py_CLEAR(s->skipkeys);
1774 return 0;
1775}
1776
1777PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
1778
1779static
1780PyTypeObject PyEncoderType = {
1781 PyVarObject_HEAD_INIT(NULL, 0)
1782 "_json.Encoder", /* tp_name */
1783 sizeof(PyEncoderObject), /* tp_basicsize */
1784 0, /* tp_itemsize */
1785 encoder_dealloc, /* tp_dealloc */
1786 0, /* tp_print */
1787 0, /* tp_getattr */
1788 0, /* tp_setattr */
1789 0, /* tp_compare */
1790 0, /* tp_repr */
1791 0, /* tp_as_number */
1792 0, /* tp_as_sequence */
1793 0, /* tp_as_mapping */
1794 0, /* tp_hash */
1795 encoder_call, /* tp_call */
1796 0, /* tp_str */
1797 0, /* tp_getattro */
1798 0, /* tp_setattro */
1799 0, /* tp_as_buffer */
1800 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1801 encoder_doc, /* tp_doc */
1802 encoder_traverse, /* tp_traverse */
1803 encoder_clear, /* tp_clear */
1804 0, /* tp_richcompare */
1805 0, /* tp_weaklistoffset */
1806 0, /* tp_iter */
1807 0, /* tp_iternext */
1808 0, /* tp_methods */
1809 encoder_members, /* tp_members */
1810 0, /* tp_getset */
1811 0, /* tp_base */
1812 0, /* tp_dict */
1813 0, /* tp_descr_get */
1814 0, /* tp_descr_set */
1815 0, /* tp_dictoffset */
1816 encoder_init, /* tp_init */
1817 0, /* tp_alloc */
1818 encoder_new, /* tp_new */
1819 0, /* tp_free */
1820};
1821
1822static PyMethodDef speedups_methods[] = {
1823 {"encode_basestring_ascii",
1824 (PyCFunction)py_encode_basestring_ascii,
1825 METH_O,
1826 pydoc_encode_basestring_ascii},
1827 {"scanstring",
1828 (PyCFunction)py_scanstring,
1829 METH_VARARGS,
1830 pydoc_scanstring},
Christian Heimes90540002008-05-08 14:29:10 +00001831 {NULL, NULL, 0, NULL}
1832};
1833
1834PyDoc_STRVAR(module_doc,
1835"json speedups\n");
1836
Martin v. Löwis1a214512008-06-11 05:26:20 +00001837static struct PyModuleDef jsonmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 PyModuleDef_HEAD_INIT,
1839 "_json",
1840 module_doc,
1841 -1,
1842 speedups_methods,
1843 NULL,
1844 NULL,
1845 NULL,
1846 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001847};
1848
1849PyObject*
1850PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +00001851{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001852 PyObject *m = PyModule_Create(&jsonmodule);
1853 if (!m)
1854 return NULL;
1855 PyScannerType.tp_new = PyType_GenericNew;
1856 if (PyType_Ready(&PyScannerType) < 0)
1857 goto fail;
1858 PyEncoderType.tp_new = PyType_GenericNew;
1859 if (PyType_Ready(&PyEncoderType) < 0)
1860 goto fail;
1861 Py_INCREF((PyObject*)&PyScannerType);
1862 if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1863 Py_DECREF((PyObject*)&PyScannerType);
1864 goto fail;
1865 }
1866 Py_INCREF((PyObject*)&PyEncoderType);
1867 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1868 Py_DECREF((PyObject*)&PyEncoderType);
1869 goto fail;
1870 }
1871 return m;
1872 fail:
1873 Py_DECREF(m);
1874 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +00001875}