Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 2 | #include "structmember.h" |
| 3 | #if PY_VERSION_HEX < 0x02060000 && !defined(Py_TYPE) |
| 4 | #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) |
| 5 | #endif |
| 6 | #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN) |
| 7 | typedef int Py_ssize_t; |
| 8 | #define PY_SSIZE_T_MAX INT_MAX |
| 9 | #define PY_SSIZE_T_MIN INT_MIN |
| 10 | #define PyInt_FromSsize_t PyInt_FromLong |
| 11 | #define PyInt_AsSsize_t PyInt_AsLong |
| 12 | #endif |
| 13 | #ifndef Py_IS_FINITE |
| 14 | #define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X)) |
| 15 | #endif |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 16 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 17 | #ifdef __GNUC__ |
| 18 | #define UNUSED __attribute__((__unused__)) |
| 19 | #else |
| 20 | #define UNUSED |
| 21 | #endif |
| 22 | |
| 23 | #define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType) |
| 24 | #define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType) |
| 25 | #define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType) |
| 26 | #define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType) |
| 27 | |
| 28 | static PyTypeObject PyScannerType; |
| 29 | static PyTypeObject PyEncoderType; |
| 30 | |
| 31 | typedef struct _PyScannerObject { |
| 32 | PyObject_HEAD |
| 33 | PyObject *strict; |
| 34 | PyObject *object_hook; |
| 35 | PyObject *object_pairs_hook; |
| 36 | PyObject *parse_float; |
| 37 | PyObject *parse_int; |
| 38 | PyObject *parse_constant; |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 39 | PyObject *memo; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 40 | } PyScannerObject; |
| 41 | |
| 42 | static PyMemberDef scanner_members[] = { |
| 43 | {"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"}, |
| 44 | {"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"}, |
| 45 | {"object_pairs_hook", T_OBJECT, offsetof(PyScannerObject, object_pairs_hook), READONLY}, |
| 46 | {"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"}, |
| 47 | {"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "parse_int"}, |
| 48 | {"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READONLY, "parse_constant"}, |
| 49 | {NULL} |
| 50 | }; |
| 51 | |
| 52 | typedef struct _PyEncoderObject { |
| 53 | PyObject_HEAD |
| 54 | PyObject *markers; |
| 55 | PyObject *defaultfn; |
| 56 | PyObject *encoder; |
| 57 | PyObject *indent; |
| 58 | PyObject *key_separator; |
| 59 | PyObject *item_separator; |
| 60 | PyObject *sort_keys; |
| 61 | PyObject *skipkeys; |
| 62 | int fast_encode; |
| 63 | int allow_nan; |
| 64 | } PyEncoderObject; |
| 65 | |
| 66 | static PyMemberDef encoder_members[] = { |
| 67 | {"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers"}, |
| 68 | {"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "default"}, |
| 69 | {"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder"}, |
| 70 | {"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"}, |
| 71 | {"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READONLY, "key_separator"}, |
| 72 | {"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READONLY, "item_separator"}, |
| 73 | {"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sort_keys"}, |
| 74 | {"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipkeys"}, |
| 75 | {NULL} |
| 76 | }; |
| 77 | |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 78 | static PyObject * |
| 79 | join_list_unicode(PyObject *lst) |
| 80 | { |
| 81 | /* return u''.join(lst) */ |
| 82 | static PyObject *sep = NULL; |
| 83 | if (sep == NULL) { |
| 84 | sep = PyUnicode_FromStringAndSize("", 0); |
| 85 | if (sep == NULL) |
| 86 | return NULL; |
| 87 | } |
| 88 | return PyUnicode_Join(sep, lst); |
| 89 | } |
| 90 | |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 91 | /* Forward decls */ |
| 92 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 93 | static PyObject * |
| 94 | ascii_escape_unicode(PyObject *pystr); |
| 95 | static PyObject * |
| 96 | py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr); |
| 97 | void init_json(void); |
| 98 | static PyObject * |
| 99 | scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr); |
| 100 | static PyObject * |
| 101 | _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx); |
| 102 | static PyObject * |
| 103 | scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 104 | static int |
| 105 | scanner_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 106 | static void |
| 107 | scanner_dealloc(PyObject *self); |
| 108 | static int |
| 109 | scanner_clear(PyObject *self); |
| 110 | static PyObject * |
| 111 | encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 112 | static int |
| 113 | encoder_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 114 | static void |
| 115 | encoder_dealloc(PyObject *self); |
| 116 | static int |
| 117 | encoder_clear(PyObject *self); |
| 118 | static int |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 119 | encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc, PyObject *seq, Py_ssize_t indent_level); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 120 | static int |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 121 | encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, PyObject *obj, Py_ssize_t indent_level); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 122 | static int |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 123 | encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, PyObject *dct, Py_ssize_t indent_level); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 124 | static PyObject * |
Hirokazu Yamamoto | fecf5d1 | 2009-05-02 15:55:19 +0000 | [diff] [blame] | 125 | _encoded_const(PyObject *obj); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 126 | static void |
| 127 | raise_errmsg(char *msg, PyObject *s, Py_ssize_t end); |
| 128 | static PyObject * |
| 129 | encoder_encode_string(PyEncoderObject *s, PyObject *obj); |
| 130 | static int |
| 131 | _convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr); |
| 132 | static PyObject * |
| 133 | _convertPyInt_FromSsize_t(Py_ssize_t *size_ptr); |
| 134 | static PyObject * |
| 135 | encoder_encode_float(PyEncoderObject *s, PyObject *obj); |
| 136 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 137 | #define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"') |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 138 | #define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r')) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 139 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 140 | static int |
| 141 | _convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 142 | { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 143 | /* PyObject to Py_ssize_t converter */ |
| 144 | *size_ptr = PyLong_AsSsize_t(o); |
Georg Brandl | 5968205 | 2009-05-05 07:52:05 +0000 | [diff] [blame] | 145 | if (*size_ptr == -1 && PyErr_Occurred()) |
| 146 | return 0; |
| 147 | return 1; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static PyObject * |
| 151 | _convertPyInt_FromSsize_t(Py_ssize_t *size_ptr) |
| 152 | { |
| 153 | /* Py_ssize_t to PyObject converter */ |
| 154 | return PyLong_FromSsize_t(*size_ptr); |
| 155 | } |
| 156 | |
| 157 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 158 | ascii_escape_unichar(Py_UCS4 c, unsigned char *output, Py_ssize_t chars) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 159 | { |
| 160 | /* Escape unicode code point c to ASCII escape sequences |
| 161 | in char *output. output must have at least 12 bytes unused to |
| 162 | accommodate an escaped surrogate pair "\uXXXX\uXXXX" */ |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 163 | output[chars++] = '\\'; |
| 164 | switch (c) { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 165 | case '\\': output[chars++] = c; break; |
| 166 | case '"': output[chars++] = c; break; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 167 | case '\b': output[chars++] = 'b'; break; |
| 168 | case '\f': output[chars++] = 'f'; break; |
| 169 | case '\n': output[chars++] = 'n'; break; |
| 170 | case '\r': output[chars++] = 'r'; break; |
| 171 | case '\t': output[chars++] = 't'; break; |
| 172 | default: |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 173 | if (c >= 0x10000) { |
| 174 | /* UTF-16 surrogate pair */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 175 | Py_UCS4 v = c - 0x10000; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 176 | c = 0xd800 | ((v >> 10) & 0x3ff); |
| 177 | output[chars++] = 'u'; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 178 | output[chars++] = "0123456789abcdef"[(c >> 12) & 0xf]; |
| 179 | output[chars++] = "0123456789abcdef"[(c >> 8) & 0xf]; |
| 180 | output[chars++] = "0123456789abcdef"[(c >> 4) & 0xf]; |
| 181 | output[chars++] = "0123456789abcdef"[(c ) & 0xf]; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 182 | c = 0xdc00 | (v & 0x3ff); |
| 183 | output[chars++] = '\\'; |
| 184 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 185 | output[chars++] = 'u'; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 186 | output[chars++] = "0123456789abcdef"[(c >> 12) & 0xf]; |
| 187 | output[chars++] = "0123456789abcdef"[(c >> 8) & 0xf]; |
| 188 | output[chars++] = "0123456789abcdef"[(c >> 4) & 0xf]; |
| 189 | output[chars++] = "0123456789abcdef"[(c ) & 0xf]; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 190 | } |
| 191 | return chars; |
| 192 | } |
| 193 | |
| 194 | static PyObject * |
| 195 | ascii_escape_unicode(PyObject *pystr) |
| 196 | { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 197 | /* Take a PyUnicode pystr and return a new ASCII-only escaped PyUnicode */ |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 198 | Py_ssize_t i; |
| 199 | Py_ssize_t input_chars; |
| 200 | Py_ssize_t output_size; |
| 201 | Py_ssize_t chars; |
| 202 | PyObject *rval; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 203 | void *input; |
| 204 | unsigned char *output; |
| 205 | int kind; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 206 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 207 | if (PyUnicode_READY(pystr) == -1) |
| 208 | return NULL; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 209 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 210 | input_chars = PyUnicode_GET_LENGTH(pystr); |
| 211 | input = PyUnicode_DATA(pystr); |
| 212 | kind = PyUnicode_KIND(pystr); |
| 213 | |
| 214 | /* Compute the output size */ |
| 215 | for (i = 0, output_size = 2; i < input_chars; i++) { |
| 216 | Py_UCS4 c = PyUnicode_READ(kind, input, i); |
| 217 | if (S_CHAR(c)) |
| 218 | output_size++; |
| 219 | else { |
| 220 | switch(c) { |
| 221 | case '\\': case '"': case '\b': case '\f': |
| 222 | case '\n': case '\r': case '\t': |
| 223 | output_size += 2; break; |
| 224 | default: |
| 225 | output_size += c >= 0x10000 ? 12 : 6; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | rval = PyUnicode_New(output_size, 127); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 231 | if (rval == NULL) { |
| 232 | return NULL; |
| 233 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 234 | output = PyUnicode_1BYTE_DATA(rval); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 235 | chars = 0; |
| 236 | output[chars++] = '"'; |
| 237 | for (i = 0; i < input_chars; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 238 | Py_UCS4 c = PyUnicode_READ(kind, input, i); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 239 | if (S_CHAR(c)) { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 240 | output[chars++] = c; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 241 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 242 | else { |
| 243 | chars = ascii_escape_unichar(c, output, chars); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 244 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 245 | } |
| 246 | output[chars++] = '"'; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 247 | return rval; |
| 248 | } |
| 249 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 250 | static void |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 251 | raise_errmsg(char *msg, PyObject *s, Py_ssize_t end) |
| 252 | { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 253 | /* Use the Python function json.decoder.errmsg to raise a nice |
| 254 | looking ValueError exception */ |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 255 | static PyObject *errmsg_fn = NULL; |
| 256 | PyObject *pymsg; |
| 257 | if (errmsg_fn == NULL) { |
| 258 | PyObject *decoder = PyImport_ImportModule("json.decoder"); |
| 259 | if (decoder == NULL) |
| 260 | return; |
| 261 | errmsg_fn = PyObject_GetAttrString(decoder, "errmsg"); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 262 | Py_DECREF(decoder); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 263 | if (errmsg_fn == NULL) |
| 264 | return; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 265 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 266 | pymsg = PyObject_CallFunction(errmsg_fn, "(zOO&)", msg, s, _convertPyInt_FromSsize_t, &end); |
Benjamin Peterson | a13d475 | 2008-10-16 21:17:24 +0000 | [diff] [blame] | 267 | if (pymsg) { |
| 268 | PyErr_SetObject(PyExc_ValueError, pymsg); |
| 269 | Py_DECREF(pymsg); |
| 270 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static PyObject * |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 274 | _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) { |
| 275 | /* return (rval, idx) tuple, stealing reference to rval */ |
| 276 | PyObject *tpl; |
| 277 | PyObject *pyidx; |
| 278 | /* |
| 279 | steal a reference to rval, returns (rval, idx) |
| 280 | */ |
| 281 | if (rval == NULL) { |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 282 | return NULL; |
| 283 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 284 | pyidx = PyLong_FromSsize_t(idx); |
| 285 | if (pyidx == NULL) { |
| 286 | Py_DECREF(rval); |
| 287 | return NULL; |
| 288 | } |
| 289 | tpl = PyTuple_New(2); |
| 290 | if (tpl == NULL) { |
| 291 | Py_DECREF(pyidx); |
| 292 | Py_DECREF(rval); |
| 293 | return NULL; |
| 294 | } |
| 295 | PyTuple_SET_ITEM(tpl, 0, rval); |
| 296 | PyTuple_SET_ITEM(tpl, 1, pyidx); |
| 297 | return tpl; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 300 | #define APPEND_OLD_CHUNK \ |
| 301 | if (chunk != NULL) { \ |
| 302 | if (chunks == NULL) { \ |
| 303 | chunks = PyList_New(0); \ |
| 304 | if (chunks == NULL) { \ |
| 305 | goto bail; \ |
| 306 | } \ |
| 307 | } \ |
| 308 | if (PyList_Append(chunks, chunk)) { \ |
| 309 | Py_DECREF(chunk); \ |
| 310 | goto bail; \ |
| 311 | } \ |
| 312 | Py_CLEAR(chunk); \ |
| 313 | } |
| 314 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 315 | static PyObject * |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 316 | scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 317 | { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 318 | /* Read the JSON string from PyUnicode pystr. |
| 319 | end is the index of the first character after the quote. |
| 320 | if strict is zero then literal control characters are allowed |
| 321 | *next_end_ptr is a return-by-reference index of the character |
| 322 | after the end quote |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 323 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 324 | Return value is a new PyUnicode |
| 325 | */ |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 326 | PyObject *rval = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 327 | Py_ssize_t len; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 328 | Py_ssize_t begin = end - 1; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 329 | Py_ssize_t next /* = begin */; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 330 | const void *buf; |
| 331 | int kind; |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 332 | PyObject *chunks = NULL; |
| 333 | PyObject *chunk = NULL; |
| 334 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 335 | if (PyUnicode_READY(pystr) == -1) |
| 336 | return 0; |
| 337 | |
| 338 | len = PyUnicode_GET_LENGTH(pystr); |
| 339 | buf = PyUnicode_DATA(pystr); |
| 340 | kind = PyUnicode_KIND(pystr); |
| 341 | |
Benjamin Peterson | 7af6eec | 2008-07-19 22:26:35 +0000 | [diff] [blame] | 342 | if (end < 0 || len <= end) { |
| 343 | PyErr_SetString(PyExc_ValueError, "end is out of bounds"); |
| 344 | goto bail; |
| 345 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 346 | while (1) { |
| 347 | /* Find the end of the string or the next escape */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 348 | Py_UCS4 c = 0; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 349 | for (next = end; next < len; next++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 350 | c = PyUnicode_READ(kind, buf, next); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 351 | if (c == '"' || c == '\\') { |
| 352 | break; |
| 353 | } |
| 354 | else if (strict && c <= 0x1f) { |
Benjamin Peterson | 7af6eec | 2008-07-19 22:26:35 +0000 | [diff] [blame] | 355 | raise_errmsg("Invalid control character at", pystr, next); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 356 | goto bail; |
| 357 | } |
| 358 | } |
| 359 | if (!(c == '"' || c == '\\')) { |
| 360 | raise_errmsg("Unterminated string starting at", pystr, begin); |
| 361 | goto bail; |
| 362 | } |
| 363 | /* Pick up this chunk if it's not zero length */ |
| 364 | if (next != end) { |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 365 | APPEND_OLD_CHUNK |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 366 | chunk = PyUnicode_FromKindAndData( |
| 367 | kind, |
| 368 | (char*)buf + PyUnicode_KIND_SIZE(kind, end), |
| 369 | next - end); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 370 | if (chunk == NULL) { |
| 371 | goto bail; |
| 372 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 373 | } |
| 374 | next++; |
| 375 | if (c == '"') { |
| 376 | end = next; |
| 377 | break; |
| 378 | } |
| 379 | if (next == len) { |
| 380 | raise_errmsg("Unterminated string starting at", pystr, begin); |
| 381 | goto bail; |
| 382 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 383 | c = PyUnicode_READ(kind, buf, next); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 384 | if (c != 'u') { |
| 385 | /* Non-unicode backslash escapes */ |
| 386 | end = next + 1; |
| 387 | switch (c) { |
| 388 | case '"': break; |
| 389 | case '\\': break; |
| 390 | case '/': break; |
| 391 | case 'b': c = '\b'; break; |
| 392 | case 'f': c = '\f'; break; |
| 393 | case 'n': c = '\n'; break; |
| 394 | case 'r': c = '\r'; break; |
| 395 | case 't': c = '\t'; break; |
| 396 | default: c = 0; |
| 397 | } |
| 398 | if (c == 0) { |
| 399 | raise_errmsg("Invalid \\escape", pystr, end - 2); |
| 400 | goto bail; |
| 401 | } |
| 402 | } |
| 403 | else { |
| 404 | c = 0; |
| 405 | next++; |
| 406 | end = next + 4; |
| 407 | if (end >= len) { |
| 408 | raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1); |
| 409 | goto bail; |
| 410 | } |
| 411 | /* Decode 4 hex digits */ |
| 412 | for (; next < end; next++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 413 | Py_UCS4 digit = PyUnicode_READ(kind, buf, next); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 414 | c <<= 4; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 415 | switch (digit) { |
| 416 | case '0': case '1': case '2': case '3': case '4': |
| 417 | case '5': case '6': case '7': case '8': case '9': |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 418 | c |= (digit - '0'); break; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 419 | case 'a': case 'b': case 'c': case 'd': case 'e': |
| 420 | case 'f': |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 421 | c |= (digit - 'a' + 10); break; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 422 | case 'A': case 'B': case 'C': case 'D': case 'E': |
| 423 | case 'F': |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 424 | c |= (digit - 'A' + 10); break; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 425 | default: |
| 426 | raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); |
| 427 | goto bail; |
| 428 | } |
| 429 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 430 | /* Surrogate pair */ |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 431 | if ((c & 0xfc00) == 0xd800) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 432 | Py_UCS4 c2 = 0; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 433 | if (end + 6 >= len) { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 434 | raise_errmsg("Unpaired high surrogate", pystr, end - 5); |
| 435 | goto bail; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 436 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 437 | if (PyUnicode_READ(kind, buf, next++) != '\\' || |
| 438 | PyUnicode_READ(kind, buf, next++) != 'u') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 439 | raise_errmsg("Unpaired high surrogate", pystr, end - 5); |
| 440 | goto bail; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 441 | } |
| 442 | end += 6; |
| 443 | /* Decode 4 hex digits */ |
| 444 | for (; next < end; next++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 445 | Py_UCS4 digit = PyUnicode_READ(kind, buf, next); |
Antoine Pitrou | 5b0e9e8 | 2010-10-09 15:24:28 +0000 | [diff] [blame] | 446 | c2 <<= 4; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 447 | switch (digit) { |
| 448 | case '0': case '1': case '2': case '3': case '4': |
| 449 | case '5': case '6': case '7': case '8': case '9': |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 450 | c2 |= (digit - '0'); break; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 451 | case 'a': case 'b': case 'c': case 'd': case 'e': |
| 452 | case 'f': |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 453 | c2 |= (digit - 'a' + 10); break; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 454 | case 'A': case 'B': case 'C': case 'D': case 'E': |
| 455 | case 'F': |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 456 | c2 |= (digit - 'A' + 10); break; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 457 | default: |
| 458 | raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); |
| 459 | goto bail; |
| 460 | } |
| 461 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 462 | if ((c2 & 0xfc00) != 0xdc00) { |
| 463 | raise_errmsg("Unpaired high surrogate", pystr, end - 5); |
| 464 | goto bail; |
| 465 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 466 | c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00)); |
| 467 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 468 | else if ((c & 0xfc00) == 0xdc00) { |
| 469 | raise_errmsg("Unpaired low surrogate", pystr, end - 5); |
| 470 | goto bail; |
| 471 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 472 | } |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 473 | APPEND_OLD_CHUNK |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 474 | chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 475 | if (chunk == NULL) { |
| 476 | goto bail; |
| 477 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 480 | if (chunks == NULL) { |
| 481 | if (chunk != NULL) |
| 482 | rval = chunk; |
| 483 | else |
| 484 | rval = PyUnicode_FromStringAndSize("", 0); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 485 | } |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 486 | else { |
| 487 | APPEND_OLD_CHUNK |
| 488 | rval = join_list_unicode(chunks); |
| 489 | if (rval == NULL) { |
| 490 | goto bail; |
| 491 | } |
| 492 | Py_CLEAR(chunks); |
| 493 | } |
| 494 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 495 | *next_end_ptr = end; |
| 496 | return rval; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 497 | bail: |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 498 | *next_end_ptr = -1; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 499 | Py_XDECREF(chunks); |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 500 | Py_XDECREF(chunk); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 501 | return NULL; |
| 502 | } |
| 503 | |
| 504 | PyDoc_STRVAR(pydoc_scanstring, |
Georg Brandl | c8284cf | 2010-08-02 20:16:18 +0000 | [diff] [blame] | 505 | "scanstring(string, end, strict=True) -> (string, end)\n" |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 506 | "\n" |
| 507 | "Scan the string s for a JSON string. End is the index of the\n" |
| 508 | "character in s after the quote that started the JSON string.\n" |
| 509 | "Unescapes all valid JSON string escape sequences and raises ValueError\n" |
| 510 | "on attempt to decode an invalid string. If strict is False then literal\n" |
| 511 | "control characters are allowed in the string.\n" |
| 512 | "\n" |
| 513 | "Returns a tuple of the decoded string and the index of the character in s\n" |
| 514 | "after the end quote." |
| 515 | ); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 516 | |
| 517 | static PyObject * |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 518 | py_scanstring(PyObject* self UNUSED, PyObject *args) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 519 | { |
| 520 | PyObject *pystr; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 521 | PyObject *rval; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 522 | Py_ssize_t end; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 523 | Py_ssize_t next_end = -1; |
| 524 | int strict = 1; |
| 525 | if (!PyArg_ParseTuple(args, "OO&|i:scanstring", &pystr, _convertPyInt_AsSsize_t, &end, &strict)) { |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 526 | return NULL; |
| 527 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 528 | if (PyUnicode_Check(pystr)) { |
| 529 | rval = scanstring_unicode(pystr, end, strict, &next_end); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 530 | } |
| 531 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | PyErr_Format(PyExc_TypeError, |
Georg Brandl | c8284cf | 2010-08-02 20:16:18 +0000 | [diff] [blame] | 533 | "first argument must be a string, not %.80s", |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 534 | Py_TYPE(pystr)->tp_name); |
| 535 | return NULL; |
| 536 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 537 | return _build_rval_index_tuple(rval, next_end); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | PyDoc_STRVAR(pydoc_encode_basestring_ascii, |
Georg Brandl | c8284cf | 2010-08-02 20:16:18 +0000 | [diff] [blame] | 541 | "encode_basestring_ascii(string) -> string\n" |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 542 | "\n" |
| 543 | "Return an ASCII-only JSON representation of a Python string" |
| 544 | ); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 545 | |
| 546 | static PyObject * |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 547 | py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 548 | { |
| 549 | PyObject *rval; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 550 | /* Return an ASCII-only JSON representation of a Python string */ |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 551 | /* METH_O */ |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 552 | if (PyUnicode_Check(pystr)) { |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 553 | rval = ascii_escape_unicode(pystr); |
| 554 | } |
| 555 | else { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 556 | PyErr_Format(PyExc_TypeError, |
| 557 | "first argument must be a string, not %.80s", |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 558 | Py_TYPE(pystr)->tp_name); |
| 559 | return NULL; |
| 560 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 561 | return rval; |
| 562 | } |
| 563 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 564 | static void |
| 565 | scanner_dealloc(PyObject *self) |
| 566 | { |
| 567 | /* Deallocate scanner object */ |
| 568 | scanner_clear(self); |
| 569 | Py_TYPE(self)->tp_free(self); |
| 570 | } |
| 571 | |
| 572 | static int |
| 573 | scanner_traverse(PyObject *self, visitproc visit, void *arg) |
| 574 | { |
| 575 | PyScannerObject *s; |
| 576 | assert(PyScanner_Check(self)); |
| 577 | s = (PyScannerObject *)self; |
| 578 | Py_VISIT(s->strict); |
| 579 | Py_VISIT(s->object_hook); |
| 580 | Py_VISIT(s->object_pairs_hook); |
| 581 | Py_VISIT(s->parse_float); |
| 582 | Py_VISIT(s->parse_int); |
| 583 | Py_VISIT(s->parse_constant); |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static int |
| 588 | scanner_clear(PyObject *self) |
| 589 | { |
| 590 | PyScannerObject *s; |
| 591 | assert(PyScanner_Check(self)); |
| 592 | s = (PyScannerObject *)self; |
| 593 | Py_CLEAR(s->strict); |
| 594 | Py_CLEAR(s->object_hook); |
| 595 | Py_CLEAR(s->object_pairs_hook); |
| 596 | Py_CLEAR(s->parse_float); |
| 597 | Py_CLEAR(s->parse_int); |
| 598 | Py_CLEAR(s->parse_constant); |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 599 | Py_CLEAR(s->memo); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static PyObject * |
| 604 | _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) { |
| 605 | /* Read a JSON object from PyUnicode pystr. |
| 606 | idx is the index of the first character after the opening curly brace. |
| 607 | *next_idx_ptr is a return-by-reference index to the first character after |
| 608 | the closing curly brace. |
| 609 | |
| 610 | Returns a new PyObject (usually a dict, but object_hook can change that) |
| 611 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 612 | void *str; |
| 613 | int kind; |
| 614 | Py_ssize_t end_idx; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 615 | PyObject *val = NULL; |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 616 | PyObject *rval = NULL; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 617 | PyObject *key = NULL; |
| 618 | int strict = PyObject_IsTrue(s->strict); |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 619 | int has_pairs_hook = (s->object_pairs_hook != Py_None); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 620 | Py_ssize_t next_idx; |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 621 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 622 | if (PyUnicode_READY(pystr) == -1) |
| 623 | return NULL; |
| 624 | |
| 625 | str = PyUnicode_DATA(pystr); |
| 626 | kind = PyUnicode_KIND(pystr); |
| 627 | end_idx = PyUnicode_GET_LENGTH(pystr) - 1; |
| 628 | |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 629 | if (has_pairs_hook) |
| 630 | rval = PyList_New(0); |
| 631 | else |
| 632 | rval = PyDict_New(); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 633 | if (rval == NULL) |
| 634 | return NULL; |
| 635 | |
| 636 | /* skip whitespace after { */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 637 | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 638 | |
| 639 | /* only loop if the object is non-empty */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 640 | if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != '}') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 641 | while (idx <= end_idx) { |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 642 | PyObject *memokey; |
| 643 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 644 | /* read key */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 645 | if (PyUnicode_READ(kind, str, idx) != '"') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 646 | raise_errmsg("Expecting property name", pystr, idx); |
| 647 | goto bail; |
| 648 | } |
| 649 | key = scanstring_unicode(pystr, idx + 1, strict, &next_idx); |
| 650 | if (key == NULL) |
| 651 | goto bail; |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 652 | memokey = PyDict_GetItem(s->memo, key); |
| 653 | if (memokey != NULL) { |
| 654 | Py_INCREF(memokey); |
| 655 | Py_DECREF(key); |
| 656 | key = memokey; |
| 657 | } |
| 658 | else { |
| 659 | if (PyDict_SetItem(s->memo, key, key) < 0) |
| 660 | goto bail; |
| 661 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 662 | idx = next_idx; |
| 663 | |
| 664 | /* skip whitespace between key and : delimiter, read :, skip whitespace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 665 | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
| 666 | if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 667 | raise_errmsg("Expecting : delimiter", pystr, idx); |
| 668 | goto bail; |
| 669 | } |
| 670 | idx++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 671 | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 672 | |
| 673 | /* read any JSON term */ |
| 674 | val = scan_once_unicode(s, pystr, idx, &next_idx); |
| 675 | if (val == NULL) |
| 676 | goto bail; |
| 677 | |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 678 | if (has_pairs_hook) { |
| 679 | PyObject *item = PyTuple_Pack(2, key, val); |
| 680 | if (item == NULL) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 681 | goto bail; |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 682 | Py_CLEAR(key); |
| 683 | Py_CLEAR(val); |
| 684 | if (PyList_Append(rval, item) == -1) { |
| 685 | Py_DECREF(item); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 686 | goto bail; |
| 687 | } |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 688 | Py_DECREF(item); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 689 | } |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 690 | else { |
| 691 | if (PyDict_SetItem(rval, key, val) < 0) |
| 692 | goto bail; |
| 693 | Py_CLEAR(key); |
| 694 | Py_CLEAR(val); |
| 695 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 696 | idx = next_idx; |
| 697 | |
| 698 | /* skip whitespace before } or , */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 699 | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 700 | |
| 701 | /* bail if the object is closed or we didn't get the , delimiter */ |
| 702 | if (idx > end_idx) break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 703 | if (PyUnicode_READ(kind, str, idx) == '}') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 704 | break; |
| 705 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 706 | else if (PyUnicode_READ(kind, str, idx) != ',') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 707 | raise_errmsg("Expecting , delimiter", pystr, idx); |
| 708 | goto bail; |
| 709 | } |
| 710 | idx++; |
| 711 | |
| 712 | /* skip whitespace after , delimiter */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 713 | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
| 717 | /* verify that idx < end_idx, str[idx] should be '}' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 718 | if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 719 | raise_errmsg("Expecting object", pystr, end_idx); |
| 720 | goto bail; |
| 721 | } |
| 722 | |
| 723 | *next_idx_ptr = idx + 1; |
| 724 | |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 725 | if (has_pairs_hook) { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 726 | val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 727 | Py_DECREF(rval); |
| 728 | return val; |
| 729 | } |
| 730 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 731 | /* if object_hook is not None: rval = object_hook(rval) */ |
| 732 | if (s->object_hook != Py_None) { |
| 733 | val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 734 | Py_DECREF(rval); |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 735 | return val; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 736 | } |
| 737 | return rval; |
| 738 | bail: |
| 739 | Py_XDECREF(key); |
| 740 | Py_XDECREF(val); |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 741 | Py_XDECREF(rval); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 742 | return NULL; |
| 743 | } |
| 744 | |
| 745 | static PyObject * |
| 746 | _parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) { |
| 747 | /* Read a JSON array from PyString pystr. |
| 748 | idx is the index of the first character after the opening brace. |
| 749 | *next_idx_ptr is a return-by-reference index to the first character after |
| 750 | the closing brace. |
| 751 | |
| 752 | Returns a new PyList |
| 753 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 754 | void *str; |
| 755 | int kind; |
| 756 | Py_ssize_t end_idx; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 757 | PyObject *val = NULL; |
| 758 | PyObject *rval = PyList_New(0); |
| 759 | Py_ssize_t next_idx; |
| 760 | if (rval == NULL) |
| 761 | return NULL; |
| 762 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 763 | if (PyUnicode_READY(pystr) == -1) |
| 764 | return NULL; |
| 765 | |
| 766 | str = PyUnicode_DATA(pystr); |
| 767 | kind = PyUnicode_KIND(pystr); |
| 768 | end_idx = PyUnicode_GET_LENGTH(pystr) - 1; |
| 769 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 770 | /* skip whitespace after [ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 771 | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 772 | |
| 773 | /* only loop if the array is non-empty */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 774 | if (idx <= end_idx && PyUnicode_READ(kind, str, idx) != ']') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 775 | while (idx <= end_idx) { |
| 776 | |
| 777 | /* read any JSON term */ |
| 778 | val = scan_once_unicode(s, pystr, idx, &next_idx); |
| 779 | if (val == NULL) |
| 780 | goto bail; |
| 781 | |
| 782 | if (PyList_Append(rval, val) == -1) |
| 783 | goto bail; |
| 784 | |
| 785 | Py_CLEAR(val); |
| 786 | idx = next_idx; |
| 787 | |
| 788 | /* skip whitespace between term and , */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 789 | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 790 | |
| 791 | /* bail if the array is closed or we didn't get the , delimiter */ |
| 792 | if (idx > end_idx) break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 793 | if (PyUnicode_READ(kind, str, idx) == ']') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 794 | break; |
| 795 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 796 | else if (PyUnicode_READ(kind, str, idx) != ',') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 797 | raise_errmsg("Expecting , delimiter", pystr, idx); |
| 798 | goto bail; |
| 799 | } |
| 800 | idx++; |
| 801 | |
| 802 | /* skip whitespace after , */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 803 | while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 807 | /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */ |
| 808 | if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 809 | raise_errmsg("Expecting object", pystr, end_idx); |
| 810 | goto bail; |
| 811 | } |
| 812 | *next_idx_ptr = idx + 1; |
| 813 | return rval; |
| 814 | bail: |
| 815 | Py_XDECREF(val); |
| 816 | Py_DECREF(rval); |
| 817 | return NULL; |
| 818 | } |
| 819 | |
| 820 | static PyObject * |
| 821 | _parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) { |
| 822 | /* Read a JSON constant from PyString pystr. |
| 823 | constant is the constant string that was found |
| 824 | ("NaN", "Infinity", "-Infinity"). |
| 825 | idx is the index of the first character of the constant |
| 826 | *next_idx_ptr is a return-by-reference index to the first character after |
| 827 | the constant. |
| 828 | |
| 829 | Returns the result of parse_constant |
| 830 | */ |
| 831 | PyObject *cstr; |
| 832 | PyObject *rval; |
| 833 | /* constant is "NaN", "Infinity", or "-Infinity" */ |
| 834 | cstr = PyUnicode_InternFromString(constant); |
| 835 | if (cstr == NULL) |
| 836 | return NULL; |
| 837 | |
| 838 | /* rval = parse_constant(constant) */ |
| 839 | rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL); |
| 840 | idx += PyUnicode_GET_SIZE(cstr); |
| 841 | Py_DECREF(cstr); |
| 842 | *next_idx_ptr = idx; |
| 843 | return rval; |
| 844 | } |
| 845 | |
| 846 | static PyObject * |
| 847 | _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) { |
| 848 | /* Read a JSON number from PyUnicode pystr. |
| 849 | idx is the index of the first character of the number |
| 850 | *next_idx_ptr is a return-by-reference index to the first character after |
| 851 | the number. |
| 852 | |
| 853 | Returns a new PyObject representation of that number: |
| 854 | PyInt, PyLong, or PyFloat. |
| 855 | May return other types if parse_int or parse_float are set |
| 856 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 857 | void *str; |
| 858 | int kind; |
| 859 | Py_ssize_t end_idx; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 860 | Py_ssize_t idx = start; |
| 861 | int is_float = 0; |
| 862 | PyObject *rval; |
Antoine Pitrou | f645451 | 2011-04-25 19:16:06 +0200 | [diff] [blame] | 863 | PyObject *numstr = NULL; |
| 864 | PyObject *custom_func; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 865 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 866 | if (PyUnicode_READY(pystr) == -1) |
| 867 | return NULL; |
| 868 | |
| 869 | str = PyUnicode_DATA(pystr); |
| 870 | kind = PyUnicode_KIND(pystr); |
| 871 | end_idx = PyUnicode_GET_LENGTH(pystr) - 1; |
| 872 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 873 | /* read a sign if it's there, make sure it's not the end of the string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 874 | if (PyUnicode_READ(kind, str, idx) == '-') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 875 | idx++; |
| 876 | if (idx > end_idx) { |
| 877 | PyErr_SetNone(PyExc_StopIteration); |
| 878 | return NULL; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | /* read as many integer digits as we find as long as it doesn't start with 0 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 883 | if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 884 | idx++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 885 | while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 886 | } |
| 887 | /* if it starts with 0 we only expect one integer digit */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 888 | else if (PyUnicode_READ(kind, str, idx) == '0') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 889 | idx++; |
| 890 | } |
| 891 | /* no integer digits, error */ |
| 892 | else { |
| 893 | PyErr_SetNone(PyExc_StopIteration); |
| 894 | return NULL; |
| 895 | } |
| 896 | |
| 897 | /* if the next char is '.' followed by a digit then read all float digits */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 898 | if (idx < end_idx && PyUnicode_READ(kind, str, idx) == '.' && PyUnicode_READ(kind, str, idx + 1) >= '0' && PyUnicode_READ(kind, str, idx + 1) <= '9') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 899 | is_float = 1; |
| 900 | idx += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 901 | while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 905 | if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 906 | Py_ssize_t e_start = idx; |
| 907 | idx++; |
| 908 | |
| 909 | /* read an exponent sign if present */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 910 | if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 911 | |
| 912 | /* read all digits */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 913 | while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 914 | |
| 915 | /* if we got a digit, then parse as float. if not, backtrack */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 916 | if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 917 | is_float = 1; |
| 918 | } |
| 919 | else { |
| 920 | idx = e_start; |
| 921 | } |
| 922 | } |
| 923 | |
Antoine Pitrou | f645451 | 2011-04-25 19:16:06 +0200 | [diff] [blame] | 924 | if (is_float && s->parse_float != (PyObject *)&PyFloat_Type) |
| 925 | custom_func = s->parse_float; |
| 926 | else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type) |
| 927 | custom_func = s->parse_int; |
| 928 | else |
| 929 | custom_func = NULL; |
| 930 | |
| 931 | if (custom_func) { |
| 932 | /* copy the section we determined to be a number */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 933 | numstr = PyUnicode_FromKindAndData(kind, |
| 934 | (char*)str + PyUnicode_KIND_SIZE(kind, start), |
| 935 | idx - start); |
Antoine Pitrou | f645451 | 2011-04-25 19:16:06 +0200 | [diff] [blame] | 936 | if (numstr == NULL) |
| 937 | return NULL; |
| 938 | rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 939 | } |
| 940 | else { |
Antoine Pitrou | f645451 | 2011-04-25 19:16:06 +0200 | [diff] [blame] | 941 | Py_ssize_t i, n; |
| 942 | char *buf; |
| 943 | /* Straight conversion to ASCII, to avoid costly conversion of |
| 944 | decimal unicode digits (which cannot appear here) */ |
| 945 | n = idx - start; |
| 946 | numstr = PyBytes_FromStringAndSize(NULL, n); |
| 947 | if (numstr == NULL) |
| 948 | return NULL; |
| 949 | buf = PyBytes_AS_STRING(numstr); |
| 950 | for (i = 0; i < n; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 951 | buf[i] = (char) PyUnicode_READ(kind, str, i + start); |
Antoine Pitrou | f645451 | 2011-04-25 19:16:06 +0200 | [diff] [blame] | 952 | } |
| 953 | if (is_float) |
| 954 | rval = PyFloat_FromString(numstr); |
| 955 | else |
| 956 | rval = PyLong_FromString(buf, NULL, 10); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 957 | } |
| 958 | Py_DECREF(numstr); |
| 959 | *next_idx_ptr = idx; |
| 960 | return rval; |
| 961 | } |
| 962 | |
| 963 | static PyObject * |
| 964 | scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) |
| 965 | { |
| 966 | /* Read one JSON term (of any kind) from PyUnicode pystr. |
| 967 | idx is the index of the first character of the term |
| 968 | *next_idx_ptr is a return-by-reference index to the first character after |
| 969 | the number. |
| 970 | |
| 971 | Returns a new PyObject representation of the term. |
| 972 | */ |
Ezio Melotti | 362b951 | 2011-05-07 17:58:09 +0300 | [diff] [blame] | 973 | PyObject *res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 974 | void *str; |
| 975 | int kind; |
| 976 | Py_ssize_t length; |
| 977 | |
| 978 | if (PyUnicode_READY(pystr) == -1) |
| 979 | return NULL; |
| 980 | |
| 981 | str = PyUnicode_DATA(pystr); |
| 982 | kind = PyUnicode_KIND(pystr); |
| 983 | length = PyUnicode_GET_LENGTH(pystr); |
| 984 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 985 | if (idx >= length) { |
| 986 | PyErr_SetNone(PyExc_StopIteration); |
| 987 | return NULL; |
| 988 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 989 | |
| 990 | switch (PyUnicode_READ(kind, str, idx)) { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 991 | case '"': |
| 992 | /* string */ |
| 993 | return scanstring_unicode(pystr, idx + 1, |
| 994 | PyObject_IsTrue(s->strict), |
| 995 | next_idx_ptr); |
| 996 | case '{': |
| 997 | /* object */ |
Ezio Melotti | 362b951 | 2011-05-07 17:58:09 +0300 | [diff] [blame] | 998 | if (Py_EnterRecursiveCall(" while decoding a JSON object " |
| 999 | "from a unicode string")) |
| 1000 | return NULL; |
| 1001 | res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr); |
| 1002 | Py_LeaveRecursiveCall(); |
| 1003 | return res; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1004 | case '[': |
| 1005 | /* array */ |
Ezio Melotti | 362b951 | 2011-05-07 17:58:09 +0300 | [diff] [blame] | 1006 | if (Py_EnterRecursiveCall(" while decoding a JSON array " |
| 1007 | "from a unicode string")) |
| 1008 | return NULL; |
| 1009 | res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr); |
| 1010 | Py_LeaveRecursiveCall(); |
| 1011 | return res; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1012 | case 'n': |
| 1013 | /* null */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1014 | 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 Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1015 | Py_INCREF(Py_None); |
| 1016 | *next_idx_ptr = idx + 4; |
| 1017 | return Py_None; |
| 1018 | } |
| 1019 | break; |
| 1020 | case 't': |
| 1021 | /* true */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1022 | 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 Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1023 | Py_INCREF(Py_True); |
| 1024 | *next_idx_ptr = idx + 4; |
| 1025 | return Py_True; |
| 1026 | } |
| 1027 | break; |
| 1028 | case 'f': |
| 1029 | /* false */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1030 | if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' && |
| 1031 | PyUnicode_READ(kind, str, idx + 2) == 'l' && |
| 1032 | PyUnicode_READ(kind, str, idx + 3) == 's' && |
| 1033 | PyUnicode_READ(kind, str, idx + 4) == 'e') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1034 | Py_INCREF(Py_False); |
| 1035 | *next_idx_ptr = idx + 5; |
| 1036 | return Py_False; |
| 1037 | } |
| 1038 | break; |
| 1039 | case 'N': |
| 1040 | /* NaN */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1041 | if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' && |
| 1042 | PyUnicode_READ(kind, str, idx + 2) == 'N') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1043 | return _parse_constant(s, "NaN", idx, next_idx_ptr); |
| 1044 | } |
| 1045 | break; |
| 1046 | case 'I': |
| 1047 | /* Infinity */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1048 | if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' && |
| 1049 | PyUnicode_READ(kind, str, idx + 2) == 'f' && |
| 1050 | PyUnicode_READ(kind, str, idx + 3) == 'i' && |
| 1051 | PyUnicode_READ(kind, str, idx + 4) == 'n' && |
| 1052 | PyUnicode_READ(kind, str, idx + 5) == 'i' && |
| 1053 | PyUnicode_READ(kind, str, idx + 6) == 't' && |
| 1054 | PyUnicode_READ(kind, str, idx + 7) == 'y') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1055 | return _parse_constant(s, "Infinity", idx, next_idx_ptr); |
| 1056 | } |
| 1057 | break; |
| 1058 | case '-': |
| 1059 | /* -Infinity */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1060 | if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' && |
| 1061 | PyUnicode_READ(kind, str, idx + 2) == 'n' && |
| 1062 | PyUnicode_READ(kind, str, idx + 3) == 'f' && |
| 1063 | PyUnicode_READ(kind, str, idx + 4) == 'i' && |
| 1064 | PyUnicode_READ(kind, str, idx + 5) == 'n' && |
| 1065 | PyUnicode_READ(kind, str, idx + 6) == 'i' && |
| 1066 | PyUnicode_READ(kind, str, idx + 7) == 't' && |
| 1067 | PyUnicode_READ(kind, str, idx + 8) == 'y') { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1068 | return _parse_constant(s, "-Infinity", idx, next_idx_ptr); |
| 1069 | } |
| 1070 | break; |
| 1071 | } |
| 1072 | /* Didn't find a string, object, array, or named constant. Look for a number. */ |
| 1073 | return _match_number_unicode(s, pystr, idx, next_idx_ptr); |
| 1074 | } |
| 1075 | |
| 1076 | static PyObject * |
| 1077 | scanner_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 1078 | { |
| 1079 | /* Python callable interface to scan_once_{str,unicode} */ |
| 1080 | PyObject *pystr; |
| 1081 | PyObject *rval; |
| 1082 | Py_ssize_t idx; |
| 1083 | Py_ssize_t next_idx = -1; |
| 1084 | static char *kwlist[] = {"string", "idx", NULL}; |
| 1085 | PyScannerObject *s; |
| 1086 | assert(PyScanner_Check(self)); |
| 1087 | s = (PyScannerObject *)self; |
| 1088 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:scan_once", kwlist, &pystr, _convertPyInt_AsSsize_t, &idx)) |
| 1089 | return NULL; |
| 1090 | |
| 1091 | if (PyUnicode_Check(pystr)) { |
| 1092 | rval = scan_once_unicode(s, pystr, idx, &next_idx); |
| 1093 | } |
| 1094 | else { |
| 1095 | PyErr_Format(PyExc_TypeError, |
| 1096 | "first argument must be a string, not %.80s", |
| 1097 | Py_TYPE(pystr)->tp_name); |
| 1098 | return NULL; |
| 1099 | } |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 1100 | PyDict_Clear(s->memo); |
| 1101 | if (rval == NULL) |
| 1102 | return NULL; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1103 | return _build_rval_index_tuple(rval, next_idx); |
| 1104 | } |
| 1105 | |
| 1106 | static PyObject * |
| 1107 | scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1108 | { |
| 1109 | PyScannerObject *s; |
| 1110 | s = (PyScannerObject *)type->tp_alloc(type, 0); |
| 1111 | if (s != NULL) { |
| 1112 | s->strict = NULL; |
| 1113 | s->object_hook = NULL; |
| 1114 | s->object_pairs_hook = NULL; |
| 1115 | s->parse_float = NULL; |
| 1116 | s->parse_int = NULL; |
| 1117 | s->parse_constant = NULL; |
| 1118 | } |
| 1119 | return (PyObject *)s; |
| 1120 | } |
| 1121 | |
| 1122 | static int |
| 1123 | scanner_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1124 | { |
| 1125 | /* Initialize Scanner object */ |
| 1126 | PyObject *ctx; |
| 1127 | static char *kwlist[] = {"context", NULL}; |
| 1128 | PyScannerObject *s; |
| 1129 | |
| 1130 | assert(PyScanner_Check(self)); |
| 1131 | s = (PyScannerObject *)self; |
| 1132 | |
| 1133 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx)) |
| 1134 | return -1; |
| 1135 | |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 1136 | if (s->memo == NULL) { |
| 1137 | s->memo = PyDict_New(); |
| 1138 | if (s->memo == NULL) |
| 1139 | goto bail; |
| 1140 | } |
| 1141 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1142 | /* All of these will fail "gracefully" so we don't need to verify them */ |
| 1143 | s->strict = PyObject_GetAttrString(ctx, "strict"); |
| 1144 | if (s->strict == NULL) |
| 1145 | goto bail; |
| 1146 | s->object_hook = PyObject_GetAttrString(ctx, "object_hook"); |
| 1147 | if (s->object_hook == NULL) |
| 1148 | goto bail; |
| 1149 | s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook"); |
| 1150 | if (s->object_pairs_hook == NULL) |
| 1151 | goto bail; |
| 1152 | s->parse_float = PyObject_GetAttrString(ctx, "parse_float"); |
| 1153 | if (s->parse_float == NULL) |
| 1154 | goto bail; |
| 1155 | s->parse_int = PyObject_GetAttrString(ctx, "parse_int"); |
| 1156 | if (s->parse_int == NULL) |
| 1157 | goto bail; |
| 1158 | s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant"); |
| 1159 | if (s->parse_constant == NULL) |
| 1160 | goto bail; |
| 1161 | |
| 1162 | return 0; |
| 1163 | |
| 1164 | bail: |
| 1165 | Py_CLEAR(s->strict); |
| 1166 | Py_CLEAR(s->object_hook); |
| 1167 | Py_CLEAR(s->object_pairs_hook); |
| 1168 | Py_CLEAR(s->parse_float); |
| 1169 | Py_CLEAR(s->parse_int); |
| 1170 | Py_CLEAR(s->parse_constant); |
| 1171 | return -1; |
| 1172 | } |
| 1173 | |
| 1174 | PyDoc_STRVAR(scanner_doc, "JSON scanner object"); |
| 1175 | |
| 1176 | static |
| 1177 | PyTypeObject PyScannerType = { |
| 1178 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1179 | "_json.Scanner", /* tp_name */ |
| 1180 | sizeof(PyScannerObject), /* tp_basicsize */ |
| 1181 | 0, /* tp_itemsize */ |
| 1182 | scanner_dealloc, /* tp_dealloc */ |
| 1183 | 0, /* tp_print */ |
| 1184 | 0, /* tp_getattr */ |
| 1185 | 0, /* tp_setattr */ |
| 1186 | 0, /* tp_compare */ |
| 1187 | 0, /* tp_repr */ |
| 1188 | 0, /* tp_as_number */ |
| 1189 | 0, /* tp_as_sequence */ |
| 1190 | 0, /* tp_as_mapping */ |
| 1191 | 0, /* tp_hash */ |
| 1192 | scanner_call, /* tp_call */ |
| 1193 | 0, /* tp_str */ |
| 1194 | 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */ |
| 1195 | 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */ |
| 1196 | 0, /* tp_as_buffer */ |
| 1197 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 1198 | scanner_doc, /* tp_doc */ |
| 1199 | scanner_traverse, /* tp_traverse */ |
| 1200 | scanner_clear, /* tp_clear */ |
| 1201 | 0, /* tp_richcompare */ |
| 1202 | 0, /* tp_weaklistoffset */ |
| 1203 | 0, /* tp_iter */ |
| 1204 | 0, /* tp_iternext */ |
| 1205 | 0, /* tp_methods */ |
| 1206 | scanner_members, /* tp_members */ |
| 1207 | 0, /* tp_getset */ |
| 1208 | 0, /* tp_base */ |
| 1209 | 0, /* tp_dict */ |
| 1210 | 0, /* tp_descr_get */ |
| 1211 | 0, /* tp_descr_set */ |
| 1212 | 0, /* tp_dictoffset */ |
| 1213 | scanner_init, /* tp_init */ |
| 1214 | 0,/* PyType_GenericAlloc, */ /* tp_alloc */ |
| 1215 | scanner_new, /* tp_new */ |
| 1216 | 0,/* PyObject_GC_Del, */ /* tp_free */ |
| 1217 | }; |
| 1218 | |
| 1219 | static PyObject * |
| 1220 | encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1221 | { |
| 1222 | PyEncoderObject *s; |
| 1223 | s = (PyEncoderObject *)type->tp_alloc(type, 0); |
| 1224 | if (s != NULL) { |
| 1225 | s->markers = NULL; |
| 1226 | s->defaultfn = NULL; |
| 1227 | s->encoder = NULL; |
| 1228 | s->indent = NULL; |
| 1229 | s->key_separator = NULL; |
| 1230 | s->item_separator = NULL; |
| 1231 | s->sort_keys = NULL; |
| 1232 | s->skipkeys = NULL; |
| 1233 | } |
| 1234 | return (PyObject *)s; |
| 1235 | } |
| 1236 | |
| 1237 | static int |
| 1238 | encoder_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1239 | { |
| 1240 | /* initialize Encoder object */ |
| 1241 | static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL}; |
| 1242 | |
| 1243 | PyEncoderObject *s; |
Antoine Pitrou | 781eba7 | 2009-12-08 15:57:31 +0000 | [diff] [blame] | 1244 | PyObject *markers, *defaultfn, *encoder, *indent, *key_separator; |
| 1245 | PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1246 | |
| 1247 | assert(PyEncoder_Check(self)); |
| 1248 | s = (PyEncoderObject *)self; |
| 1249 | |
| 1250 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist, |
Antoine Pitrou | 781eba7 | 2009-12-08 15:57:31 +0000 | [diff] [blame] | 1251 | &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator, |
| 1252 | &sort_keys, &skipkeys, &allow_nan)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1253 | return -1; |
| 1254 | |
Antoine Pitrou | 781eba7 | 2009-12-08 15:57:31 +0000 | [diff] [blame] | 1255 | s->markers = markers; |
| 1256 | s->defaultfn = defaultfn; |
| 1257 | s->encoder = encoder; |
| 1258 | s->indent = indent; |
| 1259 | s->key_separator = key_separator; |
| 1260 | s->item_separator = item_separator; |
| 1261 | s->sort_keys = sort_keys; |
| 1262 | s->skipkeys = skipkeys; |
| 1263 | s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii); |
| 1264 | s->allow_nan = PyObject_IsTrue(allow_nan); |
| 1265 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1266 | Py_INCREF(s->markers); |
| 1267 | Py_INCREF(s->defaultfn); |
| 1268 | Py_INCREF(s->encoder); |
| 1269 | Py_INCREF(s->indent); |
| 1270 | Py_INCREF(s->key_separator); |
| 1271 | Py_INCREF(s->item_separator); |
| 1272 | Py_INCREF(s->sort_keys); |
| 1273 | Py_INCREF(s->skipkeys); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static PyObject * |
| 1278 | encoder_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 1279 | { |
| 1280 | /* Python callable interface to encode_listencode_obj */ |
| 1281 | static char *kwlist[] = {"obj", "_current_indent_level", NULL}; |
| 1282 | PyObject *obj; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1283 | Py_ssize_t indent_level; |
| 1284 | PyEncoderObject *s; |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1285 | _PyAccu acc; |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1286 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1287 | assert(PyEncoder_Check(self)); |
| 1288 | s = (PyEncoderObject *)self; |
| 1289 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:_iterencode", kwlist, |
| 1290 | &obj, _convertPyInt_AsSsize_t, &indent_level)) |
| 1291 | return NULL; |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1292 | if (_PyAccu_Init(&acc)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1293 | return NULL; |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1294 | if (encoder_listencode_obj(s, &acc, obj, indent_level)) { |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1295 | _PyAccu_Destroy(&acc); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1296 | return NULL; |
| 1297 | } |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1298 | return _PyAccu_FinishAsList(&acc); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | static PyObject * |
| 1302 | _encoded_const(PyObject *obj) |
| 1303 | { |
| 1304 | /* Return the JSON string representation of None, True, False */ |
| 1305 | if (obj == Py_None) { |
| 1306 | static PyObject *s_null = NULL; |
| 1307 | if (s_null == NULL) { |
| 1308 | s_null = PyUnicode_InternFromString("null"); |
| 1309 | } |
| 1310 | Py_INCREF(s_null); |
| 1311 | return s_null; |
| 1312 | } |
| 1313 | else if (obj == Py_True) { |
| 1314 | static PyObject *s_true = NULL; |
| 1315 | if (s_true == NULL) { |
| 1316 | s_true = PyUnicode_InternFromString("true"); |
| 1317 | } |
| 1318 | Py_INCREF(s_true); |
| 1319 | return s_true; |
| 1320 | } |
| 1321 | else if (obj == Py_False) { |
| 1322 | static PyObject *s_false = NULL; |
| 1323 | if (s_false == NULL) { |
| 1324 | s_false = PyUnicode_InternFromString("false"); |
| 1325 | } |
| 1326 | Py_INCREF(s_false); |
| 1327 | return s_false; |
| 1328 | } |
| 1329 | else { |
| 1330 | PyErr_SetString(PyExc_ValueError, "not a const"); |
| 1331 | return NULL; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | static PyObject * |
| 1336 | encoder_encode_float(PyEncoderObject *s, PyObject *obj) |
| 1337 | { |
| 1338 | /* Return the JSON representation of a PyFloat */ |
| 1339 | double i = PyFloat_AS_DOUBLE(obj); |
| 1340 | if (!Py_IS_FINITE(i)) { |
| 1341 | if (!s->allow_nan) { |
| 1342 | PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant"); |
| 1343 | return NULL; |
| 1344 | } |
| 1345 | if (i > 0) { |
| 1346 | return PyUnicode_FromString("Infinity"); |
| 1347 | } |
| 1348 | else if (i < 0) { |
| 1349 | return PyUnicode_FromString("-Infinity"); |
| 1350 | } |
| 1351 | else { |
| 1352 | return PyUnicode_FromString("NaN"); |
| 1353 | } |
| 1354 | } |
| 1355 | /* Use a better float format here? */ |
| 1356 | return PyObject_Repr(obj); |
| 1357 | } |
| 1358 | |
| 1359 | static PyObject * |
| 1360 | encoder_encode_string(PyEncoderObject *s, PyObject *obj) |
| 1361 | { |
| 1362 | /* Return the JSON representation of a string */ |
| 1363 | if (s->fast_encode) |
| 1364 | return py_encode_basestring_ascii(NULL, obj); |
| 1365 | else |
| 1366 | return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL); |
| 1367 | } |
| 1368 | |
| 1369 | static int |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1370 | _steal_accumulate(_PyAccu *acc, PyObject *stolen) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1371 | { |
| 1372 | /* Append stolen and then decrement its reference count */ |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1373 | int rval = _PyAccu_Accumulate(acc, stolen); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1374 | Py_DECREF(stolen); |
| 1375 | return rval; |
| 1376 | } |
| 1377 | |
| 1378 | static int |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1379 | encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1380 | PyObject *obj, Py_ssize_t indent_level) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1381 | { |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1382 | /* Encode Python object obj to a JSON term */ |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1383 | PyObject *newobj; |
| 1384 | int rv; |
| 1385 | |
| 1386 | if (obj == Py_None || obj == Py_True || obj == Py_False) { |
| 1387 | PyObject *cstr = _encoded_const(obj); |
| 1388 | if (cstr == NULL) |
| 1389 | return -1; |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1390 | return _steal_accumulate(acc, cstr); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1391 | } |
| 1392 | else if (PyUnicode_Check(obj)) |
| 1393 | { |
| 1394 | PyObject *encoded = encoder_encode_string(s, obj); |
| 1395 | if (encoded == NULL) |
| 1396 | return -1; |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1397 | return _steal_accumulate(acc, encoded); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1398 | } |
| 1399 | else if (PyLong_Check(obj)) { |
| 1400 | PyObject *encoded = PyObject_Str(obj); |
| 1401 | if (encoded == NULL) |
| 1402 | return -1; |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1403 | return _steal_accumulate(acc, encoded); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1404 | } |
| 1405 | else if (PyFloat_Check(obj)) { |
| 1406 | PyObject *encoded = encoder_encode_float(s, obj); |
| 1407 | if (encoded == NULL) |
| 1408 | return -1; |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1409 | return _steal_accumulate(acc, encoded); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1410 | } |
| 1411 | else if (PyList_Check(obj) || PyTuple_Check(obj)) { |
Ezio Melotti | 1367265 | 2011-05-11 01:02:56 +0300 | [diff] [blame] | 1412 | if (Py_EnterRecursiveCall(" while encoding a JSON object")) |
| 1413 | return -1; |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1414 | rv = encoder_listencode_list(s, acc, obj, indent_level); |
Ezio Melotti | 1367265 | 2011-05-11 01:02:56 +0300 | [diff] [blame] | 1415 | Py_LeaveRecursiveCall(); |
| 1416 | return rv; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1417 | } |
| 1418 | else if (PyDict_Check(obj)) { |
Ezio Melotti | 1367265 | 2011-05-11 01:02:56 +0300 | [diff] [blame] | 1419 | if (Py_EnterRecursiveCall(" while encoding a JSON object")) |
| 1420 | return -1; |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1421 | rv = encoder_listencode_dict(s, acc, obj, indent_level); |
Ezio Melotti | 1367265 | 2011-05-11 01:02:56 +0300 | [diff] [blame] | 1422 | Py_LeaveRecursiveCall(); |
| 1423 | return rv; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1424 | } |
| 1425 | else { |
| 1426 | PyObject *ident = NULL; |
| 1427 | if (s->markers != Py_None) { |
| 1428 | int has_key; |
| 1429 | ident = PyLong_FromVoidPtr(obj); |
| 1430 | if (ident == NULL) |
| 1431 | return -1; |
| 1432 | has_key = PyDict_Contains(s->markers, ident); |
| 1433 | if (has_key) { |
| 1434 | if (has_key != -1) |
| 1435 | PyErr_SetString(PyExc_ValueError, "Circular reference detected"); |
| 1436 | Py_DECREF(ident); |
| 1437 | return -1; |
| 1438 | } |
| 1439 | if (PyDict_SetItem(s->markers, ident, obj)) { |
| 1440 | Py_DECREF(ident); |
| 1441 | return -1; |
| 1442 | } |
| 1443 | } |
| 1444 | newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL); |
| 1445 | if (newobj == NULL) { |
| 1446 | Py_XDECREF(ident); |
| 1447 | return -1; |
| 1448 | } |
Ezio Melotti | 1367265 | 2011-05-11 01:02:56 +0300 | [diff] [blame] | 1449 | |
| 1450 | if (Py_EnterRecursiveCall(" while encoding a JSON object")) |
| 1451 | return -1; |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1452 | rv = encoder_listencode_obj(s, acc, newobj, indent_level); |
Ezio Melotti | 1367265 | 2011-05-11 01:02:56 +0300 | [diff] [blame] | 1453 | Py_LeaveRecursiveCall(); |
| 1454 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1455 | Py_DECREF(newobj); |
| 1456 | if (rv) { |
| 1457 | Py_XDECREF(ident); |
| 1458 | return -1; |
| 1459 | } |
| 1460 | if (ident != NULL) { |
| 1461 | if (PyDict_DelItem(s->markers, ident)) { |
| 1462 | Py_XDECREF(ident); |
| 1463 | return -1; |
| 1464 | } |
| 1465 | Py_XDECREF(ident); |
| 1466 | } |
| 1467 | return rv; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | static int |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1472 | encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1473 | PyObject *dct, Py_ssize_t indent_level) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1474 | { |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1475 | /* Encode Python dict dct a JSON term */ |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1476 | static PyObject *open_dict = NULL; |
| 1477 | static PyObject *close_dict = NULL; |
| 1478 | static PyObject *empty_dict = NULL; |
| 1479 | PyObject *kstr = NULL; |
| 1480 | PyObject *ident = NULL; |
Raymond Hettinger | c8d952d | 2009-05-27 06:50:31 +0000 | [diff] [blame] | 1481 | PyObject *it = NULL; |
Raymond Hettinger | bcf6f92 | 2009-05-27 09:58:34 +0000 | [diff] [blame] | 1482 | PyObject *items; |
| 1483 | PyObject *item = NULL; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1484 | int skipkeys; |
| 1485 | Py_ssize_t idx; |
| 1486 | |
| 1487 | if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) { |
| 1488 | open_dict = PyUnicode_InternFromString("{"); |
| 1489 | close_dict = PyUnicode_InternFromString("}"); |
| 1490 | empty_dict = PyUnicode_InternFromString("{}"); |
| 1491 | if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) |
| 1492 | return -1; |
| 1493 | } |
Raymond Hettinger | c8d952d | 2009-05-27 06:50:31 +0000 | [diff] [blame] | 1494 | if (Py_SIZE(dct) == 0) |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1495 | return _PyAccu_Accumulate(acc, empty_dict); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1496 | |
| 1497 | if (s->markers != Py_None) { |
| 1498 | int has_key; |
| 1499 | ident = PyLong_FromVoidPtr(dct); |
| 1500 | if (ident == NULL) |
| 1501 | goto bail; |
| 1502 | has_key = PyDict_Contains(s->markers, ident); |
| 1503 | if (has_key) { |
| 1504 | if (has_key != -1) |
| 1505 | PyErr_SetString(PyExc_ValueError, "Circular reference detected"); |
| 1506 | goto bail; |
| 1507 | } |
| 1508 | if (PyDict_SetItem(s->markers, ident, dct)) { |
| 1509 | goto bail; |
| 1510 | } |
| 1511 | } |
| 1512 | |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1513 | if (_PyAccu_Accumulate(acc, open_dict)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1514 | goto bail; |
| 1515 | |
| 1516 | if (s->indent != Py_None) { |
| 1517 | /* TODO: DOES NOT RUN */ |
| 1518 | indent_level += 1; |
| 1519 | /* |
| 1520 | newline_indent = '\n' + (' ' * (_indent * _current_indent_level)) |
| 1521 | separator = _item_separator + newline_indent |
| 1522 | buf += newline_indent |
| 1523 | */ |
| 1524 | } |
| 1525 | |
Raymond Hettinger | bcf6f92 | 2009-05-27 09:58:34 +0000 | [diff] [blame] | 1526 | if (PyObject_IsTrue(s->sort_keys)) { |
Antoine Pitrou | 2397dd5 | 2010-11-04 16:51:32 +0000 | [diff] [blame] | 1527 | /* First sort the keys then replace them with (key, value) tuples. */ |
| 1528 | Py_ssize_t i, nitems; |
| 1529 | items = PyMapping_Keys(dct); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1530 | if (items == NULL) |
Antoine Pitrou | 2397dd5 | 2010-11-04 16:51:32 +0000 | [diff] [blame] | 1531 | goto bail; |
| 1532 | if (!PyList_Check(items)) { |
| 1533 | PyErr_SetString(PyExc_ValueError, "keys must return list"); |
| 1534 | goto bail; |
| 1535 | } |
| 1536 | if (PyList_Sort(items) < 0) |
| 1537 | goto bail; |
| 1538 | nitems = PyList_GET_SIZE(items); |
| 1539 | for (i = 0; i < nitems; i++) { |
| 1540 | PyObject *key, *value; |
| 1541 | key = PyList_GET_ITEM(items, i); |
| 1542 | value = PyDict_GetItem(dct, key); |
| 1543 | item = PyTuple_Pack(2, key, value); |
| 1544 | if (item == NULL) |
| 1545 | goto bail; |
| 1546 | PyList_SET_ITEM(items, i, item); |
| 1547 | Py_DECREF(key); |
| 1548 | } |
| 1549 | } |
| 1550 | else { |
| 1551 | items = PyMapping_Items(dct); |
| 1552 | } |
| 1553 | if (items == NULL) |
Raymond Hettinger | 491a4cb | 2009-05-27 11:19:02 +0000 | [diff] [blame] | 1554 | goto bail; |
Raymond Hettinger | bcf6f92 | 2009-05-27 09:58:34 +0000 | [diff] [blame] | 1555 | it = PyObject_GetIter(items); |
Antoine Pitrou | 2397dd5 | 2010-11-04 16:51:32 +0000 | [diff] [blame] | 1556 | Py_DECREF(items); |
| 1557 | if (it == NULL) |
Raymond Hettinger | c8d952d | 2009-05-27 06:50:31 +0000 | [diff] [blame] | 1558 | goto bail; |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1559 | skipkeys = PyObject_IsTrue(s->skipkeys); |
| 1560 | idx = 0; |
Raymond Hettinger | bcf6f92 | 2009-05-27 09:58:34 +0000 | [diff] [blame] | 1561 | while ((item = PyIter_Next(it)) != NULL) { |
| 1562 | PyObject *encoded, *key, *value; |
| 1563 | if (!PyTuple_Check(item) || Py_SIZE(item) != 2) { |
| 1564 | PyErr_SetString(PyExc_ValueError, "items must return 2-tuples"); |
| 1565 | goto bail; |
| 1566 | } |
| 1567 | key = PyTuple_GET_ITEM(item, 0); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1568 | if (PyUnicode_Check(key)) { |
| 1569 | Py_INCREF(key); |
| 1570 | kstr = key; |
| 1571 | } |
| 1572 | else if (PyFloat_Check(key)) { |
| 1573 | kstr = encoder_encode_float(s, key); |
| 1574 | if (kstr == NULL) |
| 1575 | goto bail; |
| 1576 | } |
Raymond Hettinger | bcf6f92 | 2009-05-27 09:58:34 +0000 | [diff] [blame] | 1577 | else if (key == Py_True || key == Py_False || key == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1578 | /* This must come before the PyLong_Check because |
| 1579 | True and False are also 1 and 0.*/ |
Raymond Hettinger | bcf6f92 | 2009-05-27 09:58:34 +0000 | [diff] [blame] | 1580 | kstr = _encoded_const(key); |
| 1581 | if (kstr == NULL) |
| 1582 | goto bail; |
| 1583 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1584 | else if (PyLong_Check(key)) { |
| 1585 | kstr = PyObject_Str(key); |
| 1586 | if (kstr == NULL) |
| 1587 | goto bail; |
| 1588 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1589 | else if (skipkeys) { |
Raymond Hettinger | bcf6f92 | 2009-05-27 09:58:34 +0000 | [diff] [blame] | 1590 | Py_DECREF(item); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1591 | continue; |
| 1592 | } |
| 1593 | else { |
| 1594 | /* TODO: include repr of key */ |
Doug Hellmann | 1c52475 | 2010-07-21 12:29:04 +0000 | [diff] [blame] | 1595 | PyErr_SetString(PyExc_TypeError, "keys must be a string"); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1596 | goto bail; |
| 1597 | } |
| 1598 | |
| 1599 | if (idx) { |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1600 | if (_PyAccu_Accumulate(acc, s->item_separator)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1601 | goto bail; |
| 1602 | } |
| 1603 | |
| 1604 | encoded = encoder_encode_string(s, kstr); |
| 1605 | Py_CLEAR(kstr); |
| 1606 | if (encoded == NULL) |
| 1607 | goto bail; |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1608 | if (_PyAccu_Accumulate(acc, encoded)) { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1609 | Py_DECREF(encoded); |
| 1610 | goto bail; |
| 1611 | } |
| 1612 | Py_DECREF(encoded); |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1613 | if (_PyAccu_Accumulate(acc, s->key_separator)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1614 | goto bail; |
Raymond Hettinger | c8d952d | 2009-05-27 06:50:31 +0000 | [diff] [blame] | 1615 | |
Raymond Hettinger | bcf6f92 | 2009-05-27 09:58:34 +0000 | [diff] [blame] | 1616 | value = PyTuple_GET_ITEM(item, 1); |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1617 | if (encoder_listencode_obj(s, acc, value, indent_level)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1618 | goto bail; |
| 1619 | idx += 1; |
Raymond Hettinger | bcf6f92 | 2009-05-27 09:58:34 +0000 | [diff] [blame] | 1620 | Py_DECREF(item); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1621 | } |
Raymond Hettinger | c8d952d | 2009-05-27 06:50:31 +0000 | [diff] [blame] | 1622 | if (PyErr_Occurred()) |
| 1623 | goto bail; |
| 1624 | Py_CLEAR(it); |
| 1625 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1626 | if (ident != NULL) { |
| 1627 | if (PyDict_DelItem(s->markers, ident)) |
| 1628 | goto bail; |
| 1629 | Py_CLEAR(ident); |
| 1630 | } |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1631 | /* TODO DOES NOT RUN; dead code |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1632 | if (s->indent != Py_None) { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1633 | indent_level -= 1; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1634 | |
| 1635 | yield '\n' + (' ' * (_indent * _current_indent_level)) |
| 1636 | }*/ |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1637 | if (_PyAccu_Accumulate(acc, close_dict)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1638 | goto bail; |
| 1639 | return 0; |
| 1640 | |
| 1641 | bail: |
Raymond Hettinger | c8d952d | 2009-05-27 06:50:31 +0000 | [diff] [blame] | 1642 | Py_XDECREF(it); |
Raymond Hettinger | bcf6f92 | 2009-05-27 09:58:34 +0000 | [diff] [blame] | 1643 | Py_XDECREF(item); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1644 | Py_XDECREF(kstr); |
| 1645 | Py_XDECREF(ident); |
| 1646 | return -1; |
| 1647 | } |
| 1648 | |
| 1649 | |
| 1650 | static int |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1651 | encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc, |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1652 | PyObject *seq, Py_ssize_t indent_level) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1653 | { |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1654 | /* Encode Python list seq to a JSON term */ |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1655 | static PyObject *open_array = NULL; |
| 1656 | static PyObject *close_array = NULL; |
| 1657 | static PyObject *empty_array = NULL; |
| 1658 | PyObject *ident = NULL; |
| 1659 | PyObject *s_fast = NULL; |
| 1660 | Py_ssize_t num_items; |
| 1661 | PyObject **seq_items; |
| 1662 | Py_ssize_t i; |
| 1663 | |
| 1664 | if (open_array == NULL || close_array == NULL || empty_array == NULL) { |
| 1665 | open_array = PyUnicode_InternFromString("["); |
| 1666 | close_array = PyUnicode_InternFromString("]"); |
| 1667 | empty_array = PyUnicode_InternFromString("[]"); |
| 1668 | if (open_array == NULL || close_array == NULL || empty_array == NULL) |
| 1669 | return -1; |
| 1670 | } |
| 1671 | ident = NULL; |
| 1672 | s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence"); |
| 1673 | if (s_fast == NULL) |
| 1674 | return -1; |
| 1675 | num_items = PySequence_Fast_GET_SIZE(s_fast); |
| 1676 | if (num_items == 0) { |
| 1677 | Py_DECREF(s_fast); |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1678 | return _PyAccu_Accumulate(acc, empty_array); |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | if (s->markers != Py_None) { |
| 1682 | int has_key; |
| 1683 | ident = PyLong_FromVoidPtr(seq); |
| 1684 | if (ident == NULL) |
| 1685 | goto bail; |
| 1686 | has_key = PyDict_Contains(s->markers, ident); |
| 1687 | if (has_key) { |
| 1688 | if (has_key != -1) |
| 1689 | PyErr_SetString(PyExc_ValueError, "Circular reference detected"); |
| 1690 | goto bail; |
| 1691 | } |
| 1692 | if (PyDict_SetItem(s->markers, ident, seq)) { |
| 1693 | goto bail; |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | seq_items = PySequence_Fast_ITEMS(s_fast); |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1698 | if (_PyAccu_Accumulate(acc, open_array)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1699 | goto bail; |
| 1700 | if (s->indent != Py_None) { |
| 1701 | /* TODO: DOES NOT RUN */ |
| 1702 | indent_level += 1; |
| 1703 | /* |
| 1704 | newline_indent = '\n' + (' ' * (_indent * _current_indent_level)) |
| 1705 | separator = _item_separator + newline_indent |
| 1706 | buf += newline_indent |
| 1707 | */ |
| 1708 | } |
| 1709 | for (i = 0; i < num_items; i++) { |
| 1710 | PyObject *obj = seq_items[i]; |
| 1711 | if (i) { |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1712 | if (_PyAccu_Accumulate(acc, s->item_separator)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1713 | goto bail; |
| 1714 | } |
Antoine Pitrou | df7fc9d | 2011-08-19 18:03:14 +0200 | [diff] [blame] | 1715 | if (encoder_listencode_obj(s, acc, obj, indent_level)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1716 | goto bail; |
| 1717 | } |
| 1718 | if (ident != NULL) { |
| 1719 | if (PyDict_DelItem(s->markers, ident)) |
| 1720 | goto bail; |
| 1721 | Py_CLEAR(ident); |
| 1722 | } |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1723 | |
| 1724 | /* TODO: DOES NOT RUN |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1725 | if (s->indent != Py_None) { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1726 | indent_level -= 1; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1727 | |
| 1728 | yield '\n' + (' ' * (_indent * _current_indent_level)) |
| 1729 | }*/ |
Antoine Pitrou | 90c30e8 | 2011-10-06 19:09:51 +0200 | [diff] [blame] | 1730 | if (_PyAccu_Accumulate(acc, close_array)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1731 | goto bail; |
| 1732 | Py_DECREF(s_fast); |
| 1733 | return 0; |
| 1734 | |
| 1735 | bail: |
| 1736 | Py_XDECREF(ident); |
| 1737 | Py_DECREF(s_fast); |
| 1738 | return -1; |
| 1739 | } |
| 1740 | |
| 1741 | static void |
| 1742 | encoder_dealloc(PyObject *self) |
| 1743 | { |
| 1744 | /* Deallocate Encoder */ |
| 1745 | encoder_clear(self); |
| 1746 | Py_TYPE(self)->tp_free(self); |
| 1747 | } |
| 1748 | |
| 1749 | static int |
| 1750 | encoder_traverse(PyObject *self, visitproc visit, void *arg) |
| 1751 | { |
| 1752 | PyEncoderObject *s; |
| 1753 | assert(PyEncoder_Check(self)); |
| 1754 | s = (PyEncoderObject *)self; |
| 1755 | Py_VISIT(s->markers); |
| 1756 | Py_VISIT(s->defaultfn); |
| 1757 | Py_VISIT(s->encoder); |
| 1758 | Py_VISIT(s->indent); |
| 1759 | Py_VISIT(s->key_separator); |
| 1760 | Py_VISIT(s->item_separator); |
| 1761 | Py_VISIT(s->sort_keys); |
| 1762 | Py_VISIT(s->skipkeys); |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
| 1766 | static int |
| 1767 | encoder_clear(PyObject *self) |
| 1768 | { |
| 1769 | /* Deallocate Encoder */ |
| 1770 | PyEncoderObject *s; |
| 1771 | assert(PyEncoder_Check(self)); |
| 1772 | s = (PyEncoderObject *)self; |
| 1773 | Py_CLEAR(s->markers); |
| 1774 | Py_CLEAR(s->defaultfn); |
| 1775 | Py_CLEAR(s->encoder); |
| 1776 | Py_CLEAR(s->indent); |
| 1777 | Py_CLEAR(s->key_separator); |
| 1778 | Py_CLEAR(s->item_separator); |
| 1779 | Py_CLEAR(s->sort_keys); |
| 1780 | Py_CLEAR(s->skipkeys); |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable"); |
| 1785 | |
| 1786 | static |
| 1787 | PyTypeObject PyEncoderType = { |
| 1788 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1789 | "_json.Encoder", /* tp_name */ |
| 1790 | sizeof(PyEncoderObject), /* tp_basicsize */ |
| 1791 | 0, /* tp_itemsize */ |
| 1792 | encoder_dealloc, /* tp_dealloc */ |
| 1793 | 0, /* tp_print */ |
| 1794 | 0, /* tp_getattr */ |
| 1795 | 0, /* tp_setattr */ |
| 1796 | 0, /* tp_compare */ |
| 1797 | 0, /* tp_repr */ |
| 1798 | 0, /* tp_as_number */ |
| 1799 | 0, /* tp_as_sequence */ |
| 1800 | 0, /* tp_as_mapping */ |
| 1801 | 0, /* tp_hash */ |
| 1802 | encoder_call, /* tp_call */ |
| 1803 | 0, /* tp_str */ |
| 1804 | 0, /* tp_getattro */ |
| 1805 | 0, /* tp_setattro */ |
| 1806 | 0, /* tp_as_buffer */ |
| 1807 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 1808 | encoder_doc, /* tp_doc */ |
| 1809 | encoder_traverse, /* tp_traverse */ |
| 1810 | encoder_clear, /* tp_clear */ |
| 1811 | 0, /* tp_richcompare */ |
| 1812 | 0, /* tp_weaklistoffset */ |
| 1813 | 0, /* tp_iter */ |
| 1814 | 0, /* tp_iternext */ |
| 1815 | 0, /* tp_methods */ |
| 1816 | encoder_members, /* tp_members */ |
| 1817 | 0, /* tp_getset */ |
| 1818 | 0, /* tp_base */ |
| 1819 | 0, /* tp_dict */ |
| 1820 | 0, /* tp_descr_get */ |
| 1821 | 0, /* tp_descr_set */ |
| 1822 | 0, /* tp_dictoffset */ |
| 1823 | encoder_init, /* tp_init */ |
| 1824 | 0, /* tp_alloc */ |
| 1825 | encoder_new, /* tp_new */ |
| 1826 | 0, /* tp_free */ |
| 1827 | }; |
| 1828 | |
| 1829 | static PyMethodDef speedups_methods[] = { |
| 1830 | {"encode_basestring_ascii", |
| 1831 | (PyCFunction)py_encode_basestring_ascii, |
| 1832 | METH_O, |
| 1833 | pydoc_encode_basestring_ascii}, |
| 1834 | {"scanstring", |
| 1835 | (PyCFunction)py_scanstring, |
| 1836 | METH_VARARGS, |
| 1837 | pydoc_scanstring}, |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 1838 | {NULL, NULL, 0, NULL} |
| 1839 | }; |
| 1840 | |
| 1841 | PyDoc_STRVAR(module_doc, |
| 1842 | "json speedups\n"); |
| 1843 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1844 | static struct PyModuleDef jsonmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | PyModuleDef_HEAD_INIT, |
| 1846 | "_json", |
| 1847 | module_doc, |
| 1848 | -1, |
| 1849 | speedups_methods, |
| 1850 | NULL, |
| 1851 | NULL, |
| 1852 | NULL, |
| 1853 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1854 | }; |
| 1855 | |
| 1856 | PyObject* |
| 1857 | PyInit__json(void) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 1858 | { |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1859 | PyObject *m = PyModule_Create(&jsonmodule); |
| 1860 | if (!m) |
| 1861 | return NULL; |
| 1862 | PyScannerType.tp_new = PyType_GenericNew; |
| 1863 | if (PyType_Ready(&PyScannerType) < 0) |
| 1864 | goto fail; |
| 1865 | PyEncoderType.tp_new = PyType_GenericNew; |
| 1866 | if (PyType_Ready(&PyEncoderType) < 0) |
| 1867 | goto fail; |
| 1868 | Py_INCREF((PyObject*)&PyScannerType); |
| 1869 | if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) { |
| 1870 | Py_DECREF((PyObject*)&PyScannerType); |
| 1871 | goto fail; |
| 1872 | } |
| 1873 | Py_INCREF((PyObject*)&PyEncoderType); |
| 1874 | if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) { |
| 1875 | Py_DECREF((PyObject*)&PyEncoderType); |
| 1876 | goto fail; |
| 1877 | } |
| 1878 | return m; |
| 1879 | fail: |
| 1880 | Py_DECREF(m); |
| 1881 | return NULL; |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 1882 | } |