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