3 #include "structmember.h"
5 #if PY_MAJOR_VERSION >= 3
6 #define PyInt_FromSsize_t PyLong_FromSsize_t
7 #define PyInt_AsSsize_t PyLong_AsSsize_t
8 #define PyInt_Check(obj) 0
9 #define PyInt_CheckExact(obj) 0
10 #define JSON_UNICHR Py_UCS4
11 #define JSON_InternFromString PyUnicode_InternFromString
12 #define PyString_GET_SIZE PyUnicode_GET_LENGTH
14 #define PY3_UNUSED UNUSED
16 #define PY2_UNUSED UNUSED
18 #define PyBytes_Check PyString_Check
19 #define PyUnicode_READY(obj) 0
20 #define PyUnicode_KIND(obj) (sizeof(Py_UNICODE))
21 #define PyUnicode_DATA(obj) ((void *)(PyUnicode_AS_UNICODE(obj)))
22 #define PyUnicode_READ(kind, data, index) ((JSON_UNICHR)((const Py_UNICODE *)(data))[(index)])
23 #define PyUnicode_GET_LENGTH PyUnicode_GET_SIZE
24 #define JSON_UNICHR Py_UNICODE
25 #define JSON_InternFromString PyString_InternFromString
28 #if PY_VERSION_HEX < 0x02070000
29 #if !defined(PyOS_string_to_double)
30 #define PyOS_string_to_double json_PyOS_string_to_double
37 assert(endptr == NULL);
38 assert(overflow_exception == NULL);
39 PyFPE_START_PROTECT(
"json_PyOS_string_to_double",
return -1.0;)
40 x = PyOS_ascii_atof(s);
47 #if PY_VERSION_HEX < 0x02060000
49 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
52 #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
54 #if !defined(PyVarObject_HEAD_INIT)
55 #define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
60 #define UNUSED __attribute__((__unused__))
65 #define DEFAULT_ENCODING "utf-8"
67 #define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType)
68 #define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType)
69 #define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType)
70 #define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType)
72 #define JSON_ALLOW_NAN 1
73 #define JSON_IGNORE_NAN 2
79 #if PY_MAJOR_VERSION < 3
100 #define ERR_EXPECTING_VALUE "Expecting value"
101 #define ERR_ARRAY_DELIMITER "Expecting ',' delimiter or ']'"
102 #define ERR_ARRAY_VALUE_FIRST "Expecting value or ']'"
103 #define ERR_OBJECT_DELIMITER "Expecting ',' delimiter or '}'"
104 #define ERR_OBJECT_PROPERTY "Expecting property name enclosed in double quotes"
105 #define ERR_OBJECT_PROPERTY_FIRST "Expecting property name enclosed in double quotes or '}'"
106 #define ERR_OBJECT_PROPERTY_DELIMITER "Expecting ':' delimiter"
107 #define ERR_STRING_UNTERMINATED "Unterminated string starting at"
108 #define ERR_STRING_CONTROL "Invalid control character %r at"
109 #define ERR_STRING_ESC1 "Invalid \\X escape sequence %r"
110 #define ERR_STRING_ESC4 "Invalid \\uXXXX escape sequence"
126 {
"encoding", T_OBJECT, offsetof(
PyScannerObject, encoding), READONLY,
"encoding"},
127 {
"strict", T_OBJECT, offsetof(
PyScannerObject, strict_bool), READONLY,
"strict"},
128 {
"object_hook", T_OBJECT, offsetof(
PyScannerObject, object_hook), READONLY,
"object_hook"},
129 {
"object_pairs_hook", T_OBJECT, offsetof(
PyScannerObject, pairs_hook), READONLY,
"object_pairs_hook"},
130 {
"parse_float", T_OBJECT, offsetof(
PyScannerObject, parse_float), READONLY,
"parse_float"},
131 {
"parse_int", T_OBJECT, offsetof(
PyScannerObject, parse_int), READONLY,
"parse_int"},
132 {
"parse_constant", T_OBJECT, offsetof(
PyScannerObject, parse_constant), READONLY,
"parse_constant"},
165 {
"markers", T_OBJECT, offsetof(
PyEncoderObject, markers), READONLY,
"markers"},
166 {
"default", T_OBJECT, offsetof(
PyEncoderObject, defaultfn), READONLY,
"default"},
167 {
"encoder", T_OBJECT, offsetof(
PyEncoderObject, encoder), READONLY,
"encoder"},
168 {
"encoding", T_OBJECT, offsetof(
PyEncoderObject, encoder), READONLY,
"encoding"},
169 {
"indent", T_OBJECT, offsetof(
PyEncoderObject, indent), READONLY,
"indent"},
170 {
"key_separator", T_OBJECT, offsetof(
PyEncoderObject, key_separator), READONLY,
"key_separator"},
171 {
"item_separator", T_OBJECT, offsetof(
PyEncoderObject, item_separator), READONLY,
"item_separator"},
172 {
"sort_keys", T_OBJECT, offsetof(
PyEncoderObject, sort_keys), READONLY,
"sort_keys"},
174 {
"skipkeys", T_OBJECT, offsetof(
PyEncoderObject, skipkeys_bool), READONLY,
"skipkeys"},
175 {
"key_memo", T_OBJECT, offsetof(
PyEncoderObject, key_memo), READONLY,
"key_memo"},
176 {
"item_sort_key", T_OBJECT, offsetof(
PyEncoderObject, item_sort_key), READONLY,
"item_sort_key"},
177 {
"max_long_size", T_OBJECT, offsetof(
PyEncoderObject, max_long_size), READONLY,
"max_long_size"},
178 {
"min_long_size", T_OBJECT, offsetof(
PyEncoderObject, min_long_size), READONLY,
"min_long_size"},
198 #if PY_MAJOR_VERSION < 3
204 scanstring_str(PyObject *pystr, Py_ssize_t end,
char *encoding,
int strict, Py_ssize_t *next_end_ptr);
209 scanstring_unicode(PyObject *pystr, Py_ssize_t end,
int strict, Py_ssize_t *next_end_ptr);
215 scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
221 encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
255 #define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
256 #define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
258 #define MIN_EXPANSION 6
264 return PyObject_IsInstance(obj,
RawJSONType) ? 1 : 0;
290 #if PY_MAJOR_VERSION >= 3
312 #if PY_MAJOR_VERSION >= 3
313 assert(PyUnicode_Check(unicode));
315 assert(PyString_Check(unicode) || PyUnicode_Check(unicode));
348 return PyList_New(0);
362 return c >=
'0' && c <=
'9';
369 if (PyObject_RichCompareBool(obj, s->
max_long_size, Py_GE) ||
371 #if PY_MAJOR_VERSION >= 3
372 PyObject* quoted = PyUnicode_FromFormat(
"\"%U\"", encoded);
374 PyObject* quoted = PyString_FromFormat(
"\"%s\"",
375 PyString_AsString(encoded));
389 PyObject *_asdict = PyObject_GetAttrString(obj,
"_asdict");
390 if (_asdict == NULL) {
394 rval = PyCallable_Check(_asdict);
403 PyObject *for_json = PyObject_GetAttrString(obj,
"for_json");
404 if (for_json == NULL) {
408 rval = PyCallable_Check(for_json);
417 *size_ptr = PyInt_AsSsize_t(o);
418 if (*size_ptr == -1 && PyErr_Occurred())
427 return PyInt_FromSsize_t(*size_ptr);
437 output[chars++] = (char)c;
440 output[chars++] =
'\\';
442 case '\\': output[chars++] = (char)c;
break;
443 case '"': output[chars++] = (char)c;
break;
444 case '\b': output[chars++] =
'b';
break;
445 case '\f': output[chars++] =
'f';
break;
446 case '\n': output[chars++] =
'n';
break;
447 case '\r': output[chars++] =
'r';
break;
448 case '\t': output[chars++] =
't';
break;
450 #if PY_MAJOR_VERSION >= 3 || defined(Py_UNICODE_WIDE)
454 c = 0xd800 | ((v >> 10) & 0x3ff);
455 output[chars++] =
'u';
456 output[chars++] =
"0123456789abcdef"[(c >> 12) & 0xf];
457 output[chars++] =
"0123456789abcdef"[(c >> 8) & 0xf];
458 output[chars++] =
"0123456789abcdef"[(c >> 4) & 0xf];
459 output[chars++] =
"0123456789abcdef"[(c ) & 0xf];
460 c = 0xdc00 | (v & 0x3ff);
461 output[chars++] =
'\\';
464 output[chars++] =
'u';
465 output[chars++] =
"0123456789abcdef"[(c >> 12) & 0xf];
466 output[chars++] =
"0123456789abcdef"[(c >> 8) & 0xf];
467 output[chars++] =
"0123456789abcdef"[(c >> 4) & 0xf];
468 output[chars++] =
"0123456789abcdef"[(c ) & 0xf];
480 else if (c ==
'\\' ||
489 #if PY_MAJOR_VERSION >= 3 || defined(Py_UNICODE_WIDE)
490 else if (c >= 0x10000U) {
505 Py_ssize_t output_size = 2;
513 for (i = 0; i < input_chars; i++) {
516 #if PY_MAJOR_VERSION >= 3
517 rval = PyUnicode_New(output_size, 127);
524 rval = PyString_FromStringAndSize(NULL, output_size);
528 output = PyString_AS_STRING(rval);
531 output[chars++] =
'"';
532 for (i = 0; i < input_chars; i++) {
535 output[chars++] =
'"';
536 assert(chars == output_size);
540 #if PY_MAJOR_VERSION >= 3
546 PyObject *input = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(pystr), PyBytes_GET_SIZE(pystr), NULL);
561 Py_ssize_t input_chars;
562 Py_ssize_t output_size;
568 input_chars = PyString_GET_SIZE(pystr);
569 input_str = PyString_AS_STRING(pystr);
573 for (i = 0; i < input_chars; i++) {
578 uni = PyUnicode_DecodeUTF8(input_str, input_chars,
"strict");
589 rval = PyString_FromStringAndSize(NULL, output_size);
594 output = PyString_AS_STRING(rval);
595 output[chars++] =
'"';
596 for (i = 0; i < input_chars; i++) {
599 output[chars++] =
'"';
600 assert(chars == output_size);
608 if (PyUnicode_Check(key)) {
612 #if PY_MAJOR_VERSION >= 3
614 const char *encoding = PyUnicode_AsUTF8(s->
encoding);
615 if (encoding == NULL)
617 return PyUnicode_Decode(
618 PyBytes_AS_STRING(key),
619 PyBytes_GET_SIZE(key),
624 else if (PyString_Check(key)) {
629 else if (PyFloat_Check(key)) {
632 else if (key == Py_True || key == Py_False || key == Py_None) {
637 else if (PyInt_Check(key) || PyLong_Check(key)) {
638 if (!(PyInt_CheckExact(key) || PyLong_CheckExact(key))) {
641 PyObject *tmp = PyObject_CallFunctionObjArgs((PyObject *)&PyLong_Type, key, NULL);
645 res = PyObject_Str(tmp);
650 return PyObject_Str(key);
654 return PyObject_Str(key);
660 PyErr_Format(PyExc_TypeError,
661 "keys must be str, int, float, bool or None, "
662 "not %.100s", key->ob_type->tp_name);
670 PyObject *iter = NULL;
671 PyObject *lst = NULL;
672 PyObject *item = NULL;
673 PyObject *kstr = NULL;
674 PyObject *sortfun = NULL;
676 static PyObject *sortargs = NULL;
678 if (sortargs == NULL) {
679 sortargs = PyTuple_New(0);
680 if (sortargs == NULL)
684 if (PyDict_CheckExact(dct))
685 items = PyDict_Items(dct);
687 items = PyMapping_Items(dct);
690 iter = PyObject_GetIter(items);
699 while ((item = PyIter_Next(iter))) {
700 PyObject *key, *value;
701 if (!PyTuple_Check(item) ||
Py_SIZE(item) != 2) {
702 PyErr_SetString(PyExc_ValueError,
"items must return 2-tuples");
705 key = PyTuple_GET_ITEM(item, 0);
708 #if PY_MAJOR_VERSION < 3
709 else if (PyString_Check(key)) {
713 else if (PyUnicode_Check(key)) {
721 else if (kstr == Py_None) {
726 value = PyTuple_GET_ITEM(item, 1);
729 tpl = PyTuple_Pack(2, kstr, value);
736 if (PyList_Append(lst, item))
741 if (PyErr_Occurred())
743 sortfun = PyObject_GetAttrString(lst,
"sort");
746 sortres = PyObject_Call(sortfun, sortargs, s->
item_sort_kw);
751 iter = PyObject_GetIter(lst);
782 #if PY_MAJOR_VERSION >= 3
783 #define join_list_string join_list_unicode
789 static PyObject *joinfn = NULL;
790 if (joinfn == NULL) {
795 return PyObject_CallFunctionObjArgs(joinfn, lst, NULL);
809 assert(PyErr_Occurred());
812 pyidx = PyInt_FromSsize_t(idx);
817 tpl = PyTuple_New(2);
823 PyTuple_SET_ITEM(tpl, 0, rval);
824 PyTuple_SET_ITEM(tpl, 1, pyidx);
828 #define APPEND_OLD_CHUNK \
829 if (chunk != NULL) { \
830 if (chunks == NULL) { \
831 chunks = PyList_New(0); \
832 if (chunks == NULL) { \
836 if (PyList_Append(chunks, chunk)) { \
842 #if PY_MAJOR_VERSION < 3
844 scanstring_str(PyObject *pystr, Py_ssize_t end,
char *encoding,
int strict, Py_ssize_t *next_end_ptr)
856 Py_ssize_t len = PyString_GET_SIZE(pystr);
857 Py_ssize_t begin = end - 1;
858 Py_ssize_t next = begin;
860 char *buf = PyString_AS_STRING(pystr);
861 PyObject *chunks = NULL;
862 PyObject *chunk = NULL;
863 PyObject *strchunk = NULL;
869 else if (end < 0 || len < end) {
870 PyErr_SetString(PyExc_ValueError,
"end is out of bounds");
876 for (next = end; next < len; next++) {
877 c = (
unsigned char)buf[next];
878 if (c ==
'"' || c ==
'\\') {
881 else if (strict && c <= 0x1f) {
889 if (!(c ==
'"' || c ==
'\\')) {
896 strchunk = PyString_FromStringAndSize(&buf[end], next - end);
897 if (strchunk == NULL) {
901 chunk = PyUnicode_FromEncodedObject(strchunk, encoding, NULL);
928 case 'b': c =
'\b';
break;
929 case 'f': c =
'\f';
break;
930 case 'n': c =
'\n';
break;
931 case 'r': c =
'\r';
break;
932 case 't': c =
'\t';
break;
949 for (; next < end; next++) {
953 case '0':
case '1':
case '2':
case '3':
case '4':
954 case '5':
case '6':
case '7':
case '8':
case '9':
955 c |= (digit -
'0');
break;
956 case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
958 c |= (digit -
'a' + 10);
break;
959 case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
961 c |= (digit -
'A' + 10);
break;
967 #if defined(Py_UNICODE_WIDE)
969 if ((c & 0xfc00) == 0xd800) {
970 if (end + 6 < len && buf[next] ==
'\\' && buf[next+1] ==
'u') {
974 for (next += 2; next < end; next++) {
978 case '0':
case '1':
case '2':
case '3':
case '4':
979 case '5':
case '6':
case '7':
case '8':
case '9':
980 c2 |= (digit -
'0');
break;
981 case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
983 c2 |= (digit -
'a' + 10);
break;
984 case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
986 c2 |= (digit -
'A' + 10);
break;
992 if ((c2 & 0xfc00) != 0xdc00) {
998 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
1009 chunk = PyUnicode_FromOrdinal(c);
1010 if (chunk == NULL) {
1015 char c_char = Py_CHARMASK(c);
1016 chunk = PyString_FromStringAndSize(&c_char, 1);
1017 if (chunk == NULL) {
1023 if (chunks == NULL) {
1040 *next_end_ptr = end;
1062 Py_ssize_t begin = end - 1;
1063 Py_ssize_t next = begin;
1067 PyObject *chunks = NULL;
1068 PyObject *chunk = NULL;
1074 else if (end < 0 || len < end) {
1075 PyErr_SetString(PyExc_ValueError,
"end is out of bounds");
1081 for (next = end; next < len; next++) {
1083 if (c ==
'"' || c ==
'\\') {
1086 else if (strict && c <= 0x1f) {
1091 if (!(c ==
'"' || c ==
'\\')) {
1098 #if PY_MAJOR_VERSION < 3
1099 chunk = PyUnicode_FromUnicode(&((
const Py_UNICODE *)buf)[end], next - end);
1101 chunk = PyUnicode_Substring(pystr, end, next);
1103 if (chunk == NULL) {
1124 case 'b': c =
'\b';
break;
1125 case 'f': c =
'\f';
break;
1126 case 'n': c =
'\n';
break;
1127 case 'r': c =
'\r';
break;
1128 case 't': c =
'\t';
break;
1145 for (; next < end; next++) {
1149 case '0':
case '1':
case '2':
case '3':
case '4':
1150 case '5':
case '6':
case '7':
case '8':
case '9':
1151 c |= (digit -
'0');
break;
1152 case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
1154 c |= (digit -
'a' + 10);
break;
1155 case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
1157 c |= (digit -
'A' + 10);
break;
1163 #if PY_MAJOR_VERSION >= 3 || defined(Py_UNICODE_WIDE)
1165 if ((c & 0xfc00) == 0xd800) {
1167 if (end + 6 < len &&
1172 for (next += 2; next < end; next++) {
1176 case '0':
case '1':
case '2':
case '3':
case '4':
1177 case '5':
case '6':
case '7':
case '8':
case '9':
1178 c2 |= (digit -
'0');
break;
1179 case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
1181 c2 |= (digit -
'a' + 10);
break;
1182 case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
1184 c2 |= (digit -
'A' + 10);
break;
1190 if ((c2 & 0xfc00) != 0xdc00) {
1196 c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00));
1203 chunk = PyUnicode_FromOrdinal(c);
1204 if (chunk == NULL) {
1209 if (chunks == NULL) {
1225 *next_end_ptr = end;
1235 "scanstring(basestring, end, encoding, strict=True) -> (str, end)\n"
1237 "Scan the string s for a JSON string. End is the index of the\n"
1238 "character in s after the quote that started the JSON string.\n"
1239 "Unescapes all valid JSON string escape sequences and raises ValueError\n"
1240 "on attempt to decode an invalid string. If strict is False then literal\n"
1241 "control characters are allowed in the string.\n"
1243 "Returns a tuple of the decoded string and the index of the character in s\n"
1244 "after the end quote."
1253 Py_ssize_t next_end = -1;
1254 char *encoding = NULL;
1256 if (!PyArg_ParseTuple(args,
"OO&|zi:scanstring", &pystr,
_convertPyInt_AsSsize_t, &end, &encoding, &strict)) {
1259 if (encoding == NULL) {
1262 if (PyUnicode_Check(pystr)) {
1267 #if PY_MAJOR_VERSION < 3
1270 else if (PyString_Check(pystr)) {
1275 PyErr_Format(PyExc_TypeError,
1276 "first argument must be a string, not %.80s",
1284 "encode_basestring_ascii(basestring) -> str\n"
1286 "Return an ASCII-only JSON representation of a Python string"
1297 else if (PyUnicode_Check(pystr)) {
1303 PyErr_Format(PyExc_TypeError,
1304 "first argument must be a string, not %.80s",
1314 PyObject_GC_UnTrack(
self);
1353 #if PY_MAJOR_VERSION < 3
1365 char *str = PyString_AS_STRING(pystr);
1366 Py_ssize_t end_idx = PyString_GET_SIZE(pystr) - 1;
1367 PyObject *rval = NULL;
1368 PyObject *pairs = NULL;
1370 PyObject *key = NULL;
1371 PyObject *val = NULL;
1372 char *encoding = PyString_AS_STRING(s->
encoding);
1373 int has_pairs_hook = (s->
pairs_hook != Py_None);
1375 Py_ssize_t next_idx;
1376 if (has_pairs_hook) {
1377 pairs = PyList_New(0);
1382 rval = PyDict_New();
1391 if (idx <= end_idx && str[idx] !=
'}') {
1392 int trailing_delimiter = 0;
1393 while (idx <= end_idx) {
1395 trailing_delimiter = 0;
1398 if (str[idx] !=
'"') {
1405 memokey = PyDict_GetItem(s->
memo, key);
1406 if (memokey != NULL) {
1412 if (PyDict_SetItem(s->
memo, key, key) < 0)
1419 if (idx > end_idx || str[idx] !=
':') {
1431 if (has_pairs_hook) {
1432 item = PyTuple_Pack(2, key, val);
1437 if (PyList_Append(pairs, item) == -1) {
1444 if (PyDict_SetItem(rval, key, val) < 0)
1456 if (idx > end_idx)
break;
1457 if (str[idx] ==
'}') {
1460 else if (str[idx] !=
',') {
1468 trailing_delimiter = 1;
1470 if (trailing_delimiter) {
1476 if (idx > end_idx || str[idx] !=
'}') {
1487 val = PyObject_CallFunctionObjArgs(s->
pairs_hook, pairs, NULL);
1491 *next_idx_ptr = idx + 1;
1497 val = PyObject_CallFunctionObjArgs(s->
object_hook, rval, NULL);
1504 *next_idx_ptr = idx + 1;
1528 PyObject *rval = NULL;
1529 PyObject *pairs = NULL;
1531 PyObject *key = NULL;
1532 PyObject *val = NULL;
1533 int has_pairs_hook = (s->
pairs_hook != Py_None);
1535 Py_ssize_t next_idx;
1537 if (has_pairs_hook) {
1538 pairs = PyList_New(0);
1543 rval = PyDict_New();
1553 int trailing_delimiter = 0;
1554 while (idx <= end_idx) {
1556 trailing_delimiter = 0;
1566 memokey = PyDict_GetItem(s->
memo, key);
1567 if (memokey != NULL) {
1573 if (PyDict_SetItem(s->
memo, key, key) < 0)
1593 if (has_pairs_hook) {
1594 item = PyTuple_Pack(2, key, val);
1599 if (PyList_Append(pairs, item) == -1) {
1606 if (PyDict_SetItem(rval, key, val) < 0)
1619 if (idx > end_idx)
break;
1631 trailing_delimiter = 1;
1633 if (trailing_delimiter) {
1651 val = PyObject_CallFunctionObjArgs(s->
pairs_hook, pairs, NULL);
1655 *next_idx_ptr = idx + 1;
1661 val = PyObject_CallFunctionObjArgs(s->
object_hook, rval, NULL);
1668 *next_idx_ptr = idx + 1;
1678 #if PY_MAJOR_VERSION < 3
1689 char *str = PyString_AS_STRING(pystr);
1690 Py_ssize_t end_idx = PyString_GET_SIZE(pystr) - 1;
1691 PyObject *val = NULL;
1692 PyObject *rval = PyList_New(0);
1693 Py_ssize_t next_idx;
1701 if (idx <= end_idx && str[idx] !=
']') {
1702 int trailing_delimiter = 0;
1703 while (idx <= end_idx) {
1704 trailing_delimiter = 0;
1711 if (PyList_Append(rval, val) == -1)
1721 if (idx > end_idx)
break;
1722 if (str[idx] ==
']') {
1725 else if (str[idx] !=
',') {
1733 trailing_delimiter = 1;
1735 if (trailing_delimiter) {
1742 if (idx > end_idx || str[idx] !=
']') {
1743 if (PyList_GET_SIZE(rval)) {
1750 *next_idx_ptr = idx + 1;
1772 PyObject *val = NULL;
1773 PyObject *rval = PyList_New(0);
1774 Py_ssize_t next_idx;
1783 int trailing_delimiter = 0;
1784 while (idx <= end_idx) {
1785 trailing_delimiter = 0;
1792 if (PyList_Append(rval, val) == -1)
1802 if (idx > end_idx)
break;
1814 trailing_delimiter = 1;
1816 if (trailing_delimiter) {
1824 if (PyList_GET_SIZE(rval)) {
1831 *next_idx_ptr = idx + 1;
1854 rval = PyObject_CallFunctionObjArgs(s->
parse_constant, constant, NULL);
1855 idx += PyString_GET_SIZE(constant);
1856 *next_idx_ptr = idx;
1860 #if PY_MAJOR_VERSION < 3
1873 char *str = PyString_AS_STRING(pystr);
1874 Py_ssize_t end_idx = PyString_GET_SIZE(pystr) - 1;
1875 Py_ssize_t idx =
start;
1881 if (str[idx] ==
'-') {
1882 if (idx >= end_idx) {
1890 if (str[idx] >=
'1' && str[idx] <=
'9') {
1892 while (idx <= end_idx && str[idx] >=
'0' && str[idx] <=
'9') idx++;
1895 else if (str[idx] ==
'0') {
1905 if (idx < end_idx && str[idx] ==
'.' && str[idx + 1] >=
'0' && str[idx + 1] <=
'9') {
1908 while (idx <= end_idx && str[idx] >=
'0' && str[idx] <=
'9') idx++;
1912 if (idx < end_idx && (str[idx] ==
'e' || str[idx] ==
'E')) {
1915 Py_ssize_t e_start = idx;
1919 if (idx < end_idx && (str[idx] ==
'-' || str[idx] ==
'+')) idx++;
1922 while (idx <= end_idx && str[idx] >=
'0' && str[idx] <=
'9') idx++;
1925 if (str[idx - 1] >=
'0' && str[idx - 1] <=
'9') {
1934 numstr = PyString_FromStringAndSize(&str[
start], idx -
start);
1939 if (s->
parse_float != (PyObject *)&PyFloat_Type) {
1940 rval = PyObject_CallFunctionObjArgs(s->
parse_float, numstr, NULL);
1946 if (d == -1.0 && PyErr_Occurred())
1948 rval = PyFloat_FromDouble(d);
1953 if (s->
parse_int != (PyObject *)&PyInt_Type) {
1954 rval = PyObject_CallFunctionObjArgs(s->
parse_int, numstr, NULL);
1957 rval = PyInt_FromString(PyString_AS_STRING(numstr), NULL, 10);
1961 *next_idx_ptr = idx;
1981 Py_ssize_t idx =
start;
1989 if (idx >= end_idx) {
2015 if (idx < end_idx &&
2024 if (idx < end_idx &&
2027 Py_ssize_t e_start = idx;
2031 if (idx < end_idx &&
2048 #if PY_MAJOR_VERSION >= 3
2049 numstr = PyUnicode_Substring(pystr,
start, idx);
2051 numstr = PyUnicode_FromUnicode(&((Py_UNICODE *)str)[
start], idx -
start);
2057 if (s->
parse_float != (PyObject *)&PyFloat_Type) {
2058 rval = PyObject_CallFunctionObjArgs(s->
parse_float, numstr, NULL);
2061 #if PY_MAJOR_VERSION >= 3
2062 rval = PyFloat_FromString(numstr);
2064 rval = PyFloat_FromString(numstr, NULL);
2070 rval = PyObject_CallFunctionObjArgs(s->
parse_int, numstr, NULL);
2073 *next_idx_ptr = idx;
2077 #if PY_MAJOR_VERSION < 3
2088 char *str = PyString_AS_STRING(pystr);
2089 Py_ssize_t length = PyString_GET_SIZE(pystr);
2090 PyObject *rval = NULL;
2091 int fallthrough = 0;
2092 if (idx < 0 || idx >= length) {
2106 if (Py_EnterRecursiveCall(
" while decoding a JSON object "
2110 Py_LeaveRecursiveCall();
2114 if (Py_EnterRecursiveCall(
" while decoding a JSON array "
2118 Py_LeaveRecursiveCall();
2122 if ((idx + 3 < length) && str[idx + 1] ==
'u' && str[idx + 2] ==
'l' && str[idx + 3] ==
'l') {
2124 *next_idx_ptr = idx + 4;
2132 if ((idx + 3 < length) && str[idx + 1] ==
'r' && str[idx + 2] ==
'u' && str[idx + 3] ==
'e') {
2134 *next_idx_ptr = idx + 4;
2142 if ((idx + 4 < length) && str[idx + 1] ==
'a' && str[idx + 2] ==
'l' && str[idx + 3] ==
's' && str[idx + 4] ==
'e') {
2143 Py_INCREF(Py_False);
2144 *next_idx_ptr = idx + 5;
2152 if ((idx + 2 < length) && str[idx + 1] ==
'a' && str[idx + 2] ==
'N') {
2160 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') {
2168 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') {
2198 PyObject *rval = NULL;
2199 int fallthrough = 0;
2200 if (idx < 0 || idx >= length) {
2213 if (Py_EnterRecursiveCall(
" while decoding a JSON object "
2214 "from a unicode string"))
2217 Py_LeaveRecursiveCall();
2221 if (Py_EnterRecursiveCall(
" while decoding a JSON array "
2222 "from a unicode string"))
2225 Py_LeaveRecursiveCall();
2229 if ((idx + 3 < length) &&
2234 *next_idx_ptr = idx + 4;
2242 if ((idx + 3 < length) &&
2247 *next_idx_ptr = idx + 4;
2255 if ((idx + 4 < length) &&
2260 Py_INCREF(Py_False);
2261 *next_idx_ptr = idx + 5;
2269 if ((idx + 2 < length) &&
2279 if ((idx + 7 < length) &&
2294 if ((idx + 8 < length) &&
2324 Py_ssize_t next_idx = -1;
2325 static char *kwlist[] = {
"string",
"idx", NULL};
2332 if (PyUnicode_Check(pystr)) {
2337 #if PY_MAJOR_VERSION < 3
2338 else if (PyString_Check(pystr)) {
2343 PyErr_Format(PyExc_TypeError,
2344 "first argument must be a string, not %.80s",
2348 PyDict_Clear(s->
memo);
2355 if (encoding == Py_None)
2357 #if PY_MAJOR_VERSION >= 3
2358 if (PyUnicode_Check(encoding)) {
2359 if (PyUnicode_AsUTF8(encoding) == NULL) {
2362 Py_INCREF(encoding);
2366 if (PyString_Check(encoding)) {
2367 Py_INCREF(encoding);
2370 if (PyUnicode_Check(encoding))
2371 return PyUnicode_AsEncodedString(encoding, NULL, NULL);
2373 PyErr_SetString(PyExc_TypeError,
"encoding must be a string");
2382 static char *kwlist[] = {
"context", NULL};
2386 if (!PyArg_ParseTupleAndKeywords(args, kwds,
"O:make_scanner", kwlist, &ctx))
2393 if (s->
memo == NULL) {
2394 s->
memo = PyDict_New();
2395 if (s->
memo == NULL)
2399 encoding = PyObject_GetAttrString(ctx,
"encoding");
2400 if (encoding == NULL)
2403 Py_XDECREF(encoding);
2408 s->
strict_bool = PyObject_GetAttrString(ctx,
"strict");
2414 s->
object_hook = PyObject_GetAttrString(ctx,
"object_hook");
2417 s->
pairs_hook = PyObject_GetAttrString(ctx,
"object_pairs_hook");
2420 s->
parse_float = PyObject_GetAttrString(ctx,
"parse_float");
2423 s->
parse_int = PyObject_GetAttrString(ctx,
"parse_int");
2426 s->
parse_constant = PyObject_GetAttrString(ctx,
"parse_constant");
2430 return (PyObject *)s;
2442 "simplejson._speedups.Scanner",
2460 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
2485 static char *kwlist[] = {
2497 "namedtuple_as_object",
2499 "int_as_string_bitcount",
2505 "iterable_as_array",
2509 PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
2510 PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan, *key_memo;
2511 PyObject *use_decimal, *namedtuple_as_object, *tuple_as_array, *iterable_as_array;
2512 PyObject *int_as_string_bitcount, *item_sort_key, *encoding, *for_json;
2513 PyObject *ignore_nan, *Decimal;
2516 if (!PyArg_ParseTupleAndKeywords(args, kwds,
"OOOOOOOOOOOOOOOOOOOO:make_encoder", kwlist,
2517 &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
2518 &sort_keys, &skipkeys, &allow_nan, &key_memo, &use_decimal,
2519 &namedtuple_as_object, &tuple_as_array,
2520 &int_as_string_bitcount, &item_sort_key, &encoding, &for_json,
2521 &ignore_nan, &Decimal, &iterable_as_array))
2530 Py_INCREF(defaultfn);
2534 #if PY_MAJOR_VERSION >= 3
2535 if (encoding == Py_None) {
2547 Py_INCREF(key_separator);
2549 Py_INCREF(item_separator);
2551 Py_INCREF(skipkeys);
2553 s->
skipkeys = PyObject_IsTrue(skipkeys);
2556 Py_INCREF(key_memo);
2559 is_true = PyObject_IsTrue(ignore_nan);
2563 is_true = PyObject_IsTrue(allow_nan);
2579 if (PyInt_Check(int_as_string_bitcount) || PyLong_Check(int_as_string_bitcount)) {
2580 static const unsigned long long_long_bitsize = SIZEOF_LONG_LONG * 8;
2581 long int_as_string_bitcount_val = PyLong_AsLong(int_as_string_bitcount);
2582 if (int_as_string_bitcount_val > 0 && int_as_string_bitcount_val < (
long)long_long_bitsize) {
2583 s->
max_long_size = PyLong_FromUnsignedLongLong(1ULL << (
int)int_as_string_bitcount_val);
2584 s->
min_long_size = PyLong_FromLongLong(-1LL << (
int)int_as_string_bitcount_val);
2590 PyErr_Format(PyExc_TypeError,
2591 "int_as_string_bitcount (%ld) must be greater than 0 and less than the number of bits of a `long long` type (%lu bits)",
2592 int_as_string_bitcount_val, long_long_bitsize);
2596 else if (int_as_string_bitcount == Py_None) {
2603 PyErr_SetString(PyExc_TypeError,
"int_as_string_bitcount must be None or an integer");
2606 if (item_sort_key != Py_None) {
2607 if (!PyCallable_Check(item_sort_key)) {
2608 PyErr_SetString(PyExc_TypeError,
"item_sort_key must be None or callable");
2613 is_true = PyObject_IsTrue(sort_keys);
2617 static PyObject *itemgetter0 = NULL;
2619 PyObject *
operator = PyImport_ImportModule(
"operator");
2622 itemgetter0 = PyObject_CallMethod(
operator,
"itemgetter",
"i", 0);
2623 Py_DECREF(
operator);
2625 item_sort_key = itemgetter0;
2630 if (item_sort_key == Py_None) {
2638 if (PyDict_SetItemString(s->
item_sort_kw,
"key", item_sort_key))
2641 Py_INCREF(sort_keys);
2643 Py_INCREF(item_sort_key);
2647 s->
for_json = PyObject_IsTrue(for_json);
2651 return (PyObject *)s;
2662 static char *kwlist[] = {
"obj",
"_current_indent_level", NULL};
2664 Py_ssize_t indent_level;
2669 if (!PyArg_ParseTupleAndKeywords(args, kwds,
"OO&:_iterencode", kwlist,
2685 if (obj == Py_None) {
2686 static PyObject *s_null = NULL;
2687 if (s_null == NULL) {
2693 else if (obj == Py_True) {
2694 static PyObject *s_true = NULL;
2695 if (s_true == NULL) {
2701 else if (obj == Py_False) {
2702 static PyObject *s_false = NULL;
2703 if (s_false == NULL) {
2710 PyErr_SetString(PyExc_ValueError,
"not a const");
2719 double i = PyFloat_AS_DOUBLE(obj);
2720 if (!Py_IS_FINITE(i)) {
2722 PyErr_SetString(PyExc_ValueError,
"Out of range float values are not JSON compliant");
2743 if (PyFloat_CheckExact(obj)) {
2744 return PyObject_Repr(obj);
2749 PyObject *tmp = PyObject_CallFunctionObjArgs((PyObject *)&PyFloat_Type, obj, NULL);
2753 res = PyObject_Repr(tmp);
2768 encoded = PyObject_CallFunctionObjArgs(s->
encoder, obj, NULL);
2769 if (encoded != NULL &&
2770 #
if PY_MAJOR_VERSION < 3
2771 !PyString_Check(encoded) &&
2773 !PyUnicode_Check(encoded))
2775 PyErr_Format(PyExc_TypeError,
2776 "encoder() must return a string, not %.80s",
2799 if (obj == Py_None || obj == Py_True || obj == Py_False) {
2805 PyUnicode_Check(obj))
2808 if (encoded != NULL)
2811 else if (PyInt_Check(obj) || PyLong_Check(obj)) {
2813 if (PyInt_CheckExact(obj) || PyLong_CheckExact(obj)) {
2814 encoded = PyObject_Str(obj);
2818 PyObject *tmp = PyObject_CallFunctionObjArgs((PyObject *)&PyLong_Type, obj, NULL);
2823 encoded = PyObject_Str(tmp);
2827 if (encoded != NULL) {
2829 if (encoded == NULL)
2834 else if (PyFloat_Check(obj)) {
2836 if (encoded != NULL)
2841 if (Py_EnterRecursiveCall(
" while encoding a JSON object"))
2843 newobj = PyObject_CallMethod(obj,
"for_json", NULL);
2844 if (newobj != NULL) {
2848 Py_LeaveRecursiveCall();
2852 if (Py_EnterRecursiveCall(
" while encoding a JSON object"))
2854 newobj = PyObject_CallMethod(obj,
"_asdict", NULL);
2855 if (newobj != NULL) {
2859 Py_LeaveRecursiveCall();
2861 else if (PyList_Check(obj) || (s->
tuple_as_array && PyTuple_Check(obj))) {
2862 if (Py_EnterRecursiveCall(
" while encoding a JSON object"))
2865 Py_LeaveRecursiveCall();
2867 else if (PyDict_Check(obj)) {
2868 if (Py_EnterRecursiveCall(
" while encoding a JSON object"))
2871 Py_LeaveRecursiveCall();
2874 PyObject *encoded = PyObject_Str(obj);
2875 if (encoded != NULL)
2880 PyObject *encoded = PyObject_GetAttrString(obj,
"encoded_json");
2881 if (encoded != NULL)
2885 PyObject *ident = NULL;
2888 newobj = PyObject_GetIter(obj);
2899 ident = PyLong_FromVoidPtr(obj);
2902 has_key = PyDict_Contains(s->
markers, ident);
2905 PyErr_SetString(PyExc_ValueError,
"Circular reference detected");
2909 if (PyDict_SetItem(s->
markers, ident, obj)) {
2914 if (Py_EnterRecursiveCall(
" while encoding a JSON object"))
2916 newobj = PyObject_CallFunctionObjArgs(s->
defaultfn, obj, NULL);
2917 if (newobj == NULL) {
2919 Py_LeaveRecursiveCall();
2923 Py_LeaveRecursiveCall();
2929 else if (ident != NULL) {
2930 if (PyDict_DelItem(s->
markers, ident)) {
2945 static PyObject *open_dict = NULL;
2946 static PyObject *close_dict = NULL;
2947 static PyObject *empty_dict = NULL;
2948 PyObject *kstr = NULL;
2949 PyObject *ident = NULL;
2950 PyObject *iter = NULL;
2951 PyObject *item = NULL;
2952 PyObject *items = NULL;
2953 PyObject *encoded = NULL;
2956 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
2960 if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
2963 if (PyDict_Size(dct) == 0)
2968 ident = PyLong_FromVoidPtr(dct);
2971 has_key = PyDict_Contains(s->
markers, ident);
2974 PyErr_SetString(PyExc_ValueError,
"Circular reference detected");
2977 if (PyDict_SetItem(s->
markers, ident, dct)) {
2985 if (s->
indent != Py_None) {
3000 while ((item = PyIter_Next(iter))) {
3001 PyObject *encoded, *key, *value;
3002 if (!PyTuple_Check(item) ||
Py_SIZE(item) != 2) {
3003 PyErr_SetString(PyExc_ValueError,
"items must return 2-tuples");
3006 key = PyTuple_GET_ITEM(item, 0);
3009 value = PyTuple_GET_ITEM(item, 1);
3013 encoded = PyDict_GetItem(s->
key_memo, key);
3014 if (encoded != NULL) {
3020 else if (kstr == Py_None) {
3031 if (encoded == NULL) {
3034 if (encoded == NULL)
3036 if (PyDict_SetItem(s->
key_memo, key, encoded))
3051 if (PyErr_Occurred())
3053 if (ident != NULL) {
3054 if (PyDict_DelItem(s->
markers, ident))
3058 if (s->
indent != Py_None) {
3070 Py_XDECREF(encoded);
3084 static PyObject *open_array = NULL;
3085 static PyObject *close_array = NULL;
3086 static PyObject *empty_array = NULL;
3087 PyObject *ident = NULL;
3088 PyObject *iter = NULL;
3089 PyObject *obj = NULL;
3093 if (open_array == NULL || close_array == NULL || empty_array == NULL) {
3097 if (open_array == NULL || close_array == NULL || empty_array == NULL)
3101 is_true = PyObject_IsTrue(seq);
3104 else if (is_true == 0)
3109 ident = PyLong_FromVoidPtr(seq);
3112 has_key = PyDict_Contains(s->
markers, ident);
3115 PyErr_SetString(PyExc_ValueError,
"Circular reference detected");
3118 if (PyDict_SetItem(s->
markers, ident, seq)) {
3123 iter = PyObject_GetIter(seq);
3129 if (s->
indent != Py_None) {
3138 while ((obj = PyIter_Next(iter))) {
3149 if (PyErr_Occurred())
3151 if (ident != NULL) {
3152 if (PyDict_DelItem(s->
markers, ident))
3156 if (s->
indent != Py_None) {
3178 PyObject_GC_UnTrack(
self);
3231 PyDoc_STRVAR(encoder_doc,
"_iterencode(obj, _current_indent_level) -> iterable");
3236 "simplejson._speedups.Encoder",
3254 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3277 {
"encode_basestring_ascii",
3280 pydoc_encode_basestring_ascii},
3285 {NULL, NULL, 0, NULL}
3289 "simplejson speedups\n");
3291 #if PY_MAJOR_VERSION >= 3
3292 static struct PyModuleDef moduledef = {
3293 PyModuleDef_HEAD_INIT,
3309 PyObject *module = PyImport_ImportModule(module_name);
3312 rval = PyObject_GetAttrString(module, attr_name);
3329 #if PY_MAJOR_VERSION >= 3
3354 #if PY_MAJOR_VERSION >= 3
3355 m = PyModule_Create(&moduledef);
3360 PyModule_AddObject(m,
"make_scanner", (PyObject*)&
PyScannerType);
3362 PyModule_AddObject(m,
"make_encoder", (PyObject*)&
PyEncoderType);
3372 #if PY_MAJOR_VERSION >= 3
3374 PyInit__speedups(
void)