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