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