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