| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 #include "Python.h" | 1 #include "Python.h" |
| 2 #include "structmember.h" | |
| 3 #if PY_VERSION_HEX < 0x02060000 && !defined(Py_TYPE) | |
|
Martin v. Löwis
2009/02/28 09:28:11
Is Python before 2.6 even supported anymore? ISTM
bob.ippolito
2009/02/28 10:02:57
Python >= 2.6 is the compatibility target, all of
| |
| 4 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) | |
| 5 #endif | |
| 6 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN) | |
| 7 typedef int Py_ssize_t; | |
| 8 #define PY_SSIZE_T_MAX INT_MAX | |
| 9 #define PY_SSIZE_T_MIN INT_MIN | |
| 10 #define PyInt_FromSsize_t PyInt_FromLong | |
| 11 #define PyInt_AsSsize_t PyInt_AsLong | |
| 12 #endif | |
| 13 #ifndef Py_IS_FINITE | |
| 14 #define Py_IS_FINITE(X) (!Py_IS_INFINITY(X) && !Py_IS_NAN(X)) | |
| 15 #endif | |
| 16 | |
| 17 #ifdef __GNUC__ | |
| 18 #define UNUSED __attribute__((__unused__)) | |
| 19 #else | |
| 20 #define UNUSED | |
| 21 #endif | |
| 2 | 22 |
| 3 #define DEFAULT_ENCODING "utf-8" | 23 #define DEFAULT_ENCODING "utf-8" |
| 24 | |
| 25 #define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType) | |
| 26 #define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType) | |
| 27 #define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType) | |
| 28 #define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType) | |
| 29 | |
| 30 static PyTypeObject PyScannerType; | |
| 31 static PyTypeObject PyEncoderType; | |
| 32 | |
| 33 typedef struct _PyScannerObject { | |
| 34 PyObject_HEAD | |
| 35 PyObject *encoding; | |
| 36 PyObject *strict; | |
| 37 PyObject *object_hook; | |
| 38 PyObject *parse_float; | |
| 39 PyObject *parse_int; | |
| 40 PyObject *parse_constant; | |
| 41 } PyScannerObject; | |
| 42 | |
| 43 static PyMemberDef scanner_members[] = { | |
| 44 {"encoding", T_OBJECT, offsetof(PyScannerObject, encoding), READONLY, "encod ing"}, | |
| 45 {"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"}, | |
| 46 {"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"}, | |
| 47 {"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"}, | |
| 48 {"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "par se_int"}, | |
| 49 {"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READ ONLY, "parse_constant"}, | |
| 50 {NULL} | |
| 51 }; | |
| 52 | |
| 53 typedef struct _PyEncoderObject { | |
| 54 PyObject_HEAD | |
| 55 PyObject *markers; | |
| 56 PyObject *defaultfn; | |
| 57 PyObject *encoder; | |
| 58 PyObject *indent; | |
| 59 PyObject *key_separator; | |
| 60 PyObject *item_separator; | |
| 61 PyObject *sort_keys; | |
| 62 PyObject *skipkeys; | |
| 63 int fast_encode; | |
| 64 int allow_nan; | |
| 65 } PyEncoderObject; | |
| 66 | |
| 67 static PyMemberDef encoder_members[] = { | |
| 68 {"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers "}, | |
| 69 {"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "defau lt"}, | |
| 70 {"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder "}, | |
| 71 {"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"}, | |
| 72 {"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READON LY, "key_separator"}, | |
| 73 {"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READ ONLY, "item_separator"}, | |
| 74 {"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sor t_keys"}, | |
| 75 {"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipk eys"}, | |
| 76 {NULL} | |
| 77 }; | |
| 78 | |
| 79 static Py_ssize_t | |
| 80 ascii_escape_char(Py_UNICODE c, char *output, Py_ssize_t chars); | |
| 81 static PyObject * | |
| 82 ascii_escape_unicode(PyObject *pystr); | |
| 83 static PyObject * | |
| 84 ascii_escape_str(PyObject *pystr); | |
| 85 static PyObject * | |
| 86 py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr); | |
| 87 void init_json(void); | |
| 88 static PyObject * | |
| 89 scan_once_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *n ext_idx_ptr); | |
| 90 static PyObject * | |
| 91 scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_ t *next_idx_ptr); | |
| 92 static PyObject * | |
| 93 _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx); | |
| 94 static PyObject * | |
| 95 scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | |
| 96 static int | |
| 97 scanner_init(PyObject *self, PyObject *args, PyObject *kwds); | |
| 98 static void | |
| 99 scanner_dealloc(PyObject *self); | |
| 100 static int | |
| 101 scanner_clear(PyObject *self); | |
| 102 static PyObject * | |
| 103 encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | |
| 104 static int | |
| 105 encoder_init(PyObject *self, PyObject *args, PyObject *kwds); | |
| 106 static void | |
| 107 encoder_dealloc(PyObject *self); | |
| 108 static int | |
| 109 encoder_clear(PyObject *self); | |
| 110 static int | |
| 111 encoder_listencode_list(PyEncoderObject *s, PyObject *rval, PyObject *seq, Py_ss ize_t indent_level); | |
| 112 static int | |
| 113 encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssi ze_t indent_level); | |
| 114 static int | |
| 115 encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss ize_t indent_level); | |
| 116 static PyObject * | |
| 117 _encoded_const(PyObject *const); | |
| 118 static void | |
| 119 raise_errmsg(char *msg, PyObject *s, Py_ssize_t end); | |
| 120 static PyObject * | |
| 121 encoder_encode_string(PyEncoderObject *s, PyObject *obj); | |
| 122 static int | |
| 123 _convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr); | |
| 124 static PyObject * | |
| 125 _convertPyInt_FromSsize_t(Py_ssize_t *size_ptr); | |
| 126 static PyObject * | |
| 127 encoder_encode_float(PyEncoderObject *s, PyObject *obj); | |
| 128 | |
| 4 #define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"') | 129 #define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"') |
| 130 #define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r')) | |
| 131 | |
| 5 #define MIN_EXPANSION 6 | 132 #define MIN_EXPANSION 6 |
| 6 | |
| 7 #ifdef Py_UNICODE_WIDE | 133 #ifdef Py_UNICODE_WIDE |
| 8 #define MAX_EXPANSION (2 * MIN_EXPANSION) | 134 #define MAX_EXPANSION (2 * MIN_EXPANSION) |
| 9 #else | 135 #else |
| 10 #define MAX_EXPANSION MIN_EXPANSION | 136 #define MAX_EXPANSION MIN_EXPANSION |
| 11 #endif | 137 #endif |
| 12 | 138 |
| 139 static int | |
| 140 _convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr) | |
| 141 { | |
| 142 /* PyObject to Py_ssize_t converter */ | |
| 143 *size_ptr = PyInt_AsSsize_t(o); | |
| 144 if (*size_ptr == -1 && PyErr_Occurred()); | |
| 145 return 1; | |
| 146 return 0; | |
| 147 } | |
| 148 | |
| 149 static PyObject * | |
| 150 _convertPyInt_FromSsize_t(Py_ssize_t *size_ptr) | |
| 151 { | |
| 152 /* Py_ssize_t to PyObject converter */ | |
| 153 return PyInt_FromSsize_t(*size_ptr); | |
| 154 } | |
| 155 | |
| 13 static Py_ssize_t | 156 static Py_ssize_t |
| 14 ascii_escape_char(Py_UNICODE c, char *output, Py_ssize_t chars) | 157 ascii_escape_char(Py_UNICODE c, char *output, Py_ssize_t chars) |
| 15 { | 158 { |
| 16 Py_UNICODE x; | 159 /* Escape unicode code point c to ASCII escape sequences |
| 160 in char *output. output must have at least 12 bytes unused to | |
| 161 accommodate an escaped surrogate pair "\uXXXX\uXXXX" */ | |
| 17 output[chars++] = '\\'; | 162 output[chars++] = '\\'; |
| 18 switch (c) { | 163 switch (c) { |
| 19 case '\\': output[chars++] = (char)c; break; | 164 case '\\': output[chars++] = (char)c; break; |
| 20 case '"': output[chars++] = (char)c; break; | 165 case '"': output[chars++] = (char)c; break; |
| 21 case '\b': output[chars++] = 'b'; break; | 166 case '\b': output[chars++] = 'b'; break; |
| 22 case '\f': output[chars++] = 'f'; break; | 167 case '\f': output[chars++] = 'f'; break; |
| 23 case '\n': output[chars++] = 'n'; break; | 168 case '\n': output[chars++] = 'n'; break; |
| 24 case '\r': output[chars++] = 'r'; break; | 169 case '\r': output[chars++] = 'r'; break; |
| 25 case '\t': output[chars++] = 't'; break; | 170 case '\t': output[chars++] = 't'; break; |
| 26 default: | 171 default: |
| 27 #ifdef Py_UNICODE_WIDE | 172 #ifdef Py_UNICODE_WIDE |
| 28 if (c >= 0x10000) { | 173 if (c >= 0x10000) { |
| 29 /* UTF-16 surrogate pair */ | 174 /* UTF-16 surrogate pair */ |
| 30 Py_UNICODE v = c - 0x10000; | 175 Py_UNICODE v = c - 0x10000; |
| 31 c = 0xd800 | ((v >> 10) & 0x3ff); | 176 c = 0xd800 | ((v >> 10) & 0x3ff); |
| 32 output[chars++] = 'u'; | 177 output[chars++] = 'u'; |
| 33 x = (c & 0xf000) >> 12; | 178 output[chars++] = "0123456789abcdef"[(c >> 12) & 0xf]; |
| 34 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); | 179 output[chars++] = "0123456789abcdef"[(c >> 8) & 0xf]; |
| 35 x = (c & 0x0f00) >> 8; | 180 output[chars++] = "0123456789abcdef"[(c >> 4) & 0xf]; |
| 36 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); | 181 output[chars++] = "0123456789abcdef"[(c ) & 0xf]; |
| 37 x = (c & 0x00f0) >> 4; | |
| 38 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); | |
| 39 x = (c & 0x000f); | |
| 40 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); | |
| 41 c = 0xdc00 | (v & 0x3ff); | 182 c = 0xdc00 | (v & 0x3ff); |
| 42 output[chars++] = '\\'; | 183 output[chars++] = '\\'; |
| 43 } | 184 } |
| 44 #endif | 185 #endif |
| 45 output[chars++] = 'u'; | 186 output[chars++] = 'u'; |
| 46 x = (c & 0xf000) >> 12; | 187 output[chars++] = "0123456789abcdef"[(c >> 12) & 0xf]; |
| 47 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); | 188 output[chars++] = "0123456789abcdef"[(c >> 8) & 0xf]; |
| 48 x = (c & 0x0f00) >> 8; | 189 output[chars++] = "0123456789abcdef"[(c >> 4) & 0xf]; |
| 49 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); | 190 output[chars++] = "0123456789abcdef"[(c ) & 0xf]; |
| 50 x = (c & 0x00f0) >> 4; | |
| 51 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); | |
| 52 x = (c & 0x000f); | |
| 53 output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); | |
| 54 } | 191 } |
| 55 return chars; | 192 return chars; |
| 56 } | 193 } |
| 57 | 194 |
| 58 static PyObject * | 195 static PyObject * |
| 59 ascii_escape_unicode(PyObject *pystr) | 196 ascii_escape_unicode(PyObject *pystr) |
| 60 { | 197 { |
| 198 /* Take a PyUnicode pystr and return a new ASCII-only escaped PyString */ | |
| 61 Py_ssize_t i; | 199 Py_ssize_t i; |
| 62 Py_ssize_t input_chars; | 200 Py_ssize_t input_chars; |
| 63 Py_ssize_t output_size; | 201 Py_ssize_t output_size; |
| 202 Py_ssize_t max_output_size; | |
| 64 Py_ssize_t chars; | 203 Py_ssize_t chars; |
| 65 PyObject *rval; | 204 PyObject *rval; |
| 66 char *output; | 205 char *output; |
| 67 Py_UNICODE *input_unicode; | 206 Py_UNICODE *input_unicode; |
| 68 | 207 |
| 69 input_chars = PyUnicode_GET_SIZE(pystr); | 208 input_chars = PyUnicode_GET_SIZE(pystr); |
| 70 input_unicode = PyUnicode_AS_UNICODE(pystr); | 209 input_unicode = PyUnicode_AS_UNICODE(pystr); |
| 210 | |
| 71 /* One char input can be up to 6 chars output, estimate 4 of these */ | 211 /* One char input can be up to 6 chars output, estimate 4 of these */ |
| 72 output_size = 2 + (MIN_EXPANSION * 4) + input_chars; | 212 output_size = 2 + (MIN_EXPANSION * 4) + input_chars; |
| 213 max_output_size = 2 + (input_chars * MAX_EXPANSION); | |
| 73 rval = PyString_FromStringAndSize(NULL, output_size); | 214 rval = PyString_FromStringAndSize(NULL, output_size); |
| 74 if (rval == NULL) { | 215 if (rval == NULL) { |
| 75 return NULL; | 216 return NULL; |
| 76 } | 217 } |
| 77 output = PyString_AS_STRING(rval); | 218 output = PyString_AS_STRING(rval); |
| 78 chars = 0; | 219 chars = 0; |
| 79 output[chars++] = '"'; | 220 output[chars++] = '"'; |
| 80 for (i = 0; i < input_chars; i++) { | 221 for (i = 0; i < input_chars; i++) { |
| 81 Py_UNICODE c = input_unicode[i]; | 222 Py_UNICODE c = input_unicode[i]; |
| 82 if (S_CHAR(c)) { | 223 if (S_CHAR(c)) { |
| 83 output[chars++] = (char)c; | 224 output[chars++] = (char)c; |
| 84 } | 225 } |
| 85 » else { | 226 else { |
| 86 chars = ascii_escape_char(c, output, chars); | 227 chars = ascii_escape_char(c, output, chars); |
| 87 } | 228 } |
| 88 if (output_size - chars < (1 + MAX_EXPANSION)) { | 229 if (output_size - chars < (1 + MAX_EXPANSION)) { |
| 89 /* There's more than four, so let's resize by a lot */ | 230 /* There's more than four, so let's resize by a lot */ |
| 90 output_size *= 2; | 231 Py_ssize_t new_output_size = output_size * 2; |
| 91 /* This is an upper bound */ | 232 /* This is an upper bound */ |
| 92 if (output_size > 2 + (input_chars * MAX_EXPANSION)) { | 233 if (new_output_size > max_output_size) { |
| 93 output_size = 2 + (input_chars * MAX_EXPANSION); | 234 new_output_size = max_output_size; |
| 94 } | 235 } |
| 95 if (_PyString_Resize(&rval, output_size) == -1) { | 236 /* Make sure that the output size changed before resizing */ |
| 96 return NULL; | 237 if (new_output_size != output_size) { |
| 238 output_size = new_output_size; | |
| 239 if (_PyString_Resize(&rval, output_size) == -1) { | |
| 240 return NULL; | |
| 241 } | |
| 242 output = PyString_AS_STRING(rval); | |
| 97 } | 243 } |
| 98 output = PyString_AS_STRING(rval); | |
| 99 } | 244 } |
| 100 } | 245 } |
| 101 output[chars++] = '"'; | 246 output[chars++] = '"'; |
| 102 if (_PyString_Resize(&rval, chars) == -1) { | 247 if (_PyString_Resize(&rval, chars) == -1) { |
| 103 return NULL; | 248 return NULL; |
| 104 } | 249 } |
| 105 return rval; | 250 return rval; |
| 106 } | 251 } |
| 107 | 252 |
| 108 static PyObject * | 253 static PyObject * |
| 109 ascii_escape_str(PyObject *pystr) | 254 ascii_escape_str(PyObject *pystr) |
| 110 { | 255 { |
| 256 /* Take a PyString pystr and return a new ASCII-only escaped PyString */ | |
| 111 Py_ssize_t i; | 257 Py_ssize_t i; |
| 112 Py_ssize_t input_chars; | 258 Py_ssize_t input_chars; |
| 113 Py_ssize_t output_size; | 259 Py_ssize_t output_size; |
| 114 Py_ssize_t chars; | 260 Py_ssize_t chars; |
| 115 PyObject *rval; | 261 PyObject *rval; |
| 116 char *output; | 262 char *output; |
| 117 char *input_str; | 263 char *input_str; |
| 118 | 264 |
| 119 input_chars = PyString_GET_SIZE(pystr); | 265 input_chars = PyString_GET_SIZE(pystr); |
| 120 input_str = PyString_AS_STRING(pystr); | 266 input_str = PyString_AS_STRING(pystr); |
| 121 /* One char input can be up to 6 chars output, estimate 4 of these */ | 267 |
| 122 output_size = 2 + (MIN_EXPANSION * 4) + input_chars; | 268 /* Fast path for a string that's already ASCII */ |
| 269 for (i = 0; i < input_chars; i++) { | |
| 270 Py_UNICODE c = (Py_UNICODE)(unsigned char)input_str[i]; | |
| 271 if (!S_CHAR(c)) { | |
| 272 /* If we have to escape something, scan the string for unicode */ | |
| 273 Py_ssize_t j; | |
| 274 for (j = i; j < input_chars; j++) { | |
| 275 c = (Py_UNICODE)(unsigned char)input_str[j]; | |
| 276 if (c > 0x7f) { | |
| 277 /* We hit a non-ASCII character, bail to unicode mode */ | |
| 278 PyObject *uni; | |
| 279 uni = PyUnicode_DecodeUTF8(input_str, input_chars, "strict") ; | |
| 280 if (uni == NULL) { | |
| 281 return NULL; | |
| 282 } | |
| 283 rval = ascii_escape_unicode(uni); | |
| 284 Py_DECREF(uni); | |
| 285 return rval; | |
| 286 } | |
| 287 } | |
| 288 break; | |
| 289 } | |
| 290 } | |
| 291 | |
| 292 if (i == input_chars) { | |
| 293 /* Input is already ASCII */ | |
| 294 output_size = 2 + input_chars; | |
| 295 } | |
| 296 else { | |
| 297 /* One char input can be up to 6 chars output, estimate 4 of these */ | |
| 298 output_size = 2 + (MIN_EXPANSION * 4) + input_chars; | |
| 299 } | |
| 123 rval = PyString_FromStringAndSize(NULL, output_size); | 300 rval = PyString_FromStringAndSize(NULL, output_size); |
| 124 if (rval == NULL) { | 301 if (rval == NULL) { |
| 125 return NULL; | 302 return NULL; |
| 126 } | 303 } |
| 127 output = PyString_AS_STRING(rval); | 304 output = PyString_AS_STRING(rval); |
| 128 chars = 0; | 305 output[0] = '"'; |
| 129 output[chars++] = '"'; | 306 |
| 130 for (i = 0; i < input_chars; i++) { | 307 /* We know that everything up to i is ASCII already */ |
| 131 Py_UNICODE c = (Py_UNICODE)input_str[i]; | 308 chars = i + 1; |
| 309 memcpy(&output[1], input_str, i); | |
| 310 | |
| 311 for (; i < input_chars; i++) { | |
| 312 Py_UNICODE c = (Py_UNICODE)(unsigned char)input_str[i]; | |
| 132 if (S_CHAR(c)) { | 313 if (S_CHAR(c)) { |
| 133 output[chars++] = (char)c; | 314 output[chars++] = (char)c; |
| 134 } | 315 } |
| 135 » else if (c > 0x7F) { | 316 else { |
| 136 /* We hit a non-ASCII character, bail to unicode mode */ | |
| 137 PyObject *uni; | |
| 138 Py_DECREF(rval); | |
| 139 uni = PyUnicode_DecodeUTF8(input_str, input_chars, "strict"); | |
| 140 if (uni == NULL) { | |
| 141 return NULL; | |
| 142 } | |
| 143 rval = ascii_escape_unicode(uni); | |
| 144 Py_DECREF(uni); | |
| 145 return rval; | |
| 146 } | |
| 147 » else { | |
| 148 chars = ascii_escape_char(c, output, chars); | 317 chars = ascii_escape_char(c, output, chars); |
| 149 } | 318 } |
| 150 /* An ASCII char can't possibly expand to a surrogate! */ | 319 /* An ASCII char can't possibly expand to a surrogate! */ |
| 151 if (output_size - chars < (1 + MIN_EXPANSION)) { | 320 if (output_size - chars < (1 + MIN_EXPANSION)) { |
| 152 /* There's more than four, so let's resize by a lot */ | 321 /* There's more than four, so let's resize by a lot */ |
| 153 output_size *= 2; | 322 output_size *= 2; |
| 154 if (output_size > 2 + (input_chars * MIN_EXPANSION)) { | 323 if (output_size > 2 + (input_chars * MIN_EXPANSION)) { |
| 155 output_size = 2 + (input_chars * MIN_EXPANSION); | 324 output_size = 2 + (input_chars * MIN_EXPANSION); |
| 156 } | 325 } |
| 157 if (_PyString_Resize(&rval, output_size) == -1) { | 326 if (_PyString_Resize(&rval, output_size) == -1) { |
| 158 return NULL; | 327 return NULL; |
| 159 } | 328 } |
| 160 output = PyString_AS_STRING(rval); | 329 output = PyString_AS_STRING(rval); |
| 161 } | 330 } |
| 162 } | 331 } |
| 163 output[chars++] = '"'; | 332 output[chars++] = '"'; |
| 164 if (_PyString_Resize(&rval, chars) == -1) { | 333 if (_PyString_Resize(&rval, chars) == -1) { |
| 165 return NULL; | 334 return NULL; |
| 166 } | 335 } |
| 167 return rval; | 336 return rval; |
| 168 } | 337 } |
| 169 | 338 |
| 170 void | 339 static void |
| 171 raise_errmsg(char *msg, PyObject *s, Py_ssize_t end) | 340 raise_errmsg(char *msg, PyObject *s, Py_ssize_t end) |
| 172 { | 341 { |
| 342 /* Use the Python function json.decoder.errmsg to raise a nice | |
| 343 looking ValueError exception */ | |
| 173 static PyObject *errmsg_fn = NULL; | 344 static PyObject *errmsg_fn = NULL; |
| 174 PyObject *pymsg; | 345 PyObject *pymsg; |
| 175 if (errmsg_fn == NULL) { | 346 if (errmsg_fn == NULL) { |
| 176 PyObject *decoder = PyImport_ImportModule("json.decoder"); | 347 PyObject *decoder = PyImport_ImportModule("json.decoder"); |
| 177 if (decoder == NULL) | 348 if (decoder == NULL) |
| 178 return; | 349 return; |
| 179 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg"); | 350 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg"); |
| 351 Py_DECREF(decoder); | |
| 180 if (errmsg_fn == NULL) | 352 if (errmsg_fn == NULL) |
| 181 return; | 353 return; |
| 182 Py_DECREF(decoder); | |
| 183 } | 354 } |
| 184 pymsg = PyObject_CallFunction(errmsg_fn, "(zOn)", msg, s, end); | 355 pymsg = PyObject_CallFunction(errmsg_fn, "(zOO&)", msg, s, _convertPyInt_Fro mSsize_t, &end); |
| 185 if (pymsg) { | 356 if (pymsg) { |
| 186 PyErr_SetObject(PyExc_ValueError, pymsg); | 357 PyErr_SetObject(PyExc_ValueError, pymsg); |
| 187 Py_DECREF(pymsg); | 358 Py_DECREF(pymsg); |
| 188 } | 359 } |
| 189 /* | |
| 190 | |
| 191 def linecol(doc, pos): | |
| 192 lineno = doc.count('\n', 0, pos) + 1 | |
| 193 if lineno == 1: | |
| 194 colno = pos | |
| 195 else: | |
| 196 colno = pos - doc.rindex('\n', 0, pos) | |
| 197 return lineno, colno | |
| 198 | |
| 199 def errmsg(msg, doc, pos, end=None): | |
| 200 lineno, colno = linecol(doc, pos) | |
| 201 if end is None: | |
| 202 return '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos) | |
| 203 endlineno, endcolno = linecol(doc, end) | |
| 204 return '%s: line %d column %d - line %d column %d (char %d - %d)' % ( | |
| 205 msg, lineno, colno, endlineno, endcolno, pos, end) | |
| 206 | |
| 207 */ | |
| 208 } | 360 } |
| 209 | 361 |
| 210 static PyObject * | 362 static PyObject * |
| 211 join_list_unicode(PyObject *lst) | 363 join_list_unicode(PyObject *lst) |
| 212 { | 364 { |
| 213 static PyObject *ustr = NULL; | 365 /* return u''.join(lst) */ |
| 214 static PyObject *joinstr = NULL; | 366 static PyObject *joinfn = NULL; |
| 215 if (ustr == NULL) { | 367 if (joinfn == NULL) { |
| 216 Py_UNICODE c = 0; | 368 PyObject *ustr = PyUnicode_FromUnicode(NULL, 0); |
| 217 ustr = PyUnicode_FromUnicode(&c, 0); | 369 if (ustr == NULL) |
| 370 return NULL; | |
| 371 | |
| 372 joinfn = PyObject_GetAttrString(ustr, "join"); | |
| 373 Py_DECREF(ustr); | |
| 374 if (joinfn == NULL) | |
| 375 return NULL; | |
| 218 } | 376 } |
| 219 if (joinstr == NULL) { | 377 return PyObject_CallFunctionObjArgs(joinfn, lst, NULL); |
| 220 joinstr = PyString_InternFromString("join"); | |
| 221 } | |
| 222 if (joinstr == NULL || ustr == NULL) { | |
| 223 return NULL; | |
| 224 } | |
| 225 return PyObject_CallMethodObjArgs(ustr, joinstr, lst, NULL); | |
| 226 } | 378 } |
| 227 | 379 |
| 228 static PyObject * | 380 static PyObject * |
| 229 scanstring_str(PyObject *pystr, Py_ssize_t end, char *encoding, int strict) | 381 join_list_string(PyObject *lst) |
| 230 { | 382 { |
| 383 /* return ''.join(lst) */ | |
| 384 static PyObject *joinfn = NULL; | |
| 385 if (joinfn == NULL) { | |
| 386 PyObject *ustr = PyString_FromStringAndSize(NULL, 0); | |
| 387 if (ustr == NULL) | |
| 388 return NULL; | |
| 389 | |
| 390 joinfn = PyObject_GetAttrString(ustr, "join"); | |
| 391 Py_DECREF(ustr); | |
| 392 if (joinfn == NULL) | |
| 393 return NULL; | |
| 394 } | |
| 395 return PyObject_CallFunctionObjArgs(joinfn, lst, NULL); | |
| 396 } | |
| 397 | |
| 398 static PyObject * | |
| 399 _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) { | |
| 400 /* return (rval, idx) tuple, stealing reference to rval */ | |
| 401 PyObject *tpl; | |
| 402 PyObject *pyidx; | |
| 403 /* | |
| 404 steal a reference to rval, returns (rval, idx) | |
| 405 */ | |
| 406 if (rval == NULL) { | |
| 407 return NULL; | |
| 408 } | |
| 409 pyidx = PyInt_FromSsize_t(idx); | |
| 410 if (pyidx == NULL) { | |
| 411 Py_DECREF(rval); | |
| 412 return NULL; | |
| 413 } | |
| 414 tpl = PyTuple_New(2); | |
| 415 if (tpl == NULL) { | |
| 416 Py_DECREF(pyidx); | |
| 417 Py_DECREF(rval); | |
| 418 return NULL; | |
| 419 } | |
| 420 PyTuple_SET_ITEM(tpl, 0, rval); | |
| 421 PyTuple_SET_ITEM(tpl, 1, pyidx); | |
| 422 return tpl; | |
| 423 } | |
| 424 | |
| 425 static PyObject * | |
| 426 scanstring_str(PyObject *pystr, Py_ssize_t end, char *encoding, int strict, Py_s size_t *next_end_ptr) | |
| 427 { | |
| 428 /* Read the JSON string from PyString pystr. | |
| 429 end is the index of the first character after the quote. | |
| 430 encoding is the encoding of pystr (must be an ASCII superset) | |
| 431 if strict is zero then literal control characters are allowed | |
| 432 *next_end_ptr is a return-by-reference index of the character | |
| 433 after the end quote | |
| 434 | |
| 435 Return value is a new PyString (if ASCII-only) or PyUnicode | |
| 436 */ | |
| 231 PyObject *rval; | 437 PyObject *rval; |
| 232 Py_ssize_t len = PyString_GET_SIZE(pystr); | 438 Py_ssize_t len = PyString_GET_SIZE(pystr); |
| 233 Py_ssize_t begin = end - 1; | 439 Py_ssize_t begin = end - 1; |
| 234 Py_ssize_t next = begin; | 440 Py_ssize_t next = begin; |
| 441 int has_unicode = 0; | |
| 235 char *buf = PyString_AS_STRING(pystr); | 442 char *buf = PyString_AS_STRING(pystr); |
| 236 PyObject *chunks = PyList_New(0); | 443 PyObject *chunks = PyList_New(0); |
| 237 if (chunks == NULL) { | 444 if (chunks == NULL) { |
| 238 goto bail; | 445 goto bail; |
| 239 } | 446 } |
| 240 if (end < 0 || len <= end) { | 447 if (end < 0 || len <= end) { |
| 241 PyErr_SetString(PyExc_ValueError, "end is out of bounds"); | 448 PyErr_SetString(PyExc_ValueError, "end is out of bounds"); |
| 242 goto bail; | 449 goto bail; |
| 243 } | 450 } |
| 244 while (1) { | 451 while (1) { |
| 245 /* Find the end of the string or the next escape */ | 452 /* Find the end of the string or the next escape */ |
| 246 Py_UNICODE c = 0; | 453 Py_UNICODE c = 0; |
| 247 PyObject *chunk = NULL; | 454 PyObject *chunk = NULL; |
| 248 for (next = end; next < len; next++) { | 455 for (next = end; next < len; next++) { |
| 249 c = buf[next]; | 456 c = (unsigned char)buf[next]; |
| 250 if (c == '"' || c == '\\') { | 457 if (c == '"' || c == '\\') { |
| 251 break; | 458 break; |
| 252 } | 459 } |
| 253 else if (strict && c <= 0x1f) { | 460 else if (strict && c <= 0x1f) { |
| 254 raise_errmsg("Invalid control character at", pystr, next); | 461 raise_errmsg("Invalid control character at", pystr, next); |
| 255 goto bail; | 462 goto bail; |
| 463 } | |
| 464 else if (c > 0x7f) { | |
| 465 has_unicode = 1; | |
| 256 } | 466 } |
| 257 } | 467 } |
| 258 if (!(c == '"' || c == '\\')) { | 468 if (!(c == '"' || c == '\\')) { |
| 259 raise_errmsg("Unterminated string starting at", pystr, begin); | 469 raise_errmsg("Unterminated string starting at", pystr, begin); |
| 260 goto bail; | 470 goto bail; |
| 261 } | 471 } |
| 262 /* Pick up this chunk if it's not zero length */ | 472 /* Pick up this chunk if it's not zero length */ |
| 263 if (next != end) { | 473 if (next != end) { |
| 264 PyObject *strchunk = PyBuffer_FromMemory(&buf[end], next - end); | 474 PyObject *strchunk = PyString_FromStringAndSize(&buf[end], next - en d); |
| 265 if (strchunk == NULL) { | 475 if (strchunk == NULL) { |
| 266 goto bail; | 476 goto bail; |
| 267 } | 477 } |
| 268 chunk = PyUnicode_FromEncodedObject(strchunk, encoding, NULL); | 478 if (has_unicode) { |
| 269 Py_DECREF(strchunk); | 479 chunk = PyUnicode_FromEncodedObject(strchunk, encoding, NULL); |
| 270 if (chunk == NULL) { | 480 Py_DECREF(strchunk); |
| 271 goto bail; | 481 if (chunk == NULL) { |
| 482 goto bail; | |
| 483 } | |
| 484 } | |
| 485 else { | |
| 486 chunk = strchunk; | |
| 272 } | 487 } |
| 273 if (PyList_Append(chunks, chunk)) { | 488 if (PyList_Append(chunks, chunk)) { |
| 274 Py_DECREF(chunk); | 489 Py_DECREF(chunk); |
| 275 goto bail; | 490 goto bail; |
| 276 } | 491 } |
| 277 Py_DECREF(chunk); | 492 Py_DECREF(chunk); |
| 278 } | 493 } |
| 279 next++; | 494 next++; |
| 280 if (c == '"') { | 495 if (c == '"') { |
| 281 end = next; | 496 end = next; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 308 else { | 523 else { |
| 309 c = 0; | 524 c = 0; |
| 310 next++; | 525 next++; |
| 311 end = next + 4; | 526 end = next + 4; |
| 312 if (end >= len) { | 527 if (end >= len) { |
| 313 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1); | 528 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1); |
| 314 goto bail; | 529 goto bail; |
| 315 } | 530 } |
| 316 /* Decode 4 hex digits */ | 531 /* Decode 4 hex digits */ |
| 317 for (; next < end; next++) { | 532 for (; next < end; next++) { |
| 318 Py_ssize_t shl = (end - next - 1) << 2; | |
| 319 Py_UNICODE digit = buf[next]; | 533 Py_UNICODE digit = buf[next]; |
| 534 c <<= 4; | |
| 320 switch (digit) { | 535 switch (digit) { |
| 321 case '0': case '1': case '2': case '3': case '4': | 536 case '0': case '1': case '2': case '3': case '4': |
| 322 case '5': case '6': case '7': case '8': case '9': | 537 case '5': case '6': case '7': case '8': case '9': |
| 323 c |= (digit - '0') << shl; break; | 538 c |= (digit - '0'); break; |
| 324 case 'a': case 'b': case 'c': case 'd': case 'e': | 539 case 'a': case 'b': case 'c': case 'd': case 'e': |
| 325 case 'f': | 540 case 'f': |
| 326 c |= (digit - 'a' + 10) << shl; break; | 541 c |= (digit - 'a' + 10); break; |
| 327 case 'A': case 'B': case 'C': case 'D': case 'E': | 542 case 'A': case 'B': case 'C': case 'D': case 'E': |
| 328 case 'F': | 543 case 'F': |
| 329 c |= (digit - 'A' + 10) << shl; break; | 544 c |= (digit - 'A' + 10); break; |
| 330 default: | 545 default: |
| 331 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); | 546 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); |
| 332 goto bail; | 547 goto bail; |
| 333 } | 548 } |
| 334 } | 549 } |
| 335 #ifdef Py_UNICODE_WIDE | 550 #ifdef Py_UNICODE_WIDE |
| 336 /* Surrogate pair */ | 551 /* Surrogate pair */ |
| 337 if (c >= 0xd800 && c <= 0xdbff) { | 552 if ((c & 0xfc00) == 0xd800) { |
| 338 Py_UNICODE c2 = 0; | 553 Py_UNICODE c2 = 0; |
| 339 if (end + 6 >= len) { | 554 if (end + 6 >= len) { |
| 340 raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr, | 555 raise_errmsg("Unpaired high surrogate", pystr, end - 5); |
| 341 end - 5); | 556 goto bail; |
| 342 } | 557 } |
| 343 if (buf[next++] != '\\' || buf[next++] != 'u') { | 558 if (buf[next++] != '\\' || buf[next++] != 'u') { |
| 344 raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr, | 559 raise_errmsg("Unpaired high surrogate", pystr, end - 5); |
| 345 end - 5); | 560 goto bail; |
| 346 } | 561 } |
| 347 end += 6; | 562 end += 6; |
| 348 /* Decode 4 hex digits */ | 563 /* Decode 4 hex digits */ |
| 349 for (; next < end; next++) { | 564 for (; next < end; next++) { |
| 350 Py_ssize_t shl = (end - next - 1) << 2; | 565 c2 <<= 4; |
| 351 Py_UNICODE digit = buf[next]; | 566 Py_UNICODE digit = buf[next]; |
| 352 switch (digit) { | 567 switch (digit) { |
| 353 case '0': case '1': case '2': case '3': case '4': | 568 case '0': case '1': case '2': case '3': case '4': |
| 354 case '5': case '6': case '7': case '8': case '9': | 569 case '5': case '6': case '7': case '8': case '9': |
| 355 c2 |= (digit - '0') << shl; break; | 570 c2 |= (digit - '0'); break; |
| 356 case 'a': case 'b': case 'c': case 'd': case 'e': | 571 case 'a': case 'b': case 'c': case 'd': case 'e': |
| 357 case 'f': | 572 case 'f': |
| 358 c2 |= (digit - 'a' + 10) << shl; break; | 573 c2 |= (digit - 'a' + 10); break; |
| 359 case 'A': case 'B': case 'C': case 'D': case 'E': | 574 case 'A': case 'B': case 'C': case 'D': case 'E': |
| 360 case 'F': | 575 case 'F': |
| 361 c2 |= (digit - 'A' + 10) << shl; break; | 576 c2 |= (digit - 'A' + 10); break; |
| 362 default: | 577 default: |
| 363 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); | 578 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); |
| 364 goto bail; | 579 goto bail; |
| 365 } | 580 } |
| 366 } | 581 } |
| 582 if ((c2 & 0xfc00) != 0xdc00) { | |
| 583 raise_errmsg("Unpaired high surrogate", pystr, end - 5); | |
| 584 goto bail; | |
| 585 } | |
| 367 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00)); | 586 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00)); |
| 587 } | |
| 588 else if ((c & 0xfc00) == 0xdc00) { | |
| 589 raise_errmsg("Unpaired low surrogate", pystr, end - 5); | |
| 590 goto bail; | |
| 368 } | 591 } |
| 369 #endif | 592 #endif |
| 370 } | 593 } |
| 371 chunk = PyUnicode_FromUnicode(&c, 1); | 594 if (c > 0x7f) { |
| 372 if (chunk == NULL) { | 595 has_unicode = 1; |
| 373 goto bail; | 596 } |
| 597 if (has_unicode) { | |
| 598 chunk = PyUnicode_FromUnicode(&c, 1); | |
| 599 if (chunk == NULL) { | |
| 600 goto bail; | |
| 601 } | |
| 602 } | |
| 603 else { | |
| 604 char c_char = Py_CHARMASK(c); | |
| 605 chunk = PyString_FromStringAndSize(&c_char, 1); | |
| 606 if (chunk == NULL) { | |
| 607 goto bail; | |
| 608 } | |
| 374 } | 609 } |
| 375 if (PyList_Append(chunks, chunk)) { | 610 if (PyList_Append(chunks, chunk)) { |
| 376 Py_DECREF(chunk); | 611 Py_DECREF(chunk); |
| 377 goto bail; | 612 goto bail; |
| 378 } | 613 } |
| 379 Py_DECREF(chunk); | 614 Py_DECREF(chunk); |
| 380 } | 615 } |
| 381 | 616 |
| 382 rval = join_list_unicode(chunks); | 617 rval = join_list_string(chunks); |
| 383 if (rval == NULL) { | 618 if (rval == NULL) { |
| 384 goto bail; | 619 goto bail; |
| 385 } | 620 } |
| 386 Py_CLEAR(chunks); | 621 Py_CLEAR(chunks); |
| 387 return Py_BuildValue("(Nn)", rval, end); | 622 *next_end_ptr = end; |
| 623 return rval; | |
| 388 bail: | 624 bail: |
| 625 *next_end_ptr = -1; | |
| 389 Py_XDECREF(chunks); | 626 Py_XDECREF(chunks); |
| 390 return NULL; | 627 return NULL; |
| 391 } | 628 } |
| 392 | 629 |
| 393 | 630 |
| 394 static PyObject * | 631 static PyObject * |
| 395 scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict) | 632 scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next _end_ptr) |
| 396 { | 633 { |
| 634 /* Read the JSON string from PyUnicode pystr. | |
| 635 end is the index of the first character after the quote. | |
| 636 if strict is zero then literal control characters are allowed | |
| 637 *next_end_ptr is a return-by-reference index of the character | |
| 638 after the end quote | |
| 639 | |
| 640 Return value is a new PyUnicode | |
| 641 */ | |
| 397 PyObject *rval; | 642 PyObject *rval; |
| 398 Py_ssize_t len = PyUnicode_GET_SIZE(pystr); | 643 Py_ssize_t len = PyUnicode_GET_SIZE(pystr); |
| 399 Py_ssize_t begin = end - 1; | 644 Py_ssize_t begin = end - 1; |
| 400 Py_ssize_t next = begin; | 645 Py_ssize_t next = begin; |
| 401 const Py_UNICODE *buf = PyUnicode_AS_UNICODE(pystr); | 646 const Py_UNICODE *buf = PyUnicode_AS_UNICODE(pystr); |
| 402 PyObject *chunks = PyList_New(0); | 647 PyObject *chunks = PyList_New(0); |
| 403 if (chunks == NULL) { | 648 if (chunks == NULL) { |
| 404 goto bail; | 649 goto bail; |
| 405 } | 650 } |
| 406 if (end < 0 || len <= end) { | 651 if (end < 0 || len <= end) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 469 else { | 714 else { |
| 470 c = 0; | 715 c = 0; |
| 471 next++; | 716 next++; |
| 472 end = next + 4; | 717 end = next + 4; |
| 473 if (end >= len) { | 718 if (end >= len) { |
| 474 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1); | 719 raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1); |
| 475 goto bail; | 720 goto bail; |
| 476 } | 721 } |
| 477 /* Decode 4 hex digits */ | 722 /* Decode 4 hex digits */ |
| 478 for (; next < end; next++) { | 723 for (; next < end; next++) { |
| 479 Py_ssize_t shl = (end - next - 1) << 2; | |
| 480 Py_UNICODE digit = buf[next]; | 724 Py_UNICODE digit = buf[next]; |
| 725 c <<= 4; | |
| 481 switch (digit) { | 726 switch (digit) { |
| 482 case '0': case '1': case '2': case '3': case '4': | 727 case '0': case '1': case '2': case '3': case '4': |
| 483 case '5': case '6': case '7': case '8': case '9': | 728 case '5': case '6': case '7': case '8': case '9': |
| 484 c |= (digit - '0') << shl; break; | 729 c |= (digit - '0'); break; |
| 485 case 'a': case 'b': case 'c': case 'd': case 'e': | 730 case 'a': case 'b': case 'c': case 'd': case 'e': |
| 486 case 'f': | 731 case 'f': |
| 487 c |= (digit - 'a' + 10) << shl; break; | 732 c |= (digit - 'a' + 10); break; |
| 488 case 'A': case 'B': case 'C': case 'D': case 'E': | 733 case 'A': case 'B': case 'C': case 'D': case 'E': |
| 489 case 'F': | 734 case 'F': |
| 490 c |= (digit - 'A' + 10) << shl; break; | 735 c |= (digit - 'A' + 10); break; |
| 491 default: | 736 default: |
| 492 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); | 737 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); |
| 493 goto bail; | 738 goto bail; |
| 494 } | 739 } |
| 495 } | 740 } |
| 496 #ifdef Py_UNICODE_WIDE | 741 #ifdef Py_UNICODE_WIDE |
| 497 /* Surrogate pair */ | 742 /* Surrogate pair */ |
| 498 if (c >= 0xd800 && c <= 0xdbff) { | 743 if ((c & 0xfc00) == 0xd800) { |
| 499 Py_UNICODE c2 = 0; | 744 Py_UNICODE c2 = 0; |
| 500 if (end + 6 >= len) { | 745 if (end + 6 >= len) { |
| 501 raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr, | 746 raise_errmsg("Unpaired high surrogate", pystr, end - 5); |
| 502 end - 5); | 747 goto bail; |
| 503 } | 748 } |
| 504 if (buf[next++] != '\\' || buf[next++] != 'u') { | 749 if (buf[next++] != '\\' || buf[next++] != 'u') { |
| 505 raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr, | 750 raise_errmsg("Unpaired high surrogate", pystr, end - 5); |
| 506 end - 5); | 751 goto bail; |
| 507 } | 752 } |
| 508 end += 6; | 753 end += 6; |
| 509 /* Decode 4 hex digits */ | 754 /* Decode 4 hex digits */ |
| 510 for (; next < end; next++) { | 755 for (; next < end; next++) { |
| 511 Py_ssize_t shl = (end - next - 1) << 2; | 756 c2 <<= 4; |
| 512 Py_UNICODE digit = buf[next]; | 757 Py_UNICODE digit = buf[next]; |
| 513 switch (digit) { | 758 switch (digit) { |
| 514 case '0': case '1': case '2': case '3': case '4': | 759 case '0': case '1': case '2': case '3': case '4': |
| 515 case '5': case '6': case '7': case '8': case '9': | 760 case '5': case '6': case '7': case '8': case '9': |
| 516 c2 |= (digit - '0') << shl; break; | 761 c2 |= (digit - '0'); break; |
| 517 case 'a': case 'b': case 'c': case 'd': case 'e': | 762 case 'a': case 'b': case 'c': case 'd': case 'e': |
| 518 case 'f': | 763 case 'f': |
| 519 c2 |= (digit - 'a' + 10) << shl; break; | 764 c2 |= (digit - 'a' + 10); break; |
| 520 case 'A': case 'B': case 'C': case 'D': case 'E': | 765 case 'A': case 'B': case 'C': case 'D': case 'E': |
| 521 case 'F': | 766 case 'F': |
| 522 c2 |= (digit - 'A' + 10) << shl; break; | 767 c2 |= (digit - 'A' + 10); break; |
| 523 default: | 768 default: |
| 524 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); | 769 raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); |
| 525 goto bail; | 770 goto bail; |
| 526 } | 771 } |
| 527 } | 772 } |
| 773 if ((c2 & 0xfc00) != 0xdc00) { | |
| 774 raise_errmsg("Unpaired high surrogate", pystr, end - 5); | |
| 775 goto bail; | |
| 776 } | |
| 528 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00)); | 777 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00)); |
| 778 } | |
| 779 else if ((c & 0xfc00) == 0xdc00) { | |
| 780 raise_errmsg("Unpaired low surrogate", pystr, end - 5); | |
| 781 goto bail; | |
| 529 } | 782 } |
| 530 #endif | 783 #endif |
| 531 } | 784 } |
| 532 chunk = PyUnicode_FromUnicode(&c, 1); | 785 chunk = PyUnicode_FromUnicode(&c, 1); |
| 533 if (chunk == NULL) { | 786 if (chunk == NULL) { |
| 534 goto bail; | 787 goto bail; |
| 535 } | 788 } |
| 536 if (PyList_Append(chunks, chunk)) { | 789 if (PyList_Append(chunks, chunk)) { |
| 537 Py_DECREF(chunk); | 790 Py_DECREF(chunk); |
| 538 goto bail; | 791 goto bail; |
| 539 } | 792 } |
| 540 Py_DECREF(chunk); | 793 Py_DECREF(chunk); |
| 541 } | 794 } |
| 542 | 795 |
| 543 rval = join_list_unicode(chunks); | 796 rval = join_list_unicode(chunks); |
| 544 if (rval == NULL) { | 797 if (rval == NULL) { |
| 545 goto bail; | 798 goto bail; |
| 546 } | 799 } |
| 547 Py_CLEAR(chunks); | 800 Py_DECREF(chunks); |
| 548 return Py_BuildValue("(Nn)", rval, end); | 801 *next_end_ptr = end; |
| 802 return rval; | |
| 549 bail: | 803 bail: |
| 804 *next_end_ptr = -1; | |
| 550 Py_XDECREF(chunks); | 805 Py_XDECREF(chunks); |
| 551 return NULL; | 806 return NULL; |
| 552 } | 807 } |
| 553 | 808 |
| 554 PyDoc_STRVAR(pydoc_scanstring, | 809 PyDoc_STRVAR(pydoc_scanstring, |
| 555 "scanstring(basestring, end, encoding) -> (str, end)\n"); | 810 "scanstring(basestring, end, encoding, strict=True) -> (str, end)\n" |
| 811 "\n" | |
| 812 "Scan the string s for a JSON string. End is the index of the\n" | |
| 813 "character in s after the quote that started the JSON string.\n" | |
| 814 "Unescapes all valid JSON string escape sequences and raises ValueError\n" | |
| 815 "on attempt to decode an invalid string. If strict is False then literal\n" | |
| 816 "control characters are allowed in the string.\n" | |
| 817 "\n" | |
| 818 "Returns a tuple of the decoded string and the index of the character in s\n " | |
| 819 "after the end quote." | |
| 820 ); | |
| 556 | 821 |
| 557 static PyObject * | 822 static PyObject * |
| 558 py_scanstring(PyObject* self, PyObject *args) | 823 py_scanstring(PyObject* self UNUSED, PyObject *args) |
| 559 { | 824 { |
| 560 PyObject *pystr; | 825 PyObject *pystr; |
| 826 PyObject *rval; | |
| 561 Py_ssize_t end; | 827 Py_ssize_t end; |
| 828 Py_ssize_t next_end = -1; | |
| 562 char *encoding = NULL; | 829 char *encoding = NULL; |
| 563 int strict = 0; | 830 int strict = 1; |
| 564 if (!PyArg_ParseTuple(args, "On|zi:scanstring", &pystr, &end, &encoding, &st rict)) { | 831 if (!PyArg_ParseTuple(args, "OO&|zi:scanstring", &pystr, _convertPyInt_AsSsi ze_t, &end, &encoding, &strict)) { |
| 565 return NULL; | 832 return NULL; |
| 566 } | 833 } |
| 567 if (encoding == NULL) { | 834 if (encoding == NULL) { |
| 568 encoding = DEFAULT_ENCODING; | 835 encoding = DEFAULT_ENCODING; |
| 569 } | 836 } |
| 570 if (PyString_Check(pystr)) { | 837 if (PyString_Check(pystr)) { |
| 571 return scanstring_str(pystr, end, encoding, strict); | 838 rval = scanstring_str(pystr, end, encoding, strict, &next_end); |
| 572 } | 839 } |
| 573 else if (PyUnicode_Check(pystr)) { | 840 else if (PyUnicode_Check(pystr)) { |
| 574 return scanstring_unicode(pystr, end, strict); | 841 rval = scanstring_unicode(pystr, end, strict, &next_end); |
| 575 } | 842 } |
| 576 else { | 843 else { |
| 577 PyErr_Format(PyExc_TypeError, | 844 PyErr_Format(PyExc_TypeError, |
| 578 "first argument must be a string or unicode, not %.80s", | 845 "first argument must be a string, not %.80s", |
| 579 Py_TYPE(pystr)->tp_name); | 846 Py_TYPE(pystr)->tp_name); |
| 580 return NULL; | 847 return NULL; |
| 581 } | 848 } |
| 849 return _build_rval_index_tuple(rval, next_end); | |
| 582 } | 850 } |
| 583 | 851 |
| 584 PyDoc_STRVAR(pydoc_encode_basestring_ascii, | 852 PyDoc_STRVAR(pydoc_encode_basestring_ascii, |
| 585 "encode_basestring_ascii(basestring) -> str\n"); | 853 "encode_basestring_ascii(basestring) -> str\n" |
| 854 "\n" | |
| 855 "Return an ASCII-only JSON representation of a Python string" | |
| 856 ); | |
| 586 | 857 |
| 587 static PyObject * | 858 static PyObject * |
| 588 py_encode_basestring_ascii(PyObject* self, PyObject *pystr) | 859 py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr) |
| 589 { | 860 { |
| 861 /* Return an ASCII-only JSON representation of a Python string */ | |
| 590 /* METH_O */ | 862 /* METH_O */ |
| 591 if (PyString_Check(pystr)) { | 863 if (PyString_Check(pystr)) { |
| 592 return ascii_escape_str(pystr); | 864 return ascii_escape_str(pystr); |
| 593 } | 865 } |
| 594 else if (PyUnicode_Check(pystr)) { | 866 else if (PyUnicode_Check(pystr)) { |
| 595 return ascii_escape_unicode(pystr); | 867 return ascii_escape_unicode(pystr); |
| 596 } | 868 } |
| 597 else { | 869 else { |
| 598 PyErr_Format(PyExc_TypeError, | 870 PyErr_Format(PyExc_TypeError, |
| 599 "first argument must be a string or unicode, not %.80s", | 871 "first argument must be a string, not %.80s", |
| 600 Py_TYPE(pystr)->tp_name); | 872 Py_TYPE(pystr)->tp_name); |
| 601 return NULL; | 873 return NULL; |
| 602 } | 874 } |
| 603 } | 875 } |
| 604 | 876 |
| 605 static PyMethodDef json_methods[] = { | 877 static void |
| 606 {"encode_basestring_ascii", (PyCFunction)py_encode_basestring_ascii, | 878 scanner_dealloc(PyObject *self) |
| 607 METH_O, pydoc_encode_basestring_ascii}, | 879 { |
| 608 {"scanstring", (PyCFunction)py_scanstring, METH_VARARGS, | 880 /* Deallocate scanner object */ |
| 609 pydoc_scanstring}, | 881 scanner_clear(self); |
| 882 Py_TYPE(self)->tp_free(self); | |
| 883 } | |
| 884 | |
| 885 static int | |
| 886 scanner_traverse(PyObject *self, visitproc visit, void *arg) | |
| 887 { | |
| 888 PyScannerObject *s; | |
| 889 assert(PyScanner_Check(self)); | |
| 890 s = (PyScannerObject *)self; | |
| 891 Py_VISIT(s->encoding); | |
| 892 Py_VISIT(s->strict); | |
| 893 Py_VISIT(s->object_hook); | |
| 894 Py_VISIT(s->parse_float); | |
| 895 Py_VISIT(s->parse_int); | |
| 896 Py_VISIT(s->parse_constant); | |
| 897 return 0; | |
| 898 } | |
| 899 | |
| 900 static int | |
| 901 scanner_clear(PyObject *self) | |
| 902 { | |
| 903 PyScannerObject *s; | |
| 904 assert(PyScanner_Check(self)); | |
| 905 s = (PyScannerObject *)self; | |
| 906 Py_CLEAR(s->encoding); | |
| 907 Py_CLEAR(s->strict); | |
| 908 Py_CLEAR(s->object_hook); | |
| 909 Py_CLEAR(s->parse_float); | |
| 910 Py_CLEAR(s->parse_int); | |
| 911 Py_CLEAR(s->parse_constant); | |
| 912 return 0; | |
| 913 } | |
| 914 | |
| 915 static PyObject * | |
| 916 _parse_object_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_ t *next_idx_ptr) { | |
| 917 /* Read a JSON object from PyString pystr. | |
| 918 idx is the index of the first character after the opening curly brace. | |
| 919 *next_idx_ptr is a return-by-reference index to the first character after | |
| 920 the closing curly brace. | |
| 921 | |
| 922 Returns a new PyObject (usually a dict, but object_hook can change that) | |
| 923 */ | |
| 924 char *str = PyString_AS_STRING(pystr); | |
| 925 Py_ssize_t end_idx = PyString_GET_SIZE(pystr) - 1; | |
| 926 PyObject *rval = PyDict_New(); | |
| 927 PyObject *key = NULL; | |
| 928 PyObject *val = NULL; | |
| 929 char *encoding = PyString_AS_STRING(s->encoding); | |
| 930 int strict = PyObject_IsTrue(s->strict); | |
| 931 Py_ssize_t next_idx; | |
| 932 if (rval == NULL) | |
| 933 return NULL; | |
| 934 | |
| 935 /* skip whitespace after { */ | |
| 936 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 937 | |
| 938 /* only loop if the object is non-empty */ | |
| 939 if (idx <= end_idx && str[idx] != '}') { | |
| 940 while (idx <= end_idx) { | |
| 941 /* read key */ | |
| 942 if (str[idx] != '"') { | |
| 943 raise_errmsg("Expecting property name", pystr, idx); | |
| 944 goto bail; | |
| 945 } | |
| 946 key = scanstring_str(pystr, idx + 1, encoding, strict, &next_idx); | |
| 947 if (key == NULL) | |
| 948 goto bail; | |
| 949 idx = next_idx; | |
| 950 | |
| 951 /* skip whitespace between key and : delimiter, read :, skip whitesp ace */ | |
| 952 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 953 if (idx > end_idx || str[idx] != ':') { | |
| 954 raise_errmsg("Expecting : delimiter", pystr, idx); | |
| 955 goto bail; | |
| 956 } | |
| 957 idx++; | |
| 958 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 959 | |
| 960 /* read any JSON data type */ | |
| 961 val = scan_once_str(s, pystr, idx, &next_idx); | |
| 962 if (val == NULL) | |
| 963 goto bail; | |
| 964 | |
| 965 if (PyDict_SetItem(rval, key, val) == -1) | |
| 966 goto bail; | |
| 967 | |
| 968 Py_CLEAR(key); | |
| 969 Py_CLEAR(val); | |
| 970 idx = next_idx; | |
| 971 | |
| 972 /* skip whitespace before } or , */ | |
| 973 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 974 | |
| 975 /* bail if the object is closed or we didn't get the , delimiter */ | |
| 976 if (idx > end_idx) break; | |
| 977 if (str[idx] == '}') { | |
| 978 break; | |
| 979 } | |
| 980 else if (str[idx] != ',') { | |
| 981 raise_errmsg("Expecting , delimiter", pystr, idx); | |
| 982 goto bail; | |
| 983 } | |
| 984 idx++; | |
| 985 | |
| 986 /* skip whitespace after , delimiter */ | |
| 987 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 988 } | |
| 989 } | |
| 990 /* verify that idx < end_idx, str[idx] should be '}' */ | |
| 991 if (idx > end_idx || str[idx] != '}') { | |
| 992 raise_errmsg("Expecting object", pystr, end_idx); | |
| 993 goto bail; | |
| 994 } | |
| 995 /* if object_hook is not None: rval = object_hook(rval) */ | |
| 996 if (s->object_hook != Py_None) { | |
| 997 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL); | |
| 998 if (val == NULL) | |
| 999 goto bail; | |
| 1000 Py_DECREF(rval); | |
| 1001 rval = val; | |
| 1002 val = NULL; | |
| 1003 } | |
| 1004 *next_idx_ptr = idx + 1; | |
| 1005 return rval; | |
| 1006 bail: | |
| 1007 Py_XDECREF(key); | |
| 1008 Py_XDECREF(val); | |
| 1009 Py_DECREF(rval); | |
| 1010 return NULL; | |
| 1011 } | |
| 1012 | |
| 1013 static PyObject * | |
| 1014 _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss ize_t *next_idx_ptr) { | |
| 1015 /* Read a JSON object from PyUnicode pystr. | |
| 1016 idx is the index of the first character after the opening curly brace. | |
| 1017 *next_idx_ptr is a return-by-reference index to the first character after | |
| 1018 the closing curly brace. | |
| 1019 | |
| 1020 Returns a new PyObject (usually a dict, but object_hook can change that) | |
| 1021 */ | |
| 1022 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr); | |
| 1023 Py_ssize_t end_idx = PyUnicode_GET_SIZE(pystr) - 1; | |
| 1024 PyObject *val = NULL; | |
| 1025 PyObject *rval = PyDict_New(); | |
| 1026 PyObject *key = NULL; | |
| 1027 int strict = PyObject_IsTrue(s->strict); | |
| 1028 Py_ssize_t next_idx; | |
| 1029 if (rval == NULL) | |
| 1030 return NULL; | |
| 1031 | |
| 1032 /* skip whitespace after { */ | |
| 1033 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 1034 | |
| 1035 /* only loop if the object is non-empty */ | |
| 1036 if (idx <= end_idx && str[idx] != '}') { | |
| 1037 while (idx <= end_idx) { | |
| 1038 /* read key */ | |
| 1039 if (str[idx] != '"') { | |
| 1040 raise_errmsg("Expecting property name", pystr, idx); | |
| 1041 goto bail; | |
| 1042 } | |
| 1043 key = scanstring_unicode(pystr, idx + 1, strict, &next_idx); | |
| 1044 if (key == NULL) | |
| 1045 goto bail; | |
| 1046 idx = next_idx; | |
| 1047 | |
| 1048 /* skip whitespace between key and : delimiter, read :, skip whitesp ace */ | |
| 1049 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 1050 if (idx > end_idx || str[idx] != ':') { | |
| 1051 raise_errmsg("Expecting : delimiter", pystr, idx); | |
| 1052 goto bail; | |
| 1053 } | |
| 1054 idx++; | |
| 1055 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 1056 | |
| 1057 /* read any JSON term */ | |
| 1058 val = scan_once_unicode(s, pystr, idx, &next_idx); | |
| 1059 if (val == NULL) | |
| 1060 goto bail; | |
| 1061 | |
| 1062 if (PyDict_SetItem(rval, key, val) == -1) | |
| 1063 goto bail; | |
| 1064 | |
| 1065 Py_CLEAR(key); | |
| 1066 Py_CLEAR(val); | |
| 1067 idx = next_idx; | |
| 1068 | |
| 1069 /* skip whitespace before } or , */ | |
| 1070 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 1071 | |
| 1072 /* bail if the object is closed or we didn't get the , delimiter */ | |
| 1073 if (idx > end_idx) break; | |
| 1074 if (str[idx] == '}') { | |
| 1075 break; | |
| 1076 } | |
| 1077 else if (str[idx] != ',') { | |
| 1078 raise_errmsg("Expecting , delimiter", pystr, idx); | |
| 1079 goto bail; | |
| 1080 } | |
| 1081 idx++; | |
| 1082 | |
| 1083 /* skip whitespace after , delimiter */ | |
| 1084 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 1085 } | |
| 1086 } | |
| 1087 | |
| 1088 /* verify that idx < end_idx, str[idx] should be '}' */ | |
| 1089 if (idx > end_idx || str[idx] != '}') { | |
| 1090 raise_errmsg("Expecting object", pystr, end_idx); | |
| 1091 goto bail; | |
| 1092 } | |
| 1093 | |
| 1094 /* if object_hook is not None: rval = object_hook(rval) */ | |
| 1095 if (s->object_hook != Py_None) { | |
| 1096 val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL); | |
| 1097 if (val == NULL) | |
| 1098 goto bail; | |
| 1099 Py_DECREF(rval); | |
| 1100 rval = val; | |
| 1101 val = NULL; | |
| 1102 } | |
| 1103 *next_idx_ptr = idx + 1; | |
| 1104 return rval; | |
| 1105 bail: | |
| 1106 Py_XDECREF(key); | |
| 1107 Py_XDECREF(val); | |
| 1108 Py_DECREF(rval); | |
| 1109 return NULL; | |
| 1110 } | |
| 1111 | |
| 1112 static PyObject * | |
| 1113 _parse_array_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) { | |
| 1114 /* Read a JSON array from PyString pystr. | |
| 1115 idx is the index of the first character after the opening brace. | |
| 1116 *next_idx_ptr is a return-by-reference index to the first character after | |
| 1117 the closing brace. | |
| 1118 | |
| 1119 Returns a new PyList | |
| 1120 */ | |
| 1121 char *str = PyString_AS_STRING(pystr); | |
| 1122 Py_ssize_t end_idx = PyString_GET_SIZE(pystr) - 1; | |
| 1123 PyObject *val = NULL; | |
| 1124 PyObject *rval = PyList_New(0); | |
| 1125 Py_ssize_t next_idx; | |
| 1126 if (rval == NULL) | |
| 1127 return NULL; | |
| 1128 | |
| 1129 /* skip whitespace after [ */ | |
| 1130 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 1131 | |
| 1132 /* only loop if the array is non-empty */ | |
| 1133 if (idx <= end_idx && str[idx] != ']') { | |
| 1134 while (idx <= end_idx) { | |
| 1135 | |
| 1136 /* read any JSON term and de-tuplefy the (rval, idx) */ | |
| 1137 val = scan_once_str(s, pystr, idx, &next_idx); | |
| 1138 if (val == NULL) | |
| 1139 goto bail; | |
| 1140 | |
| 1141 if (PyList_Append(rval, val) == -1) | |
| 1142 goto bail; | |
| 1143 | |
| 1144 Py_CLEAR(val); | |
| 1145 idx = next_idx; | |
| 1146 | |
| 1147 /* skip whitespace between term and , */ | |
| 1148 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 1149 | |
| 1150 /* bail if the array is closed or we didn't get the , delimiter */ | |
| 1151 if (idx > end_idx) break; | |
| 1152 if (str[idx] == ']') { | |
| 1153 break; | |
| 1154 } | |
| 1155 else if (str[idx] != ',') { | |
| 1156 raise_errmsg("Expecting , delimiter", pystr, idx); | |
| 1157 goto bail; | |
| 1158 } | |
| 1159 idx++; | |
| 1160 | |
| 1161 /* skip whitespace after , */ | |
| 1162 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 1163 } | |
| 1164 } | |
| 1165 | |
| 1166 /* verify that idx < end_idx, str[idx] should be ']' */ | |
| 1167 if (idx > end_idx || str[idx] != ']') { | |
| 1168 raise_errmsg("Expecting object", pystr, end_idx); | |
| 1169 goto bail; | |
| 1170 } | |
| 1171 *next_idx_ptr = idx + 1; | |
| 1172 return rval; | |
| 1173 bail: | |
| 1174 Py_XDECREF(val); | |
| 1175 Py_DECREF(rval); | |
| 1176 return NULL; | |
| 1177 } | |
| 1178 | |
| 1179 static PyObject * | |
| 1180 _parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssi ze_t *next_idx_ptr) { | |
| 1181 /* Read a JSON array from PyString pystr. | |
| 1182 idx is the index of the first character after the opening brace. | |
| 1183 *next_idx_ptr is a return-by-reference index to the first character after | |
| 1184 the closing brace. | |
| 1185 | |
| 1186 Returns a new PyList | |
| 1187 */ | |
| 1188 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr); | |
| 1189 Py_ssize_t end_idx = PyUnicode_GET_SIZE(pystr) - 1; | |
| 1190 PyObject *val = NULL; | |
| 1191 PyObject *rval = PyList_New(0); | |
| 1192 Py_ssize_t next_idx; | |
| 1193 if (rval == NULL) | |
| 1194 return NULL; | |
| 1195 | |
| 1196 /* skip whitespace after [ */ | |
| 1197 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 1198 | |
| 1199 /* only loop if the array is non-empty */ | |
| 1200 if (idx <= end_idx && str[idx] != ']') { | |
| 1201 while (idx <= end_idx) { | |
| 1202 | |
| 1203 /* read any JSON term */ | |
| 1204 val = scan_once_unicode(s, pystr, idx, &next_idx); | |
| 1205 if (val == NULL) | |
| 1206 goto bail; | |
| 1207 | |
| 1208 if (PyList_Append(rval, val) == -1) | |
| 1209 goto bail; | |
| 1210 | |
| 1211 Py_CLEAR(val); | |
| 1212 idx = next_idx; | |
| 1213 | |
| 1214 /* skip whitespace between term and , */ | |
| 1215 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 1216 | |
| 1217 /* bail if the array is closed or we didn't get the , delimiter */ | |
| 1218 if (idx > end_idx) break; | |
| 1219 if (str[idx] == ']') { | |
| 1220 break; | |
| 1221 } | |
| 1222 else if (str[idx] != ',') { | |
| 1223 raise_errmsg("Expecting , delimiter", pystr, idx); | |
| 1224 goto bail; | |
| 1225 } | |
| 1226 idx++; | |
| 1227 | |
| 1228 /* skip whitespace after , */ | |
| 1229 while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++; | |
| 1230 } | |
| 1231 } | |
| 1232 | |
| 1233 /* verify that idx < end_idx, str[idx] should be ']' */ | |
| 1234 if (idx > end_idx || str[idx] != ']') { | |
| 1235 raise_errmsg("Expecting object", pystr, end_idx); | |
| 1236 goto bail; | |
| 1237 } | |
| 1238 *next_idx_ptr = idx + 1; | |
| 1239 return rval; | |
| 1240 bail: | |
| 1241 Py_XDECREF(val); | |
| 1242 Py_DECREF(rval); | |
| 1243 return NULL; | |
| 1244 } | |
| 1245 | |
| 1246 static PyObject * | |
| 1247 _parse_constant(PyScannerObject *s, char *constant, Py_ssize_t idx, Py_ssize_t * next_idx_ptr) { | |
| 1248 /* Read a JSON constant from PyString pystr. | |
| 1249 constant is the constant string that was found | |
| 1250 ("NaN", "Infinity", "-Infinity"). | |
| 1251 idx is the index of the first character of the constant | |
| 1252 *next_idx_ptr is a return-by-reference index to the first character after | |
| 1253 the constant. | |
| 1254 | |
| 1255 Returns the result of parse_constant | |
| 1256 */ | |
| 1257 PyObject *cstr; | |
| 1258 PyObject *rval; | |
| 1259 /* constant is "NaN", "Infinity", or "-Infinity" */ | |
| 1260 cstr = PyString_InternFromString(constant); | |
| 1261 if (cstr == NULL) | |
| 1262 return NULL; | |
| 1263 | |
| 1264 /* rval = parse_constant(constant) */ | |
| 1265 rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL); | |
| 1266 idx += PyString_GET_SIZE(cstr); | |
| 1267 Py_DECREF(cstr); | |
| 1268 *next_idx_ptr = idx; | |
| 1269 return rval; | |
| 1270 } | |
| 1271 | |
| 1272 static PyObject * | |
| 1273 _match_number_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssiz e_t *next_idx_ptr) { | |
| 1274 /* Read a JSON number from PyString pystr. | |
| 1275 idx is the index of the first character of the number | |
| 1276 *next_idx_ptr is a return-by-reference index to the first character after | |
| 1277 the number. | |
| 1278 | |
| 1279 Returns a new PyObject representation of that number: | |
| 1280 PyInt, PyLong, or PyFloat. | |
| 1281 May return other types if parse_int or parse_float are set | |
| 1282 */ | |
| 1283 char *str = PyString_AS_STRING(pystr); | |
| 1284 Py_ssize_t end_idx = PyString_GET_SIZE(pystr) - 1; | |
| 1285 Py_ssize_t idx = start; | |
| 1286 int is_float = 0; | |
| 1287 PyObject *rval; | |
| 1288 PyObject *numstr; | |
| 1289 | |
| 1290 /* read a sign if it's there, make sure it's not the end of the string */ | |
| 1291 if (str[idx] == '-') { | |
| 1292 idx++; | |
| 1293 if (idx > end_idx) { | |
| 1294 PyErr_SetNone(PyExc_StopIteration); | |
| 1295 return NULL; | |
| 1296 } | |
| 1297 } | |
| 1298 | |
| 1299 /* read as many integer digits as we find as long as it doesn't start with 0 */ | |
| 1300 if (str[idx] >= '1' && str[idx] <= '9') { | |
| 1301 idx++; | |
| 1302 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++; | |
| 1303 } | |
| 1304 /* if it starts with 0 we only expect one integer digit */ | |
| 1305 else if (str[idx] == '0') { | |
| 1306 idx++; | |
| 1307 } | |
| 1308 /* no integer digits, error */ | |
| 1309 else { | |
| 1310 PyErr_SetNone(PyExc_StopIteration); | |
| 1311 return NULL; | |
| 1312 } | |
| 1313 | |
| 1314 /* if the next char is '.' followed by a digit then read all float digits */ | |
| 1315 if (idx < end_idx && str[idx] == '.' && str[idx + 1] >= '0' && str[idx + 1] <= '9') { | |
| 1316 is_float = 1; | |
| 1317 idx += 2; | |
| 1318 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++; | |
| 1319 } | |
| 1320 | |
| 1321 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack ) */ | |
| 1322 if (idx < end_idx && (str[idx] == 'e' || str[idx] == 'E')) { | |
| 1323 | |
| 1324 /* save the index of the 'e' or 'E' just in case we need to backtrack */ | |
| 1325 Py_ssize_t e_start = idx; | |
| 1326 idx++; | |
| 1327 | |
| 1328 /* read an exponent sign if present */ | |
| 1329 if (idx < end_idx && (str[idx] == '-' || str[idx] == '+')) idx++; | |
| 1330 | |
| 1331 /* read all digits */ | |
| 1332 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++; | |
| 1333 | |
| 1334 /* if we got a digit, then parse as float. if not, backtrack */ | |
| 1335 if (str[idx - 1] >= '0' && str[idx - 1] <= '9') { | |
| 1336 is_float = 1; | |
| 1337 } | |
| 1338 else { | |
| 1339 idx = e_start; | |
| 1340 } | |
| 1341 } | |
| 1342 | |
| 1343 /* copy the section we determined to be a number */ | |
| 1344 numstr = PyString_FromStringAndSize(&str[start], idx - start); | |
| 1345 if (numstr == NULL) | |
| 1346 return NULL; | |
| 1347 if (is_float) { | |
| 1348 /* parse as a float using a fast path if available, otherwise call user defined method */ | |
| 1349 if (s->parse_float != (PyObject *)&PyFloat_Type) { | |
| 1350 rval = PyObject_CallFunctionObjArgs(s->parse_float, numstr, NULL); | |
| 1351 } | |
| 1352 else { | |
| 1353 rval = PyFloat_FromDouble(PyOS_ascii_atof(PyString_AS_STRING(numstr) )); | |
| 1354 } | |
| 1355 } | |
| 1356 else { | |
| 1357 /* parse as an int using a fast path if available, otherwise call user d efined method */ | |
| 1358 if (s->parse_int != (PyObject *)&PyInt_Type) { | |
| 1359 rval = PyObject_CallFunctionObjArgs(s->parse_int, numstr, NULL); | |
| 1360 } | |
| 1361 else { | |
| 1362 rval = PyInt_FromString(PyString_AS_STRING(numstr), NULL, 10); | |
| 1363 } | |
| 1364 } | |
| 1365 Py_DECREF(numstr); | |
| 1366 *next_idx_ptr = idx; | |
| 1367 return rval; | |
| 1368 } | |
| 1369 | |
| 1370 static PyObject * | |
| 1371 _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ ssize_t *next_idx_ptr) { | |
| 1372 /* Read a JSON number from PyUnicode pystr. | |
| 1373 idx is the index of the first character of the number | |
| 1374 *next_idx_ptr is a return-by-reference index to the first character after | |
| 1375 the number. | |
| 1376 | |
| 1377 Returns a new PyObject representation of that number: | |
| 1378 PyInt, PyLong, or PyFloat. | |
| 1379 May return other types if parse_int or parse_float are set | |
| 1380 */ | |
| 1381 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr); | |
| 1382 Py_ssize_t end_idx = PyUnicode_GET_SIZE(pystr) - 1; | |
| 1383 Py_ssize_t idx = start; | |
| 1384 int is_float = 0; | |
| 1385 PyObject *rval; | |
| 1386 PyObject *numstr; | |
| 1387 | |
| 1388 /* read a sign if it's there, make sure it's not the end of the string */ | |
| 1389 if (str[idx] == '-') { | |
| 1390 idx++; | |
| 1391 if (idx > end_idx) { | |
| 1392 PyErr_SetNone(PyExc_StopIteration); | |
| 1393 return NULL; | |
| 1394 } | |
| 1395 } | |
| 1396 | |
| 1397 /* read as many integer digits as we find as long as it doesn't start with 0 */ | |
| 1398 if (str[idx] >= '1' && str[idx] <= '9') { | |
| 1399 idx++; | |
| 1400 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++; | |
| 1401 } | |
| 1402 /* if it starts with 0 we only expect one integer digit */ | |
| 1403 else if (str[idx] == '0') { | |
| 1404 idx++; | |
| 1405 } | |
| 1406 /* no integer digits, error */ | |
| 1407 else { | |
| 1408 PyErr_SetNone(PyExc_StopIteration); | |
| 1409 return NULL; | |
| 1410 } | |
| 1411 | |
| 1412 /* if the next char is '.' followed by a digit then read all float digits */ | |
| 1413 if (idx < end_idx && str[idx] == '.' && str[idx + 1] >= '0' && str[idx + 1] <= '9') { | |
| 1414 is_float = 1; | |
| 1415 idx += 2; | |
| 1416 while (idx < end_idx && str[idx] >= '0' && str[idx] <= '9') idx++; | |
| 1417 } | |
| 1418 | |
| 1419 /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack ) */ | |
| 1420 if (idx < end_idx && (str[idx] == 'e' || str[idx] == 'E')) { | |
| 1421 Py_ssize_t e_start = idx; | |
| 1422 idx++; | |
| 1423 | |
| 1424 /* read an exponent sign if present */ | |
| 1425 if (idx < end_idx && (str[idx] == '-' || str[idx] == '+')) idx++; | |
| 1426 | |
| 1427 /* read all digits */ | |
| 1428 while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++; | |
| 1429 | |
| 1430 /* if we got a digit, then parse as float. if not, backtrack */ | |
| 1431 if (str[idx - 1] >= '0' && str[idx - 1] <= '9') { | |
| 1432 is_float = 1; | |
| 1433 } | |
| 1434 else { | |
| 1435 idx = e_start; | |
| 1436 } | |
| 1437 } | |
| 1438 | |
| 1439 /* copy the section we determined to be a number */ | |
| 1440 numstr = PyUnicode_FromUnicode(&str[start], idx - start); | |
| 1441 if (numstr == NULL) | |
| 1442 return NULL; | |
| 1443 if (is_float) { | |
| 1444 /* parse as a float using a fast path if available, otherwise call user defined method */ | |
| 1445 if (s->parse_float != (PyObject *)&PyFloat_Type) { | |
| 1446 rval = PyObject_CallFunctionObjArgs(s->parse_float, numstr, NULL); | |
| 1447 } | |
| 1448 else { | |
| 1449 rval = PyFloat_FromString(numstr, NULL); | |
| 1450 } | |
| 1451 } | |
| 1452 else { | |
| 1453 /* no fast path for unicode -> int, just call */ | |
| 1454 rval = PyObject_CallFunctionObjArgs(s->parse_int, numstr, NULL); | |
| 1455 } | |
| 1456 Py_DECREF(numstr); | |
| 1457 *next_idx_ptr = idx; | |
| 1458 return rval; | |
| 1459 } | |
| 1460 | |
| 1461 static PyObject * | |
| 1462 scan_once_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *n ext_idx_ptr) | |
| 1463 { | |
| 1464 /* Read one JSON term (of any kind) from PyString pystr. | |
| 1465 idx is the index of the first character of the term | |
| 1466 *next_idx_ptr is a return-by-reference index to the first character after | |
| 1467 the number. | |
| 1468 | |
| 1469 Returns a new PyObject representation of the term. | |
| 1470 */ | |
| 1471 char *str = PyString_AS_STRING(pystr); | |
| 1472 Py_ssize_t length = PyString_GET_SIZE(pystr); | |
| 1473 if (idx >= length) { | |
| 1474 PyErr_SetNone(PyExc_StopIteration); | |
| 1475 return NULL; | |
| 1476 } | |
| 1477 switch (str[idx]) { | |
| 1478 case '"': | |
| 1479 /* string */ | |
| 1480 return scanstring_str(pystr, idx + 1, | |
| 1481 PyString_AS_STRING(s->encoding), | |
| 1482 PyObject_IsTrue(s->strict), | |
| 1483 next_idx_ptr); | |
| 1484 case '{': | |
| 1485 /* object */ | |
| 1486 return _parse_object_str(s, pystr, idx + 1, next_idx_ptr); | |
| 1487 case '[': | |
| 1488 /* array */ | |
| 1489 return _parse_array_str(s, pystr, idx + 1, next_idx_ptr); | |
| 1490 case 'n': | |
| 1491 /* null */ | |
| 1492 if ((idx + 3 < length) && str[idx + 1] == 'u' && str[idx + 2] == 'l' && str[idx + 3] == 'l') { | |
| 1493 Py_INCREF(Py_None); | |
| 1494 *next_idx_ptr = idx + 4; | |
| 1495 return Py_None; | |
| 1496 } | |
| 1497 break; | |
| 1498 case 't': | |
| 1499 /* true */ | |
| 1500 if ((idx + 3 < length) && str[idx + 1] == 'r' && str[idx + 2] == 'u' && str[idx + 3] == 'e') { | |
| 1501 Py_INCREF(Py_True); | |
| 1502 *next_idx_ptr = idx + 4; | |
| 1503 return Py_True; | |
| 1504 } | |
| 1505 break; | |
| 1506 case 'f': | |
| 1507 /* false */ | |
| 1508 if ((idx + 4 < length) && str[idx + 1] == 'a' && str[idx + 2] == 'l' && str[idx + 3] == 's' && str[idx + 4] == 'e') { | |
| 1509 Py_INCREF(Py_False); | |
| 1510 *next_idx_ptr = idx + 5; | |
| 1511 return Py_False; | |
| 1512 } | |
| 1513 break; | |
| 1514 case 'N': | |
| 1515 /* NaN */ | |
| 1516 if ((idx + 2 < length) && str[idx + 1] == 'a' && str[idx + 2] == 'N' ) { | |
| 1517 return _parse_constant(s, "NaN", idx, next_idx_ptr); | |
| 1518 } | |
| 1519 break; | |
| 1520 case 'I': | |
| 1521 /* Infinity */ | |
| 1522 if ((idx + 7 < length) && str[idx + 1] == 'n' && str[idx + 2] == 'f' && str[idx + 3] == 'i' && str[idx + 4] == 'n' && str[idx + 5] == 'i' && str[idx + 6] == 't' && str[idx + 7] == 'y') { | |
| 1523 return _parse_constant(s, "Infinity", idx, next_idx_ptr); | |
| 1524 } | |
| 1525 break; | |
| 1526 case '-': | |
| 1527 /* -Infinity */ | |
| 1528 if ((idx + 8 < length) && str[idx + 1] == 'I' && str[idx + 2] == 'n' && str[idx + 3] == 'f' && str[idx + 4] == 'i' && str[idx + 5] == 'n' && str[idx + 6] == 'i' && str[idx + 7] == 't' && str[idx + 8] == 'y') { | |
| 1529 return _parse_constant(s, "-Infinity", idx, next_idx_ptr); | |
| 1530 } | |
| 1531 break; | |
| 1532 } | |
| 1533 /* Didn't find a string, object, array, or named constant. Look for a number . */ | |
| 1534 return _match_number_str(s, pystr, idx, next_idx_ptr); | |
| 1535 } | |
| 1536 | |
| 1537 static PyObject * | |
| 1538 scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_ t *next_idx_ptr) | |
| 1539 { | |
| 1540 /* Read one JSON term (of any kind) from PyUnicode pystr. | |
| 1541 idx is the index of the first character of the term | |
| 1542 *next_idx_ptr is a return-by-reference index to the first character after | |
| 1543 the number. | |
| 1544 | |
| 1545 Returns a new PyObject representation of the term. | |
| 1546 */ | |
| 1547 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr); | |
| 1548 Py_ssize_t length = PyUnicode_GET_SIZE(pystr); | |
| 1549 if (idx >= length) { | |
| 1550 PyErr_SetNone(PyExc_StopIteration); | |
| 1551 return NULL; | |
| 1552 } | |
| 1553 switch (str[idx]) { | |
| 1554 case '"': | |
| 1555 /* string */ | |
| 1556 return scanstring_unicode(pystr, idx + 1, | |
| 1557 PyObject_IsTrue(s->strict), | |
| 1558 next_idx_ptr); | |
| 1559 case '{': | |
| 1560 /* object */ | |
| 1561 return _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr); | |
| 1562 case '[': | |
| 1563 /* array */ | |
| 1564 return _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr); | |
| 1565 case 'n': | |
| 1566 /* null */ | |
| 1567 if ((idx + 3 < length) && str[idx + 1] == 'u' && str[idx + 2] == 'l' && str[idx + 3] == 'l') { | |
| 1568 Py_INCREF(Py_None); | |
| 1569 *next_idx_ptr = idx + 4; | |
| 1570 return Py_None; | |
| 1571 } | |
| 1572 break; | |
| 1573 case 't': | |
| 1574 /* true */ | |
| 1575 if ((idx + 3 < length) && str[idx + 1] == 'r' && str[idx + 2] == 'u' && str[idx + 3] == 'e') { | |
| 1576 Py_INCREF(Py_True); | |
| 1577 *next_idx_ptr = idx + 4; | |
| 1578 return Py_True; | |
| 1579 } | |
| 1580 break; | |
| 1581 case 'f': | |
| 1582 /* false */ | |
| 1583 if ((idx + 4 < length) && str[idx + 1] == 'a' && str[idx + 2] == 'l' && str[idx + 3] == 's' && str[idx + 4] == 'e') { | |
| 1584 Py_INCREF(Py_False); | |
| 1585 *next_idx_ptr = idx + 5; | |
| 1586 return Py_False; | |
| 1587 } | |
| 1588 break; | |
| 1589 case 'N': | |
| 1590 /* NaN */ | |
| 1591 if ((idx + 2 < length) && str[idx + 1] == 'a' && str[idx + 2] == 'N' ) { | |
| 1592 return _parse_constant(s, "NaN", idx, next_idx_ptr); | |
| 1593 } | |
| 1594 break; | |
| 1595 case 'I': | |
| 1596 /* Infinity */ | |
| 1597 if ((idx + 7 < length) && str[idx + 1] == 'n' && str[idx + 2] == 'f' && str[idx + 3] == 'i' && str[idx + 4] == 'n' && str[idx + 5] == 'i' && str[idx + 6] == 't' && str[idx + 7] == 'y') { | |
| 1598 return _parse_constant(s, "Infinity", idx, next_idx_ptr); | |
| 1599 } | |
| 1600 break; | |
| 1601 case '-': | |
| 1602 /* -Infinity */ | |
| 1603 if ((idx + 8 < length) && str[idx + 1] == 'I' && str[idx + 2] == 'n' && str[idx + 3] == 'f' && str[idx + 4] == 'i' && str[idx + 5] == 'n' && str[idx + 6] == 'i' && str[idx + 7] == 't' && str[idx + 8] == 'y') { | |
| 1604 return _parse_constant(s, "-Infinity", idx, next_idx_ptr); | |
| 1605 } | |
| 1606 break; | |
| 1607 } | |
| 1608 /* Didn't find a string, object, array, or named constant. Look for a number . */ | |
| 1609 return _match_number_unicode(s, pystr, idx, next_idx_ptr); | |
| 1610 } | |
| 1611 | |
| 1612 static PyObject * | |
| 1613 scanner_call(PyObject *self, PyObject *args, PyObject *kwds) | |
| 1614 { | |
| 1615 /* Python callable interface to scan_once_{str,unicode} */ | |
| 1616 PyObject *pystr; | |
| 1617 PyObject *rval; | |
| 1618 Py_ssize_t idx; | |
| 1619 Py_ssize_t next_idx = -1; | |
| 1620 static char *kwlist[] = {"string", "idx", NULL}; | |
| 1621 PyScannerObject *s; | |
| 1622 assert(PyScanner_Check(self)); | |
| 1623 s = (PyScannerObject *)self; | |
| 1624 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:scan_once", kwlist, &pystr , _convertPyInt_AsSsize_t, &idx)) | |
| 1625 return NULL; | |
| 1626 | |
| 1627 if (PyString_Check(pystr)) { | |
| 1628 rval = scan_once_str(s, pystr, idx, &next_idx); | |
| 1629 } | |
| 1630 else if (PyUnicode_Check(pystr)) { | |
| 1631 rval = scan_once_unicode(s, pystr, idx, &next_idx); | |
| 1632 } | |
| 1633 else { | |
| 1634 PyErr_Format(PyExc_TypeError, | |
| 1635 "first argument must be a string, not %.80s", | |
| 1636 Py_TYPE(pystr)->tp_name); | |
| 1637 return NULL; | |
| 1638 } | |
| 1639 return _build_rval_index_tuple(rval, next_idx); | |
| 1640 } | |
| 1641 | |
| 1642 static PyObject * | |
| 1643 scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
| 1644 { | |
| 1645 PyScannerObject *s; | |
| 1646 s = (PyScannerObject *)type->tp_alloc(type, 0); | |
| 1647 if (s != NULL) { | |
| 1648 s->encoding = NULL; | |
| 1649 s->strict = NULL; | |
| 1650 s->object_hook = NULL; | |
| 1651 s->parse_float = NULL; | |
| 1652 s->parse_int = NULL; | |
| 1653 s->parse_constant = NULL; | |
| 1654 } | |
| 1655 return (PyObject *)s; | |
| 1656 } | |
| 1657 | |
| 1658 static int | |
| 1659 scanner_init(PyObject *self, PyObject *args, PyObject *kwds) | |
| 1660 { | |
| 1661 /* Initialize Scanner object */ | |
| 1662 PyObject *ctx; | |
| 1663 static char *kwlist[] = {"context", NULL}; | |
| 1664 PyScannerObject *s; | |
| 1665 | |
| 1666 assert(PyScanner_Check(self)); | |
| 1667 s = (PyScannerObject *)self; | |
| 1668 | |
| 1669 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx) ) | |
| 1670 return -1; | |
| 1671 | |
| 1672 /* PyString_AS_STRING is used on encoding */ | |
| 1673 s->encoding = PyObject_GetAttrString(ctx, "encoding"); | |
| 1674 if (s->encoding == Py_None) { | |
| 1675 Py_DECREF(Py_None); | |
| 1676 s->encoding = PyString_InternFromString(DEFAULT_ENCODING); | |
| 1677 } | |
| 1678 else if (PyUnicode_Check(s->encoding)) { | |
| 1679 PyObject *tmp = PyUnicode_AsEncodedString(s->encoding, NULL, NULL); | |
| 1680 Py_DECREF(s->encoding); | |
| 1681 s->encoding = tmp; | |
| 1682 } | |
| 1683 if (s->encoding == NULL || !PyString_Check(s->encoding)) | |
| 1684 goto bail; | |
| 1685 | |
| 1686 /* All of these will fail "gracefully" so we don't need to verify them */ | |
| 1687 s->strict = PyObject_GetAttrString(ctx, "strict"); | |
| 1688 if (s->strict == NULL) | |
| 1689 goto bail; | |
| 1690 s->object_hook = PyObject_GetAttrString(ctx, "object_hook"); | |
| 1691 if (s->object_hook == NULL) | |
| 1692 goto bail; | |
| 1693 s->parse_float = PyObject_GetAttrString(ctx, "parse_float"); | |
| 1694 if (s->parse_float == NULL) | |
| 1695 goto bail; | |
| 1696 s->parse_int = PyObject_GetAttrString(ctx, "parse_int"); | |
| 1697 if (s->parse_int == NULL) | |
| 1698 goto bail; | |
| 1699 s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant"); | |
| 1700 if (s->parse_constant == NULL) | |
| 1701 goto bail; | |
| 1702 | |
| 1703 return 0; | |
| 1704 | |
| 1705 bail: | |
| 1706 Py_CLEAR(s->encoding); | |
| 1707 Py_CLEAR(s->strict); | |
| 1708 Py_CLEAR(s->object_hook); | |
| 1709 Py_CLEAR(s->parse_float); | |
| 1710 Py_CLEAR(s->parse_int); | |
| 1711 Py_CLEAR(s->parse_constant); | |
| 1712 return -1; | |
| 1713 } | |
| 1714 | |
| 1715 PyDoc_STRVAR(scanner_doc, "JSON scanner object"); | |
| 1716 | |
| 1717 static | |
| 1718 PyTypeObject PyScannerType = { | |
| 1719 PyObject_HEAD_INIT(NULL) | |
| 1720 0, /* tp_internal */ | |
| 1721 "_json.Scanner", /* tp_name */ | |
| 1722 sizeof(PyScannerObject), /* tp_basicsize */ | |
| 1723 0, /* tp_itemsize */ | |
| 1724 scanner_dealloc, /* tp_dealloc */ | |
| 1725 0, /* tp_print */ | |
| 1726 0, /* tp_getattr */ | |
| 1727 0, /* tp_setattr */ | |
| 1728 0, /* tp_compare */ | |
| 1729 0, /* tp_repr */ | |
| 1730 0, /* tp_as_number */ | |
| 1731 0, /* tp_as_sequence */ | |
| 1732 0, /* tp_as_mapping */ | |
| 1733 0, /* tp_hash */ | |
| 1734 scanner_call, /* tp_call */ | |
| 1735 0, /* tp_str */ | |
| 1736 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */ | |
| 1737 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */ | |
| 1738 0, /* tp_as_buffer */ | |
| 1739 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ | |
| 1740 scanner_doc, /* tp_doc */ | |
| 1741 scanner_traverse, /* tp_traverse */ | |
| 1742 scanner_clear, /* tp_clear */ | |
| 1743 0, /* tp_richcompare */ | |
| 1744 0, /* tp_weaklistoffset */ | |
| 1745 0, /* tp_iter */ | |
| 1746 0, /* tp_iternext */ | |
| 1747 0, /* tp_methods */ | |
| 1748 scanner_members, /* tp_members */ | |
| 1749 0, /* tp_getset */ | |
| 1750 0, /* tp_base */ | |
| 1751 0, /* tp_dict */ | |
| 1752 0, /* tp_descr_get */ | |
| 1753 0, /* tp_descr_set */ | |
| 1754 0, /* tp_dictoffset */ | |
| 1755 scanner_init, /* tp_init */ | |
| 1756 0,/* PyType_GenericAlloc, */ /* tp_alloc */ | |
| 1757 scanner_new, /* tp_new */ | |
| 1758 0,/* PyObject_GC_Del, */ /* tp_free */ | |
| 1759 }; | |
| 1760 | |
| 1761 static PyObject * | |
| 1762 encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
| 1763 { | |
| 1764 PyEncoderObject *s; | |
| 1765 s = (PyEncoderObject *)type->tp_alloc(type, 0); | |
| 1766 if (s != NULL) { | |
| 1767 s->markers = NULL; | |
| 1768 s->defaultfn = NULL; | |
| 1769 s->encoder = NULL; | |
| 1770 s->indent = NULL; | |
| 1771 s->key_separator = NULL; | |
| 1772 s->item_separator = NULL; | |
| 1773 s->sort_keys = NULL; | |
| 1774 s->skipkeys = NULL; | |
| 1775 } | |
| 1776 return (PyObject *)s; | |
| 1777 } | |
| 1778 | |
| 1779 static int | |
| 1780 encoder_init(PyObject *self, PyObject *args, PyObject *kwds) | |
| 1781 { | |
| 1782 /* initialize Encoder object */ | |
| 1783 static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_sep arator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL}; | |
| 1784 | |
| 1785 PyEncoderObject *s; | |
| 1786 PyObject *allow_nan; | |
| 1787 | |
| 1788 assert(PyEncoder_Check(self)); | |
| 1789 s = (PyEncoderObject *)self; | |
| 1790 | |
| 1791 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlis t, | |
| 1792 &s->markers, &s->defaultfn, &s->encoder, &s->indent, &s->key_separator, &s->item_separator, &s->sort_keys, &s->skipkeys, &allow_nan)) | |
| 1793 return -1; | |
| 1794 | |
| 1795 Py_INCREF(s->markers); | |
| 1796 Py_INCREF(s->defaultfn); | |
| 1797 Py_INCREF(s->encoder); | |
| 1798 Py_INCREF(s->indent); | |
| 1799 Py_INCREF(s->key_separator); | |
| 1800 Py_INCREF(s->item_separator); | |
| 1801 Py_INCREF(s->sort_keys); | |
| 1802 Py_INCREF(s->skipkeys); | |
| 1803 s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s ->encoder) == (PyCFunction)py_encode_basestring_ascii); | |
| 1804 s->allow_nan = PyObject_IsTrue(allow_nan); | |
| 1805 return 0; | |
| 1806 } | |
| 1807 | |
| 1808 static PyObject * | |
| 1809 encoder_call(PyObject *self, PyObject *args, PyObject *kwds) | |
| 1810 { | |
| 1811 /* Python callable interface to encode_listencode_obj */ | |
| 1812 static char *kwlist[] = {"obj", "_current_indent_level", NULL}; | |
| 1813 PyObject *obj; | |
| 1814 PyObject *rval; | |
| 1815 Py_ssize_t indent_level; | |
| 1816 PyEncoderObject *s; | |
| 1817 assert(PyEncoder_Check(self)); | |
| 1818 s = (PyEncoderObject *)self; | |
| 1819 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&:_iterencode", kwlist, | |
| 1820 &obj, _convertPyInt_AsSsize_t, &indent_level)) | |
| 1821 return NULL; | |
| 1822 rval = PyList_New(0); | |
| 1823 if (rval == NULL) | |
| 1824 return NULL; | |
| 1825 if (encoder_listencode_obj(s, rval, obj, indent_level)) { | |
| 1826 Py_DECREF(rval); | |
| 1827 return NULL; | |
| 1828 } | |
| 1829 return rval; | |
| 1830 } | |
| 1831 | |
| 1832 static PyObject * | |
| 1833 _encoded_const(PyObject *obj) | |
| 1834 { | |
| 1835 /* Return the JSON string representation of None, True, False */ | |
| 1836 if (obj == Py_None) { | |
| 1837 static PyObject *s_null = NULL; | |
| 1838 if (s_null == NULL) { | |
| 1839 s_null = PyString_InternFromString("null"); | |
| 1840 } | |
| 1841 Py_INCREF(s_null); | |
| 1842 return s_null; | |
| 1843 } | |
| 1844 else if (obj == Py_True) { | |
| 1845 static PyObject *s_true = NULL; | |
| 1846 if (s_true == NULL) { | |
| 1847 s_true = PyString_InternFromString("true"); | |
| 1848 } | |
| 1849 Py_INCREF(s_true); | |
| 1850 return s_true; | |
| 1851 } | |
| 1852 else if (obj == Py_False) { | |
| 1853 static PyObject *s_false = NULL; | |
| 1854 if (s_false == NULL) { | |
| 1855 s_false = PyString_InternFromString("false"); | |
| 1856 } | |
| 1857 Py_INCREF(s_false); | |
| 1858 return s_false; | |
| 1859 } | |
| 1860 else { | |
| 1861 PyErr_SetString(PyExc_ValueError, "not a const"); | |
| 1862 return NULL; | |
| 1863 } | |
| 1864 } | |
| 1865 | |
| 1866 static PyObject * | |
| 1867 encoder_encode_float(PyEncoderObject *s, PyObject *obj) | |
| 1868 { | |
| 1869 /* Return the JSON representation of a PyFloat */ | |
| 1870 double i = PyFloat_AS_DOUBLE(obj); | |
| 1871 if (!Py_IS_FINITE(i)) { | |
| 1872 if (!s->allow_nan) { | |
| 1873 PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant"); | |
| 1874 return NULL; | |
| 1875 } | |
| 1876 if (i > 0) { | |
| 1877 return PyString_FromString("Infinity"); | |
| 1878 } | |
| 1879 else if (i < 0) { | |
| 1880 return PyString_FromString("-Infinity"); | |
| 1881 } | |
| 1882 else { | |
| 1883 return PyString_FromString("NaN"); | |
| 1884 } | |
| 1885 } | |
| 1886 /* Use a better float format here? */ | |
| 1887 return PyObject_Repr(obj); | |
| 1888 } | |
| 1889 | |
| 1890 static PyObject * | |
| 1891 encoder_encode_string(PyEncoderObject *s, PyObject *obj) | |
| 1892 { | |
| 1893 /* Return the JSON representation of a string */ | |
| 1894 if (s->fast_encode) | |
| 1895 return py_encode_basestring_ascii(NULL, obj); | |
| 1896 else | |
| 1897 return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL); | |
| 1898 } | |
| 1899 | |
| 1900 static int | |
| 1901 _steal_list_append(PyObject *lst, PyObject *stolen) | |
| 1902 { | |
| 1903 /* Append stolen and then decrement its reference count */ | |
| 1904 int rval = PyList_Append(lst, stolen); | |
| 1905 Py_DECREF(stolen); | |
| 1906 return rval; | |
| 1907 } | |
| 1908 | |
| 1909 static int | |
| 1910 encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssi ze_t indent_level) | |
| 1911 { | |
| 1912 /* Encode Python object obj to a JSON term, rval is a PyList */ | |
| 1913 PyObject *newobj; | |
| 1914 int rv; | |
| 1915 | |
| 1916 if (obj == Py_None || obj == Py_True || obj == Py_False) { | |
| 1917 PyObject *cstr = _encoded_const(obj); | |
| 1918 if (cstr == NULL) | |
| 1919 return -1; | |
| 1920 return _steal_list_append(rval, cstr); | |
| 1921 } | |
| 1922 else if (PyString_Check(obj) || PyUnicode_Check(obj)) | |
| 1923 { | |
| 1924 PyObject *encoded = encoder_encode_string(s, obj); | |
| 1925 if (encoded == NULL) | |
| 1926 return -1; | |
| 1927 return _steal_list_append(rval, encoded); | |
| 1928 } | |
| 1929 else if (PyInt_Check(obj) || PyLong_Check(obj)) { | |
| 1930 PyObject *encoded = PyObject_Str(obj); | |
| 1931 if (encoded == NULL) | |
| 1932 return -1; | |
| 1933 return _steal_list_append(rval, encoded); | |
| 1934 } | |
| 1935 else if (PyFloat_Check(obj)) { | |
| 1936 PyObject *encoded = encoder_encode_float(s, obj); | |
| 1937 if (encoded == NULL) | |
| 1938 return -1; | |
| 1939 return _steal_list_append(rval, encoded); | |
| 1940 } | |
| 1941 else if (PyList_Check(obj) || PyTuple_Check(obj)) { | |
| 1942 return encoder_listencode_list(s, rval, obj, indent_level); | |
| 1943 } | |
| 1944 else if (PyDict_Check(obj)) { | |
| 1945 return encoder_listencode_dict(s, rval, obj, indent_level); | |
| 1946 } | |
| 1947 else { | |
| 1948 PyObject *ident = NULL; | |
| 1949 if (s->markers != Py_None) { | |
| 1950 int has_key; | |
| 1951 ident = PyLong_FromVoidPtr(obj); | |
| 1952 if (ident == NULL) | |
| 1953 return -1; | |
| 1954 has_key = PyDict_Contains(s->markers, ident); | |
| 1955 if (has_key) { | |
| 1956 if (has_key != -1) | |
| 1957 PyErr_SetString(PyExc_ValueError, "Circular reference detect ed"); | |
| 1958 Py_DECREF(ident); | |
| 1959 return -1; | |
| 1960 } | |
| 1961 if (PyDict_SetItem(s->markers, ident, obj)) { | |
| 1962 Py_DECREF(ident); | |
| 1963 return -1; | |
| 1964 } | |
| 1965 } | |
| 1966 newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL); | |
| 1967 if (newobj == NULL) { | |
| 1968 Py_XDECREF(ident); | |
| 1969 return -1; | |
| 1970 } | |
| 1971 rv = encoder_listencode_obj(s, rval, newobj, indent_level); | |
| 1972 Py_DECREF(newobj); | |
| 1973 if (rv) { | |
| 1974 Py_XDECREF(ident); | |
| 1975 return -1; | |
| 1976 } | |
| 1977 if (ident != NULL) { | |
| 1978 if (PyDict_DelItem(s->markers, ident)) { | |
| 1979 Py_XDECREF(ident); | |
| 1980 return -1; | |
| 1981 } | |
| 1982 Py_XDECREF(ident); | |
| 1983 } | |
| 1984 return rv; | |
| 1985 } | |
| 1986 } | |
| 1987 | |
| 1988 static int | |
| 1989 encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss ize_t indent_level) | |
| 1990 { | |
| 1991 /* Encode Python dict dct a JSON term, rval is a PyList */ | |
| 1992 static PyObject *open_dict = NULL; | |
| 1993 static PyObject *close_dict = NULL; | |
| 1994 static PyObject *empty_dict = NULL; | |
| 1995 PyObject *kstr = NULL; | |
| 1996 PyObject *ident = NULL; | |
| 1997 PyObject *key, *value; | |
| 1998 Py_ssize_t pos; | |
| 1999 int skipkeys; | |
| 2000 Py_ssize_t idx; | |
| 2001 | |
| 2002 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) { | |
| 2003 open_dict = PyString_InternFromString("{"); | |
| 2004 close_dict = PyString_InternFromString("}"); | |
| 2005 empty_dict = PyString_InternFromString("{}"); | |
| 2006 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) | |
| 2007 return -1; | |
| 2008 } | |
| 2009 if (PyDict_Size(dct) == 0) | |
| 2010 return PyList_Append(rval, empty_dict); | |
| 2011 | |
| 2012 if (s->markers != Py_None) { | |
| 2013 int has_key; | |
| 2014 ident = PyLong_FromVoidPtr(dct); | |
| 2015 if (ident == NULL) | |
| 2016 goto bail; | |
| 2017 has_key = PyDict_Contains(s->markers, ident); | |
| 2018 if (has_key) { | |
| 2019 if (has_key != -1) | |
| 2020 PyErr_SetString(PyExc_ValueError, "Circular reference detected") ; | |
| 2021 goto bail; | |
| 2022 } | |
| 2023 if (PyDict_SetItem(s->markers, ident, dct)) { | |
| 2024 goto bail; | |
| 2025 } | |
| 2026 } | |
| 2027 | |
| 2028 if (PyList_Append(rval, open_dict)) | |
| 2029 goto bail; | |
| 2030 | |
| 2031 if (s->indent != Py_None) { | |
| 2032 /* TODO: DOES NOT RUN */ | |
| 2033 indent_level += 1; | |
| 2034 /* | |
| 2035 newline_indent = '\n' + (' ' * (_indent * _current_indent_level)) | |
| 2036 separator = _item_separator + newline_indent | |
| 2037 buf += newline_indent | |
| 2038 */ | |
| 2039 } | |
| 2040 | |
| 2041 /* TODO: C speedup not implemented for sort_keys */ | |
| 2042 | |
| 2043 pos = 0; | |
| 2044 skipkeys = PyObject_IsTrue(s->skipkeys); | |
| 2045 idx = 0; | |
| 2046 while (PyDict_Next(dct, &pos, &key, &value)) { | |
| 2047 PyObject *encoded; | |
| 2048 | |
| 2049 if (PyString_Check(key) || PyUnicode_Check(key)) { | |
| 2050 Py_INCREF(key); | |
| 2051 kstr = key; | |
| 2052 } | |
| 2053 else if (PyFloat_Check(key)) { | |
| 2054 kstr = encoder_encode_float(s, key); | |
| 2055 if (kstr == NULL) | |
| 2056 goto bail; | |
| 2057 } | |
| 2058 else if (PyInt_Check(key) || PyLong_Check(key)) { | |
| 2059 kstr = PyObject_Str(key); | |
| 2060 if (kstr == NULL) | |
| 2061 goto bail; | |
| 2062 } | |
| 2063 else if (key == Py_True || key == Py_False || key == Py_None) { | |
| 2064 kstr = _encoded_const(key); | |
| 2065 if (kstr == NULL) | |
| 2066 goto bail; | |
| 2067 } | |
| 2068 else if (skipkeys) { | |
| 2069 continue; | |
| 2070 } | |
| 2071 else { | |
| 2072 /* TODO: include repr of key */ | |
| 2073 PyErr_SetString(PyExc_ValueError, "keys must be a string"); | |
| 2074 goto bail; | |
| 2075 } | |
| 2076 | |
| 2077 if (idx) { | |
| 2078 if (PyList_Append(rval, s->item_separator)) | |
| 2079 goto bail; | |
| 2080 } | |
| 2081 | |
| 2082 encoded = encoder_encode_string(s, kstr); | |
| 2083 Py_CLEAR(kstr); | |
| 2084 if (encoded == NULL) | |
| 2085 goto bail; | |
| 2086 if (PyList_Append(rval, encoded)) { | |
| 2087 Py_DECREF(encoded); | |
| 2088 goto bail; | |
| 2089 } | |
| 2090 Py_DECREF(encoded); | |
| 2091 if (PyList_Append(rval, s->key_separator)) | |
| 2092 goto bail; | |
| 2093 if (encoder_listencode_obj(s, rval, value, indent_level)) | |
| 2094 goto bail; | |
| 2095 idx += 1; | |
| 2096 } | |
| 2097 if (ident != NULL) { | |
| 2098 if (PyDict_DelItem(s->markers, ident)) | |
| 2099 goto bail; | |
| 2100 Py_CLEAR(ident); | |
| 2101 } | |
| 2102 if (s->indent != Py_None) { | |
| 2103 /* TODO: DOES NOT RUN */ | |
| 2104 indent_level -= 1; | |
| 2105 /* | |
| 2106 yield '\n' + (' ' * (_indent * _current_indent_level)) | |
| 2107 */ | |
| 2108 } | |
| 2109 if (PyList_Append(rval, close_dict)) | |
| 2110 goto bail; | |
| 2111 return 0; | |
| 2112 | |
| 2113 bail: | |
| 2114 Py_XDECREF(kstr); | |
| 2115 Py_XDECREF(ident); | |
| 2116 return -1; | |
| 2117 } | |
| 2118 | |
| 2119 | |
| 2120 static int | |
| 2121 encoder_listencode_list(PyEncoderObject *s, PyObject *rval, PyObject *seq, Py_ss ize_t indent_level) | |
| 2122 { | |
| 2123 /* Encode Python list seq to a JSON term, rval is a PyList */ | |
| 2124 static PyObject *open_array = NULL; | |
| 2125 static PyObject *close_array = NULL; | |
| 2126 static PyObject *empty_array = NULL; | |
| 2127 PyObject *ident = NULL; | |
| 2128 PyObject *s_fast = NULL; | |
| 2129 Py_ssize_t num_items; | |
| 2130 PyObject **seq_items; | |
| 2131 Py_ssize_t i; | |
| 2132 | |
| 2133 if (open_array == NULL || close_array == NULL || empty_array == NULL) { | |
| 2134 open_array = PyString_InternFromString("["); | |
| 2135 close_array = PyString_InternFromString("]"); | |
| 2136 empty_array = PyString_InternFromString("[]"); | |
| 2137 if (open_array == NULL || close_array == NULL || empty_array == NULL) | |
| 2138 return -1; | |
| 2139 } | |
| 2140 ident = NULL; | |
| 2141 s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence"); | |
| 2142 if (s_fast == NULL) | |
| 2143 return -1; | |
| 2144 num_items = PySequence_Fast_GET_SIZE(s_fast); | |
| 2145 if (num_items == 0) { | |
| 2146 Py_DECREF(s_fast); | |
| 2147 return PyList_Append(rval, empty_array); | |
| 2148 } | |
| 2149 | |
| 2150 if (s->markers != Py_None) { | |
| 2151 int has_key; | |
| 2152 ident = PyLong_FromVoidPtr(seq); | |
| 2153 if (ident == NULL) | |
| 2154 goto bail; | |
| 2155 has_key = PyDict_Contains(s->markers, ident); | |
| 2156 if (has_key) { | |
| 2157 if (has_key != -1) | |
| 2158 PyErr_SetString(PyExc_ValueError, "Circular reference detected") ; | |
| 2159 goto bail; | |
| 2160 } | |
| 2161 if (PyDict_SetItem(s->markers, ident, seq)) { | |
| 2162 goto bail; | |
| 2163 } | |
| 2164 } | |
| 2165 | |
| 2166 seq_items = PySequence_Fast_ITEMS(s_fast); | |
| 2167 if (PyList_Append(rval, open_array)) | |
| 2168 goto bail; | |
| 2169 if (s->indent != Py_None) { | |
| 2170 /* TODO: DOES NOT RUN */ | |
| 2171 indent_level += 1; | |
| 2172 /* | |
| 2173 newline_indent = '\n' + (' ' * (_indent * _current_indent_level)) | |
| 2174 separator = _item_separator + newline_indent | |
| 2175 buf += newline_indent | |
| 2176 */ | |
| 2177 } | |
| 2178 for (i = 0; i < num_items; i++) { | |
| 2179 PyObject *obj = seq_items[i]; | |
| 2180 if (i) { | |
| 2181 if (PyList_Append(rval, s->item_separator)) | |
| 2182 goto bail; | |
| 2183 } | |
| 2184 if (encoder_listencode_obj(s, rval, obj, indent_level)) | |
| 2185 goto bail; | |
| 2186 } | |
| 2187 if (ident != NULL) { | |
| 2188 if (PyDict_DelItem(s->markers, ident)) | |
| 2189 goto bail; | |
| 2190 Py_CLEAR(ident); | |
| 2191 } | |
| 2192 if (s->indent != Py_None) { | |
| 2193 /* TODO: DOES NOT RUN */ | |
| 2194 indent_level -= 1; | |
| 2195 /* | |
| 2196 yield '\n' + (' ' * (_indent * _current_indent_level)) | |
| 2197 */ | |
| 2198 } | |
| 2199 if (PyList_Append(rval, close_array)) | |
| 2200 goto bail; | |
| 2201 Py_DECREF(s_fast); | |
| 2202 return 0; | |
| 2203 | |
| 2204 bail: | |
| 2205 Py_XDECREF(ident); | |
| 2206 Py_DECREF(s_fast); | |
| 2207 return -1; | |
| 2208 } | |
| 2209 | |
| 2210 static void | |
| 2211 encoder_dealloc(PyObject *self) | |
| 2212 { | |
| 2213 /* Deallocate Encoder */ | |
| 2214 encoder_clear(self); | |
| 2215 Py_TYPE(self)->tp_free(self); | |
| 2216 } | |
| 2217 | |
| 2218 static int | |
| 2219 encoder_traverse(PyObject *self, visitproc visit, void *arg) | |
| 2220 { | |
| 2221 PyEncoderObject *s; | |
| 2222 assert(PyEncoder_Check(self)); | |
| 2223 s = (PyEncoderObject *)self; | |
| 2224 Py_VISIT(s->markers); | |
| 2225 Py_VISIT(s->defaultfn); | |
| 2226 Py_VISIT(s->encoder); | |
| 2227 Py_VISIT(s->indent); | |
| 2228 Py_VISIT(s->key_separator); | |
| 2229 Py_VISIT(s->item_separator); | |
| 2230 Py_VISIT(s->sort_keys); | |
| 2231 Py_VISIT(s->skipkeys); | |
| 2232 return 0; | |
| 2233 } | |
| 2234 | |
| 2235 static int | |
| 2236 encoder_clear(PyObject *self) | |
| 2237 { | |
| 2238 /* Deallocate Encoder */ | |
| 2239 PyEncoderObject *s; | |
| 2240 assert(PyEncoder_Check(self)); | |
| 2241 s = (PyEncoderObject *)self; | |
| 2242 Py_CLEAR(s->markers); | |
| 2243 Py_CLEAR(s->defaultfn); | |
| 2244 Py_CLEAR(s->encoder); | |
| 2245 Py_CLEAR(s->indent); | |
| 2246 Py_CLEAR(s->key_separator); | |
| 2247 Py_CLEAR(s->item_separator); | |
| 2248 Py_CLEAR(s->sort_keys); | |
| 2249 Py_CLEAR(s->skipkeys); | |
| 2250 return 0; | |
| 2251 } | |
| 2252 | |
| 2253 PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable") ; | |
| 2254 | |
| 2255 static | |
| 2256 PyTypeObject PyEncoderType = { | |
| 2257 PyObject_HEAD_INIT(NULL) | |
| 2258 0, /* tp_internal */ | |
| 2259 "_json.Encoder", /* tp_name */ | |
| 2260 sizeof(PyEncoderObject), /* tp_basicsize */ | |
| 2261 0, /* tp_itemsize */ | |
| 2262 encoder_dealloc, /* tp_dealloc */ | |
| 2263 0, /* tp_print */ | |
| 2264 0, /* tp_getattr */ | |
| 2265 0, /* tp_setattr */ | |
| 2266 0, /* tp_compare */ | |
| 2267 0, /* tp_repr */ | |
| 2268 0, /* tp_as_number */ | |
| 2269 0, /* tp_as_sequence */ | |
| 2270 0, /* tp_as_mapping */ | |
| 2271 0, /* tp_hash */ | |
| 2272 encoder_call, /* tp_call */ | |
| 2273 0, /* tp_str */ | |
| 2274 0, /* tp_getattro */ | |
| 2275 0, /* tp_setattro */ | |
| 2276 0, /* tp_as_buffer */ | |
| 2277 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ | |
| 2278 encoder_doc, /* tp_doc */ | |
| 2279 encoder_traverse, /* tp_traverse */ | |
| 2280 encoder_clear, /* tp_clear */ | |
| 2281 0, /* tp_richcompare */ | |
| 2282 0, /* tp_weaklistoffset */ | |
| 2283 0, /* tp_iter */ | |
| 2284 0, /* tp_iternext */ | |
| 2285 0, /* tp_methods */ | |
| 2286 encoder_members, /* tp_members */ | |
| 2287 0, /* tp_getset */ | |
| 2288 0, /* tp_base */ | |
| 2289 0, /* tp_dict */ | |
| 2290 0, /* tp_descr_get */ | |
| 2291 0, /* tp_descr_set */ | |
| 2292 0, /* tp_dictoffset */ | |
| 2293 encoder_init, /* tp_init */ | |
| 2294 0, /* tp_alloc */ | |
| 2295 encoder_new, /* tp_new */ | |
| 2296 0, /* tp_free */ | |
| 2297 }; | |
| 2298 | |
| 2299 static PyMethodDef speedups_methods[] = { | |
| 2300 {"encode_basestring_ascii", | |
| 2301 (PyCFunction)py_encode_basestring_ascii, | |
| 2302 METH_O, | |
| 2303 pydoc_encode_basestring_ascii}, | |
| 2304 {"scanstring", | |
| 2305 (PyCFunction)py_scanstring, | |
| 2306 METH_VARARGS, | |
| 2307 pydoc_scanstring}, | |
| 610 {NULL, NULL, 0, NULL} | 2308 {NULL, NULL, 0, NULL} |
| 611 }; | 2309 }; |
| 612 | 2310 |
| 613 PyDoc_STRVAR(module_doc, | 2311 PyDoc_STRVAR(module_doc, |
| 614 "json speedups\n"); | 2312 "json speedups\n"); |
| 615 | 2313 |
| 616 void | 2314 void |
| 617 init_json(void) | 2315 init_json(void) |
| 618 { | 2316 { |
| 619 PyObject *m; | 2317 PyObject *m; |
| 620 m = Py_InitModule3("_json", json_methods, module_doc); | 2318 PyScannerType.tp_new = PyType_GenericNew; |
| 2319 if (PyType_Ready(&PyScannerType) < 0) | |
| 2320 return; | |
| 2321 PyEncoderType.tp_new = PyType_GenericNew; | |
| 2322 if (PyType_Ready(&PyEncoderType) < 0) | |
| 2323 return; | |
| 2324 m = Py_InitModule3("_json", speedups_methods, module_doc); | |
| 2325 Py_INCREF((PyObject*)&PyScannerType); | |
| 2326 PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType); | |
| 2327 Py_INCREF((PyObject*)&PyEncoderType); | |
| 2328 PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType); | |
| 621 } | 2329 } |
| OLD | NEW |