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