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