blob: 95c658ca7c5c861e16b12f4ccda3928322e36004 [file] [log] [blame]
Christian Heimes90540002008-05-08 14:29:10 +00001#include "Python.h"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00002#include "structmember.h"
Antoine Pitroud0acb412012-03-22 14:42:18 +01003#include "accu.h"
4
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00005#if PY_VERSION_HEX < 0x02060000 && !defined(Py_TYPE)
6#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
7#endif
8#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
9typedef int Py_ssize_t;
10#define PY_SSIZE_T_MAX INT_MAX
11#define PY_SSIZE_T_MIN INT_MIN
12#define PyInt_FromSsize_t PyInt_FromLong
13#define PyInt_AsSsize_t PyInt_AsLong
14#endif
15#ifndef Py_IS_FINITE
16#define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X))
17#endif
Christian Heimes90540002008-05-08 14:29:10 +000018
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000019#ifdef __GNUC__
20#define UNUSED __attribute__((__unused__))
21#else
22#define UNUSED
23#endif
24
25#define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType)
26#define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType)
27#define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType)
28#define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType)
29
30static PyTypeObject PyScannerType;
31static PyTypeObject PyEncoderType;
32
33typedef struct _PyScannerObject {
34 PyObject_HEAD
35 PyObject *strict;
36 PyObject *object_hook;
37 PyObject *object_pairs_hook;
38 PyObject *parse_float;
39 PyObject *parse_int;
40 PyObject *parse_constant;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +000041 PyObject *memo;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000042} PyScannerObject;
43
44static PyMemberDef scanner_members[] = {
45 {"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"},
46 {"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"},
47 {"object_pairs_hook", T_OBJECT, offsetof(PyScannerObject, object_pairs_hook), READONLY},
48 {"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"},
49 {"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "parse_int"},
50 {"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READONLY, "parse_constant"},
51 {NULL}
52};
53
54typedef struct _PyEncoderObject {
55 PyObject_HEAD
56 PyObject *markers;
57 PyObject *defaultfn;
58 PyObject *encoder;
59 PyObject *indent;
60 PyObject *key_separator;
61 PyObject *item_separator;
62 PyObject *sort_keys;
63 PyObject *skipkeys;
64 int fast_encode;
65 int allow_nan;
66} PyEncoderObject;
67
68static PyMemberDef encoder_members[] = {
69 {"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers"},
70 {"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "default"},
71 {"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder"},
72 {"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"},
73 {"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READONLY, "key_separator"},
74 {"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READONLY, "item_separator"},
75 {"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sort_keys"},
76 {"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipkeys"},
77 {NULL}
78};
79
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +020080static PyObject *
81join_list_unicode(PyObject *lst)
82{
83 /* return u''.join(lst) */
84 static PyObject *sep = NULL;
85 if (sep == NULL) {
86 sep = PyUnicode_FromStringAndSize("", 0);
87 if (sep == NULL)
88 return NULL;
89 }
90 return PyUnicode_Join(sep, lst);
91}
92
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +020093/* Forward decls */
94
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000095static PyObject *
96ascii_escape_unicode(PyObject *pystr);
97static PyObject *
98py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr);
99void init_json(void);
100static PyObject *
101scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr);
102static PyObject *
103_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx);
104static PyObject *
105scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
106static int
107scanner_init(PyObject *self, PyObject *args, PyObject *kwds);
108static void
109scanner_dealloc(PyObject *self);
110static int
111scanner_clear(PyObject *self);
112static PyObject *
113encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
114static int
115encoder_init(PyObject *self, PyObject *args, PyObject *kwds);
116static void
117encoder_dealloc(PyObject *self);
118static int
119encoder_clear(PyObject *self);
120static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200121encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc, PyObject *seq, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000122static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200123encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, PyObject *obj, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000124static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +0200125encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, PyObject *dct, Py_ssize_t indent_level);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000126static PyObject *
Hirokazu Yamamotofecf5d12009-05-02 15:55:19 +0000127_encoded_const(PyObject *obj);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000128static void
129raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
130static PyObject *
131encoder_encode_string(PyEncoderObject *s, PyObject *obj);
132static int
133_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr);
134static PyObject *
135_convertPyInt_FromSsize_t(Py_ssize_t *size_ptr);
136static PyObject *
137encoder_encode_float(PyEncoderObject *s, PyObject *obj);
138
Christian Heimes90540002008-05-08 14:29:10 +0000139#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000140#define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
Christian Heimes90540002008-05-08 14:29:10 +0000141
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000142static int
143_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000144{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000145 /* PyObject to Py_ssize_t converter */
146 *size_ptr = PyLong_AsSsize_t(o);
Georg Brandl59682052009-05-05 07:52:05 +0000147 if (*size_ptr == -1 && PyErr_Occurred())
148 return 0;
149 return 1;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000150}
151
152static PyObject *
153_convertPyInt_FromSsize_t(Py_ssize_t *size_ptr)
154{
155 /* Py_ssize_t to PyObject converter */
156 return PyLong_FromSsize_t(*size_ptr);
157}
158
159static Py_ssize_t
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200160ascii_escape_unichar(Py_UCS4 c, unsigned char *output, Py_ssize_t chars)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000161{
162 /* Escape unicode code point c to ASCII escape sequences
163 in char *output. output must have at least 12 bytes unused to
164 accommodate an escaped surrogate pair "\uXXXX\uXXXX" */
Christian Heimes90540002008-05-08 14:29:10 +0000165 output[chars++] = '\\';
166 switch (c) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000167 case '\\': output[chars++] = c; break;
168 case '"': output[chars++] = c; break;
Christian Heimes90540002008-05-08 14:29:10 +0000169 case '\b': output[chars++] = 'b'; break;
170 case '\f': output[chars++] = 'f'; break;
171 case '\n': output[chars++] = 'n'; break;
172 case '\r': output[chars++] = 'r'; break;
173 case '\t': output[chars++] = 't'; break;
174 default:
Christian Heimes90540002008-05-08 14:29:10 +0000175 if (c >= 0x10000) {
176 /* UTF-16 surrogate pair */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200177 Py_UCS4 v = c - 0x10000;
Christian Heimes90540002008-05-08 14:29:10 +0000178 c = 0xd800 | ((v >> 10) & 0x3ff);
179 output[chars++] = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200180 output[chars++] = Py_hexdigits[(c >> 12) & 0xf];
181 output[chars++] = Py_hexdigits[(c >> 8) & 0xf];
182 output[chars++] = Py_hexdigits[(c >> 4) & 0xf];
183 output[chars++] = Py_hexdigits[(c ) & 0xf];
Christian Heimes90540002008-05-08 14:29:10 +0000184 c = 0xdc00 | (v & 0x3ff);
185 output[chars++] = '\\';
186 }
Christian Heimes90540002008-05-08 14:29:10 +0000187 output[chars++] = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200188 output[chars++] = Py_hexdigits[(c >> 12) & 0xf];
189 output[chars++] = Py_hexdigits[(c >> 8) & 0xf];
190 output[chars++] = Py_hexdigits[(c >> 4) & 0xf];
191 output[chars++] = Py_hexdigits[(c ) & 0xf];
Christian Heimes90540002008-05-08 14:29:10 +0000192 }
193 return chars;
194}
195
196static PyObject *
197ascii_escape_unicode(PyObject *pystr)
198{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000199 /* Take a PyUnicode pystr and return a new ASCII-only escaped PyUnicode */
Christian Heimes90540002008-05-08 14:29:10 +0000200 Py_ssize_t i;
201 Py_ssize_t input_chars;
202 Py_ssize_t output_size;
203 Py_ssize_t chars;
204 PyObject *rval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200205 void *input;
206 unsigned char *output;
207 int kind;
Christian Heimes90540002008-05-08 14:29:10 +0000208
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200209 if (PyUnicode_READY(pystr) == -1)
210 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000211
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200212 input_chars = PyUnicode_GET_LENGTH(pystr);
213 input = PyUnicode_DATA(pystr);
214 kind = PyUnicode_KIND(pystr);
215
216 /* Compute the output size */
217 for (i = 0, output_size = 2; i < input_chars; i++) {
218 Py_UCS4 c = PyUnicode_READ(kind, input, i);
219 if (S_CHAR(c))
220 output_size++;
221 else {
222 switch(c) {
Victor Stinnerd9c06312011-10-11 21:56:19 +0200223 case '\\': case '"': case '\b': case '\f':
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200224 case '\n': case '\r': case '\t':
225 output_size += 2; break;
226 default:
227 output_size += c >= 0x10000 ? 12 : 6;
228 }
229 }
230 }
231
232 rval = PyUnicode_New(output_size, 127);
Christian Heimes90540002008-05-08 14:29:10 +0000233 if (rval == NULL) {
234 return NULL;
235 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200236 output = PyUnicode_1BYTE_DATA(rval);
Christian Heimes90540002008-05-08 14:29:10 +0000237 chars = 0;
238 output[chars++] = '"';
239 for (i = 0; i < input_chars; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200240 Py_UCS4 c = PyUnicode_READ(kind, input, i);
Christian Heimes90540002008-05-08 14:29:10 +0000241 if (S_CHAR(c)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000242 output[chars++] = c;
Christian Heimes90540002008-05-08 14:29:10 +0000243 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000244 else {
245 chars = ascii_escape_unichar(c, output, chars);
Christian Heimes90540002008-05-08 14:29:10 +0000246 }
Christian Heimes90540002008-05-08 14:29:10 +0000247 }
248 output[chars++] = '"';
Christian Heimes90540002008-05-08 14:29:10 +0000249 return rval;
250}
251
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000252static void
Christian Heimes90540002008-05-08 14:29:10 +0000253raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
254{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000255 /* Use the Python function json.decoder.errmsg to raise a nice
256 looking ValueError exception */
Christian Heimes90540002008-05-08 14:29:10 +0000257 static PyObject *errmsg_fn = NULL;
258 PyObject *pymsg;
259 if (errmsg_fn == NULL) {
260 PyObject *decoder = PyImport_ImportModule("json.decoder");
261 if (decoder == NULL)
262 return;
263 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000264 Py_DECREF(decoder);
Christian Heimes90540002008-05-08 14:29:10 +0000265 if (errmsg_fn == NULL)
266 return;
Christian Heimes90540002008-05-08 14:29:10 +0000267 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000268 pymsg = PyObject_CallFunction(errmsg_fn, "(zOO&)", msg, s, _convertPyInt_FromSsize_t, &end);
Benjamin Petersona13d4752008-10-16 21:17:24 +0000269 if (pymsg) {
270 PyErr_SetObject(PyExc_ValueError, pymsg);
271 Py_DECREF(pymsg);
272 }
Christian Heimes90540002008-05-08 14:29:10 +0000273}
274
275static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000276_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
277 /* return (rval, idx) tuple, stealing reference to rval */
278 PyObject *tpl;
279 PyObject *pyidx;
280 /*
281 steal a reference to rval, returns (rval, idx)
282 */
283 if (rval == NULL) {
Christian Heimes90540002008-05-08 14:29:10 +0000284 return NULL;
285 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000286 pyidx = PyLong_FromSsize_t(idx);
287 if (pyidx == NULL) {
288 Py_DECREF(rval);
289 return NULL;
290 }
291 tpl = PyTuple_New(2);
292 if (tpl == NULL) {
293 Py_DECREF(pyidx);
294 Py_DECREF(rval);
295 return NULL;
296 }
297 PyTuple_SET_ITEM(tpl, 0, rval);
298 PyTuple_SET_ITEM(tpl, 1, pyidx);
299 return tpl;
Christian Heimes90540002008-05-08 14:29:10 +0000300}
301
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000302#define APPEND_OLD_CHUNK \
303 if (chunk != NULL) { \
304 if (chunks == NULL) { \
305 chunks = PyList_New(0); \
306 if (chunks == NULL) { \
307 goto bail; \
308 } \
309 } \
310 if (PyList_Append(chunks, chunk)) { \
311 Py_DECREF(chunk); \
312 goto bail; \
313 } \
314 Py_CLEAR(chunk); \
315 }
316
Christian Heimes90540002008-05-08 14:29:10 +0000317static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000318scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
Christian Heimes90540002008-05-08 14:29:10 +0000319{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000320 /* Read the JSON string from PyUnicode pystr.
321 end is the index of the first character after the quote.
322 if strict is zero then literal control characters are allowed
323 *next_end_ptr is a return-by-reference index of the character
324 after the end quote
Christian Heimes90540002008-05-08 14:29:10 +0000325
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000326 Return value is a new PyUnicode
327 */
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000328 PyObject *rval = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200329 Py_ssize_t len;
Christian Heimes90540002008-05-08 14:29:10 +0000330 Py_ssize_t begin = end - 1;
Brett Cannonb94767f2011-02-22 20:15:44 +0000331 Py_ssize_t next /* = begin */;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200332 const void *buf;
333 int kind;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000334 PyObject *chunks = NULL;
335 PyObject *chunk = NULL;
336
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200337 if (PyUnicode_READY(pystr) == -1)
338 return 0;
339
340 len = PyUnicode_GET_LENGTH(pystr);
341 buf = PyUnicode_DATA(pystr);
342 kind = PyUnicode_KIND(pystr);
343
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000344 if (end < 0 || len <= end) {
345 PyErr_SetString(PyExc_ValueError, "end is out of bounds");
346 goto bail;
347 }
Christian Heimes90540002008-05-08 14:29:10 +0000348 while (1) {
349 /* Find the end of the string or the next escape */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200350 Py_UCS4 c = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000351 for (next = end; next < len; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200352 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000353 if (c == '"' || c == '\\') {
354 break;
355 }
356 else if (strict && c <= 0x1f) {
Benjamin Peterson7af6eec2008-07-19 22:26:35 +0000357 raise_errmsg("Invalid control character at", pystr, next);
Christian Heimes90540002008-05-08 14:29:10 +0000358 goto bail;
359 }
360 }
361 if (!(c == '"' || c == '\\')) {
362 raise_errmsg("Unterminated string starting at", pystr, begin);
363 goto bail;
364 }
365 /* Pick up this chunk if it's not zero length */
366 if (next != end) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000367 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200368 chunk = PyUnicode_FromKindAndData(
369 kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200370 (char*)buf + kind * end,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200371 next - end);
Christian Heimes90540002008-05-08 14:29:10 +0000372 if (chunk == NULL) {
373 goto bail;
374 }
Christian Heimes90540002008-05-08 14:29:10 +0000375 }
376 next++;
377 if (c == '"') {
378 end = next;
379 break;
380 }
381 if (next == len) {
382 raise_errmsg("Unterminated string starting at", pystr, begin);
383 goto bail;
384 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200385 c = PyUnicode_READ(kind, buf, next);
Christian Heimes90540002008-05-08 14:29:10 +0000386 if (c != 'u') {
387 /* Non-unicode backslash escapes */
388 end = next + 1;
389 switch (c) {
390 case '"': break;
391 case '\\': break;
392 case '/': break;
393 case 'b': c = '\b'; break;
394 case 'f': c = '\f'; break;
395 case 'n': c = '\n'; break;
396 case 'r': c = '\r'; break;
397 case 't': c = '\t'; break;
398 default: c = 0;
399 }
400 if (c == 0) {
401 raise_errmsg("Invalid \\escape", pystr, end - 2);
402 goto bail;
403 }
404 }
405 else {
406 c = 0;
407 next++;
408 end = next + 4;
409 if (end >= len) {
410 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
411 goto bail;
412 }
413 /* Decode 4 hex digits */
414 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200415 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000416 c <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000417 switch (digit) {
418 case '0': case '1': case '2': case '3': case '4':
419 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000420 c |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000421 case 'a': case 'b': case 'c': case 'd': case 'e':
422 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000423 c |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000424 case 'A': case 'B': case 'C': case 'D': case 'E':
425 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000426 c |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000427 default:
428 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
429 goto bail;
430 }
431 }
Christian Heimes90540002008-05-08 14:29:10 +0000432 /* Surrogate pair */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000433 if ((c & 0xfc00) == 0xd800) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200434 Py_UCS4 c2 = 0;
Christian Heimes90540002008-05-08 14:29:10 +0000435 if (end + 6 >= len) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000436 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
437 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000438 }
Victor Stinnerd9c06312011-10-11 21:56:19 +0200439 if (PyUnicode_READ(kind, buf, next++) != '\\' ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200440 PyUnicode_READ(kind, buf, next++) != 'u') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000441 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
442 goto bail;
Christian Heimes90540002008-05-08 14:29:10 +0000443 }
444 end += 6;
445 /* Decode 4 hex digits */
446 for (; next < end; next++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200447 Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
Antoine Pitrou5b0e9e82010-10-09 15:24:28 +0000448 c2 <<= 4;
Christian Heimes90540002008-05-08 14:29:10 +0000449 switch (digit) {
450 case '0': case '1': case '2': case '3': case '4':
451 case '5': case '6': case '7': case '8': case '9':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000452 c2 |= (digit - '0'); break;
Christian Heimes90540002008-05-08 14:29:10 +0000453 case 'a': case 'b': case 'c': case 'd': case 'e':
454 case 'f':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000455 c2 |= (digit - 'a' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000456 case 'A': case 'B': case 'C': case 'D': case 'E':
457 case 'F':
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000458 c2 |= (digit - 'A' + 10); break;
Christian Heimes90540002008-05-08 14:29:10 +0000459 default:
460 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
461 goto bail;
462 }
463 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000464 if ((c2 & 0xfc00) != 0xdc00) {
465 raise_errmsg("Unpaired high surrogate", pystr, end - 5);
466 goto bail;
467 }
Christian Heimes90540002008-05-08 14:29:10 +0000468 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
469 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000470 else if ((c & 0xfc00) == 0xdc00) {
471 raise_errmsg("Unpaired low surrogate", pystr, end - 5);
472 goto bail;
473 }
Christian Heimes90540002008-05-08 14:29:10 +0000474 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000475 APPEND_OLD_CHUNK
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200476 chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1);
Christian Heimes90540002008-05-08 14:29:10 +0000477 if (chunk == NULL) {
478 goto bail;
479 }
Christian Heimes90540002008-05-08 14:29:10 +0000480 }
481
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000482 if (chunks == NULL) {
483 if (chunk != NULL)
484 rval = chunk;
485 else
486 rval = PyUnicode_FromStringAndSize("", 0);
Christian Heimes90540002008-05-08 14:29:10 +0000487 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000488 else {
489 APPEND_OLD_CHUNK
490 rval = join_list_unicode(chunks);
491 if (rval == NULL) {
492 goto bail;
493 }
494 Py_CLEAR(chunks);
495 }
496
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000497 *next_end_ptr = end;
498 return rval;
Christian Heimes90540002008-05-08 14:29:10 +0000499bail:
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000500 *next_end_ptr = -1;
Christian Heimes90540002008-05-08 14:29:10 +0000501 Py_XDECREF(chunks);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000502 Py_XDECREF(chunk);
Christian Heimes90540002008-05-08 14:29:10 +0000503 return NULL;
504}
505
506PyDoc_STRVAR(pydoc_scanstring,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000507 "scanstring(string, end, strict=True) -> (string, end)\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000508 "\n"
509 "Scan the string s for a JSON string. End is the index of the\n"
510 "character in s after the quote that started the JSON string.\n"
511 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
512 "on attempt to decode an invalid string. If strict is False then literal\n"
513 "control characters are allowed in the string.\n"
514 "\n"
515 "Returns a tuple of the decoded string and the index of the character in s\n"
516 "after the end quote."
517);
Christian Heimes90540002008-05-08 14:29:10 +0000518
519static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000520py_scanstring(PyObject* self UNUSED, PyObject *args)
Christian Heimes90540002008-05-08 14:29:10 +0000521{
522 PyObject *pystr;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000523 PyObject *rval;
Christian Heimes90540002008-05-08 14:29:10 +0000524 Py_ssize_t end;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000525 Py_ssize_t next_end = -1;
526 int strict = 1;
527 if (!PyArg_ParseTuple(args, "OO&|i:scanstring", &pystr, _convertPyInt_AsSsize_t, &end, &strict)) {
Christian Heimes90540002008-05-08 14:29:10 +0000528 return NULL;
529 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000530 if (PyUnicode_Check(pystr)) {
531 rval = scanstring_unicode(pystr, end, strict, &next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000532 }
533 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 PyErr_Format(PyExc_TypeError,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000535 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000536 Py_TYPE(pystr)->tp_name);
537 return NULL;
538 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000539 return _build_rval_index_tuple(rval, next_end);
Christian Heimes90540002008-05-08 14:29:10 +0000540}
541
542PyDoc_STRVAR(pydoc_encode_basestring_ascii,
Georg Brandlc8284cf2010-08-02 20:16:18 +0000543 "encode_basestring_ascii(string) -> string\n"
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000544 "\n"
545 "Return an ASCII-only JSON representation of a Python string"
546);
Christian Heimes90540002008-05-08 14:29:10 +0000547
548static PyObject *
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000549py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
Christian Heimes90540002008-05-08 14:29:10 +0000550{
551 PyObject *rval;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000552 /* Return an ASCII-only JSON representation of a Python string */
Christian Heimes90540002008-05-08 14:29:10 +0000553 /* METH_O */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000554 if (PyUnicode_Check(pystr)) {
Christian Heimes90540002008-05-08 14:29:10 +0000555 rval = ascii_escape_unicode(pystr);
556 }
557 else {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000558 PyErr_Format(PyExc_TypeError,
559 "first argument must be a string, not %.80s",
Christian Heimes90540002008-05-08 14:29:10 +0000560 Py_TYPE(pystr)->tp_name);
561 return NULL;
562 }
Christian Heimes90540002008-05-08 14:29:10 +0000563 return rval;
564}
565
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000566static void
567scanner_dealloc(PyObject *self)
568{
569 /* Deallocate scanner object */
570 scanner_clear(self);
571 Py_TYPE(self)->tp_free(self);
572}
573
574static int
575scanner_traverse(PyObject *self, visitproc visit, void *arg)
576{
577 PyScannerObject *s;
578 assert(PyScanner_Check(self));
579 s = (PyScannerObject *)self;
580 Py_VISIT(s->strict);
581 Py_VISIT(s->object_hook);
582 Py_VISIT(s->object_pairs_hook);
583 Py_VISIT(s->parse_float);
584 Py_VISIT(s->parse_int);
585 Py_VISIT(s->parse_constant);
586 return 0;
587}
588
589static int
590scanner_clear(PyObject *self)
591{
592 PyScannerObject *s;
593 assert(PyScanner_Check(self));
594 s = (PyScannerObject *)self;
595 Py_CLEAR(s->strict);
596 Py_CLEAR(s->object_hook);
597 Py_CLEAR(s->object_pairs_hook);
598 Py_CLEAR(s->parse_float);
599 Py_CLEAR(s->parse_int);
600 Py_CLEAR(s->parse_constant);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000601 Py_CLEAR(s->memo);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000602 return 0;
603}
604
605static PyObject *
606_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
607 /* Read a JSON object from PyUnicode pystr.
608 idx is the index of the first character after the opening curly brace.
609 *next_idx_ptr is a return-by-reference index to the first character after
610 the closing curly brace.
611
612 Returns a new PyObject (usually a dict, but object_hook can change that)
613 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200614 void *str;
615 int kind;
616 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000617 PyObject *val = NULL;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000618 PyObject *rval = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000619 PyObject *key = NULL;
620 int strict = PyObject_IsTrue(s->strict);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000621 int has_pairs_hook = (s->object_pairs_hook != Py_None);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000622 Py_ssize_t next_idx;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000623
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200624 if (PyUnicode_READY(pystr) == -1)
625 return NULL;
626
627 str = PyUnicode_DATA(pystr);
628 kind = PyUnicode_KIND(pystr);
629 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
630
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000631 if (has_pairs_hook)
632 rval = PyList_New(0);
633 else
634 rval = PyDict_New();
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000635 if (rval == NULL)
636 return NULL;
637
638 /* skip whitespace after { */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200639 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000640
641 /* only loop if the object is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200642 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000643 while (idx <= end_idx) {
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000644 PyObject *memokey;
645
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000646 /* read key */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200647 if (PyUnicode_READ(kind, str, idx) != '"') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000648 raise_errmsg("Expecting property name", pystr, idx);
649 goto bail;
650 }
651 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
652 if (key == NULL)
653 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000654 memokey = PyDict_GetItem(s->memo, key);
655 if (memokey != NULL) {
656 Py_INCREF(memokey);
657 Py_DECREF(key);
658 key = memokey;
659 }
660 else {
661 if (PyDict_SetItem(s->memo, key, key) < 0)
662 goto bail;
663 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000664 idx = next_idx;
665
666 /* skip whitespace between key and : delimiter, read :, skip whitespace */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200667 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
668 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000669 raise_errmsg("Expecting : delimiter", pystr, idx);
670 goto bail;
671 }
672 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200673 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000674
675 /* read any JSON term */
676 val = scan_once_unicode(s, pystr, idx, &next_idx);
677 if (val == NULL)
678 goto bail;
679
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000680 if (has_pairs_hook) {
681 PyObject *item = PyTuple_Pack(2, key, val);
682 if (item == NULL)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000683 goto bail;
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000684 Py_CLEAR(key);
685 Py_CLEAR(val);
686 if (PyList_Append(rval, item) == -1) {
687 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000688 goto bail;
689 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000690 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000691 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000692 else {
693 if (PyDict_SetItem(rval, key, val) < 0)
694 goto bail;
695 Py_CLEAR(key);
696 Py_CLEAR(val);
697 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000698 idx = next_idx;
699
700 /* skip whitespace before } or , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200701 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000702
703 /* bail if the object is closed or we didn't get the , delimiter */
704 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200705 if (PyUnicode_READ(kind, str, idx) == '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000706 break;
707 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200708 else if (PyUnicode_READ(kind, str, idx) != ',') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000709 raise_errmsg("Expecting , delimiter", pystr, idx);
710 goto bail;
711 }
712 idx++;
713
714 /* skip whitespace after , delimiter */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200715 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000716 }
717 }
718
719 /* verify that idx < end_idx, str[idx] should be '}' */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200720 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000721 raise_errmsg("Expecting object", pystr, end_idx);
722 goto bail;
723 }
724
725 *next_idx_ptr = idx + 1;
726
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000727 if (has_pairs_hook) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000728 val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000729 Py_DECREF(rval);
730 return val;
731 }
732
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000733 /* if object_hook is not None: rval = object_hook(rval) */
734 if (s->object_hook != Py_None) {
735 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000736 Py_DECREF(rval);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000737 return val;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000738 }
739 return rval;
740bail:
741 Py_XDECREF(key);
742 Py_XDECREF(val);
Antoine Pitrou7d6e0762010-09-04 20:16:53 +0000743 Py_XDECREF(rval);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000744 return NULL;
745}
746
747static PyObject *
748_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
749 /* Read a JSON array from PyString pystr.
750 idx is the index of the first character after the opening brace.
751 *next_idx_ptr is a return-by-reference index to the first character after
752 the closing brace.
753
754 Returns a new PyList
755 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200756 void *str;
757 int kind;
758 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000759 PyObject *val = NULL;
760 PyObject *rval = PyList_New(0);
761 Py_ssize_t next_idx;
762 if (rval == NULL)
763 return NULL;
764
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200765 if (PyUnicode_READY(pystr) == -1)
766 return NULL;
767
768 str = PyUnicode_DATA(pystr);
769 kind = PyUnicode_KIND(pystr);
770 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
771
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000772 /* skip whitespace after [ */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200773 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000774
775 /* only loop if the array is non-empty */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200776 if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000777 while (idx <= end_idx) {
778
779 /* read any JSON term */
780 val = scan_once_unicode(s, pystr, idx, &next_idx);
781 if (val == NULL)
782 goto bail;
783
784 if (PyList_Append(rval, val) == -1)
785 goto bail;
786
787 Py_CLEAR(val);
788 idx = next_idx;
789
790 /* skip whitespace between term and , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200791 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000792
793 /* bail if the array is closed or we didn't get the , delimiter */
794 if (idx > end_idx) break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200795 if (PyUnicode_READ(kind, str, idx) == ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000796 break;
797 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200798 else if (PyUnicode_READ(kind, str, idx) != ',') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000799 raise_errmsg("Expecting , delimiter", pystr, idx);
800 goto bail;
801 }
802 idx++;
803
804 /* skip whitespace after , */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200805 while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000806 }
807 }
808
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200809 /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */
810 if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000811 raise_errmsg("Expecting object", pystr, end_idx);
812 goto bail;
813 }
814 *next_idx_ptr = idx + 1;
815 return rval;
816bail:
817 Py_XDECREF(val);
818 Py_DECREF(rval);
819 return NULL;
820}
821
822static PyObject *
823_parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
824 /* Read a JSON constant from PyString pystr.
825 constant is the constant string that was found
826 ("NaN", "Infinity", "-Infinity").
827 idx is the index of the first character of the constant
828 *next_idx_ptr is a return-by-reference index to the first character after
829 the constant.
830
831 Returns the result of parse_constant
832 */
833 PyObject *cstr;
834 PyObject *rval;
835 /* constant is "NaN", "Infinity", or "-Infinity" */
836 cstr = PyUnicode_InternFromString(constant);
837 if (cstr == NULL)
838 return NULL;
839
840 /* rval = parse_constant(constant) */
841 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
Victor Stinnerc4f281e2011-10-11 22:11:42 +0200842 idx += PyUnicode_GET_LENGTH(cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000843 Py_DECREF(cstr);
844 *next_idx_ptr = idx;
845 return rval;
846}
847
848static PyObject *
849_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
850 /* Read a JSON number from PyUnicode pystr.
851 idx is the index of the first character of the number
852 *next_idx_ptr is a return-by-reference index to the first character after
853 the number.
854
855 Returns a new PyObject representation of that number:
856 PyInt, PyLong, or PyFloat.
857 May return other types if parse_int or parse_float are set
858 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200859 void *str;
860 int kind;
861 Py_ssize_t end_idx;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000862 Py_ssize_t idx = start;
863 int is_float = 0;
864 PyObject *rval;
Antoine Pitrouf6454512011-04-25 19:16:06 +0200865 PyObject *numstr = NULL;
866 PyObject *custom_func;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000867
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200868 if (PyUnicode_READY(pystr) == -1)
869 return NULL;
870
871 str = PyUnicode_DATA(pystr);
872 kind = PyUnicode_KIND(pystr);
873 end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
874
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000875 /* 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 +0200876 if (PyUnicode_READ(kind, str, idx) == '-') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000877 idx++;
878 if (idx > end_idx) {
879 PyErr_SetNone(PyExc_StopIteration);
880 return NULL;
881 }
882 }
883
884 /* 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 +0200885 if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000886 idx++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200887 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000888 }
889 /* if it starts with 0 we only expect one integer digit */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200890 else if (PyUnicode_READ(kind, str, idx) == '0') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000891 idx++;
892 }
893 /* no integer digits, error */
894 else {
895 PyErr_SetNone(PyExc_StopIteration);
896 return NULL;
897 }
898
899 /* if the next char is '.' followed by a digit then read all float digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200900 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 +0000901 is_float = 1;
902 idx += 2;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200903 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000904 }
905
906 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200907 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000908 Py_ssize_t e_start = idx;
909 idx++;
910
911 /* read an exponent sign if present */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200912 if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000913
914 /* read all digits */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200915 while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000916
917 /* if we got a digit, then parse as float. if not, backtrack */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200918 if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000919 is_float = 1;
920 }
921 else {
922 idx = e_start;
923 }
924 }
925
Antoine Pitrouf6454512011-04-25 19:16:06 +0200926 if (is_float && s->parse_float != (PyObject *)&PyFloat_Type)
927 custom_func = s->parse_float;
928 else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type)
929 custom_func = s->parse_int;
930 else
931 custom_func = NULL;
932
933 if (custom_func) {
934 /* copy the section we determined to be a number */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200935 numstr = PyUnicode_FromKindAndData(kind,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200936 (char*)str + kind * start,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200937 idx - start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200938 if (numstr == NULL)
939 return NULL;
940 rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000941 }
942 else {
Antoine Pitrouf6454512011-04-25 19:16:06 +0200943 Py_ssize_t i, n;
944 char *buf;
945 /* Straight conversion to ASCII, to avoid costly conversion of
946 decimal unicode digits (which cannot appear here) */
947 n = idx - start;
948 numstr = PyBytes_FromStringAndSize(NULL, n);
949 if (numstr == NULL)
950 return NULL;
951 buf = PyBytes_AS_STRING(numstr);
952 for (i = 0; i < n; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200953 buf[i] = (char) PyUnicode_READ(kind, str, i + start);
Antoine Pitrouf6454512011-04-25 19:16:06 +0200954 }
955 if (is_float)
956 rval = PyFloat_FromString(numstr);
957 else
958 rval = PyLong_FromString(buf, NULL, 10);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000959 }
960 Py_DECREF(numstr);
961 *next_idx_ptr = idx;
962 return rval;
963}
964
965static PyObject *
966scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
967{
968 /* Read one JSON term (of any kind) from PyUnicode pystr.
969 idx is the index of the first character of the term
970 *next_idx_ptr is a return-by-reference index to the first character after
971 the number.
972
973 Returns a new PyObject representation of the term.
974 */
Ezio Melotti362b9512011-05-07 17:58:09 +0300975 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200976 void *str;
977 int kind;
978 Py_ssize_t length;
979
980 if (PyUnicode_READY(pystr) == -1)
981 return NULL;
982
983 str = PyUnicode_DATA(pystr);
984 kind = PyUnicode_KIND(pystr);
985 length = PyUnicode_GET_LENGTH(pystr);
986
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000987 if (idx >= length) {
988 PyErr_SetNone(PyExc_StopIteration);
989 return NULL;
990 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200991
992 switch (PyUnicode_READ(kind, str, idx)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000993 case '"':
994 /* string */
995 return scanstring_unicode(pystr, idx + 1,
996 PyObject_IsTrue(s->strict),
997 next_idx_ptr);
998 case '{':
999 /* object */
Ezio Melotti362b9512011-05-07 17:58:09 +03001000 if (Py_EnterRecursiveCall(" while decoding a JSON object "
1001 "from a unicode string"))
1002 return NULL;
1003 res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
1004 Py_LeaveRecursiveCall();
1005 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001006 case '[':
1007 /* array */
Ezio Melotti362b9512011-05-07 17:58:09 +03001008 if (Py_EnterRecursiveCall(" while decoding a JSON array "
1009 "from a unicode string"))
1010 return NULL;
1011 res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
1012 Py_LeaveRecursiveCall();
1013 return res;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001014 case 'n':
1015 /* null */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001016 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 +00001017 Py_INCREF(Py_None);
1018 *next_idx_ptr = idx + 4;
1019 return Py_None;
1020 }
1021 break;
1022 case 't':
1023 /* true */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001024 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 +00001025 Py_INCREF(Py_True);
1026 *next_idx_ptr = idx + 4;
1027 return Py_True;
1028 }
1029 break;
1030 case 'f':
1031 /* false */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001032 if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
1033 PyUnicode_READ(kind, str, idx + 2) == 'l' &&
1034 PyUnicode_READ(kind, str, idx + 3) == 's' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001035 PyUnicode_READ(kind, str, idx + 4) == 'e') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001036 Py_INCREF(Py_False);
1037 *next_idx_ptr = idx + 5;
1038 return Py_False;
1039 }
1040 break;
1041 case 'N':
1042 /* NaN */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001043 if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001044 PyUnicode_READ(kind, str, idx + 2) == 'N') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001045 return _parse_constant(s, "NaN", idx, next_idx_ptr);
1046 }
1047 break;
1048 case 'I':
1049 /* Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001050 if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
1051 PyUnicode_READ(kind, str, idx + 2) == 'f' &&
1052 PyUnicode_READ(kind, str, idx + 3) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001053 PyUnicode_READ(kind, str, idx + 4) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001054 PyUnicode_READ(kind, str, idx + 5) == 'i' &&
1055 PyUnicode_READ(kind, str, idx + 6) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001056 PyUnicode_READ(kind, str, idx + 7) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001057 return _parse_constant(s, "Infinity", idx, next_idx_ptr);
1058 }
1059 break;
1060 case '-':
1061 /* -Infinity */
Victor Stinnerd9c06312011-10-11 21:56:19 +02001062 if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001063 PyUnicode_READ(kind, str, idx + 2) == 'n' &&
1064 PyUnicode_READ(kind, str, idx + 3) == 'f' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001065 PyUnicode_READ(kind, str, idx + 4) == 'i' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001066 PyUnicode_READ(kind, str, idx + 5) == 'n' &&
Victor Stinnerd9c06312011-10-11 21:56:19 +02001067 PyUnicode_READ(kind, str, idx + 6) == 'i' &&
1068 PyUnicode_READ(kind, str, idx + 7) == 't' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001069 PyUnicode_READ(kind, str, idx + 8) == 'y') {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001070 return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
1071 }
1072 break;
1073 }
1074 /* Didn't find a string, object, array, or named constant. Look for a number. */
1075 return _match_number_unicode(s, pystr, idx, next_idx_ptr);
1076}
1077
1078static PyObject *
1079scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
1080{
1081 /* Python callable interface to scan_once_{str,unicode} */
1082 PyObject *pystr;
1083 PyObject *rval;
1084 Py_ssize_t idx;
1085 Py_ssize_t next_idx = -1;
1086 static char *kwlist[] = {"string", "idx", NULL};
1087 PyScannerObject *s;
1088 assert(PyScanner_Check(self));
1089 s = (PyScannerObject *)self;
1090 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:scan_once", kwlist, &pystr, _convertPyInt_AsSsize_t, &idx))
1091 return NULL;
1092
1093 if (PyUnicode_Check(pystr)) {
1094 rval = scan_once_unicode(s, pystr, idx, &next_idx);
1095 }
1096 else {
1097 PyErr_Format(PyExc_TypeError,
1098 "first argument must be a string, not %.80s",
1099 Py_TYPE(pystr)->tp_name);
1100 return NULL;
1101 }
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001102 PyDict_Clear(s->memo);
1103 if (rval == NULL)
1104 return NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001105 return _build_rval_index_tuple(rval, next_idx);
1106}
1107
1108static PyObject *
1109scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1110{
1111 PyScannerObject *s;
1112 s = (PyScannerObject *)type->tp_alloc(type, 0);
1113 if (s != NULL) {
1114 s->strict = NULL;
1115 s->object_hook = NULL;
1116 s->object_pairs_hook = NULL;
1117 s->parse_float = NULL;
1118 s->parse_int = NULL;
1119 s->parse_constant = NULL;
1120 }
1121 return (PyObject *)s;
1122}
1123
1124static int
1125scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
1126{
1127 /* Initialize Scanner object */
1128 PyObject *ctx;
1129 static char *kwlist[] = {"context", NULL};
1130 PyScannerObject *s;
1131
1132 assert(PyScanner_Check(self));
1133 s = (PyScannerObject *)self;
1134
1135 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
1136 return -1;
1137
Antoine Pitrou7d6e0762010-09-04 20:16:53 +00001138 if (s->memo == NULL) {
1139 s->memo = PyDict_New();
1140 if (s->memo == NULL)
1141 goto bail;
1142 }
1143
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001144 /* All of these will fail "gracefully" so we don't need to verify them */
1145 s->strict = PyObject_GetAttrString(ctx, "strict");
1146 if (s->strict == NULL)
1147 goto bail;
1148 s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
1149 if (s->object_hook == NULL)
1150 goto bail;
1151 s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
1152 if (s->object_pairs_hook == NULL)
1153 goto bail;
1154 s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
1155 if (s->parse_float == NULL)
1156 goto bail;
1157 s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
1158 if (s->parse_int == NULL)
1159 goto bail;
1160 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
1161 if (s->parse_constant == NULL)
1162 goto bail;
1163
1164 return 0;
1165
1166bail:
1167 Py_CLEAR(s->strict);
1168 Py_CLEAR(s->object_hook);
1169 Py_CLEAR(s->object_pairs_hook);
1170 Py_CLEAR(s->parse_float);
1171 Py_CLEAR(s->parse_int);
1172 Py_CLEAR(s->parse_constant);
1173 return -1;
1174}
1175
1176PyDoc_STRVAR(scanner_doc, "JSON scanner object");
1177
1178static
1179PyTypeObject PyScannerType = {
1180 PyVarObject_HEAD_INIT(NULL, 0)
1181 "_json.Scanner", /* tp_name */
1182 sizeof(PyScannerObject), /* tp_basicsize */
1183 0, /* tp_itemsize */
1184 scanner_dealloc, /* tp_dealloc */
1185 0, /* tp_print */
1186 0, /* tp_getattr */
1187 0, /* tp_setattr */
1188 0, /* tp_compare */
1189 0, /* tp_repr */
1190 0, /* tp_as_number */
1191 0, /* tp_as_sequence */
1192 0, /* tp_as_mapping */
1193 0, /* tp_hash */
1194 scanner_call, /* tp_call */
1195 0, /* tp_str */
1196 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
1197 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
1198 0, /* tp_as_buffer */
1199 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1200 scanner_doc, /* tp_doc */
1201 scanner_traverse, /* tp_traverse */
1202 scanner_clear, /* tp_clear */
1203 0, /* tp_richcompare */
1204 0, /* tp_weaklistoffset */
1205 0, /* tp_iter */
1206 0, /* tp_iternext */
1207 0, /* tp_methods */
1208 scanner_members, /* tp_members */
1209 0, /* tp_getset */
1210 0, /* tp_base */
1211 0, /* tp_dict */
1212 0, /* tp_descr_get */
1213 0, /* tp_descr_set */
1214 0, /* tp_dictoffset */
1215 scanner_init, /* tp_init */
1216 0,/* PyType_GenericAlloc, */ /* tp_alloc */
1217 scanner_new, /* tp_new */
1218 0,/* PyObject_GC_Del, */ /* tp_free */
1219};
1220
1221static PyObject *
1222encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1223{
1224 PyEncoderObject *s;
1225 s = (PyEncoderObject *)type->tp_alloc(type, 0);
1226 if (s != NULL) {
1227 s->markers = NULL;
1228 s->defaultfn = NULL;
1229 s->encoder = NULL;
1230 s->indent = NULL;
1231 s->key_separator = NULL;
1232 s->item_separator = NULL;
1233 s->sort_keys = NULL;
1234 s->skipkeys = NULL;
1235 }
1236 return (PyObject *)s;
1237}
1238
1239static int
1240encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
1241{
1242 /* initialize Encoder object */
1243 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
1244
1245 PyEncoderObject *s;
Antoine Pitrou781eba72009-12-08 15:57:31 +00001246 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
1247 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001248
1249 assert(PyEncoder_Check(self));
1250 s = (PyEncoderObject *)self;
1251
1252 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
Antoine Pitrou781eba72009-12-08 15:57:31 +00001253 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1254 &sort_keys, &skipkeys, &allow_nan))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001255 return -1;
1256
Antoine Pitrou781eba72009-12-08 15:57:31 +00001257 s->markers = markers;
1258 s->defaultfn = defaultfn;
1259 s->encoder = encoder;
1260 s->indent = indent;
1261 s->key_separator = key_separator;
1262 s->item_separator = item_separator;
1263 s->sort_keys = sort_keys;
1264 s->skipkeys = skipkeys;
1265 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
1266 s->allow_nan = PyObject_IsTrue(allow_nan);
1267
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001268 Py_INCREF(s->markers);
1269 Py_INCREF(s->defaultfn);
1270 Py_INCREF(s->encoder);
1271 Py_INCREF(s->indent);
1272 Py_INCREF(s->key_separator);
1273 Py_INCREF(s->item_separator);
1274 Py_INCREF(s->sort_keys);
1275 Py_INCREF(s->skipkeys);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001276 return 0;
1277}
1278
1279static PyObject *
1280encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
1281{
1282 /* Python callable interface to encode_listencode_obj */
1283 static char *kwlist[] = {"obj", "_current_indent_level", NULL};
1284 PyObject *obj;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001285 Py_ssize_t indent_level;
1286 PyEncoderObject *s;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001287 _PyAccu acc;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001288
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001289 assert(PyEncoder_Check(self));
1290 s = (PyEncoderObject *)self;
1291 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:_iterencode", kwlist,
1292 &obj, _convertPyInt_AsSsize_t, &indent_level))
1293 return NULL;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001294 if (_PyAccu_Init(&acc))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001295 return NULL;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001296 if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001297 _PyAccu_Destroy(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001298 return NULL;
1299 }
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001300 return _PyAccu_FinishAsList(&acc);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001301}
1302
1303static PyObject *
1304_encoded_const(PyObject *obj)
1305{
1306 /* Return the JSON string representation of None, True, False */
1307 if (obj == Py_None) {
1308 static PyObject *s_null = NULL;
1309 if (s_null == NULL) {
1310 s_null = PyUnicode_InternFromString("null");
1311 }
1312 Py_INCREF(s_null);
1313 return s_null;
1314 }
1315 else if (obj == Py_True) {
1316 static PyObject *s_true = NULL;
1317 if (s_true == NULL) {
1318 s_true = PyUnicode_InternFromString("true");
1319 }
1320 Py_INCREF(s_true);
1321 return s_true;
1322 }
1323 else if (obj == Py_False) {
1324 static PyObject *s_false = NULL;
1325 if (s_false == NULL) {
1326 s_false = PyUnicode_InternFromString("false");
1327 }
1328 Py_INCREF(s_false);
1329 return s_false;
1330 }
1331 else {
1332 PyErr_SetString(PyExc_ValueError, "not a const");
1333 return NULL;
1334 }
1335}
1336
1337static PyObject *
1338encoder_encode_float(PyEncoderObject *s, PyObject *obj)
1339{
1340 /* Return the JSON representation of a PyFloat */
1341 double i = PyFloat_AS_DOUBLE(obj);
1342 if (!Py_IS_FINITE(i)) {
1343 if (!s->allow_nan) {
1344 PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant");
1345 return NULL;
1346 }
1347 if (i > 0) {
1348 return PyUnicode_FromString("Infinity");
1349 }
1350 else if (i < 0) {
1351 return PyUnicode_FromString("-Infinity");
1352 }
1353 else {
1354 return PyUnicode_FromString("NaN");
1355 }
1356 }
1357 /* Use a better float format here? */
1358 return PyObject_Repr(obj);
1359}
1360
1361static PyObject *
1362encoder_encode_string(PyEncoderObject *s, PyObject *obj)
1363{
1364 /* Return the JSON representation of a string */
1365 if (s->fast_encode)
1366 return py_encode_basestring_ascii(NULL, obj);
1367 else
1368 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1369}
1370
1371static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001372_steal_accumulate(_PyAccu *acc, PyObject *stolen)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001373{
1374 /* Append stolen and then decrement its reference count */
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001375 int rval = _PyAccu_Accumulate(acc, stolen);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001376 Py_DECREF(stolen);
1377 return rval;
1378}
1379
1380static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001381encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001382 PyObject *obj, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001383{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001384 /* Encode Python object obj to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001385 PyObject *newobj;
1386 int rv;
1387
1388 if (obj == Py_None || obj == Py_True || obj == Py_False) {
1389 PyObject *cstr = _encoded_const(obj);
1390 if (cstr == NULL)
1391 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001392 return _steal_accumulate(acc, cstr);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001393 }
1394 else if (PyUnicode_Check(obj))
1395 {
1396 PyObject *encoded = encoder_encode_string(s, obj);
1397 if (encoded == NULL)
1398 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001399 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001400 }
1401 else if (PyLong_Check(obj)) {
1402 PyObject *encoded = PyObject_Str(obj);
1403 if (encoded == NULL)
1404 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001405 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001406 }
1407 else if (PyFloat_Check(obj)) {
1408 PyObject *encoded = encoder_encode_float(s, obj);
1409 if (encoded == NULL)
1410 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001411 return _steal_accumulate(acc, encoded);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001412 }
1413 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001414 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1415 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001416 rv = encoder_listencode_list(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001417 Py_LeaveRecursiveCall();
1418 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001419 }
1420 else if (PyDict_Check(obj)) {
Ezio Melotti13672652011-05-11 01:02:56 +03001421 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1422 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001423 rv = encoder_listencode_dict(s, acc, obj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001424 Py_LeaveRecursiveCall();
1425 return rv;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001426 }
1427 else {
1428 PyObject *ident = NULL;
1429 if (s->markers != Py_None) {
1430 int has_key;
1431 ident = PyLong_FromVoidPtr(obj);
1432 if (ident == NULL)
1433 return -1;
1434 has_key = PyDict_Contains(s->markers, ident);
1435 if (has_key) {
1436 if (has_key != -1)
1437 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1438 Py_DECREF(ident);
1439 return -1;
1440 }
1441 if (PyDict_SetItem(s->markers, ident, obj)) {
1442 Py_DECREF(ident);
1443 return -1;
1444 }
1445 }
1446 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1447 if (newobj == NULL) {
1448 Py_XDECREF(ident);
1449 return -1;
1450 }
Ezio Melotti13672652011-05-11 01:02:56 +03001451
1452 if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1453 return -1;
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001454 rv = encoder_listencode_obj(s, acc, newobj, indent_level);
Ezio Melotti13672652011-05-11 01:02:56 +03001455 Py_LeaveRecursiveCall();
1456
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001457 Py_DECREF(newobj);
1458 if (rv) {
1459 Py_XDECREF(ident);
1460 return -1;
1461 }
1462 if (ident != NULL) {
1463 if (PyDict_DelItem(s->markers, ident)) {
1464 Py_XDECREF(ident);
1465 return -1;
1466 }
1467 Py_XDECREF(ident);
1468 }
1469 return rv;
1470 }
1471}
1472
1473static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001474encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001475 PyObject *dct, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001476{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001477 /* Encode Python dict dct a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001478 static PyObject *open_dict = NULL;
1479 static PyObject *close_dict = NULL;
1480 static PyObject *empty_dict = NULL;
1481 PyObject *kstr = NULL;
1482 PyObject *ident = NULL;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001483 PyObject *it = NULL;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001484 PyObject *items;
1485 PyObject *item = NULL;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001486 int skipkeys;
1487 Py_ssize_t idx;
1488
1489 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
1490 open_dict = PyUnicode_InternFromString("{");
1491 close_dict = PyUnicode_InternFromString("}");
1492 empty_dict = PyUnicode_InternFromString("{}");
1493 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
1494 return -1;
1495 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001496 if (Py_SIZE(dct) == 0)
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001497 return _PyAccu_Accumulate(acc, empty_dict);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001498
1499 if (s->markers != Py_None) {
1500 int has_key;
1501 ident = PyLong_FromVoidPtr(dct);
1502 if (ident == NULL)
1503 goto bail;
1504 has_key = PyDict_Contains(s->markers, ident);
1505 if (has_key) {
1506 if (has_key != -1)
1507 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1508 goto bail;
1509 }
1510 if (PyDict_SetItem(s->markers, ident, dct)) {
1511 goto bail;
1512 }
1513 }
1514
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001515 if (_PyAccu_Accumulate(acc, open_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001516 goto bail;
1517
1518 if (s->indent != Py_None) {
1519 /* TODO: DOES NOT RUN */
1520 indent_level += 1;
1521 /*
1522 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1523 separator = _item_separator + newline_indent
1524 buf += newline_indent
1525 */
1526 }
1527
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001528 if (PyObject_IsTrue(s->sort_keys)) {
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001529 /* First sort the keys then replace them with (key, value) tuples. */
1530 Py_ssize_t i, nitems;
1531 items = PyMapping_Keys(dct);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 if (items == NULL)
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001533 goto bail;
1534 if (!PyList_Check(items)) {
1535 PyErr_SetString(PyExc_ValueError, "keys must return list");
1536 goto bail;
1537 }
1538 if (PyList_Sort(items) < 0)
1539 goto bail;
1540 nitems = PyList_GET_SIZE(items);
1541 for (i = 0; i < nitems; i++) {
1542 PyObject *key, *value;
1543 key = PyList_GET_ITEM(items, i);
1544 value = PyDict_GetItem(dct, key);
1545 item = PyTuple_Pack(2, key, value);
1546 if (item == NULL)
1547 goto bail;
1548 PyList_SET_ITEM(items, i, item);
1549 Py_DECREF(key);
1550 }
1551 }
1552 else {
1553 items = PyMapping_Items(dct);
1554 }
1555 if (items == NULL)
Raymond Hettinger491a4cb2009-05-27 11:19:02 +00001556 goto bail;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001557 it = PyObject_GetIter(items);
Antoine Pitrou2397dd52010-11-04 16:51:32 +00001558 Py_DECREF(items);
1559 if (it == NULL)
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001560 goto bail;
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001561 skipkeys = PyObject_IsTrue(s->skipkeys);
1562 idx = 0;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001563 while ((item = PyIter_Next(it)) != NULL) {
1564 PyObject *encoded, *key, *value;
1565 if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
1566 PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
1567 goto bail;
1568 }
1569 key = PyTuple_GET_ITEM(item, 0);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001570 if (PyUnicode_Check(key)) {
1571 Py_INCREF(key);
1572 kstr = key;
1573 }
1574 else if (PyFloat_Check(key)) {
1575 kstr = encoder_encode_float(s, key);
1576 if (kstr == NULL)
1577 goto bail;
1578 }
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001579 else if (key == Py_True || key == Py_False || key == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 /* This must come before the PyLong_Check because
1581 True and False are also 1 and 0.*/
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001582 kstr = _encoded_const(key);
1583 if (kstr == NULL)
1584 goto bail;
1585 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001586 else if (PyLong_Check(key)) {
1587 kstr = PyObject_Str(key);
1588 if (kstr == NULL)
1589 goto bail;
1590 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001591 else if (skipkeys) {
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001592 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001593 continue;
1594 }
1595 else {
1596 /* TODO: include repr of key */
Doug Hellmann1c524752010-07-21 12:29:04 +00001597 PyErr_SetString(PyExc_TypeError, "keys must be a string");
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001598 goto bail;
1599 }
1600
1601 if (idx) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001602 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001603 goto bail;
1604 }
1605
1606 encoded = encoder_encode_string(s, kstr);
1607 Py_CLEAR(kstr);
1608 if (encoded == NULL)
1609 goto bail;
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001610 if (_PyAccu_Accumulate(acc, encoded)) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001611 Py_DECREF(encoded);
1612 goto bail;
1613 }
1614 Py_DECREF(encoded);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001615 if (_PyAccu_Accumulate(acc, s->key_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001616 goto bail;
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001617
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001618 value = PyTuple_GET_ITEM(item, 1);
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001619 if (encoder_listencode_obj(s, acc, value, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001620 goto bail;
1621 idx += 1;
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001622 Py_DECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001623 }
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001624 if (PyErr_Occurred())
1625 goto bail;
1626 Py_CLEAR(it);
1627
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001628 if (ident != NULL) {
1629 if (PyDict_DelItem(s->markers, ident))
1630 goto bail;
1631 Py_CLEAR(ident);
1632 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001633 /* TODO DOES NOT RUN; dead code
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001634 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001635 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001636
1637 yield '\n' + (' ' * (_indent * _current_indent_level))
1638 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001639 if (_PyAccu_Accumulate(acc, close_dict))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001640 goto bail;
1641 return 0;
1642
1643bail:
Raymond Hettingerc8d952d2009-05-27 06:50:31 +00001644 Py_XDECREF(it);
Raymond Hettingerbcf6f922009-05-27 09:58:34 +00001645 Py_XDECREF(item);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001646 Py_XDECREF(kstr);
1647 Py_XDECREF(ident);
1648 return -1;
1649}
1650
1651
1652static int
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001653encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001654 PyObject *seq, Py_ssize_t indent_level)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001655{
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001656 /* Encode Python list seq to a JSON term */
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001657 static PyObject *open_array = NULL;
1658 static PyObject *close_array = NULL;
1659 static PyObject *empty_array = NULL;
1660 PyObject *ident = NULL;
1661 PyObject *s_fast = NULL;
1662 Py_ssize_t num_items;
1663 PyObject **seq_items;
1664 Py_ssize_t i;
1665
1666 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
1667 open_array = PyUnicode_InternFromString("[");
1668 close_array = PyUnicode_InternFromString("]");
1669 empty_array = PyUnicode_InternFromString("[]");
1670 if (open_array == NULL || close_array == NULL || empty_array == NULL)
1671 return -1;
1672 }
1673 ident = NULL;
1674 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
1675 if (s_fast == NULL)
1676 return -1;
1677 num_items = PySequence_Fast_GET_SIZE(s_fast);
1678 if (num_items == 0) {
1679 Py_DECREF(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001680 return _PyAccu_Accumulate(acc, empty_array);
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001681 }
1682
1683 if (s->markers != Py_None) {
1684 int has_key;
1685 ident = PyLong_FromVoidPtr(seq);
1686 if (ident == NULL)
1687 goto bail;
1688 has_key = PyDict_Contains(s->markers, ident);
1689 if (has_key) {
1690 if (has_key != -1)
1691 PyErr_SetString(PyExc_ValueError, "Circular reference detected");
1692 goto bail;
1693 }
1694 if (PyDict_SetItem(s->markers, ident, seq)) {
1695 goto bail;
1696 }
1697 }
1698
1699 seq_items = PySequence_Fast_ITEMS(s_fast);
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001700 if (_PyAccu_Accumulate(acc, open_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001701 goto bail;
1702 if (s->indent != Py_None) {
1703 /* TODO: DOES NOT RUN */
1704 indent_level += 1;
1705 /*
1706 newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
1707 separator = _item_separator + newline_indent
1708 buf += newline_indent
1709 */
1710 }
1711 for (i = 0; i < num_items; i++) {
1712 PyObject *obj = seq_items[i];
1713 if (i) {
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001714 if (_PyAccu_Accumulate(acc, s->item_separator))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001715 goto bail;
1716 }
Antoine Pitroudf7fc9d2011-08-19 18:03:14 +02001717 if (encoder_listencode_obj(s, acc, obj, indent_level))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001718 goto bail;
1719 }
1720 if (ident != NULL) {
1721 if (PyDict_DelItem(s->markers, ident))
1722 goto bail;
1723 Py_CLEAR(ident);
1724 }
Brett Cannonb94767f2011-02-22 20:15:44 +00001725
1726 /* TODO: DOES NOT RUN
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001727 if (s->indent != Py_None) {
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001728 indent_level -= 1;
Brett Cannonb94767f2011-02-22 20:15:44 +00001729
1730 yield '\n' + (' ' * (_indent * _current_indent_level))
1731 }*/
Antoine Pitrou90c30e82011-10-06 19:09:51 +02001732 if (_PyAccu_Accumulate(acc, close_array))
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001733 goto bail;
1734 Py_DECREF(s_fast);
1735 return 0;
1736
1737bail:
1738 Py_XDECREF(ident);
1739 Py_DECREF(s_fast);
1740 return -1;
1741}
1742
1743static void
1744encoder_dealloc(PyObject *self)
1745{
1746 /* Deallocate Encoder */
1747 encoder_clear(self);
1748 Py_TYPE(self)->tp_free(self);
1749}
1750
1751static int
1752encoder_traverse(PyObject *self, visitproc visit, void *arg)
1753{
1754 PyEncoderObject *s;
1755 assert(PyEncoder_Check(self));
1756 s = (PyEncoderObject *)self;
1757 Py_VISIT(s->markers);
1758 Py_VISIT(s->defaultfn);
1759 Py_VISIT(s->encoder);
1760 Py_VISIT(s->indent);
1761 Py_VISIT(s->key_separator);
1762 Py_VISIT(s->item_separator);
1763 Py_VISIT(s->sort_keys);
1764 Py_VISIT(s->skipkeys);
1765 return 0;
1766}
1767
1768static int
1769encoder_clear(PyObject *self)
1770{
1771 /* Deallocate Encoder */
1772 PyEncoderObject *s;
1773 assert(PyEncoder_Check(self));
1774 s = (PyEncoderObject *)self;
1775 Py_CLEAR(s->markers);
1776 Py_CLEAR(s->defaultfn);
1777 Py_CLEAR(s->encoder);
1778 Py_CLEAR(s->indent);
1779 Py_CLEAR(s->key_separator);
1780 Py_CLEAR(s->item_separator);
1781 Py_CLEAR(s->sort_keys);
1782 Py_CLEAR(s->skipkeys);
1783 return 0;
1784}
1785
1786PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
1787
1788static
1789PyTypeObject PyEncoderType = {
1790 PyVarObject_HEAD_INIT(NULL, 0)
1791 "_json.Encoder", /* tp_name */
1792 sizeof(PyEncoderObject), /* tp_basicsize */
1793 0, /* tp_itemsize */
1794 encoder_dealloc, /* tp_dealloc */
1795 0, /* tp_print */
1796 0, /* tp_getattr */
1797 0, /* tp_setattr */
1798 0, /* tp_compare */
1799 0, /* tp_repr */
1800 0, /* tp_as_number */
1801 0, /* tp_as_sequence */
1802 0, /* tp_as_mapping */
1803 0, /* tp_hash */
1804 encoder_call, /* tp_call */
1805 0, /* tp_str */
1806 0, /* tp_getattro */
1807 0, /* tp_setattro */
1808 0, /* tp_as_buffer */
1809 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1810 encoder_doc, /* tp_doc */
1811 encoder_traverse, /* tp_traverse */
1812 encoder_clear, /* tp_clear */
1813 0, /* tp_richcompare */
1814 0, /* tp_weaklistoffset */
1815 0, /* tp_iter */
1816 0, /* tp_iternext */
1817 0, /* tp_methods */
1818 encoder_members, /* tp_members */
1819 0, /* tp_getset */
1820 0, /* tp_base */
1821 0, /* tp_dict */
1822 0, /* tp_descr_get */
1823 0, /* tp_descr_set */
1824 0, /* tp_dictoffset */
1825 encoder_init, /* tp_init */
1826 0, /* tp_alloc */
1827 encoder_new, /* tp_new */
1828 0, /* tp_free */
1829};
1830
1831static PyMethodDef speedups_methods[] = {
1832 {"encode_basestring_ascii",
1833 (PyCFunction)py_encode_basestring_ascii,
1834 METH_O,
1835 pydoc_encode_basestring_ascii},
1836 {"scanstring",
1837 (PyCFunction)py_scanstring,
1838 METH_VARARGS,
1839 pydoc_scanstring},
Christian Heimes90540002008-05-08 14:29:10 +00001840 {NULL, NULL, 0, NULL}
1841};
1842
1843PyDoc_STRVAR(module_doc,
1844"json speedups\n");
1845
Martin v. Löwis1a214512008-06-11 05:26:20 +00001846static struct PyModuleDef jsonmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 PyModuleDef_HEAD_INIT,
1848 "_json",
1849 module_doc,
1850 -1,
1851 speedups_methods,
1852 NULL,
1853 NULL,
1854 NULL,
1855 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001856};
1857
1858PyObject*
1859PyInit__json(void)
Christian Heimes90540002008-05-08 14:29:10 +00001860{
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001861 PyObject *m = PyModule_Create(&jsonmodule);
1862 if (!m)
1863 return NULL;
1864 PyScannerType.tp_new = PyType_GenericNew;
1865 if (PyType_Ready(&PyScannerType) < 0)
1866 goto fail;
1867 PyEncoderType.tp_new = PyType_GenericNew;
1868 if (PyType_Ready(&PyEncoderType) < 0)
1869 goto fail;
1870 Py_INCREF((PyObject*)&PyScannerType);
1871 if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1872 Py_DECREF((PyObject*)&PyScannerType);
1873 goto fail;
1874 }
1875 Py_INCREF((PyObject*)&PyEncoderType);
1876 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1877 Py_DECREF((PyObject*)&PyEncoderType);
1878 goto fail;
1879 }
1880 return m;
1881 fail:
1882 Py_DECREF(m);
1883 return NULL;
Christian Heimes90540002008-05-08 14:29:10 +00001884}