blob: 301bc87d27a961ab9be51d8b48af1c6bac2b1e63 [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 */
Victor Stinner76df43d2012-10-30 01:42:39 +0100412 if (Py_UNICODE_IS_HIGH_SURROGATE(c)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200413 Py_UCS4 c2 = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000414 if (end + 6 >= len) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000415 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
416 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000417 }
Victor Stinnerd9c06312011-10-11 21:56:19 +0200418 if (PyUnicode_READ(kind, buf, next++) != '\\' ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200419 PyUnicode_READ(kind, buf, next++) != 'u') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000420 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
421 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000422 }
423 end += 6;
424 /* Decode 4 hex digits */
425 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200426 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Antoine Pitrou5b0e9e82010-10-09 15:24:28 +0000427 c2 <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000428 switch (digit) {
429 case '0': case '1': case '2': case '3': case '4':
430 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000431 c2 |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000432 case 'a': case 'b': case 'c': case 'd': case 'e':
433 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000434 c2 |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000435 case 'A': case 'B': case 'C': case 'D': case 'E':
436 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000437 c2 |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000438 default:
439 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
440 goto bail;
441 }
442 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100443 if (!Py_UNICODE_IS_LOW_SURROGATE(c2)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000444 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
445 goto bail;
446 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100447 c = Py_UNICODE_JOIN_SURROGATES(c, c2);
Christian Heimes90540002008-05-08 14:29:10 +0000448 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100449 else if (Py_UNICODE_IS_LOW_SURROGATE(c)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000450 raise_errmsg("Unpaired low surrogate", pystr, end - 5);
451 goto bail;
452 }
Christian Heimes90540002008-05-08 14:29:10 +0000453 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000454 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200455 chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1);
Christian Heimes90540002008-05-08 14:29:10 +0000456 if (chunk == NULL) {
457 goto bail;
458 }
Christian Heimes90540002008-05-08 14:29:10 +0000459 }
460
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000461 if (chunks == NULL) {
462 if (chunk != NULL)
463 rval = chunk;
464 else
465 rval = PyUnicode_FromStringAndSize("", 0);
Christian Heimes90540002008-05-08 14:29:10 +0000466 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000467 else {
468 APPEND_OLD_CHUNK
469 rval = join_list_unicode(chunks);
470 if (rval == NULL) {
471 goto bail;
472 }
473 Py_CLEAR(chunks);
474 }
475
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000476 *next_end_ptr = end;
477 return rval;
Christian Heimes90540002008-05-08 14:29:10 +0000478bail:
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000479 *next_end_ptr = -1;
Christian Heimes90540002008-05-08 14:29:10 +0000480 Py_XDECREF(chunks);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000481 Py_XDECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000482 return NULL;
483}
484
485PyDoc_STRVAR(pydoc_scanstring,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000486 "scanstring(string, end, strict=True) -> (string, end)\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000487 "\n"
488 "Scan the string s for a JSON string. End is the index of the\n"
489 "character in s after the quote that started the JSON string.\n"
490 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
491 "on attempt to decode an invalid string. If strict is False then literal\n"
492 "control characters are allowed in the string.\n"
493 "\n"
494 "Returns a tuple of the decoded string and the index of the character in s\n"
495 "after the end quote."
496);
Christian Heimes90540002008-05-08 14:29:10 +0000497
498static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000499py_scanstring(PyObject* self UNUSED, PyObject *args)
Christian Heimes90540002008-05-08 14:29:10 +0000500{
501 PyObject *pystr;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000502 PyObject *rval;
Christian Heimes90540002008-05-08 14:29:10 +0000503 Py_ssize_t end;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000504 Py_ssize_t next_end = -1;
505 int strict = 1;
Antoine Pitroucbb02842012-12-01 19:34:16 +0100506 if (!PyArg_ParseTuple(args, "On|i:scanstring", &pystr, &end, &strict)) {
Christian Heimes90540002008-05-08 14:29:10 +0000507 return NULL;
508 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000509 if (PyUnicode_Check(pystr)) {
510 rval = scanstring_unicode(pystr, end, strict, &next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000511 }
512 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 PyErr_Format(PyExc_TypeError,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000514 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000515 Py_TYPE(pystr)->tp_name);
516 return NULL;
517 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000518 return _build_rval_index_tuple(rval, next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000519}
520
521PyDoc_STRVAR(pydoc_encode_basestring_ascii,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000522 "encode_basestring_ascii(string) -> string\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000523 "\n"
524 "Return an ASCII-only JSON representation of a Python string"
525);
Christian Heimes90540002008-05-08 14:29:10 +0000526
527static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000528py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
Christian Heimes90540002008-05-08 14:29:10 +0000529{
530 PyObject *rval;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000531 /* Return an ASCII-only JSON representation of a Python string */
Christian Heimes90540002008-05-08 14:29:10 +0000532 /* METH_O */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000533 if (PyUnicode_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000534 rval = ascii_escape_unicode(pystr);
535 }
536 else {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000537 PyErr_Format(PyExc_TypeError,
538 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000539 Py_TYPE(pystr)->tp_name);
540 return NULL;
541 }
Christian Heimes90540002008-05-08 14:29:10 +0000542 return rval;
543}
544
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000545static void
546scanner_dealloc(PyObject *self)
547{
548 /* Deallocate scanner object */
549 scanner_clear(self);
550 Py_TYPE(self)->tp_free(self);
551}
552
553static int
554scanner_traverse(PyObject *self, visitproc visit, void *arg)
555{
556 PyScannerObject *s;
557 assert(PyScanner_Check(self));
558 s = (PyScannerObject *)self;
559 Py_VISIT(s->strict);
560 Py_VISIT(s->object_hook);
561 Py_VISIT(s->object_pairs_hook);
562 Py_VISIT(s->parse_float);
563 Py_VISIT(s->parse_int);
564 Py_VISIT(s->parse_constant);
565 return 0;
566}
567
568static int
569scanner_clear(PyObject *self)
570{
571 PyScannerObject *s;
572 assert(PyScanner_Check(self));
573 s = (PyScannerObject *)self;
574 Py_CLEAR(s->strict);
575 Py_CLEAR(s->object_hook);
576 Py_CLEAR(s->object_pairs_hook);
577 Py_CLEAR(s->parse_float);
578 Py_CLEAR(s->parse_int);
579 Py_CLEAR(s->parse_constant);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000580 Py_CLEAR(s->memo);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000581 return 0;
582}
583
584static PyObject *
585_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
586 /* Read a JSON object from PyUnicode pystr.
587 idx is the index of the first character after the opening curly brace.
588 *next_idx_ptr is a return-by-reference index to the first character after
589 the closing curly brace.
590
591 Returns a new PyObject (usually a dict, but object_hook can change that)
592 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200593 void *str;
594 int kind;
595 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000596 PyObject *val = NULL;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000597 PyObject *rval = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000598 PyObject *key = NULL;
599 int strict = PyObject_IsTrue(s->strict);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000600 int has_pairs_hook = (s->object_pairs_hook != Py_None);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000601 Py_ssize_t next_idx;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000602
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200603 if (PyUnicode_READY(pystr) == -1)
604 return NULL;
605
606 str = PyUnicode_DATA(pystr);
607 kind = PyUnicode_KIND(pystr);
608 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
609
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000610 if (has_pairs_hook)
611 rval = PyList_New(0);
612 else
613 rval = PyDict_New();
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000614 if (rval == NULL)
615 return NULL;
616
617 /* skip whitespace after { */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200618 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000619
620 /* only loop if the object is non-empty */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200621 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') {
622 while (1) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000623 PyObject *memokey;
624
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000625 /* read key */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200626 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '"') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200627 raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000628 goto bail;
629 }
630 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
631 if (key == NULL)
632 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000633 memokey = PyDict_GetItem(s->memo, key);
634 if (memokey != NULL) {
635 Py_INCREF(memokey);
636 Py_DECREF(key);
637 key = memokey;
638 }
639 else {
640 if (PyDict_SetItem(s->memo, key, key) < 0)
641 goto bail;
642 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000643 idx = next_idx;
644
645 /* skip whitespace between key and : delimiter, read :, skip whitespace */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200646 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
647 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200648 raise_errmsg("Expecting ':' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000649 goto bail;
650 }
651 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200652 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000653
654 /* read any JSON term */
655 val = scan_once_unicode(s, pystr, idx, &next_idx);
656 if (val == NULL)
657 goto bail;
658
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000659 if (has_pairs_hook) {
660 PyObject *item = PyTuple_Pack(2, key, val);
661 if (item == NULL)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000662 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000663 Py_CLEAR(key);
664 Py_CLEAR(val);
665 if (PyList_Append(rval, item) == -1) {
666 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000667 goto bail;
668 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000669 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000670 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000671 else {
672 if (PyDict_SetItem(rval, key, val) < 0)
673 goto bail;
674 Py_CLEAR(key);
675 Py_CLEAR(val);
676 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000677 idx = next_idx;
678
679 /* skip whitespace before } or , */
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 /* bail if the object is closed or we didn't get the , delimiter */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200683 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) == '}')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000684 break;
Ezio Melotti37623ab2013-01-03 08:44:15 +0200685 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200686 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000687 goto bail;
688 }
689 idx++;
690
691 /* skip whitespace after , delimiter */
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 }
695
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000696 *next_idx_ptr = idx + 1;
697
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000698 if (has_pairs_hook) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000699 val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000700 Py_DECREF(rval);
701 return val;
702 }
703
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000704 /* if object_hook is not None: rval = object_hook(rval) */
705 if (s->object_hook != Py_None) {
706 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000707 Py_DECREF(rval);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000708 return val;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000709 }
710 return rval;
711bail:
712 Py_XDECREF(key);
713 Py_XDECREF(val);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000714 Py_XDECREF(rval);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000715 return NULL;
716}
717
718static PyObject *
719_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
720 /* Read a JSON array from PyString pystr.
721 idx is the index of the first character after the opening brace.
722 *next_idx_ptr is a return-by-reference index to the first character after
723 the closing brace.
724
725 Returns a new PyList
726 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200727 void *str;
728 int kind;
729 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000730 PyObject *val = NULL;
731 PyObject *rval = PyList_New(0);
732 Py_ssize_t next_idx;
733 if (rval == NULL)
734 return NULL;
735
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200736 if (PyUnicode_READY(pystr) == -1)
737 return NULL;
738
739 str = PyUnicode_DATA(pystr);
740 kind = PyUnicode_KIND(pystr);
741 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
742
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000743 /* skip whitespace after [ */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200744 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000745
746 /* only loop if the array is non-empty */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200747 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
748 while (1) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000749
750 /* read any JSON term */
751 val = scan_once_unicode(s, pystr, idx, &next_idx);
752 if (val == NULL)
753 goto bail;
754
755 if (PyList_Append(rval, val) == -1)
756 goto bail;
757
758 Py_CLEAR(val);
759 idx = next_idx;
760
761 /* skip whitespace between term and , */
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 /* bail if the array is closed or we didn't get the , delimiter */
Ezio Melotti37623ab2013-01-03 08:44:15 +0200765 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) == ']')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000766 break;
Ezio Melotti37623ab2013-01-03 08:44:15 +0200767 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ',') {
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200768 raise_errmsg("Expecting ',' delimiter", pystr, idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000769 goto bail;
770 }
771 idx++;
772
773 /* skip whitespace after , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200774 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000775 }
776 }
777
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200778 /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */
779 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
Ezio Melotti37623ab2013-01-03 08:44:15 +0200780 raise_errmsg("Expecting value", pystr, end_idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000781 goto bail;
782 }
783 *next_idx_ptr = idx + 1;
784 return rval;
785bail:
786 Py_XDECREF(val);
787 Py_DECREF(rval);
788 return NULL;
789}
790
791static PyObject *
792_parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
793 /* Read a JSON constant from PyString pystr.
794 constant is the constant string that was found
795 ("NaN", "Infinity", "-Infinity").
796 idx is the index of the first character of the constant
797 *next_idx_ptr is a return-by-reference index to the first character after
798 the constant.
799
800 Returns the result of parse_constant
801 */
802 PyObject *cstr;
803 PyObject *rval;
804 /* constant is "NaN", "Infinity", or "-Infinity" */
805 cstr = PyUnicode_InternFromString(constant);
806 if (cstr == NULL)
807 return NULL;
808
809 /* rval = parse_constant(constant) */
810 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
Victor Stinnerc4f281e2011-10-11 22:11:42 +0200811 idx += PyUnicode_GET_LENGTH(cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000812 Py_DECREF(cstr);
813 *next_idx_ptr = idx;
814 return rval;
815}
816
817static PyObject *
818_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
819 /* Read a JSON number from PyUnicode pystr.
820 idx is the index of the first character of the number
821 *next_idx_ptr is a return-by-reference index to the first character after
822 the number.
823
824 Returns a new PyObject representation of that number:
825 PyInt, PyLong, or PyFloat.
826 May return other types if parse_int or parse_float are set
827 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200828 void *str;
829 int kind;
830 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000831 Py_ssize_t idx = start;
832 int is_float = 0;
833 PyObject *rval;
Antoine Pitrouf6454512011-04-25 19:16:06 +0200834 PyObject *numstr = NULL;
835 PyObject *custom_func;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000836
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200837 if (PyUnicode_READY(pystr) == -1)
838 return NULL;
839
840 str = PyUnicode_DATA(pystr);
841 kind = PyUnicode_KIND(pystr);
842 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
843
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000844 /* 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 +0200845 if (PyUnicode_READ(kind, str, idx) == '-') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000846 idx++;
847 if (idx > end_idx) {
Ezio Melotti37623ab2013-01-03 08:44:15 +0200848 raise_stop_iteration(start);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000849 return NULL;
850 }
851 }
852
853 /* 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 +0200854 if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000855 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200856 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000857 }
858 /* if it starts with 0 we only expect one integer digit */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200859 else if (PyUnicode_READ(kind, str, idx) == '0') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000860 idx++;
861 }
862 /* no integer digits, error */
863 else {
Ezio Melotti37623ab2013-01-03 08:44:15 +0200864 raise_stop_iteration(start);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000865 return NULL;
866 }
867
868 /* if the next char is '.' followed by a digit then read all float digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200869 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 +0000870 is_float = 1;
871 idx += 2;
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
875 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200876 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000877 Py_ssize_t e_start = idx;
878 idx++;
879
880 /* read an exponent sign if present */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200881 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000882
883 /* read all digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200884 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000885
886 /* if we got a digit, then parse as float. if not, backtrack */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200887 if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000888 is_float = 1;
889 }
890 else {
891 idx = e_start;
892 }
893 }
894
Antoine Pitrouf6454512011-04-25 19:16:06 +0200895 if (is_float && s->parse_float != (PyObject *)&PyFloat_Type)
896 custom_func = s->parse_float;
897 else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type)
898 custom_func = s->parse_int;
899 else
900 custom_func = NULL;
901
902 if (custom_func) {
903 /* copy the section we determined to be a number */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200904 numstr = PyUnicode_FromKindAndData(kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200905 (char*)str + kind * start,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200906 idx - start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200907 if (numstr == NULL)
908 return NULL;
909 rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000910 }
911 else {
Antoine Pitrouf6454512011-04-25 19:16:06 +0200912 Py_ssize_t i, n;
913 char *buf;
914 /* Straight conversion to ASCII, to avoid costly conversion of
915 decimal unicode digits (which cannot appear here) */
916 n = idx - start;
917 numstr = PyBytes_FromStringAndSize(NULL, n);
918 if (numstr == NULL)
919 return NULL;
920 buf = PyBytes_AS_STRING(numstr);
921 for (i = 0; i < n; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200922 buf[i] = (char) PyUnicode_READ(kind, str, i + start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200923 }
924 if (is_float)
925 rval = PyFloat_FromString(numstr);
926 else
927 rval = PyLong_FromString(buf, NULL, 10);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000928 }
929 Py_DECREF(numstr);
930 *next_idx_ptr = idx;
931 return rval;
932}
933
934static PyObject *
935scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
936{
937 /* Read one JSON term (of any kind) from PyUnicode pystr.
938 idx is the index of the first character of the term
939 *next_idx_ptr is a return-by-reference index to the first character after
940 the number.
941
942 Returns a new PyObject representation of the term.
943 */
Ezio Melotti362b9512011-05-07 17:58:09 +0300944 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200945 void *str;
946 int kind;
947 Py_ssize_t length;
948
949 if (PyUnicode_READY(pystr) == -1)
950 return NULL;
951
952 str = PyUnicode_DATA(pystr);
953 kind = PyUnicode_KIND(pystr);
954 length = PyUnicode_GET_LENGTH(pystr);
955
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000956 if (idx >= length) {
Ezio Melotti37623ab2013-01-03 08:44:15 +0200957 raise_stop_iteration(idx);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000958 return NULL;
959 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200960
961 switch (PyUnicode_READ(kind, str, idx)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000962 case '"':
963 /* string */
964 return scanstring_unicode(pystr, idx + 1,
965 PyObject_IsTrue(s->strict),
966 next_idx_ptr);
967 case '{':
968 /* object */
Ezio Melotti362b9512011-05-07 17:58:09 +0300969 if (Py_EnterRecursiveCall(" while decoding a JSON object "
970 "from a unicode string"))
971 return NULL;
972 res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
973 Py_LeaveRecursiveCall();
974 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000975 case '[':
976 /* array */
Ezio Melotti362b9512011-05-07 17:58:09 +0300977 if (Py_EnterRecursiveCall(" while decoding a JSON array "
978 "from a unicode string"))
979 return NULL;
980 res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
981 Py_LeaveRecursiveCall();
982 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000983 case 'n':
984 /* null */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200985 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 +0000986 Py_INCREF(Py_None);
987 *next_idx_ptr = idx + 4;
988 return Py_None;
989 }
990 break;
991 case 't':
992 /* true */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200993 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 +0000994 Py_INCREF(Py_True);
995 *next_idx_ptr = idx + 4;
996 return Py_True;
997 }
998 break;
999 case 'f':
1000 /* false */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001001 if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
1002 PyUnicode_READ(kind, str, idx + 2) == 'l' &&
1003 PyUnicode_READ(kind, str, idx + 3) == 's' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001004 PyUnicode_READ(kind, str, idx + 4) == 'e') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001005 Py_INCREF(Py_False);
1006 *next_idx_ptr = idx + 5;
1007 return Py_False;
1008 }
1009 break;
1010 case 'N':
1011 /* NaN */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001012 if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001013 PyUnicode_READ(kind, str, idx + 2) == 'N') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001014 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1015 }
1016 break;
1017 case 'I':
1018 /* Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001019 if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
1020 PyUnicode_READ(kind, str, idx + 2) == 'f' &&
1021 PyUnicode_READ(kind, str, idx + 3) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001022 PyUnicode_READ(kind, str, idx + 4) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001023 PyUnicode_READ(kind, str, idx + 5) == 'i' &&
1024 PyUnicode_READ(kind, str, idx + 6) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001025 PyUnicode_READ(kind, str, idx + 7) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001026 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1027 }
1028 break;
1029 case '-':
1030 /* -Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001031 if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001032 PyUnicode_READ(kind, str, idx + 2) == 'n' &&
1033 PyUnicode_READ(kind, str, idx + 3) == 'f' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001034 PyUnicode_READ(kind, str, idx + 4) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001035 PyUnicode_READ(kind, str, idx + 5) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001036 PyUnicode_READ(kind, str, idx + 6) == 'i' &&
1037 PyUnicode_READ(kind, str, idx + 7) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001038 PyUnicode_READ(kind, str, idx + 8) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001039 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1040 }
1041 break;
1042 }
1043 /* Didn't find a string, object, array, or named constant. Look for a number. */
1044 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
1045}
1046
1047static PyObject *
1048scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
1049{
1050 /* Python callable interface to scan_once_{str,unicode} */
1051 PyObject *pystr;
1052 PyObject *rval;
1053 Py_ssize_t idx;
1054 Py_ssize_t next_idx = -1;
1055 static char *kwlist[] = {"string", "idx", NULL};
1056 PyScannerObject *s;
1057 assert(PyScanner_Check(self));
1058 s = (PyScannerObject *)self;
Antoine Pitroucbb02842012-12-01 19:34:16 +01001059 if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:scan_once", kwlist, &pystr, &idx))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001060 return NULL;
1061
1062 if (PyUnicode_Check(pystr)) {
1063 rval = scan_once_unicode(s, pystr, idx, &next_idx);
1064 }
1065 else {
1066 PyErr_Format(PyExc_TypeError,
1067 "first argument must be a string, not %.80s",
1068 Py_TYPE(pystr)->tp_name);
1069 return NULL;
1070 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001071 PyDict_Clear(s->memo);
1072 if (rval == NULL)
1073 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001074 return _build_rval_index_tuple(rval, next_idx);
1075}
1076
1077static PyObject *
1078scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1079{
1080 PyScannerObject *s;
1081 s = (PyScannerObject *)type->tp_alloc(type, 0);
1082 if (s != NULL) {
1083 s->strict = NULL;
1084 s->object_hook = NULL;
1085 s->object_pairs_hook = NULL;
1086 s->parse_float = NULL;
1087 s->parse_int = NULL;
1088 s->parse_constant = NULL;
1089 }
1090 return (PyObject *)s;
1091}
1092
1093static int
1094scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1095{
1096 /* Initialize Scanner object */
1097 PyObject *ctx;
1098 static char *kwlist[] = {"context", NULL};
1099 PyScannerObject *s;
1100
1101 assert(PyScanner_Check(self));
1102 s = (PyScannerObject *)self;
1103
1104 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1105 return -1;
1106
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001107 if (s->memo == NULL) {
1108 s->memo = PyDict_New();
1109 if (s->memo == NULL)
1110 goto bail;
1111 }
1112
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001113 /* All of these will fail "gracefully" so we don't need to verify them */
1114 s->strict = PyObject_GetAttrString(ctx, "strict");
1115 if (s->strict == NULL)
1116 goto bail;
1117 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1118 if (s->object_hook == NULL)
1119 goto bail;
1120 s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1121 if (s->object_pairs_hook == NULL)
1122 goto bail;
1123 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1124 if (s->parse_float == NULL)
1125 goto bail;
1126 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1127 if (s->parse_int == NULL)
1128 goto bail;
1129 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1130 if (s->parse_constant == NULL)
1131 goto bail;
1132
1133 return 0;
1134
1135bail:
1136 Py_CLEAR(s->strict);
1137 Py_CLEAR(s->object_hook);
1138 Py_CLEAR(s->object_pairs_hook);
1139 Py_CLEAR(s->parse_float);
1140 Py_CLEAR(s->parse_int);
1141 Py_CLEAR(s->parse_constant);
1142 return -1;
1143}
1144
1145PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1146
1147static
1148PyTypeObject PyScannerType = {
1149 PyVarObject_HEAD_INIT(NULL, 0)
1150 "_json.Scanner", /* tp_name */
1151 sizeof(PyScannerObject), /* tp_basicsize */
1152 0, /* tp_itemsize */
1153 scanner_dealloc, /* tp_dealloc */
1154 0, /* tp_print */
1155 0, /* tp_getattr */
1156 0, /* tp_setattr */
1157 0, /* tp_compare */
1158 0, /* tp_repr */
1159 0, /* tp_as_number */
1160 0, /* tp_as_sequence */
1161 0, /* tp_as_mapping */
1162 0, /* tp_hash */
1163 scanner_call, /* tp_call */
1164 0, /* tp_str */
1165 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1166 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1167 0, /* tp_as_buffer */
1168 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1169 scanner_doc, /* tp_doc */
1170 scanner_traverse, /* tp_traverse */
1171 scanner_clear, /* tp_clear */
1172 0, /* tp_richcompare */
1173 0, /* tp_weaklistoffset */
1174 0, /* tp_iter */
1175 0, /* tp_iternext */
1176 0, /* tp_methods */
1177 scanner_members, /* tp_members */
1178 0, /* tp_getset */
1179 0, /* tp_base */
1180 0, /* tp_dict */
1181 0, /* tp_descr_get */
1182 0, /* tp_descr_set */
1183 0, /* tp_dictoffset */
1184 scanner_init, /* tp_init */
1185 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1186 scanner_new, /* tp_new */
1187 0,/* PyObject_GC_Del, */ /* tp_free */
1188};
1189
1190static PyObject *
1191encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1192{
1193 PyEncoderObject *s;
1194 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1195 if (s != NULL) {
1196 s->markers = NULL;
1197 s->defaultfn = NULL;
1198 s->encoder = NULL;
1199 s->indent = NULL;
1200 s->key_separator = NULL;
1201 s->item_separator = NULL;
1202 s->sort_keys = NULL;
1203 s->skipkeys = NULL;
1204 }
1205 return (PyObject *)s;
1206}
1207
1208static int
1209encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1210{
1211 /* initialize Encoder object */
1212 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1213
1214 PyEncoderObject *s;
Antoine Pitrou781eba72009-12-08 15:57:31 +00001215 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1216 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001217
1218 assert(PyEncoder_Check(self));
1219 s = (PyEncoderObject *)self;
1220
1221 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
Antoine Pitrou781eba72009-12-08 15:57:31 +00001222 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1223 &sort_keys, &skipkeys, &allow_nan))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001224 return -1;
1225
Antoine Pitrou781eba72009-12-08 15:57:31 +00001226 s->markers = markers;
1227 s->defaultfn = defaultfn;
1228 s->encoder = encoder;
1229 s->indent = indent;
1230 s->key_separator = key_separator;
1231 s->item_separator = item_separator;
1232 s->sort_keys = sort_keys;
1233 s->skipkeys = skipkeys;
1234 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1235 s->allow_nan = PyObject_IsTrue(allow_nan);
1236
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001237 Py_INCREF(s->markers);
1238 Py_INCREF(s->defaultfn);
1239 Py_INCREF(s->encoder);
1240 Py_INCREF(s->indent);
1241 Py_INCREF(s->key_separator);
1242 Py_INCREF(s->item_separator);
1243 Py_INCREF(s->sort_keys);
1244 Py_INCREF(s->skipkeys);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001245 return 0;
1246}
1247
1248static PyObject *
1249encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1250{
1251 /* Python callable interface to encode_listencode_obj */
1252 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1253 PyObject *obj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001254 Py_ssize_t indent_level;
1255 PyEncoderObject *s;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001256 _PyAccu acc;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001257
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001258 assert(PyEncoder_Check(self));
1259 s = (PyEncoderObject *)self;
Antoine Pitroucbb02842012-12-01 19:34:16 +01001260 if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:_iterencode", kwlist,
1261 &obj, &indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001262 return NULL;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001263 if (_PyAccu_Init(&acc))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001264 return NULL;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001265 if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001266 _PyAccu_Destroy(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001267 return NULL;
1268 }
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001269 return _PyAccu_FinishAsList(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001270}
1271
1272static PyObject *
1273_encoded_const(PyObject *obj)
1274{
1275 /* Return the JSON string representation of None, True, False */
1276 if (obj == Py_None) {
1277 static PyObject *s_null = NULL;
1278 if (s_null == NULL) {
1279 s_null = PyUnicode_InternFromString("null");
1280 }
1281 Py_INCREF(s_null);
1282 return s_null;
1283 }
1284 else if (obj == Py_True) {
1285 static PyObject *s_true = NULL;
1286 if (s_true == NULL) {
1287 s_true = PyUnicode_InternFromString("true");
1288 }
1289 Py_INCREF(s_true);
1290 return s_true;
1291 }
1292 else if (obj == Py_False) {
1293 static PyObject *s_false = NULL;
1294 if (s_false == NULL) {
1295 s_false = PyUnicode_InternFromString("false");
1296 }
1297 Py_INCREF(s_false);
1298 return s_false;
1299 }
1300 else {
1301 PyErr_SetString(PyExc_ValueError, "not a const");
1302 return NULL;
1303 }
1304}
1305
1306static PyObject *
Ethan Furmana4998a72013-08-10 13:01:45 -07001307encoder_encode_long(PyEncoderObject* s UNUSED, PyObject *obj)
1308{
1309 /* Return the JSON representation of a PyLong and PyLong subclasses.
1310 Calls int() on PyLong subclasses in case the str() was changed.
1311 Added specifically to deal with IntEnum. See Issue18264. */
1312 PyObject *encoded, *longobj;
1313 if (PyLong_CheckExact(obj)) {
1314 encoded = PyObject_Str(obj);
1315 }
1316 else {
1317 longobj = PyNumber_Long(obj);
1318 if (longobj == NULL) {
1319 PyErr_SetString(
1320 PyExc_ValueError,
1321 "Unable to coerce int subclass to int"
1322 );
1323 return NULL;
1324 }
1325 encoded = PyObject_Str(longobj);
1326 Py_DECREF(longobj);
1327 }
1328 return encoded;
1329}
1330
1331
1332static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001333encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1334{
Ethan Furmana4998a72013-08-10 13:01:45 -07001335 /* Return the JSON representation of a PyFloat.
1336 Modified to call float() on float subclasses in case the subclass
1337 changes the repr. See Issue18264. */
1338 PyObject *encoded, *floatobj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001339 double i = PyFloat_AS_DOUBLE(obj);
1340 if (!Py_IS_FINITE(i)) {
1341 if (!s->allow_nan) {
Ethan Furmana4998a72013-08-10 13:01:45 -07001342 PyErr_SetString(
1343 PyExc_ValueError,
1344 "Out of range float values are not JSON compliant"
1345 );
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001346 return NULL;
1347 }
1348 if (i > 0) {
1349 return PyUnicode_FromString("Infinity");
1350 }
1351 else if (i < 0) {
1352 return PyUnicode_FromString("-Infinity");
1353 }
1354 else {
1355 return PyUnicode_FromString("NaN");
1356 }
1357 }
Ethan Furmana4998a72013-08-10 13:01:45 -07001358 /* coerce float subclasses to float (primarily for Enum) */
1359 if (PyFloat_CheckExact(obj)) {
1360 /* Use a better float format here? */
1361 encoded = PyObject_Repr(obj);
1362 }
1363 else {
1364 floatobj = PyNumber_Float(obj);
1365 if (floatobj == NULL) {
1366 PyErr_SetString(
1367 PyExc_ValueError,
1368 "Unable to coerce float subclass to float"
1369 );
1370 return NULL;
1371 }
1372 encoded = PyObject_Repr(floatobj);
1373 Py_DECREF(floatobj);
1374 }
1375 return encoded;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001376}
1377
1378static PyObject *
1379encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1380{
1381 /* Return the JSON representation of a string */
1382 if (s->fast_encode)
1383 return py_encode_basestring_ascii(NULL, obj);
1384 else
1385 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1386}
1387
1388static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001389_steal_accumulate(_PyAccu *acc, PyObject *stolen)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001390{
1391 /* Append stolen and then decrement its reference count */
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001392 int rval = _PyAccu_Accumulate(acc, stolen);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001393 Py_DECREF(stolen);
1394 return rval;
1395}
1396
1397static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001398encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001399 PyObject *obj, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001400{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001401 /* Encode Python object obj to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001402 PyObject *newobj;
1403 int rv;
1404
1405 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1406 PyObject *cstr = _encoded_const(obj);
1407 if (cstr == NULL)
1408 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001409 return _steal_accumulate(acc, cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001410 }
1411 else if (PyUnicode_Check(obj))
1412 {
1413 PyObject *encoded = encoder_encode_string(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 (PyLong_Check(obj)) {
Ethan Furmana4998a72013-08-10 13:01:45 -07001419 PyObject *encoded = encoder_encode_long(s, obj);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001420 if (encoded == NULL)
1421 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001422 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001423 }
1424 else if (PyFloat_Check(obj)) {
1425 PyObject *encoded = encoder_encode_float(s, obj);
1426 if (encoded == NULL)
1427 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001428 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001429 }
1430 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001431 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1432 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001433 rv = encoder_listencode_list(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001434 Py_LeaveRecursiveCall();
1435 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001436 }
1437 else if (PyDict_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001438 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1439 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001440 rv = encoder_listencode_dict(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001441 Py_LeaveRecursiveCall();
1442 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001443 }
1444 else {
1445 PyObject *ident = NULL;
1446 if (s->markers != Py_None) {
1447 int has_key;
1448 ident = PyLong_FromVoidPtr(obj);
1449 if (ident == NULL)
1450 return -1;
1451 has_key = PyDict_Contains(s->markers, ident);
1452 if (has_key) {
1453 if (has_key != -1)
1454 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1455 Py_DECREF(ident);
1456 return -1;
1457 }
1458 if (PyDict_SetItem(s->markers, ident, obj)) {
1459 Py_DECREF(ident);
1460 return -1;
1461 }
1462 }
1463 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1464 if (newobj == NULL) {
1465 Py_XDECREF(ident);
1466 return -1;
1467 }
Ezio Melotti13672652011-05-11 01:02:56 +03001468
1469 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1470 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001471 rv = encoder_listencode_obj(s, acc, newobj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001472 Py_LeaveRecursiveCall();
1473
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001474 Py_DECREF(newobj);
1475 if (rv) {
1476 Py_XDECREF(ident);
1477 return -1;
1478 }
1479 if (ident != NULL) {
1480 if (PyDict_DelItem(s->markers, ident)) {
1481 Py_XDECREF(ident);
1482 return -1;
1483 }
1484 Py_XDECREF(ident);
1485 }
1486 return rv;
1487 }
1488}
1489
1490static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001491encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001492 PyObject *dct, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001493{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001494 /* Encode Python dict dct a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001495 static PyObject *open_dict = NULL;
1496 static PyObject *close_dict = NULL;
1497 static PyObject *empty_dict = NULL;
1498 PyObject *kstr = NULL;
1499 PyObject *ident = NULL;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001500 PyObject *it = NULL;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001501 PyObject *items;
1502 PyObject *item = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001503 int skipkeys;
1504 Py_ssize_t idx;
1505
1506 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
1507 open_dict = PyUnicode_InternFromString("{");
1508 close_dict = PyUnicode_InternFromString("}");
1509 empty_dict = PyUnicode_InternFromString("{}");
1510 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
1511 return -1;
1512 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001513 if (Py_SIZE(dct) == 0)
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001514 return _PyAccu_Accumulate(acc, empty_dict);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001515
1516 if (s->markers != Py_None) {
1517 int has_key;
1518 ident = PyLong_FromVoidPtr(dct);
1519 if (ident == NULL)
1520 goto bail;
1521 has_key = PyDict_Contains(s->markers, ident);
1522 if (has_key) {
1523 if (has_key != -1)
1524 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1525 goto bail;
1526 }
1527 if (PyDict_SetItem(s->markers, ident, dct)) {
1528 goto bail;
1529 }
1530 }
1531
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001532 if (_PyAccu_Accumulate(acc, open_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001533 goto bail;
1534
1535 if (s->indent != Py_None) {
1536 /* TODO: DOES NOT RUN */
1537 indent_level += 1;
1538 /*
1539 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1540 separator = _item_separator + newline_indent
1541 buf += newline_indent
1542 */
1543 }
1544
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001545 if (PyObject_IsTrue(s->sort_keys)) {
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001546 /* First sort the keys then replace them with (key, value) tuples. */
1547 Py_ssize_t i, nitems;
1548 items = PyMapping_Keys(dct);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 if (items == NULL)
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001550 goto bail;
1551 if (!PyList_Check(items)) {
1552 PyErr_SetString(PyExc_ValueError, "keys must return list");
1553 goto bail;
1554 }
1555 if (PyList_Sort(items) < 0)
1556 goto bail;
1557 nitems = PyList_GET_SIZE(items);
1558 for (i = 0; i < nitems; i++) {
1559 PyObject *key, *value;
1560 key = PyList_GET_ITEM(items, i);
1561 value = PyDict_GetItem(dct, key);
1562 item = PyTuple_Pack(2, key, value);
1563 if (item == NULL)
1564 goto bail;
1565 PyList_SET_ITEM(items, i, item);
1566 Py_DECREF(key);
1567 }
1568 }
1569 else {
1570 items = PyMapping_Items(dct);
1571 }
1572 if (items == NULL)
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001573 goto bail;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001574 it = PyObject_GetIter(items);
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001575 Py_DECREF(items);
1576 if (it == NULL)
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001577 goto bail;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001578 skipkeys = PyObject_IsTrue(s->skipkeys);
1579 idx = 0;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001580 while ((item = PyIter_Next(it)) != NULL) {
1581 PyObject *encoded, *key, *value;
1582 if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
1583 PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
1584 goto bail;
1585 }
1586 key = PyTuple_GET_ITEM(item, 0);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001587 if (PyUnicode_Check(key)) {
1588 Py_INCREF(key);
1589 kstr = key;
1590 }
1591 else if (PyFloat_Check(key)) {
1592 kstr = encoder_encode_float(s, key);
1593 if (kstr == NULL)
1594 goto bail;
1595 }
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001596 else if (key == Py_True || key == Py_False || key == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 /* This must come before the PyLong_Check because
1598 True and False are also 1 and 0.*/
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001599 kstr = _encoded_const(key);
1600 if (kstr == NULL)
1601 goto bail;
1602 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001603 else if (PyLong_Check(key)) {
Ethan Furmana4998a72013-08-10 13:01:45 -07001604 kstr = encoder_encode_long(s, key);
1605 if (kstr == NULL) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001606 goto bail;
Ethan Furmana4998a72013-08-10 13:01:45 -07001607 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001608 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001609 else if (skipkeys) {
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001610 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001611 continue;
1612 }
1613 else {
1614 /* TODO: include repr of key */
Doug Hellmann1c524752010-07-21 12:29:04 +00001615 PyErr_SetString(PyExc_TypeError, "keys must be a string");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001616 goto bail;
1617 }
1618
1619 if (idx) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001620 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001621 goto bail;
1622 }
1623
1624 encoded = encoder_encode_string(s, kstr);
1625 Py_CLEAR(kstr);
1626 if (encoded == NULL)
1627 goto bail;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001628 if (_PyAccu_Accumulate(acc, encoded)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001629 Py_DECREF(encoded);
1630 goto bail;
1631 }
1632 Py_DECREF(encoded);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001633 if (_PyAccu_Accumulate(acc, s->key_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001634 goto bail;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001635
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001636 value = PyTuple_GET_ITEM(item, 1);
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001637 if (encoder_listencode_obj(s, acc, value, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001638 goto bail;
1639 idx += 1;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001640 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001641 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001642 if (PyErr_Occurred())
1643 goto bail;
1644 Py_CLEAR(it);
1645
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001646 if (ident != NULL) {
1647 if (PyDict_DelItem(s->markers, ident))
1648 goto bail;
1649 Py_CLEAR(ident);
1650 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001651 /* TODO DOES NOT RUN; dead code
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001652 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001653 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001654
1655 yield '\n' + (' ' * (_indent * _current_indent_level))
1656 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001657 if (_PyAccu_Accumulate(acc, close_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001658 goto bail;
1659 return 0;
1660
1661bail:
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001662 Py_XDECREF(it);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001663 Py_XDECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001664 Py_XDECREF(kstr);
1665 Py_XDECREF(ident);
1666 return -1;
1667}
1668
1669
1670static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001671encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001672 PyObject *seq, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001673{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001674 /* Encode Python list seq to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001675 static PyObject *open_array = NULL;
1676 static PyObject *close_array = NULL;
1677 static PyObject *empty_array = NULL;
1678 PyObject *ident = NULL;
1679 PyObject *s_fast = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001680 Py_ssize_t i;
1681
1682 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
1683 open_array = PyUnicode_InternFromString("[");
1684 close_array = PyUnicode_InternFromString("]");
1685 empty_array = PyUnicode_InternFromString("[]");
1686 if (open_array == NULL || close_array == NULL || empty_array == NULL)
1687 return -1;
1688 }
1689 ident = NULL;
1690 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
1691 if (s_fast == NULL)
1692 return -1;
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001693 if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001694 Py_DECREF(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001695 return _PyAccu_Accumulate(acc, empty_array);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001696 }
1697
1698 if (s->markers != Py_None) {
1699 int has_key;
1700 ident = PyLong_FromVoidPtr(seq);
1701 if (ident == NULL)
1702 goto bail;
1703 has_key = PyDict_Contains(s->markers, ident);
1704 if (has_key) {
1705 if (has_key != -1)
1706 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1707 goto bail;
1708 }
1709 if (PyDict_SetItem(s->markers, ident, seq)) {
1710 goto bail;
1711 }
1712 }
1713
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001714 if (_PyAccu_Accumulate(acc, open_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001715 goto bail;
1716 if (s->indent != Py_None) {
1717 /* TODO: DOES NOT RUN */
1718 indent_level += 1;
1719 /*
1720 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1721 separator = _item_separator + newline_indent
1722 buf += newline_indent
1723 */
1724 }
Antoine Pitrou9f69e792012-11-01 19:52:06 +01001725 for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
1726 PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001727 if (i) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001728 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001729 goto bail;
1730 }
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001731 if (encoder_listencode_obj(s, acc, obj, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001732 goto bail;
1733 }
1734 if (ident != NULL) {
1735 if (PyDict_DelItem(s->markers, ident))
1736 goto bail;
1737 Py_CLEAR(ident);
1738 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001739
1740 /* TODO: DOES NOT RUN
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001741 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001742 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001743
1744 yield '\n' + (' ' * (_indent * _current_indent_level))
1745 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001746 if (_PyAccu_Accumulate(acc, close_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001747 goto bail;
1748 Py_DECREF(s_fast);
1749 return 0;
1750
1751bail:
1752 Py_XDECREF(ident);
1753 Py_DECREF(s_fast);
1754 return -1;
1755}
1756
1757static void
1758encoder_dealloc(PyObject *self)
1759{
1760 /* Deallocate Encoder */
1761 encoder_clear(self);
1762 Py_TYPE(self)->tp_free(self);
1763}
1764
1765static int
1766encoder_traverse(PyObject *self, visitproc visit, void *arg)
1767{
1768 PyEncoderObject *s;
1769 assert(PyEncoder_Check(self));
1770 s = (PyEncoderObject *)self;
1771 Py_VISIT(s->markers);
1772 Py_VISIT(s->defaultfn);
1773 Py_VISIT(s->encoder);
1774 Py_VISIT(s->indent);
1775 Py_VISIT(s->key_separator);
1776 Py_VISIT(s->item_separator);
1777 Py_VISIT(s->sort_keys);
1778 Py_VISIT(s->skipkeys);
1779 return 0;
1780}
1781
1782static int
1783encoder_clear(PyObject *self)
1784{
1785 /* Deallocate Encoder */
1786 PyEncoderObject *s;
1787 assert(PyEncoder_Check(self));
1788 s = (PyEncoderObject *)self;
1789 Py_CLEAR(s->markers);
1790 Py_CLEAR(s->defaultfn);
1791 Py_CLEAR(s->encoder);
1792 Py_CLEAR(s->indent);
1793 Py_CLEAR(s->key_separator);
1794 Py_CLEAR(s->item_separator);
1795 Py_CLEAR(s->sort_keys);
1796 Py_CLEAR(s->skipkeys);
1797 return 0;
1798}
1799
1800PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
1801
1802static
1803PyTypeObject PyEncoderType = {
1804 PyVarObject_HEAD_INIT(NULL, 0)
1805 "_json.Encoder", /* tp_name */
1806 sizeof(PyEncoderObject), /* tp_basicsize */
1807 0, /* tp_itemsize */
1808 encoder_dealloc, /* tp_dealloc */
1809 0, /* tp_print */
1810 0, /* tp_getattr */
1811 0, /* tp_setattr */
1812 0, /* tp_compare */
1813 0, /* tp_repr */
1814 0, /* tp_as_number */
1815 0, /* tp_as_sequence */
1816 0, /* tp_as_mapping */
1817 0, /* tp_hash */
1818 encoder_call, /* tp_call */
1819 0, /* tp_str */
1820 0, /* tp_getattro */
1821 0, /* tp_setattro */
1822 0, /* tp_as_buffer */
1823 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1824 encoder_doc, /* tp_doc */
1825 encoder_traverse, /* tp_traverse */
1826 encoder_clear, /* tp_clear */
1827 0, /* tp_richcompare */
1828 0, /* tp_weaklistoffset */
1829 0, /* tp_iter */
1830 0, /* tp_iternext */
1831 0, /* tp_methods */
1832 encoder_members, /* tp_members */
1833 0, /* tp_getset */
1834 0, /* tp_base */
1835 0, /* tp_dict */
1836 0, /* tp_descr_get */
1837 0, /* tp_descr_set */
1838 0, /* tp_dictoffset */
1839 encoder_init, /* tp_init */
1840 0, /* tp_alloc */
1841 encoder_new, /* tp_new */
1842 0, /* tp_free */
1843};
1844
1845static PyMethodDef speedups_methods[] = {
1846 {"encode_basestring_ascii",
1847 (PyCFunction)py_encode_basestring_ascii,
1848 METH_O,
1849 pydoc_encode_basestring_ascii},
1850 {"scanstring",
1851 (PyCFunction)py_scanstring,
1852 METH_VARARGS,
1853 pydoc_scanstring},
Christian Heimes90540002008-05-08 14:29:10 +00001854 {NULL, NULL, 0, NULL}
1855};
1856
1857PyDoc_STRVAR(module_doc,
1858"json speedups\n");
1859
Martin v. Löwis1a214512008-06-11 05:26:20 +00001860static struct PyModuleDef jsonmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 PyModuleDef_HEAD_INIT,
1862 "_json",
1863 module_doc,
1864 -1,
1865 speedups_methods,
1866 NULL,
1867 NULL,
1868 NULL,
1869 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001870};
1871
1872PyObject*
1873PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +00001874{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001875 PyObject *m = PyModule_Create(&jsonmodule);
1876 if (!m)
1877 return NULL;
1878 PyScannerType.tp_new = PyType_GenericNew;
1879 if (PyType_Ready(&PyScannerType) < 0)
1880 goto fail;
1881 PyEncoderType.tp_new = PyType_GenericNew;
1882 if (PyType_Ready(&PyEncoderType) < 0)
1883 goto fail;
1884 Py_INCREF((PyObject*)&PyScannerType);
1885 if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1886 Py_DECREF((PyObject*)&PyScannerType);
1887 goto fail;
1888 }
1889 Py_INCREF((PyObject*)&PyEncoderType);
1890 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1891 Py_DECREF((PyObject*)&PyEncoderType);
1892 goto fail;
1893 }
1894 return m;
1895 fail:
1896 Py_DECREF(m);
1897 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +00001898}