blob: 125101fa7c38ca625b462354fa90b6517a34db42 [file] [log] [blame]
Christian Heimes90540002008-05-08 14:29:10 +00001#include "Python.h"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00002#include "structmember.h"
Antoine Pitroud0acb412012-03-22 14:42:18 +01003#include "accu.h"
4
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00005#ifdef __GNUC__
6#define UNUSED __attribute__((__unused__))
7#else
8#define UNUSED
9#endif
10
11#define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType)
12#define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType)
13#define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType)
14#define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType)
15
16static PyTypeObject PyScannerType;
17static PyTypeObject PyEncoderType;
18
19typedef struct _PyScannerObject {
20 PyObject_HEAD
21 PyObject *strict;
22 PyObject *object_hook;
23 PyObject *object_pairs_hook;
24 PyObject *parse_float;
25 PyObject *parse_int;
26 PyObject *parse_constant;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +000027 PyObject *memo;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000028} PyScannerObject;
29
30static PyMemberDef scanner_members[] = {
31 {"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"},
32 {"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"},
33 {"object_pairs_hook", T_OBJECT, offsetof(PyScannerObject, object_pairs_hook), READONLY},
34 {"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"},
35 {"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "parse_int"},
36 {"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READONLY, "parse_constant"},
37 {NULL}
38};
39
40typedef struct _PyEncoderObject {
41 PyObject_HEAD
42 PyObject *markers;
43 PyObject *defaultfn;
44 PyObject *encoder;
45 PyObject *indent;
46 PyObject *key_separator;
47 PyObject *item_separator;
48 PyObject *sort_keys;
49 PyObject *skipkeys;
50 int fast_encode;
51 int allow_nan;
52} PyEncoderObject;
53
54static PyMemberDef encoder_members[] = {
55 {"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers"},
56 {"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "default"},
57 {"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder"},
58 {"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"},
59 {"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READONLY, "key_separator"},
60 {"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READONLY, "item_separator"},
61 {"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sort_keys"},
62 {"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipkeys"},
63 {NULL}
64};
65
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +020066static PyObject *
67join_list_unicode(PyObject *lst)
68{
69 /* return u''.join(lst) */
70 static PyObject *sep = NULL;
71 if (sep == NULL) {
72 sep = PyUnicode_FromStringAndSize("", 0);
73 if (sep == NULL)
74 return NULL;
75 }
76 return PyUnicode_Join(sep, lst);
77}
78
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +020079/* Forward decls */
80
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000081static PyObject *
82ascii_escape_unicode(PyObject *pystr);
83static PyObject *
84py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr);
85void init_json(void);
86static PyObject *
87scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr);
88static PyObject *
89_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx);
90static PyObject *
91scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
92static int
93scanner_init(PyObject *self, PyObject *args, PyObject *kwds);
94static void
95scanner_dealloc(PyObject *self);
96static int
97scanner_clear(PyObject *self);
98static PyObject *
99encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
100static int
101encoder_init(PyObject *self, PyObject *args, PyObject *kwds);
102static void
103encoder_dealloc(PyObject *self);
104static int
105encoder_clear(PyObject *self);
106static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200107encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc, PyObject *seq, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000108static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200109encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, PyObject *obj, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000110static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200111encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, PyObject *dct, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000112static PyObject *
Hirokazu Yamamotofecf5d12009-05-02 15:55:19 +0000113_encoded_const(PyObject *obj);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000114static void
115raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
116static PyObject *
117encoder_encode_string(PyEncoderObject *s, PyObject *obj);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000118static PyObject *
Ethan Furmana4998a72013-08-10 13:01:45 -0700119encoder_encode_long(PyEncoderObject* s UNUSED, PyObject *obj);
120static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000121encoder_encode_float(PyEncoderObject *s, PyObject *obj);
122
Christian Heimes90540002008-05-08 14:29:10 +0000123#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000124#define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
Christian Heimes90540002008-05-08 14:29:10 +0000125
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000126static Py_ssize_t
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200127ascii_escape_unichar(Py_UCS4 c, unsigned char *output, Py_ssize_t chars)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000128{
129 /* Escape unicode code point c to ASCII escape sequences
130 in char *output. output must have at least 12 bytes unused to
131 accommodate an escaped surrogate pair "\uXXXX\uXXXX" */
Christian Heimes90540002008-05-08 14:29:10 +0000132 output[chars++] = '\\';
133 switch (c) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000134 case '\\': output[chars++] = c; break;
135 case '"': output[chars++] = c; break;
Christian Heimes90540002008-05-08 14:29:10 +0000136 case '\b': output[chars++] = 'b'; break;
137 case '\f': output[chars++] = 'f'; break;
138 case '\n': output[chars++] = 'n'; break;
139 case '\r': output[chars++] = 'r'; break;
140 case '\t': output[chars++] = 't'; break;
141 default:
Christian Heimes90540002008-05-08 14:29:10 +0000142 if (c >= 0x10000) {
143 /* UTF-16 surrogate pair */
Victor Stinner76df43d2012-10-30 01:42:39 +0100144 Py_UCS4 v = Py_UNICODE_HIGH_SURROGATE(c);
Christian Heimes90540002008-05-08 14:29:10 +0000145 output[chars++] = 'u';
Victor Stinner76df43d2012-10-30 01:42:39 +0100146 output[chars++] = Py_hexdigits[(v >> 12) & 0xf];
147 output[chars++] = Py_hexdigits[(v >> 8) & 0xf];
148 output[chars++] = Py_hexdigits[(v >> 4) & 0xf];
149 output[chars++] = Py_hexdigits[(v ) & 0xf];
150 c = Py_UNICODE_LOW_SURROGATE(c);
Christian Heimes90540002008-05-08 14:29:10 +0000151 output[chars++] = '\\';
152 }
Christian Heimes90540002008-05-08 14:29:10 +0000153 output[chars++] = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200154 output[chars++] = Py_hexdigits[(c >> 12) & 0xf];
155 output[chars++] = Py_hexdigits[(c >> 8) & 0xf];
156 output[chars++] = Py_hexdigits[(c >> 4) & 0xf];
157 output[chars++] = Py_hexdigits[(c ) & 0xf];
Christian Heimes90540002008-05-08 14:29:10 +0000158 }
159 return chars;
160}
161
162static PyObject *
163ascii_escape_unicode(PyObject *pystr)
164{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000165 /* Take a PyUnicode pystr and return a new ASCII-only escaped PyUnicode */
Christian Heimes90540002008-05-08 14:29:10 +0000166 Py_ssize_t i;
167 Py_ssize_t input_chars;
168 Py_ssize_t output_size;
169 Py_ssize_t chars;
170 PyObject *rval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200171 void *input;
172 unsigned char *output;
173 int kind;
Christian Heimes90540002008-05-08 14:29:10 +0000174
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200175 if (PyUnicode_READY(pystr) == -1)
176 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000177
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200178 input_chars = PyUnicode_GET_LENGTH(pystr);
179 input = PyUnicode_DATA(pystr);
180 kind = PyUnicode_KIND(pystr);
181
182 /* Compute the output size */
183 for (i = 0, output_size = 2; i < input_chars; i++) {
184 Py_UCS4 c = PyUnicode_READ(kind, input, i);
185 if (S_CHAR(c))
186 output_size++;
187 else {
188 switch(c) {
Victor Stinnerd9c06312011-10-11 21:56:19 +0200189 case '\\': case '"': case '\b': case '\f':
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200190 case '\n': case '\r': case '\t':
191 output_size += 2; break;
192 default:
193 output_size += c >= 0x10000 ? 12 : 6;
194 }
195 }
196 }
197
198 rval = PyUnicode_New(output_size, 127);
Christian Heimes90540002008-05-08 14:29:10 +0000199 if (rval == NULL) {
200 return NULL;
201 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200202 output = PyUnicode_1BYTE_DATA(rval);
Christian Heimes90540002008-05-08 14:29:10 +0000203 chars = 0;
204 output[chars++] = '"';
205 for (i = 0; i < input_chars; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200206 Py_UCS4 c = PyUnicode_READ(kind, input, i);
Christian Heimes90540002008-05-08 14:29:10 +0000207 if (S_CHAR(c)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000208 output[chars++] = c;
Christian Heimes90540002008-05-08 14:29:10 +0000209 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000210 else {
211 chars = ascii_escape_unichar(c, output, chars);
Christian Heimes90540002008-05-08 14:29:10 +0000212 }
Christian Heimes90540002008-05-08 14:29:10 +0000213 }
214 output[chars++] = '"';
Christian Heimesf402e922013-01-03 09:21:55 +0100215#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200216 assert(_PyUnicode_CheckConsistency(rval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100217#endif
Christian Heimes90540002008-05-08 14:29:10 +0000218 return rval;
219}
220
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000221static void
Christian Heimes90540002008-05-08 14:29:10 +0000222raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
223{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000224 /* Use the Python function json.decoder.errmsg to raise a nice
225 looking ValueError exception */
Christian Heimes90540002008-05-08 14:29:10 +0000226 static PyObject *errmsg_fn = NULL;
227 PyObject *pymsg;
228 if (errmsg_fn == NULL) {
229 PyObject *decoder = PyImport_ImportModule("json.decoder");
230 if (decoder == NULL)
231 return;
232 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000233 Py_DECREF(decoder);
Christian Heimes90540002008-05-08 14:29:10 +0000234 if (errmsg_fn == NULL)
235 return;
Christian Heimes90540002008-05-08 14:29:10 +0000236 }
Antoine Pitroucbb02842012-12-01 19:34:16 +0100237 pymsg = PyObject_CallFunction(errmsg_fn, "(zOn)", msg, s, end);
Benjamin Petersona13d4752008-10-16 21:17:24 +0000238 if (pymsg) {
239 PyErr_SetObject(PyExc_ValueError, pymsg);
240 Py_DECREF(pymsg);
241 }
Christian Heimes90540002008-05-08 14:29:10 +0000242}
243
Ezio Melotti37623ab2013-01-03 08:44:15 +0200244static void
245raise_stop_iteration(Py_ssize_t idx)
246{
247 PyObject *value = PyLong_FromSsize_t(idx);
248 if (value != NULL) {
249 PyErr_SetObject(PyExc_StopIteration, value);
250 Py_DECREF(value);
251 }
252}
253
Christian Heimes90540002008-05-08 14:29:10 +0000254static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000255_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
256 /* return (rval, idx) tuple, stealing reference to rval */
257 PyObject *tpl;
258 PyObject *pyidx;
259 /*
260 steal a reference to rval, returns (rval, idx)
261 */
262 if (rval == NULL) {
Christian Heimes90540002008-05-08 14:29:10 +0000263 return NULL;
264 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000265 pyidx = PyLong_FromSsize_t(idx);
266 if (pyidx == NULL) {
267 Py_DECREF(rval);
268 return NULL;
269 }
270 tpl = PyTuple_New(2);
271 if (tpl == NULL) {
272 Py_DECREF(pyidx);
273 Py_DECREF(rval);
274 return NULL;
275 }
276 PyTuple_SET_ITEM(tpl, 0, rval);
277 PyTuple_SET_ITEM(tpl, 1, pyidx);
278 return tpl;
Christian Heimes90540002008-05-08 14:29:10 +0000279}
280
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000281#define APPEND_OLD_CHUNK \
282 if (chunk != NULL) { \
283 if (chunks == NULL) { \
284 chunks = PyList_New(0); \
285 if (chunks == NULL) { \
286 goto bail; \
287 } \
288 } \
289 if (PyList_Append(chunks, chunk)) { \
290 Py_DECREF(chunk); \
291 goto bail; \
292 } \
293 Py_CLEAR(chunk); \
294 }
295
Christian Heimes90540002008-05-08 14:29:10 +0000296static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000297scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000298{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000299 /* Read the JSON string from PyUnicode pystr.
300 end is the index of the first character after the quote.
301 if strict is zero then literal control characters are allowed
302 *next_end_ptr is a return-by-reference index of the character
303 after the end quote
Christian Heimes90540002008-05-08 14:29:10 +0000304
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000305 Return value is a new PyUnicode
306 */
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000307 PyObject *rval = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200308 Py_ssize_t len;
Christian Heimes90540002008-05-08 14:29:10 +0000309 Py_ssize_t begin = end - 1;
Brett Cannonb94767f2011-02-22 20:15:44 +0000310 Py_ssize_t next /* = begin */;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200311 const void *buf;
312 int kind;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000313 PyObject *chunks = NULL;
314 PyObject *chunk = NULL;
315
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200316 if (PyUnicode_READY(pystr) == -1)
317 return 0;
318
319 len = PyUnicode_GET_LENGTH(pystr);
320 buf = PyUnicode_DATA(pystr);
321 kind = PyUnicode_KIND(pystr);
322
Ezio Melotti37623ab2013-01-03 08:44:15 +0200323 if (end < 0 || len < end) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000324 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
325 goto bail;
326 }
Christian Heimes90540002008-05-08 14:29:10 +0000327 while (1) {
328 /* Find the end of the string or the next escape */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200329 Py_UCS4 c = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000330 for (next = end; next < len; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200331 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000332 if (c == '"' || c == '\\') {
333 break;
334 }
335 else if (strict && c <= 0x1f) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000336 raise_errmsg("Invalid control character at", pystr, next);
Christian Heimes90540002008-05-08 14:29:10 +0000337 goto bail;
338 }
339 }
340 if (!(c == '"' || c == '\\')) {
341 raise_errmsg("Unterminated string starting at", pystr, begin);
342 goto bail;
343 }
344 /* Pick up this chunk if it's not zero length */
345 if (next != end) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000346 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200347 chunk = PyUnicode_FromKindAndData(
348 kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200349 (char*)buf + kind * end,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200350 next - end);
Christian Heimes90540002008-05-08 14:29:10 +0000351 if (chunk == NULL) {
352 goto bail;
353 }
Christian Heimes90540002008-05-08 14:29:10 +0000354 }
355 next++;
356 if (c == '"') {
357 end = next;
358 break;
359 }
360 if (next == len) {
361 raise_errmsg("Unterminated string starting at", pystr, begin);
362 goto bail;
363 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200364 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000365 if (c != 'u') {
366 /* Non-unicode backslash escapes */
367 end = next + 1;
368 switch (c) {
369 case '"': break;
370 case '\\': break;
371 case '/': break;
372 case 'b': c = '\b'; break;
373 case 'f': c = '\f'; break;
374 case 'n': c = '\n'; break;
375 case 'r': c = '\r'; break;
376 case 't': c = '\t'; break;
377 default: c = 0;
378 }
379 if (c == 0) {
380 raise_errmsg("Invalid \\escape", pystr, end - 2);
381 goto bail;
382 }
383 }
384 else {
385 c = 0;
386 next++;
387 end = next + 4;
388 if (end >= len) {
389 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
390 goto bail;
391 }
392 /* Decode 4 hex digits */
393 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200394 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000395 c <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000396 switch (digit) {
397 case '0': case '1': case '2': case '3': case '4':
398 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000399 c |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000400 case 'a': case 'b': case 'c': case 'd': case 'e':
401 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000402 c |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000403 case 'A': case 'B': case 'C': case 'D': case 'E':
404 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000405 c |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000406 default:
407 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
408 goto bail;
409 }
410 }
Christian Heimes90540002008-05-08 14:29:10 +0000411 /* Surrogate pair */
Serhiy Storchakac93329b2013-11-26 21:25:28 +0200412 if (Py_UNICODE_IS_HIGH_SURROGATE(c) && end + 6 < len &&
413 PyUnicode_READ(kind, buf, next++) == '\\' &&
414 PyUnicode_READ(kind, buf, next++) == 'u') {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200415 Py_UCS4 c2 = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000416 end += 6;
417 /* Decode 4 hex digits */
418 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200419 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Antoine Pitrou5b0e9e82010-10-09 15:24:28 +0000420 c2 <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000421 switch (digit) {
422 case '0': case '1': case '2': case '3': case '4':
423 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000424 c2 |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000425 case 'a': case 'b': case 'c': case 'd': case 'e':
426 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000427 c2 |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000428 case 'A': case 'B': case 'C': case 'D': case 'E':
429 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000430 c2 |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000431 default:
432 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
433 goto bail;
434 }
435 }
Serhiy Storchakac93329b2013-11-26 21:25:28 +0200436 if (Py_UNICODE_IS_LOW_SURROGATE(c2))
437 c = Py_UNICODE_JOIN_SURROGATES(c, c2);
438 else
439 end -= 6;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000440 }
Christian Heimes90540002008-05-08 14:29:10 +0000441 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000442 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200443 chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1);
Christian Heimes90540002008-05-08 14:29:10 +0000444 if (chunk == NULL) {
445 goto bail;
446 }
Christian Heimes90540002008-05-08 14:29:10 +0000447 }
448
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000449 if (chunks == NULL) {
450 if (chunk != NULL)
451 rval = chunk;
452 else
453 rval = PyUnicode_FromStringAndSize("", 0);
Christian Heimes90540002008-05-08 14:29:10 +0000454 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000455 else {
456 APPEND_OLD_CHUNK
457 rval = join_list_unicode(chunks);
458 if (rval == NULL) {
459 goto bail;
460 }
461 Py_CLEAR(chunks);
462 }
463
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000464 *next_end_ptr = end;
465 return rval;
Christian Heimes90540002008-05-08 14:29:10 +0000466bail:
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000467 *next_end_ptr = -1;
Christian Heimes90540002008-05-08 14:29:10 +0000468 Py_XDECREF(chunks);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000469 Py_XDECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000470 return NULL;
471}
472
473PyDoc_STRVAR(pydoc_scanstring,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000474 "scanstring(string, end, strict=True) -> (string, end)\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000475 "\n"
476 "Scan the string s for a JSON string. End is the index of the\n"
477 "character in s after the quote that started the JSON string.\n"
478 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
479 "on attempt to decode an invalid string. If strict is False then literal\n"
480 "control characters are allowed in the string.\n"
481 "\n"
482 "Returns a tuple of the decoded string and the index of the character in s\n"
483 "after the end quote."
484);
Christian Heimes90540002008-05-08 14:29:10 +0000485
486static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000487py_scanstring(PyObject* self UNUSED, PyObject *args)
Christian Heimes90540002008-05-08 14:29:10 +0000488{
489 PyObject *pystr;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000490 PyObject *rval;
Christian Heimes90540002008-05-08 14:29:10 +0000491 Py_ssize_t end;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000492 Py_ssize_t next_end = -1;
493 int strict = 1;
Antoine Pitroucbb02842012-12-01 19:34:16 +0100494 if (!PyArg_ParseTuple(args, "On|i:scanstring", &pystr, &end, &strict)) {
Christian Heimes90540002008-05-08 14:29:10 +0000495 return NULL;
496 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000497 if (PyUnicode_Check(pystr)) {
498 rval = scanstring_unicode(pystr, end, strict, &next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000499 }
500 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 PyErr_Format(PyExc_TypeError,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000502 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000503 Py_TYPE(pystr)->tp_name);
504 return NULL;
505 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000506 return _build_rval_index_tuple(rval, next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000507}
508
509PyDoc_STRVAR(pydoc_encode_basestring_ascii,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000510 "encode_basestring_ascii(string) -> string\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000511 "\n"
512 "Return an ASCII-only JSON representation of a Python string"
513);
Christian Heimes90540002008-05-08 14:29:10 +0000514
515static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000516py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
Christian Heimes90540002008-05-08 14:29:10 +0000517{
518 PyObject *rval;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000519 /* Return an ASCII-only JSON representation of a Python string */
Christian Heimes90540002008-05-08 14:29:10 +0000520 /* METH_O */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000521 if (PyUnicode_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000522 rval = ascii_escape_unicode(pystr);
523 }
524 else {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000525 PyErr_Format(PyExc_TypeError,
526 "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 }
Christian Heimes90540002008-05-08 14:29:10 +0000530 return rval;
531}
532
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000533static void
534scanner_dealloc(PyObject *self)
535{
536 /* Deallocate scanner object */
537 scanner_clear(self);
538 Py_TYPE(self)->tp_free(self);
539}
540
541static int
542scanner_traverse(PyObject *self, visitproc visit, void *arg)
543{
544 PyScannerObject *s;
545 assert(PyScanner_Check(self));
546 s = (PyScannerObject *)self;
547 Py_VISIT(s->strict);
548 Py_VISIT(s->object_hook);
549 Py_VISIT(s->object_pairs_hook);
550 Py_VISIT(s->parse_float);
551 Py_VISIT(s->parse_int);
552 Py_VISIT(s->parse_constant);
553 return 0;
554}
555
556static int
557scanner_clear(PyObject *self)
558{
559 PyScannerObject *s;
560 assert(PyScanner_Check(self));
561 s = (PyScannerObject *)self;
562 Py_CLEAR(s->strict);
563 Py_CLEAR(s->object_hook);
564 Py_CLEAR(s->object_pairs_hook);
565 Py_CLEAR(s->parse_float);
566 Py_CLEAR(s->parse_int);
567 Py_CLEAR(s->parse_constant);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000568 Py_CLEAR(s->memo);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000569 return 0;
570}
571
572static PyObject *
573_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
574 /* Read a JSON object from PyUnicode pystr.
575 idx is the index of the first character after the opening curly brace.
576 *next_idx_ptr is a return-by-reference index to the first character after
577 the closing curly brace.
578
579 Returns a new PyObject (usually a dict, but object_hook can change that)
580 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200581 void *str;
582 int kind;
583 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000584 PyObject *val = NULL;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000585 PyObject *rval = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000586 PyObject *key = NULL;
587 int strict = PyObject_IsTrue(s->strict);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000588 int has_pairs_hook = (s->object_pairs_hook != Py_None);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000589 Py_ssize_t next_idx;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000590
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200591 if (PyUnicode_READY(pystr) == -1)
592 return NULL;
593
594 str = PyUnicode_DATA(pystr);
595 kind = PyUnicode_KIND(pystr);
596 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
597
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000598 if (has_pairs_hook)
599 rval = PyList_New(0);
600 else
601 rval = PyDict_New();
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000602 if (rval == NULL)
603 return NULL;
604
605 /* skip whitespace after { */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200606 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000607
608 /* only loop if the object is non-empty */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200609 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') {
610 while (1) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000611 PyObject *memokey;
612
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000613 /* read key */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200614 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '"') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200615 raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000616 goto bail;
617 }
618 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
619 if (key == NULL)
620 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000621 memokey = PyDict_GetItem(s->memo, key);
622 if (memokey != NULL) {
623 Py_INCREF(memokey);
624 Py_DECREF(key);
625 key = memokey;
626 }
627 else {
628 if (PyDict_SetItem(s->memo, key, key) < 0)
629 goto bail;
630 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000631 idx = next_idx;
632
633 /* skip whitespace between key and : delimiter, read :, skip whitespace */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200634 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
635 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200636 raise_errmsg("Expecting ':' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000637 goto bail;
638 }
639 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200640 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000641
642 /* read any JSON term */
643 val = scan_once_unicode(s, pystr, idx, &next_idx);
644 if (val == NULL)
645 goto bail;
646
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000647 if (has_pairs_hook) {
648 PyObject *item = PyTuple_Pack(2, key, val);
649 if (item == NULL)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000650 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000651 Py_CLEAR(key);
652 Py_CLEAR(val);
653 if (PyList_Append(rval, item) == -1) {
654 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000655 goto bail;
656 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000657 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000658 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000659 else {
660 if (PyDict_SetItem(rval, key, val) < 0)
661 goto bail;
662 Py_CLEAR(key);
663 Py_CLEAR(val);
664 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000665 idx = next_idx;
666
667 /* skip whitespace before } or , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200668 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000669
670 /* bail if the object is closed or we didn't get the , delimiter */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200671 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) == '}')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000672 break;
Ezio Melotti37623ab2013-01-03 08:44:15 +0200673 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200674 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000675 goto bail;
676 }
677 idx++;
678
679 /* skip whitespace after , delimiter */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200680 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000681 }
682 }
683
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000684 *next_idx_ptr = idx + 1;
685
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000686 if (has_pairs_hook) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000687 val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000688 Py_DECREF(rval);
689 return val;
690 }
691
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000692 /* if object_hook is not None: rval = object_hook(rval) */
693 if (s->object_hook != Py_None) {
694 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000695 Py_DECREF(rval);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000696 return val;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000697 }
698 return rval;
699bail:
700 Py_XDECREF(key);
701 Py_XDECREF(val);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000702 Py_XDECREF(rval);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000703 return NULL;
704}
705
706static PyObject *
707_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
708 /* Read a JSON array from PyString pystr.
709 idx is the index of the first character after the opening brace.
710 *next_idx_ptr is a return-by-reference index to the first character after
711 the closing brace.
712
713 Returns a new PyList
714 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200715 void *str;
716 int kind;
717 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000718 PyObject *val = NULL;
719 PyObject *rval = PyList_New(0);
720 Py_ssize_t next_idx;
721 if (rval == NULL)
722 return NULL;
723
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200724 if (PyUnicode_READY(pystr) == -1)
725 return NULL;
726
727 str = PyUnicode_DATA(pystr);
728 kind = PyUnicode_KIND(pystr);
729 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
730
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000731 /* skip whitespace after [ */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200732 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000733
734 /* only loop if the array is non-empty */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200735 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
736 while (1) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000737
738 /* read any JSON term */
739 val = scan_once_unicode(s, pystr, idx, &next_idx);
740 if (val == NULL)
741 goto bail;
742
743 if (PyList_Append(rval, val) == -1)
744 goto bail;
745
746 Py_CLEAR(val);
747 idx = next_idx;
748
749 /* skip whitespace between term and , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200750 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000751
752 /* bail if the array is closed or we didn't get the , delimiter */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200753 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) == ']')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000754 break;
Ezio Melotti37623ab2013-01-03 08:44:15 +0200755 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200756 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000757 goto bail;
758 }
759 idx++;
760
761 /* skip whitespace after , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200762 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000763 }
764 }
765
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200766 /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */
767 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
Ezio Melotti37623ab2013-01-03 08:44:15 +0200768 raise_errmsg("Expecting value", pystr, end_idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000769 goto bail;
770 }
771 *next_idx_ptr = idx + 1;
772 return rval;
773bail:
774 Py_XDECREF(val);
775 Py_DECREF(rval);
776 return NULL;
777}
778
779static PyObject *
780_parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
781 /* Read a JSON constant from PyString pystr.
782 constant is the constant string that was found
783 ("NaN", "Infinity", "-Infinity").
784 idx is the index of the first character of the constant
785 *next_idx_ptr is a return-by-reference index to the first character after
786 the constant.
787
788 Returns the result of parse_constant
789 */
790 PyObject *cstr;
791 PyObject *rval;
792 /* constant is "NaN", "Infinity", or "-Infinity" */
793 cstr = PyUnicode_InternFromString(constant);
794 if (cstr == NULL)
795 return NULL;
796
797 /* rval = parse_constant(constant) */
798 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
Victor Stinnerc4f281e2011-10-11 22:11:42 +0200799 idx += PyUnicode_GET_LENGTH(cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000800 Py_DECREF(cstr);
801 *next_idx_ptr = idx;
802 return rval;
803}
804
805static PyObject *
806_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
807 /* Read a JSON number from PyUnicode pystr.
808 idx is the index of the first character of the number
809 *next_idx_ptr is a return-by-reference index to the first character after
810 the number.
811
812 Returns a new PyObject representation of that number:
813 PyInt, PyLong, or PyFloat.
814 May return other types if parse_int or parse_float are set
815 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200816 void *str;
817 int kind;
818 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000819 Py_ssize_t idx = start;
820 int is_float = 0;
821 PyObject *rval;
Antoine Pitrouf6454512011-04-25 19:16:06 +0200822 PyObject *numstr = NULL;
823 PyObject *custom_func;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000824
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200825 if (PyUnicode_READY(pystr) == -1)
826 return NULL;
827
828 str = PyUnicode_DATA(pystr);
829 kind = PyUnicode_KIND(pystr);
830 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
831
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000832 /* 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 +0200833 if (PyUnicode_READ(kind, str, idx) == '-') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000834 idx++;
835 if (idx > end_idx) {
Ezio Melotti37623ab2013-01-03 08:44:15 +0200836 raise_stop_iteration(start);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000837 return NULL;
838 }
839 }
840
841 /* 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 +0200842 if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000843 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200844 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000845 }
846 /* if it starts with 0 we only expect one integer digit */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200847 else if (PyUnicode_READ(kind, str, idx) == '0') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000848 idx++;
849 }
850 /* no integer digits, error */
851 else {
Ezio Melotti37623ab2013-01-03 08:44:15 +0200852 raise_stop_iteration(start);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000853 return NULL;
854 }
855
856 /* if the next char is '.' followed by a digit then read all float digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200857 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 +0000858 is_float = 1;
859 idx += 2;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200860 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000861 }
862
863 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200864 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000865 Py_ssize_t e_start = idx;
866 idx++;
867
868 /* read an exponent sign if present */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200869 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000870
871 /* read all digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200872 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000873
874 /* if we got a digit, then parse as float. if not, backtrack */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200875 if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000876 is_float = 1;
877 }
878 else {
879 idx = e_start;
880 }
881 }
882
Antoine Pitrouf6454512011-04-25 19:16:06 +0200883 if (is_float && s->parse_float != (PyObject *)&PyFloat_Type)
884 custom_func = s->parse_float;
885 else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type)
886 custom_func = s->parse_int;
887 else
888 custom_func = NULL;
889
890 if (custom_func) {
891 /* copy the section we determined to be a number */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200892 numstr = PyUnicode_FromKindAndData(kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200893 (char*)str + kind * start,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200894 idx - start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200895 if (numstr == NULL)
896 return NULL;
897 rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000898 }
899 else {
Antoine Pitrouf6454512011-04-25 19:16:06 +0200900 Py_ssize_t i, n;
901 char *buf;
902 /* Straight conversion to ASCII, to avoid costly conversion of
903 decimal unicode digits (which cannot appear here) */
904 n = idx - start;
905 numstr = PyBytes_FromStringAndSize(NULL, n);
906 if (numstr == NULL)
907 return NULL;
908 buf = PyBytes_AS_STRING(numstr);
909 for (i = 0; i < n; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200910 buf[i] = (char) PyUnicode_READ(kind, str, i + start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200911 }
912 if (is_float)
913 rval = PyFloat_FromString(numstr);
914 else
915 rval = PyLong_FromString(buf, NULL, 10);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000916 }
917 Py_DECREF(numstr);
918 *next_idx_ptr = idx;
919 return rval;
920}
921
922static PyObject *
923scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
924{
925 /* Read one JSON term (of any kind) from PyUnicode pystr.
926 idx is the index of the first character of the term
927 *next_idx_ptr is a return-by-reference index to the first character after
928 the number.
929
930 Returns a new PyObject representation of the term.
931 */
Ezio Melotti362b9512011-05-07 17:58:09 +0300932 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200933 void *str;
934 int kind;
935 Py_ssize_t length;
936
937 if (PyUnicode_READY(pystr) == -1)
938 return NULL;
939
940 str = PyUnicode_DATA(pystr);
941 kind = PyUnicode_KIND(pystr);
942 length = PyUnicode_GET_LENGTH(pystr);
943
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000944 if (idx >= length) {
Ezio Melotti37623ab2013-01-03 08:44:15 +0200945 raise_stop_iteration(idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000946 return NULL;
947 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200948
949 switch (PyUnicode_READ(kind, str, idx)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000950 case '"':
951 /* string */
952 return scanstring_unicode(pystr, idx + 1,
953 PyObject_IsTrue(s->strict),
954 next_idx_ptr);
955 case '{':
956 /* object */
Ezio Melotti362b9512011-05-07 17:58:09 +0300957 if (Py_EnterRecursiveCall(" while decoding a JSON object "
958 "from a unicode string"))
959 return NULL;
960 res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
961 Py_LeaveRecursiveCall();
962 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000963 case '[':
964 /* array */
Ezio Melotti362b9512011-05-07 17:58:09 +0300965 if (Py_EnterRecursiveCall(" while decoding a JSON array "
966 "from a unicode string"))
967 return NULL;
968 res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
969 Py_LeaveRecursiveCall();
970 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000971 case 'n':
972 /* null */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200973 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 +0000974 Py_INCREF(Py_None);
975 *next_idx_ptr = idx + 4;
976 return Py_None;
977 }
978 break;
979 case 't':
980 /* true */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200981 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 +0000982 Py_INCREF(Py_True);
983 *next_idx_ptr = idx + 4;
984 return Py_True;
985 }
986 break;
987 case 'f':
988 /* false */
Victor Stinnerd9c06312011-10-11 21:56:19 +0200989 if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
990 PyUnicode_READ(kind, str, idx + 2) == 'l' &&
991 PyUnicode_READ(kind, str, idx + 3) == 's' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200992 PyUnicode_READ(kind, str, idx + 4) == 'e') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000993 Py_INCREF(Py_False);
994 *next_idx_ptr = idx + 5;
995 return Py_False;
996 }
997 break;
998 case 'N':
999 /* NaN */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001000 if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001001 PyUnicode_READ(kind, str, idx + 2) == 'N') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001002 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1003 }
1004 break;
1005 case 'I':
1006 /* Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001007 if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
1008 PyUnicode_READ(kind, str, idx + 2) == 'f' &&
1009 PyUnicode_READ(kind, str, idx + 3) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001010 PyUnicode_READ(kind, str, idx + 4) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001011 PyUnicode_READ(kind, str, idx + 5) == 'i' &&
1012 PyUnicode_READ(kind, str, idx + 6) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001013 PyUnicode_READ(kind, str, idx + 7) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001014 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1015 }
1016 break;
1017 case '-':
1018 /* -Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001019 if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001020 PyUnicode_READ(kind, str, idx + 2) == 'n' &&
1021 PyUnicode_READ(kind, str, idx + 3) == 'f' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001022 PyUnicode_READ(kind, str, idx + 4) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001023 PyUnicode_READ(kind, str, idx + 5) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001024 PyUnicode_READ(kind, str, idx + 6) == 'i' &&
1025 PyUnicode_READ(kind, str, idx + 7) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001026 PyUnicode_READ(kind, str, idx + 8) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001027 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1028 }
1029 break;
1030 }
1031 /* Didn't find a string, object, array, or named constant. Look for a number. */
1032 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
1033}
1034
1035static PyObject *
1036scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
1037{
1038 /* Python callable interface to scan_once_{str,unicode} */
1039 PyObject *pystr;
1040 PyObject *rval;
1041 Py_ssize_t idx;
1042 Py_ssize_t next_idx = -1;
1043 static char *kwlist[] = {"string", "idx", NULL};
1044 PyScannerObject *s;
1045 assert(PyScanner_Check(self));
1046 s = (PyScannerObject *)self;
Antoine Pitroucbb02842012-12-01 19:34:16 +01001047 if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:scan_once", kwlist, &pystr, &idx))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001048 return NULL;
1049
1050 if (PyUnicode_Check(pystr)) {
1051 rval = scan_once_unicode(s, pystr, idx, &next_idx);
1052 }
1053 else {
1054 PyErr_Format(PyExc_TypeError,
1055 "first argument must be a string, not %.80s",
1056 Py_TYPE(pystr)->tp_name);
1057 return NULL;
1058 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001059 PyDict_Clear(s->memo);
1060 if (rval == NULL)
1061 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001062 return _build_rval_index_tuple(rval, next_idx);
1063}
1064
1065static PyObject *
1066scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1067{
1068 PyScannerObject *s;
1069 s = (PyScannerObject *)type->tp_alloc(type, 0);
1070 if (s != NULL) {
1071 s->strict = NULL;
1072 s->object_hook = NULL;
1073 s->object_pairs_hook = NULL;
1074 s->parse_float = NULL;
1075 s->parse_int = NULL;
1076 s->parse_constant = NULL;
1077 }
1078 return (PyObject *)s;
1079}
1080
1081static int
1082scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1083{
1084 /* Initialize Scanner object */
1085 PyObject *ctx;
1086 static char *kwlist[] = {"context", NULL};
1087 PyScannerObject *s;
1088
1089 assert(PyScanner_Check(self));
1090 s = (PyScannerObject *)self;
1091
1092 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1093 return -1;
1094
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001095 if (s->memo == NULL) {
1096 s->memo = PyDict_New();
1097 if (s->memo == NULL)
1098 goto bail;
1099 }
1100
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001101 /* All of these will fail "gracefully" so we don't need to verify them */
1102 s->strict = PyObject_GetAttrString(ctx, "strict");
1103 if (s->strict == NULL)
1104 goto bail;
1105 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1106 if (s->object_hook == NULL)
1107 goto bail;
1108 s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1109 if (s->object_pairs_hook == NULL)
1110 goto bail;
1111 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1112 if (s->parse_float == NULL)
1113 goto bail;
1114 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1115 if (s->parse_int == NULL)
1116 goto bail;
1117 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1118 if (s->parse_constant == NULL)
1119 goto bail;
1120
1121 return 0;
1122
1123bail:
1124 Py_CLEAR(s->strict);
1125 Py_CLEAR(s->object_hook);
1126 Py_CLEAR(s->object_pairs_hook);
1127 Py_CLEAR(s->parse_float);
1128 Py_CLEAR(s->parse_int);
1129 Py_CLEAR(s->parse_constant);
1130 return -1;
1131}
1132
1133PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1134
1135static
1136PyTypeObject PyScannerType = {
1137 PyVarObject_HEAD_INIT(NULL, 0)
1138 "_json.Scanner", /* tp_name */
1139 sizeof(PyScannerObject), /* tp_basicsize */
1140 0, /* tp_itemsize */
1141 scanner_dealloc, /* tp_dealloc */
1142 0, /* tp_print */
1143 0, /* tp_getattr */
1144 0, /* tp_setattr */
1145 0, /* tp_compare */
1146 0, /* tp_repr */
1147 0, /* tp_as_number */
1148 0, /* tp_as_sequence */
1149 0, /* tp_as_mapping */
1150 0, /* tp_hash */
1151 scanner_call, /* tp_call */
1152 0, /* tp_str */
1153 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1154 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1155 0, /* tp_as_buffer */
1156 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1157 scanner_doc, /* tp_doc */
1158 scanner_traverse, /* tp_traverse */
1159 scanner_clear, /* tp_clear */
1160 0, /* tp_richcompare */
1161 0, /* tp_weaklistoffset */
1162 0, /* tp_iter */
1163 0, /* tp_iternext */
1164 0, /* tp_methods */
1165 scanner_members, /* tp_members */
1166 0, /* tp_getset */
1167 0, /* tp_base */
1168 0, /* tp_dict */
1169 0, /* tp_descr_get */
1170 0, /* tp_descr_set */
1171 0, /* tp_dictoffset */
1172 scanner_init, /* tp_init */
1173 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1174 scanner_new, /* tp_new */
1175 0,/* PyObject_GC_Del, */ /* tp_free */
1176};
1177
1178static PyObject *
1179encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1180{
1181 PyEncoderObject *s;
1182 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1183 if (s != NULL) {
1184 s->markers = NULL;
1185 s->defaultfn = NULL;
1186 s->encoder = NULL;
1187 s->indent = NULL;
1188 s->key_separator = NULL;
1189 s->item_separator = NULL;
1190 s->sort_keys = NULL;
1191 s->skipkeys = NULL;
1192 }
1193 return (PyObject *)s;
1194}
1195
1196static int
1197encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1198{
1199 /* initialize Encoder object */
1200 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1201
1202 PyEncoderObject *s;
Antoine Pitrou781eba72009-12-08 15:57:31 +00001203 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1204 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001205
1206 assert(PyEncoder_Check(self));
1207 s = (PyEncoderObject *)self;
1208
1209 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
Antoine Pitrou781eba72009-12-08 15:57:31 +00001210 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1211 &sort_keys, &skipkeys, &allow_nan))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001212 return -1;
1213
Antoine Pitrou781eba72009-12-08 15:57:31 +00001214 s->markers = markers;
1215 s->defaultfn = defaultfn;
1216 s->encoder = encoder;
1217 s->indent = indent;
1218 s->key_separator = key_separator;
1219 s->item_separator = item_separator;
1220 s->sort_keys = sort_keys;
1221 s->skipkeys = skipkeys;
1222 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1223 s->allow_nan = PyObject_IsTrue(allow_nan);
1224
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001225 Py_INCREF(s->markers);
1226 Py_INCREF(s->defaultfn);
1227 Py_INCREF(s->encoder);
1228 Py_INCREF(s->indent);
1229 Py_INCREF(s->key_separator);
1230 Py_INCREF(s->item_separator);
1231 Py_INCREF(s->sort_keys);
1232 Py_INCREF(s->skipkeys);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001233 return 0;
1234}
1235
1236static PyObject *
1237encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1238{
1239 /* Python callable interface to encode_listencode_obj */
1240 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1241 PyObject *obj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001242 Py_ssize_t indent_level;
1243 PyEncoderObject *s;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001244 _PyAccu acc;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001245
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001246 assert(PyEncoder_Check(self));
1247 s = (PyEncoderObject *)self;
Antoine Pitroucbb02842012-12-01 19:34:16 +01001248 if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:_iterencode", kwlist,
1249 &obj, &indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001250 return NULL;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001251 if (_PyAccu_Init(&acc))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001252 return NULL;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001253 if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001254 _PyAccu_Destroy(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001255 return NULL;
1256 }
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001257 return _PyAccu_FinishAsList(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001258}
1259
1260static PyObject *
1261_encoded_const(PyObject *obj)
1262{
1263 /* Return the JSON string representation of None, True, False */
1264 if (obj == Py_None) {
1265 static PyObject *s_null = NULL;
1266 if (s_null == NULL) {
1267 s_null = PyUnicode_InternFromString("null");
1268 }
1269 Py_INCREF(s_null);
1270 return s_null;
1271 }
1272 else if (obj == Py_True) {
1273 static PyObject *s_true = NULL;
1274 if (s_true == NULL) {
1275 s_true = PyUnicode_InternFromString("true");
1276 }
1277 Py_INCREF(s_true);
1278 return s_true;
1279 }
1280 else if (obj == Py_False) {
1281 static PyObject *s_false = NULL;
1282 if (s_false == NULL) {
1283 s_false = PyUnicode_InternFromString("false");
1284 }
1285 Py_INCREF(s_false);
1286 return s_false;
1287 }
1288 else {
1289 PyErr_SetString(PyExc_ValueError, "not a const");
1290 return NULL;
1291 }
1292}
1293
1294static PyObject *
Ethan Furmana4998a72013-08-10 13:01:45 -07001295encoder_encode_long(PyEncoderObject* s UNUSED, PyObject *obj)
1296{
1297 /* Return the JSON representation of a PyLong and PyLong subclasses.
1298 Calls int() on PyLong subclasses in case the str() was changed.
1299 Added specifically to deal with IntEnum. See Issue18264. */
1300 PyObject *encoded, *longobj;
1301 if (PyLong_CheckExact(obj)) {
1302 encoded = PyObject_Str(obj);
1303 }
1304 else {
1305 longobj = PyNumber_Long(obj);
1306 if (longobj == NULL) {
1307 PyErr_SetString(
1308 PyExc_ValueError,
1309 "Unable to coerce int subclass to int"
1310 );
1311 return NULL;
1312 }
1313 encoded = PyObject_Str(longobj);
1314 Py_DECREF(longobj);
1315 }
1316 return encoded;
1317}
1318
1319
1320static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001321encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1322{
Ethan Furmana4998a72013-08-10 13:01:45 -07001323 /* Return the JSON representation of a PyFloat.
1324 Modified to call float() on float subclasses in case the subclass
1325 changes the repr. See Issue18264. */
1326 PyObject *encoded, *floatobj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001327 double i = PyFloat_AS_DOUBLE(obj);
1328 if (!Py_IS_FINITE(i)) {
1329 if (!s->allow_nan) {
Ethan Furmana4998a72013-08-10 13:01:45 -07001330 PyErr_SetString(
1331 PyExc_ValueError,
1332 "Out of range float values are not JSON compliant"
1333 );
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001334 return NULL;
1335 }
1336 if (i > 0) {
1337 return PyUnicode_FromString("Infinity");
1338 }
1339 else if (i < 0) {
1340 return PyUnicode_FromString("-Infinity");
1341 }
1342 else {
1343 return PyUnicode_FromString("NaN");
1344 }
1345 }
Ethan Furmana4998a72013-08-10 13:01:45 -07001346 /* coerce float subclasses to float (primarily for Enum) */
1347 if (PyFloat_CheckExact(obj)) {
1348 /* Use a better float format here? */
1349 encoded = PyObject_Repr(obj);
1350 }
1351 else {
1352 floatobj = PyNumber_Float(obj);
1353 if (floatobj == NULL) {
1354 PyErr_SetString(
1355 PyExc_ValueError,
1356 "Unable to coerce float subclass to float"
1357 );
1358 return NULL;
1359 }
1360 encoded = PyObject_Repr(floatobj);
1361 Py_DECREF(floatobj);
1362 }
1363 return encoded;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001364}
1365
1366static PyObject *
1367encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1368{
1369 /* Return the JSON representation of a string */
1370 if (s->fast_encode)
1371 return py_encode_basestring_ascii(NULL, obj);
1372 else
1373 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1374}
1375
1376static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001377_steal_accumulate(_PyAccu *acc, PyObject *stolen)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001378{
1379 /* Append stolen and then decrement its reference count */
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001380 int rval = _PyAccu_Accumulate(acc, stolen);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001381 Py_DECREF(stolen);
1382 return rval;
1383}
1384
1385static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001386encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001387 PyObject *obj, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001388{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001389 /* Encode Python object obj to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001390 PyObject *newobj;
1391 int rv;
1392
1393 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1394 PyObject *cstr = _encoded_const(obj);
1395 if (cstr == NULL)
1396 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001397 return _steal_accumulate(acc, cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001398 }
1399 else if (PyUnicode_Check(obj))
1400 {
1401 PyObject *encoded = encoder_encode_string(s, obj);
1402 if (encoded == NULL)
1403 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001404 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001405 }
1406 else if (PyLong_Check(obj)) {
Ethan Furmana4998a72013-08-10 13:01:45 -07001407 PyObject *encoded = encoder_encode_long(s, obj);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001408 if (encoded == NULL)
1409 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001410 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001411 }
1412 else if (PyFloat_Check(obj)) {
1413 PyObject *encoded = encoder_encode_float(s, obj);
1414 if (encoded == NULL)
1415 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001416 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001417 }
1418 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001419 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1420 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001421 rv = encoder_listencode_list(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001422 Py_LeaveRecursiveCall();
1423 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001424 }
1425 else if (PyDict_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001426 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1427 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001428 rv = encoder_listencode_dict(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001429 Py_LeaveRecursiveCall();
1430 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001431 }
1432 else {
1433 PyObject *ident = NULL;
1434 if (s->markers != Py_None) {
1435 int has_key;
1436 ident = PyLong_FromVoidPtr(obj);
1437 if (ident == NULL)
1438 return -1;
1439 has_key = PyDict_Contains(s->markers, ident);
1440 if (has_key) {
1441 if (has_key != -1)
1442 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1443 Py_DECREF(ident);
1444 return -1;
1445 }
1446 if (PyDict_SetItem(s->markers, ident, obj)) {
1447 Py_DECREF(ident);
1448 return -1;
1449 }
1450 }
1451 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1452 if (newobj == NULL) {
1453 Py_XDECREF(ident);
1454 return -1;
1455 }
Ezio Melotti13672652011-05-11 01:02:56 +03001456
1457 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1458 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001459 rv = encoder_listencode_obj(s, acc, newobj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001460 Py_LeaveRecursiveCall();
1461
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001462 Py_DECREF(newobj);
1463 if (rv) {
1464 Py_XDECREF(ident);
1465 return -1;
1466 }
1467 if (ident != NULL) {
1468 if (PyDict_DelItem(s->markers, ident)) {
1469 Py_XDECREF(ident);
1470 return -1;
1471 }
1472 Py_XDECREF(ident);
1473 }
1474 return rv;
1475 }
1476}
1477
1478static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001479encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001480 PyObject *dct, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001481{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001482 /* Encode Python dict dct a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001483 static PyObject *open_dict = NULL;
1484 static PyObject *close_dict = NULL;
1485 static PyObject *empty_dict = NULL;
1486 PyObject *kstr = NULL;
1487 PyObject *ident = NULL;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001488 PyObject *it = NULL;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001489 PyObject *items;
1490 PyObject *item = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001491 int skipkeys;
1492 Py_ssize_t idx;
1493
1494 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
1495 open_dict = PyUnicode_InternFromString("{");
1496 close_dict = PyUnicode_InternFromString("}");
1497 empty_dict = PyUnicode_InternFromString("{}");
1498 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
1499 return -1;
1500 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001501 if (Py_SIZE(dct) == 0)
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001502 return _PyAccu_Accumulate(acc, empty_dict);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001503
1504 if (s->markers != Py_None) {
1505 int has_key;
1506 ident = PyLong_FromVoidPtr(dct);
1507 if (ident == NULL)
1508 goto bail;
1509 has_key = PyDict_Contains(s->markers, ident);
1510 if (has_key) {
1511 if (has_key != -1)
1512 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1513 goto bail;
1514 }
1515 if (PyDict_SetItem(s->markers, ident, dct)) {
1516 goto bail;
1517 }
1518 }
1519
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001520 if (_PyAccu_Accumulate(acc, open_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001521 goto bail;
1522
1523 if (s->indent != Py_None) {
1524 /* TODO: DOES NOT RUN */
1525 indent_level += 1;
1526 /*
1527 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1528 separator = _item_separator + newline_indent
1529 buf += newline_indent
1530 */
1531 }
1532
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001533 if (PyObject_IsTrue(s->sort_keys)) {
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001534 /* First sort the keys then replace them with (key, value) tuples. */
1535 Py_ssize_t i, nitems;
1536 items = PyMapping_Keys(dct);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 if (items == NULL)
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001538 goto bail;
1539 if (!PyList_Check(items)) {
1540 PyErr_SetString(PyExc_ValueError, "keys must return list");
1541 goto bail;
1542 }
1543 if (PyList_Sort(items) < 0)
1544 goto bail;
1545 nitems = PyList_GET_SIZE(items);
1546 for (i = 0; i < nitems; i++) {
1547 PyObject *key, *value;
1548 key = PyList_GET_ITEM(items, i);
1549 value = PyDict_GetItem(dct, key);
1550 item = PyTuple_Pack(2, key, value);
1551 if (item == NULL)
1552 goto bail;
1553 PyList_SET_ITEM(items, i, item);
1554 Py_DECREF(key);
1555 }
1556 }
1557 else {
1558 items = PyMapping_Items(dct);
1559 }
1560 if (items == NULL)
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001561 goto bail;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001562 it = PyObject_GetIter(items);
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001563 Py_DECREF(items);
1564 if (it == NULL)
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001565 goto bail;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001566 skipkeys = PyObject_IsTrue(s->skipkeys);
1567 idx = 0;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001568 while ((item = PyIter_Next(it)) != NULL) {
1569 PyObject *encoded, *key, *value;
1570 if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
1571 PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
1572 goto bail;
1573 }
1574 key = PyTuple_GET_ITEM(item, 0);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001575 if (PyUnicode_Check(key)) {
1576 Py_INCREF(key);
1577 kstr = key;
1578 }
1579 else if (PyFloat_Check(key)) {
1580 kstr = encoder_encode_float(s, key);
1581 if (kstr == NULL)
1582 goto bail;
1583 }
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001584 else if (key == Py_True || key == Py_False || key == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 /* This must come before the PyLong_Check because
1586 True and False are also 1 and 0.*/
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001587 kstr = _encoded_const(key);
1588 if (kstr == NULL)
1589 goto bail;
1590 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001591 else if (PyLong_Check(key)) {
Ethan Furmana4998a72013-08-10 13:01:45 -07001592 kstr = encoder_encode_long(s, key);
1593 if (kstr == NULL) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001594 goto bail;
Ethan Furmana4998a72013-08-10 13:01:45 -07001595 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001596 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001597 else if (skipkeys) {
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001598 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001599 continue;
1600 }
1601 else {
1602 /* TODO: include repr of key */
Doug Hellmann1c524752010-07-21 12:29:04 +00001603 PyErr_SetString(PyExc_TypeError, "keys must be a string");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001604 goto bail;
1605 }
1606
1607 if (idx) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001608 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001609 goto bail;
1610 }
1611
1612 encoded = encoder_encode_string(s, kstr);
1613 Py_CLEAR(kstr);
1614 if (encoded == NULL)
1615 goto bail;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001616 if (_PyAccu_Accumulate(acc, encoded)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001617 Py_DECREF(encoded);
1618 goto bail;
1619 }
1620 Py_DECREF(encoded);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001621 if (_PyAccu_Accumulate(acc, s->key_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001622 goto bail;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001623
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001624 value = PyTuple_GET_ITEM(item, 1);
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001625 if (encoder_listencode_obj(s, acc, value, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001626 goto bail;
1627 idx += 1;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001628 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001629 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001630 if (PyErr_Occurred())
1631 goto bail;
1632 Py_CLEAR(it);
1633
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001634 if (ident != NULL) {
1635 if (PyDict_DelItem(s->markers, ident))
1636 goto bail;
1637 Py_CLEAR(ident);
1638 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001639 /* TODO DOES NOT RUN; dead code
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001640 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001641 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001642
1643 yield '\n' + (' ' * (_indent * _current_indent_level))
1644 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001645 if (_PyAccu_Accumulate(acc, close_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001646 goto bail;
1647 return 0;
1648
1649bail:
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001650 Py_XDECREF(it);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001651 Py_XDECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001652 Py_XDECREF(kstr);
1653 Py_XDECREF(ident);
1654 return -1;
1655}
1656
1657
1658static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001659encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001660 PyObject *seq, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001661{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001662 /* Encode Python list seq to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001663 static PyObject *open_array = NULL;
1664 static PyObject *close_array = NULL;
1665 static PyObject *empty_array = NULL;
1666 PyObject *ident = NULL;
1667 PyObject *s_fast = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001668 Py_ssize_t i;
1669
1670 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
1671 open_array = PyUnicode_InternFromString("[");
1672 close_array = PyUnicode_InternFromString("]");
1673 empty_array = PyUnicode_InternFromString("[]");
1674 if (open_array == NULL || close_array == NULL || empty_array == NULL)
1675 return -1;
1676 }
1677 ident = NULL;
1678 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
1679 if (s_fast == NULL)
1680 return -1;
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001681 if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001682 Py_DECREF(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001683 return _PyAccu_Accumulate(acc, empty_array);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001684 }
1685
1686 if (s->markers != Py_None) {
1687 int has_key;
1688 ident = PyLong_FromVoidPtr(seq);
1689 if (ident == NULL)
1690 goto bail;
1691 has_key = PyDict_Contains(s->markers, ident);
1692 if (has_key) {
1693 if (has_key != -1)
1694 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1695 goto bail;
1696 }
1697 if (PyDict_SetItem(s->markers, ident, seq)) {
1698 goto bail;
1699 }
1700 }
1701
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001702 if (_PyAccu_Accumulate(acc, open_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001703 goto bail;
1704 if (s->indent != Py_None) {
1705 /* TODO: DOES NOT RUN */
1706 indent_level += 1;
1707 /*
1708 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1709 separator = _item_separator + newline_indent
1710 buf += newline_indent
1711 */
1712 }
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001713 for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
1714 PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001715 if (i) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001716 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001717 goto bail;
1718 }
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001719 if (encoder_listencode_obj(s, acc, obj, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001720 goto bail;
1721 }
1722 if (ident != NULL) {
1723 if (PyDict_DelItem(s->markers, ident))
1724 goto bail;
1725 Py_CLEAR(ident);
1726 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001727
1728 /* TODO: DOES NOT RUN
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001729 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001730 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001731
1732 yield '\n' + (' ' * (_indent * _current_indent_level))
1733 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001734 if (_PyAccu_Accumulate(acc, close_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001735 goto bail;
1736 Py_DECREF(s_fast);
1737 return 0;
1738
1739bail:
1740 Py_XDECREF(ident);
1741 Py_DECREF(s_fast);
1742 return -1;
1743}
1744
1745static void
1746encoder_dealloc(PyObject *self)
1747{
1748 /* Deallocate Encoder */
1749 encoder_clear(self);
1750 Py_TYPE(self)->tp_free(self);
1751}
1752
1753static int
1754encoder_traverse(PyObject *self, visitproc visit, void *arg)
1755{
1756 PyEncoderObject *s;
1757 assert(PyEncoder_Check(self));
1758 s = (PyEncoderObject *)self;
1759 Py_VISIT(s->markers);
1760 Py_VISIT(s->defaultfn);
1761 Py_VISIT(s->encoder);
1762 Py_VISIT(s->indent);
1763 Py_VISIT(s->key_separator);
1764 Py_VISIT(s->item_separator);
1765 Py_VISIT(s->sort_keys);
1766 Py_VISIT(s->skipkeys);
1767 return 0;
1768}
1769
1770static int
1771encoder_clear(PyObject *self)
1772{
1773 /* Deallocate Encoder */
1774 PyEncoderObject *s;
1775 assert(PyEncoder_Check(self));
1776 s = (PyEncoderObject *)self;
1777 Py_CLEAR(s->markers);
1778 Py_CLEAR(s->defaultfn);
1779 Py_CLEAR(s->encoder);
1780 Py_CLEAR(s->indent);
1781 Py_CLEAR(s->key_separator);
1782 Py_CLEAR(s->item_separator);
1783 Py_CLEAR(s->sort_keys);
1784 Py_CLEAR(s->skipkeys);
1785 return 0;
1786}
1787
1788PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
1789
1790static
1791PyTypeObject PyEncoderType = {
1792 PyVarObject_HEAD_INIT(NULL, 0)
1793 "_json.Encoder", /* tp_name */
1794 sizeof(PyEncoderObject), /* tp_basicsize */
1795 0, /* tp_itemsize */
1796 encoder_dealloc, /* tp_dealloc */
1797 0, /* tp_print */
1798 0, /* tp_getattr */
1799 0, /* tp_setattr */
1800 0, /* tp_compare */
1801 0, /* tp_repr */
1802 0, /* tp_as_number */
1803 0, /* tp_as_sequence */
1804 0, /* tp_as_mapping */
1805 0, /* tp_hash */
1806 encoder_call, /* tp_call */
1807 0, /* tp_str */
1808 0, /* tp_getattro */
1809 0, /* tp_setattro */
1810 0, /* tp_as_buffer */
1811 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1812 encoder_doc, /* tp_doc */
1813 encoder_traverse, /* tp_traverse */
1814 encoder_clear, /* tp_clear */
1815 0, /* tp_richcompare */
1816 0, /* tp_weaklistoffset */
1817 0, /* tp_iter */
1818 0, /* tp_iternext */
1819 0, /* tp_methods */
1820 encoder_members, /* tp_members */
1821 0, /* tp_getset */
1822 0, /* tp_base */
1823 0, /* tp_dict */
1824 0, /* tp_descr_get */
1825 0, /* tp_descr_set */
1826 0, /* tp_dictoffset */
1827 encoder_init, /* tp_init */
1828 0, /* tp_alloc */
1829 encoder_new, /* tp_new */
1830 0, /* tp_free */
1831};
1832
1833static PyMethodDef speedups_methods[] = {
1834 {"encode_basestring_ascii",
1835 (PyCFunction)py_encode_basestring_ascii,
1836 METH_O,
1837 pydoc_encode_basestring_ascii},
1838 {"scanstring",
1839 (PyCFunction)py_scanstring,
1840 METH_VARARGS,
1841 pydoc_scanstring},
Christian Heimes90540002008-05-08 14:29:10 +00001842 {NULL, NULL, 0, NULL}
1843};
1844
1845PyDoc_STRVAR(module_doc,
1846"json speedups\n");
1847
Martin v. Löwis1a214512008-06-11 05:26:20 +00001848static struct PyModuleDef jsonmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 PyModuleDef_HEAD_INIT,
1850 "_json",
1851 module_doc,
1852 -1,
1853 speedups_methods,
1854 NULL,
1855 NULL,
1856 NULL,
1857 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001858};
1859
1860PyObject*
1861PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +00001862{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001863 PyObject *m = PyModule_Create(&jsonmodule);
1864 if (!m)
1865 return NULL;
1866 PyScannerType.tp_new = PyType_GenericNew;
1867 if (PyType_Ready(&PyScannerType) < 0)
1868 goto fail;
1869 PyEncoderType.tp_new = PyType_GenericNew;
1870 if (PyType_Ready(&PyEncoderType) < 0)
1871 goto fail;
1872 Py_INCREF((PyObject*)&PyScannerType);
1873 if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1874 Py_DECREF((PyObject*)&PyScannerType);
1875 goto fail;
1876 }
1877 Py_INCREF((PyObject*)&PyEncoderType);
1878 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1879 Py_DECREF((PyObject*)&PyEncoderType);
1880 goto fail;
1881 }
1882 return m;
1883 fail:
1884 Py_DECREF(m);
1885 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +00001886}