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