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