2008-05-08 11:29:10 -03:00
|
|
|
#include "Python.h"
|
2009-05-02 09:36:44 -03:00
|
|
|
#include "structmember.h"
|
2012-03-22 10:42:18 -03:00
|
|
|
#include "accu.h"
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#define UNUSED __attribute__((__unused__))
|
|
|
|
#else
|
|
|
|
#define UNUSED
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType)
|
|
|
|
#define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType)
|
|
|
|
#define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType)
|
|
|
|
#define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType)
|
|
|
|
|
|
|
|
static PyTypeObject PyScannerType;
|
|
|
|
static PyTypeObject PyEncoderType;
|
|
|
|
|
|
|
|
typedef struct _PyScannerObject {
|
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *strict;
|
|
|
|
PyObject *object_hook;
|
|
|
|
PyObject *object_pairs_hook;
|
|
|
|
PyObject *parse_float;
|
|
|
|
PyObject *parse_int;
|
|
|
|
PyObject *parse_constant;
|
2010-09-04 17:16:53 -03:00
|
|
|
PyObject *memo;
|
2009-05-02 09:36:44 -03:00
|
|
|
} PyScannerObject;
|
|
|
|
|
|
|
|
static PyMemberDef scanner_members[] = {
|
|
|
|
{"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"},
|
|
|
|
{"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"},
|
|
|
|
{"object_pairs_hook", T_OBJECT, offsetof(PyScannerObject, object_pairs_hook), READONLY},
|
|
|
|
{"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"},
|
|
|
|
{"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "parse_int"},
|
|
|
|
{"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READONLY, "parse_constant"},
|
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _PyEncoderObject {
|
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *markers;
|
|
|
|
PyObject *defaultfn;
|
|
|
|
PyObject *encoder;
|
|
|
|
PyObject *indent;
|
|
|
|
PyObject *key_separator;
|
|
|
|
PyObject *item_separator;
|
|
|
|
PyObject *sort_keys;
|
|
|
|
PyObject *skipkeys;
|
2015-01-11 11:41:01 -04:00
|
|
|
PyCFunction fast_encode;
|
2009-05-02 09:36:44 -03:00
|
|
|
int allow_nan;
|
|
|
|
} PyEncoderObject;
|
|
|
|
|
|
|
|
static PyMemberDef encoder_members[] = {
|
|
|
|
{"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers"},
|
|
|
|
{"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "default"},
|
|
|
|
{"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder"},
|
|
|
|
{"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"},
|
|
|
|
{"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READONLY, "key_separator"},
|
|
|
|
{"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READONLY, "item_separator"},
|
|
|
|
{"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sort_keys"},
|
|
|
|
{"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipkeys"},
|
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
2011-08-19 13:03:14 -03:00
|
|
|
static PyObject *
|
|
|
|
join_list_unicode(PyObject *lst)
|
|
|
|
{
|
|
|
|
/* return u''.join(lst) */
|
|
|
|
static PyObject *sep = NULL;
|
|
|
|
if (sep == NULL) {
|
|
|
|
sep = PyUnicode_FromStringAndSize("", 0);
|
|
|
|
if (sep == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyUnicode_Join(sep, lst);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Forward decls */
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
static PyObject *
|
|
|
|
ascii_escape_unicode(PyObject *pystr);
|
|
|
|
static PyObject *
|
|
|
|
py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr);
|
|
|
|
void init_json(void);
|
|
|
|
static PyObject *
|
|
|
|
scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr);
|
|
|
|
static PyObject *
|
|
|
|
_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx);
|
|
|
|
static PyObject *
|
|
|
|
scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
|
|
|
|
static int
|
|
|
|
scanner_init(PyObject *self, PyObject *args, PyObject *kwds);
|
|
|
|
static void
|
|
|
|
scanner_dealloc(PyObject *self);
|
|
|
|
static int
|
|
|
|
scanner_clear(PyObject *self);
|
|
|
|
static PyObject *
|
|
|
|
encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
|
|
|
|
static int
|
|
|
|
encoder_init(PyObject *self, PyObject *args, PyObject *kwds);
|
|
|
|
static void
|
|
|
|
encoder_dealloc(PyObject *self);
|
|
|
|
static int
|
|
|
|
encoder_clear(PyObject *self);
|
|
|
|
static int
|
2011-10-06 14:09:51 -03:00
|
|
|
encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc, PyObject *seq, Py_ssize_t indent_level);
|
2009-05-02 09:36:44 -03:00
|
|
|
static int
|
2011-10-06 14:09:51 -03:00
|
|
|
encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, PyObject *obj, Py_ssize_t indent_level);
|
2009-05-02 09:36:44 -03:00
|
|
|
static int
|
2011-10-06 14:09:51 -03:00
|
|
|
encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, PyObject *dct, Py_ssize_t indent_level);
|
2009-05-02 09:36:44 -03:00
|
|
|
static PyObject *
|
2009-05-02 12:55:19 -03:00
|
|
|
_encoded_const(PyObject *obj);
|
2009-05-02 09:36:44 -03:00
|
|
|
static void
|
|
|
|
raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
|
|
|
|
static PyObject *
|
|
|
|
encoder_encode_string(PyEncoderObject *s, PyObject *obj);
|
|
|
|
static PyObject *
|
2013-08-10 17:01:45 -03:00
|
|
|
encoder_encode_long(PyEncoderObject* s UNUSED, PyObject *obj);
|
|
|
|
static PyObject *
|
2009-05-02 09:36:44 -03:00
|
|
|
encoder_encode_float(PyEncoderObject *s, PyObject *obj);
|
2008-05-08 11:29:10 -03:00
|
|
|
|
|
|
|
#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
|
2009-05-02 09:36:44 -03:00
|
|
|
#define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
|
2008-05-08 11:29:10 -03:00
|
|
|
|
|
|
|
static Py_ssize_t
|
2011-09-28 02:41:54 -03:00
|
|
|
ascii_escape_unichar(Py_UCS4 c, unsigned char *output, Py_ssize_t chars)
|
2008-05-08 11:29:10 -03:00
|
|
|
{
|
2009-05-02 09:36:44 -03:00
|
|
|
/* Escape unicode code point c to ASCII escape sequences
|
|
|
|
in char *output. output must have at least 12 bytes unused to
|
|
|
|
accommodate an escaped surrogate pair "\uXXXX\uXXXX" */
|
2008-05-08 11:29:10 -03:00
|
|
|
output[chars++] = '\\';
|
|
|
|
switch (c) {
|
2009-05-02 09:36:44 -03:00
|
|
|
case '\\': output[chars++] = c; break;
|
|
|
|
case '"': output[chars++] = c; break;
|
2008-05-08 11:29:10 -03:00
|
|
|
case '\b': output[chars++] = 'b'; break;
|
|
|
|
case '\f': output[chars++] = 'f'; break;
|
|
|
|
case '\n': output[chars++] = 'n'; break;
|
|
|
|
case '\r': output[chars++] = 'r'; break;
|
|
|
|
case '\t': output[chars++] = 't'; break;
|
|
|
|
default:
|
|
|
|
if (c >= 0x10000) {
|
|
|
|
/* UTF-16 surrogate pair */
|
2012-10-29 21:42:39 -03:00
|
|
|
Py_UCS4 v = Py_UNICODE_HIGH_SURROGATE(c);
|
2008-05-08 11:29:10 -03:00
|
|
|
output[chars++] = 'u';
|
2012-10-29 21:42:39 -03:00
|
|
|
output[chars++] = Py_hexdigits[(v >> 12) & 0xf];
|
|
|
|
output[chars++] = Py_hexdigits[(v >> 8) & 0xf];
|
|
|
|
output[chars++] = Py_hexdigits[(v >> 4) & 0xf];
|
|
|
|
output[chars++] = Py_hexdigits[(v ) & 0xf];
|
|
|
|
c = Py_UNICODE_LOW_SURROGATE(c);
|
2008-05-08 11:29:10 -03:00
|
|
|
output[chars++] = '\\';
|
|
|
|
}
|
|
|
|
output[chars++] = 'u';
|
2011-10-13 21:13:11 -03:00
|
|
|
output[chars++] = Py_hexdigits[(c >> 12) & 0xf];
|
|
|
|
output[chars++] = Py_hexdigits[(c >> 8) & 0xf];
|
|
|
|
output[chars++] = Py_hexdigits[(c >> 4) & 0xf];
|
|
|
|
output[chars++] = Py_hexdigits[(c ) & 0xf];
|
2008-05-08 11:29:10 -03:00
|
|
|
}
|
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
ascii_escape_unicode(PyObject *pystr)
|
|
|
|
{
|
2009-05-02 09:36:44 -03:00
|
|
|
/* Take a PyUnicode pystr and return a new ASCII-only escaped PyUnicode */
|
2008-05-08 11:29:10 -03:00
|
|
|
Py_ssize_t i;
|
|
|
|
Py_ssize_t input_chars;
|
|
|
|
Py_ssize_t output_size;
|
|
|
|
Py_ssize_t chars;
|
|
|
|
PyObject *rval;
|
2011-09-28 02:41:54 -03:00
|
|
|
void *input;
|
|
|
|
unsigned char *output;
|
|
|
|
int kind;
|
2008-05-08 11:29:10 -03:00
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READY(pystr) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
input_chars = PyUnicode_GET_LENGTH(pystr);
|
|
|
|
input = PyUnicode_DATA(pystr);
|
|
|
|
kind = PyUnicode_KIND(pystr);
|
|
|
|
|
|
|
|
/* Compute the output size */
|
|
|
|
for (i = 0, output_size = 2; i < input_chars; i++) {
|
|
|
|
Py_UCS4 c = PyUnicode_READ(kind, input, i);
|
2015-02-01 18:53:53 -04:00
|
|
|
Py_ssize_t d;
|
|
|
|
if (S_CHAR(c)) {
|
|
|
|
d = 1;
|
|
|
|
}
|
2011-09-28 02:41:54 -03:00
|
|
|
else {
|
|
|
|
switch(c) {
|
2011-10-11 16:56:19 -03:00
|
|
|
case '\\': case '"': case '\b': case '\f':
|
2011-09-28 02:41:54 -03:00
|
|
|
case '\n': case '\r': case '\t':
|
2015-02-01 18:53:53 -04:00
|
|
|
d = 2; break;
|
2011-09-28 02:41:54 -03:00
|
|
|
default:
|
2015-02-01 18:53:53 -04:00
|
|
|
d = c >= 0x10000 ? 12 : 6;
|
2011-09-28 02:41:54 -03:00
|
|
|
}
|
|
|
|
}
|
2015-02-01 18:53:53 -04:00
|
|
|
if (output_size > PY_SSIZE_T_MAX - d) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError, "string is too long to escape");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
output_size += d;
|
2011-09-28 02:41:54 -03:00
|
|
|
}
|
2009-05-02 09:36:44 -03:00
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
rval = PyUnicode_New(output_size, 127);
|
2008-05-08 11:29:10 -03:00
|
|
|
if (rval == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-28 02:41:54 -03:00
|
|
|
output = PyUnicode_1BYTE_DATA(rval);
|
2008-05-08 11:29:10 -03:00
|
|
|
chars = 0;
|
|
|
|
output[chars++] = '"';
|
|
|
|
for (i = 0; i < input_chars; i++) {
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_UCS4 c = PyUnicode_READ(kind, input, i);
|
2008-05-08 11:29:10 -03:00
|
|
|
if (S_CHAR(c)) {
|
2009-05-02 09:36:44 -03:00
|
|
|
output[chars++] = c;
|
2008-05-08 11:29:10 -03:00
|
|
|
}
|
2009-05-02 09:36:44 -03:00
|
|
|
else {
|
|
|
|
chars = ascii_escape_unichar(c, output, chars);
|
2008-05-08 11:29:10 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
output[chars++] = '"';
|
2013-01-03 04:21:55 -04:00
|
|
|
#ifdef Py_DEBUG
|
2012-04-27 08:55:39 -03:00
|
|
|
assert(_PyUnicode_CheckConsistency(rval, 1));
|
2013-01-03 04:21:55 -04:00
|
|
|
#endif
|
2008-05-08 11:29:10 -03:00
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2015-01-11 11:41:01 -04:00
|
|
|
static PyObject *
|
|
|
|
escape_unicode(PyObject *pystr)
|
|
|
|
{
|
|
|
|
/* Take a PyUnicode pystr and return a new escaped PyUnicode */
|
|
|
|
Py_ssize_t i;
|
|
|
|
Py_ssize_t input_chars;
|
|
|
|
Py_ssize_t output_size;
|
|
|
|
Py_ssize_t chars;
|
|
|
|
PyObject *rval;
|
|
|
|
void *input;
|
|
|
|
int kind;
|
|
|
|
Py_UCS4 maxchar;
|
|
|
|
|
|
|
|
if (PyUnicode_READY(pystr) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
maxchar = PyUnicode_MAX_CHAR_VALUE(pystr);
|
|
|
|
input_chars = PyUnicode_GET_LENGTH(pystr);
|
|
|
|
input = PyUnicode_DATA(pystr);
|
|
|
|
kind = PyUnicode_KIND(pystr);
|
|
|
|
|
|
|
|
/* Compute the output size */
|
|
|
|
for (i = 0, output_size = 2; i < input_chars; i++) {
|
|
|
|
Py_UCS4 c = PyUnicode_READ(kind, input, i);
|
|
|
|
switch (c) {
|
|
|
|
case '\\': case '"': case '\b': case '\f':
|
|
|
|
case '\n': case '\r': case '\t':
|
|
|
|
output_size += 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (c <= 0x1f)
|
|
|
|
output_size += 6;
|
|
|
|
else
|
|
|
|
output_size++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rval = PyUnicode_New(output_size, maxchar);
|
|
|
|
if (rval == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
kind = PyUnicode_KIND(rval);
|
|
|
|
|
|
|
|
#define ENCODE_OUTPUT do { \
|
|
|
|
chars = 0; \
|
|
|
|
output[chars++] = '"'; \
|
|
|
|
for (i = 0; i < input_chars; i++) { \
|
|
|
|
Py_UCS4 c = PyUnicode_READ(kind, input, i); \
|
|
|
|
switch (c) { \
|
|
|
|
case '\\': output[chars++] = '\\'; output[chars++] = c; break; \
|
|
|
|
case '"': output[chars++] = '\\'; output[chars++] = c; break; \
|
|
|
|
case '\b': output[chars++] = '\\'; output[chars++] = 'b'; break; \
|
|
|
|
case '\f': output[chars++] = '\\'; output[chars++] = 'f'; break; \
|
|
|
|
case '\n': output[chars++] = '\\'; output[chars++] = 'n'; break; \
|
|
|
|
case '\r': output[chars++] = '\\'; output[chars++] = 'r'; break; \
|
|
|
|
case '\t': output[chars++] = '\\'; output[chars++] = 't'; break; \
|
|
|
|
default: \
|
|
|
|
if (c <= 0x1f) { \
|
|
|
|
output[chars++] = '\\'; \
|
|
|
|
output[chars++] = 'u'; \
|
|
|
|
output[chars++] = '0'; \
|
|
|
|
output[chars++] = '0'; \
|
|
|
|
output[chars++] = Py_hexdigits[(c >> 4) & 0xf]; \
|
|
|
|
output[chars++] = Py_hexdigits[(c ) & 0xf]; \
|
|
|
|
} else { \
|
|
|
|
output[chars++] = c; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
output[chars++] = '"'; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
if (kind == PyUnicode_1BYTE_KIND) {
|
|
|
|
Py_UCS1 *output = PyUnicode_1BYTE_DATA(rval);
|
|
|
|
ENCODE_OUTPUT;
|
|
|
|
} else if (kind == PyUnicode_2BYTE_KIND) {
|
|
|
|
Py_UCS2 *output = PyUnicode_2BYTE_DATA(rval);
|
|
|
|
ENCODE_OUTPUT;
|
|
|
|
} else {
|
|
|
|
Py_UCS4 *output = PyUnicode_4BYTE_DATA(rval);
|
|
|
|
assert(kind == PyUnicode_4BYTE_KIND);
|
|
|
|
ENCODE_OUTPUT;
|
|
|
|
}
|
|
|
|
#undef ENCODE_OUTPUT
|
|
|
|
|
|
|
|
#ifdef Py_DEBUG
|
|
|
|
assert(_PyUnicode_CheckConsistency(rval, 1));
|
|
|
|
#endif
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
static void
|
2008-05-08 11:29:10 -03:00
|
|
|
raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
|
|
|
|
{
|
2015-01-26 07:16:30 -04:00
|
|
|
/* Use JSONDecodeError exception to raise a nice looking ValueError subclass */
|
|
|
|
static PyObject *JSONDecodeError = NULL;
|
|
|
|
PyObject *exc;
|
|
|
|
if (JSONDecodeError == NULL) {
|
2008-05-08 11:29:10 -03:00
|
|
|
PyObject *decoder = PyImport_ImportModule("json.decoder");
|
|
|
|
if (decoder == NULL)
|
|
|
|
return;
|
2015-01-26 07:16:30 -04:00
|
|
|
JSONDecodeError = PyObject_GetAttrString(decoder, "JSONDecodeError");
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_DECREF(decoder);
|
2015-01-26 07:16:30 -04:00
|
|
|
if (JSONDecodeError == NULL)
|
2008-05-08 11:29:10 -03:00
|
|
|
return;
|
|
|
|
}
|
2015-01-26 07:16:30 -04:00
|
|
|
exc = PyObject_CallFunction(JSONDecodeError, "(zOn)", msg, s, end);
|
|
|
|
if (exc) {
|
|
|
|
PyErr_SetObject(JSONDecodeError, exc);
|
|
|
|
Py_DECREF(exc);
|
2008-10-16 18:17:24 -03:00
|
|
|
}
|
2008-05-08 11:29:10 -03:00
|
|
|
}
|
|
|
|
|
2013-01-03 02:44:15 -04:00
|
|
|
static void
|
|
|
|
raise_stop_iteration(Py_ssize_t idx)
|
|
|
|
{
|
|
|
|
PyObject *value = PyLong_FromSsize_t(idx);
|
|
|
|
if (value != NULL) {
|
|
|
|
PyErr_SetObject(PyExc_StopIteration, value);
|
|
|
|
Py_DECREF(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-08 11:29:10 -03:00
|
|
|
static PyObject *
|
2009-05-02 09:36:44 -03:00
|
|
|
_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
|
|
|
|
/* return (rval, idx) tuple, stealing reference to rval */
|
|
|
|
PyObject *tpl;
|
|
|
|
PyObject *pyidx;
|
|
|
|
/*
|
|
|
|
steal a reference to rval, returns (rval, idx)
|
|
|
|
*/
|
|
|
|
if (rval == NULL) {
|
|
|
|
return NULL;
|
2008-07-19 19:26:35 -03:00
|
|
|
}
|
2009-05-02 09:36:44 -03:00
|
|
|
pyidx = PyLong_FromSsize_t(idx);
|
|
|
|
if (pyidx == NULL) {
|
|
|
|
Py_DECREF(rval);
|
|
|
|
return NULL;
|
2008-05-08 11:29:10 -03:00
|
|
|
}
|
2009-05-02 09:36:44 -03:00
|
|
|
tpl = PyTuple_New(2);
|
|
|
|
if (tpl == NULL) {
|
|
|
|
Py_DECREF(pyidx);
|
|
|
|
Py_DECREF(rval);
|
|
|
|
return NULL;
|
2008-05-08 11:29:10 -03:00
|
|
|
}
|
2009-05-02 09:36:44 -03:00
|
|
|
PyTuple_SET_ITEM(tpl, 0, rval);
|
|
|
|
PyTuple_SET_ITEM(tpl, 1, pyidx);
|
|
|
|
return tpl;
|
2008-05-08 11:29:10 -03:00
|
|
|
}
|
|
|
|
|
2010-09-04 17:16:53 -03:00
|
|
|
#define APPEND_OLD_CHUNK \
|
|
|
|
if (chunk != NULL) { \
|
|
|
|
if (chunks == NULL) { \
|
|
|
|
chunks = PyList_New(0); \
|
|
|
|
if (chunks == NULL) { \
|
|
|
|
goto bail; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if (PyList_Append(chunks, chunk)) { \
|
2014-09-10 18:31:42 -03:00
|
|
|
Py_CLEAR(chunk); \
|
2010-09-04 17:16:53 -03:00
|
|
|
goto bail; \
|
|
|
|
} \
|
|
|
|
Py_CLEAR(chunk); \
|
|
|
|
}
|
|
|
|
|
2008-05-08 11:29:10 -03:00
|
|
|
static PyObject *
|
2009-05-02 09:36:44 -03:00
|
|
|
scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
|
2008-05-08 11:29:10 -03:00
|
|
|
{
|
2009-05-02 09:36:44 -03:00
|
|
|
/* Read the JSON string from PyUnicode pystr.
|
|
|
|
end is the index of the first character after the quote.
|
|
|
|
if strict is zero then literal control characters are allowed
|
|
|
|
*next_end_ptr is a return-by-reference index of the character
|
|
|
|
after the end quote
|
|
|
|
|
|
|
|
Return value is a new PyUnicode
|
|
|
|
*/
|
2010-09-04 17:16:53 -03:00
|
|
|
PyObject *rval = NULL;
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_ssize_t len;
|
2008-05-08 11:29:10 -03:00
|
|
|
Py_ssize_t begin = end - 1;
|
2011-02-22 16:15:44 -04:00
|
|
|
Py_ssize_t next /* = begin */;
|
2011-09-28 02:41:54 -03:00
|
|
|
const void *buf;
|
|
|
|
int kind;
|
2010-09-04 17:16:53 -03:00
|
|
|
PyObject *chunks = NULL;
|
|
|
|
PyObject *chunk = NULL;
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READY(pystr) == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
len = PyUnicode_GET_LENGTH(pystr);
|
|
|
|
buf = PyUnicode_DATA(pystr);
|
|
|
|
kind = PyUnicode_KIND(pystr);
|
|
|
|
|
2013-01-03 02:44:15 -04:00
|
|
|
if (end < 0 || len < end) {
|
2008-07-19 19:26:35 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "end is out of bounds");
|
|
|
|
goto bail;
|
|
|
|
}
|
2008-05-08 11:29:10 -03:00
|
|
|
while (1) {
|
|
|
|
/* Find the end of the string or the next escape */
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_UCS4 c = 0;
|
2008-05-08 11:29:10 -03:00
|
|
|
for (next = end; next < len; next++) {
|
2011-09-28 02:41:54 -03:00
|
|
|
c = PyUnicode_READ(kind, buf, next);
|
2008-05-08 11:29:10 -03:00
|
|
|
if (c == '"' || c == '\\') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (strict && c <= 0x1f) {
|
2008-07-19 19:26:35 -03:00
|
|
|
raise_errmsg("Invalid control character at", pystr, next);
|
2008-05-08 11:29:10 -03:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(c == '"' || c == '\\')) {
|
|
|
|
raise_errmsg("Unterminated string starting at", pystr, begin);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
/* Pick up this chunk if it's not zero length */
|
|
|
|
if (next != end) {
|
2010-09-04 17:16:53 -03:00
|
|
|
APPEND_OLD_CHUNK
|
2011-09-28 02:41:54 -03:00
|
|
|
chunk = PyUnicode_FromKindAndData(
|
|
|
|
kind,
|
2011-10-07 15:55:35 -03:00
|
|
|
(char*)buf + kind * end,
|
2011-09-28 02:41:54 -03:00
|
|
|
next - end);
|
2008-05-08 11:29:10 -03:00
|
|
|
if (chunk == NULL) {
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next++;
|
|
|
|
if (c == '"') {
|
|
|
|
end = next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (next == len) {
|
|
|
|
raise_errmsg("Unterminated string starting at", pystr, begin);
|
|
|
|
goto bail;
|
|
|
|
}
|
2011-09-28 02:41:54 -03:00
|
|
|
c = PyUnicode_READ(kind, buf, next);
|
2008-05-08 11:29:10 -03:00
|
|
|
if (c != 'u') {
|
|
|
|
/* Non-unicode backslash escapes */
|
|
|
|
end = next + 1;
|
|
|
|
switch (c) {
|
|
|
|
case '"': break;
|
|
|
|
case '\\': break;
|
|
|
|
case '/': break;
|
|
|
|
case 'b': c = '\b'; break;
|
|
|
|
case 'f': c = '\f'; break;
|
|
|
|
case 'n': c = '\n'; break;
|
|
|
|
case 'r': c = '\r'; break;
|
|
|
|
case 't': c = '\t'; break;
|
|
|
|
default: c = 0;
|
|
|
|
}
|
|
|
|
if (c == 0) {
|
|
|
|
raise_errmsg("Invalid \\escape", pystr, end - 2);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c = 0;
|
|
|
|
next++;
|
|
|
|
end = next + 4;
|
|
|
|
if (end >= len) {
|
|
|
|
raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
/* Decode 4 hex digits */
|
|
|
|
for (; next < end; next++) {
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
|
2009-05-02 09:36:44 -03:00
|
|
|
c <<= 4;
|
2008-05-08 11:29:10 -03:00
|
|
|
switch (digit) {
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
2009-05-02 09:36:44 -03:00
|
|
|
c |= (digit - '0'); break;
|
2008-05-08 11:29:10 -03:00
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e':
|
|
|
|
case 'f':
|
2009-05-02 09:36:44 -03:00
|
|
|
c |= (digit - 'a' + 10); break;
|
2008-05-08 11:29:10 -03:00
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E':
|
|
|
|
case 'F':
|
2009-05-02 09:36:44 -03:00
|
|
|
c |= (digit - 'A' + 10); break;
|
2008-05-08 11:29:10 -03:00
|
|
|
default:
|
|
|
|
raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Surrogate pair */
|
2013-11-26 15:25:28 -04:00
|
|
|
if (Py_UNICODE_IS_HIGH_SURROGATE(c) && end + 6 < len &&
|
|
|
|
PyUnicode_READ(kind, buf, next++) == '\\' &&
|
|
|
|
PyUnicode_READ(kind, buf, next++) == 'u') {
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_UCS4 c2 = 0;
|
2008-05-08 11:29:10 -03:00
|
|
|
end += 6;
|
|
|
|
/* Decode 4 hex digits */
|
|
|
|
for (; next < end; next++) {
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
|
2010-10-09 12:24:28 -03:00
|
|
|
c2 <<= 4;
|
2008-05-08 11:29:10 -03:00
|
|
|
switch (digit) {
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
2009-05-02 09:36:44 -03:00
|
|
|
c2 |= (digit - '0'); break;
|
2008-05-08 11:29:10 -03:00
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e':
|
|
|
|
case 'f':
|
2009-05-02 09:36:44 -03:00
|
|
|
c2 |= (digit - 'a' + 10); break;
|
2008-05-08 11:29:10 -03:00
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E':
|
|
|
|
case 'F':
|
2009-05-02 09:36:44 -03:00
|
|
|
c2 |= (digit - 'A' + 10); break;
|
2008-05-08 11:29:10 -03:00
|
|
|
default:
|
|
|
|
raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
2013-11-26 15:25:28 -04:00
|
|
|
if (Py_UNICODE_IS_LOW_SURROGATE(c2))
|
|
|
|
c = Py_UNICODE_JOIN_SURROGATES(c, c2);
|
|
|
|
else
|
|
|
|
end -= 6;
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
2008-05-08 11:29:10 -03:00
|
|
|
}
|
2010-09-04 17:16:53 -03:00
|
|
|
APPEND_OLD_CHUNK
|
2011-09-28 02:41:54 -03:00
|
|
|
chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1);
|
2008-05-08 11:29:10 -03:00
|
|
|
if (chunk == NULL) {
|
|
|
|
goto bail;
|
|
|
|
}
|
2010-09-04 17:16:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (chunks == NULL) {
|
|
|
|
if (chunk != NULL)
|
|
|
|
rval = chunk;
|
|
|
|
else
|
|
|
|
rval = PyUnicode_FromStringAndSize("", 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
APPEND_OLD_CHUNK
|
|
|
|
rval = join_list_unicode(chunks);
|
|
|
|
if (rval == NULL) {
|
2008-05-08 11:29:10 -03:00
|
|
|
goto bail;
|
|
|
|
}
|
2010-09-04 17:16:53 -03:00
|
|
|
Py_CLEAR(chunks);
|
2008-05-08 11:29:10 -03:00
|
|
|
}
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
*next_end_ptr = end;
|
|
|
|
return rval;
|
2008-05-08 11:29:10 -03:00
|
|
|
bail:
|
2009-05-02 09:36:44 -03:00
|
|
|
*next_end_ptr = -1;
|
2008-05-08 11:29:10 -03:00
|
|
|
Py_XDECREF(chunks);
|
2010-09-04 17:16:53 -03:00
|
|
|
Py_XDECREF(chunk);
|
2008-05-08 11:29:10 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pydoc_scanstring,
|
2010-08-02 17:16:18 -03:00
|
|
|
"scanstring(string, end, strict=True) -> (string, end)\n"
|
2009-05-02 09:36:44 -03:00
|
|
|
"\n"
|
|
|
|
"Scan the string s for a JSON string. End is the index of the\n"
|
|
|
|
"character in s after the quote that started the JSON string.\n"
|
|
|
|
"Unescapes all valid JSON string escape sequences and raises ValueError\n"
|
|
|
|
"on attempt to decode an invalid string. If strict is False then literal\n"
|
|
|
|
"control characters are allowed in the string.\n"
|
|
|
|
"\n"
|
|
|
|
"Returns a tuple of the decoded string and the index of the character in s\n"
|
|
|
|
"after the end quote."
|
|
|
|
);
|
2008-05-08 11:29:10 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2009-05-02 09:36:44 -03:00
|
|
|
py_scanstring(PyObject* self UNUSED, PyObject *args)
|
2008-05-08 11:29:10 -03:00
|
|
|
{
|
|
|
|
PyObject *pystr;
|
2009-05-02 09:36:44 -03:00
|
|
|
PyObject *rval;
|
2008-05-08 11:29:10 -03:00
|
|
|
Py_ssize_t end;
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_ssize_t next_end = -1;
|
|
|
|
int strict = 1;
|
2012-12-01 14:34:16 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "On|i:scanstring", &pystr, &end, &strict)) {
|
2008-05-08 11:29:10 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-05-02 09:36:44 -03:00
|
|
|
if (PyUnicode_Check(pystr)) {
|
|
|
|
rval = scanstring_unicode(pystr, end, strict, &next_end);
|
2008-05-08 11:29:10 -03:00
|
|
|
}
|
|
|
|
else {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
2010-08-02 17:16:18 -03:00
|
|
|
"first argument must be a string, not %.80s",
|
2008-05-08 11:29:10 -03:00
|
|
|
Py_TYPE(pystr)->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-05-02 09:36:44 -03:00
|
|
|
return _build_rval_index_tuple(rval, next_end);
|
2008-05-08 11:29:10 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pydoc_encode_basestring_ascii,
|
2010-08-02 17:16:18 -03:00
|
|
|
"encode_basestring_ascii(string) -> string\n"
|
2009-05-02 09:36:44 -03:00
|
|
|
"\n"
|
|
|
|
"Return an ASCII-only JSON representation of a Python string"
|
|
|
|
);
|
2008-05-08 11:29:10 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2009-05-02 09:36:44 -03:00
|
|
|
py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
|
2008-05-08 11:29:10 -03:00
|
|
|
{
|
|
|
|
PyObject *rval;
|
2009-05-02 09:36:44 -03:00
|
|
|
/* Return an ASCII-only JSON representation of a Python string */
|
2008-05-08 11:29:10 -03:00
|
|
|
/* METH_O */
|
2009-05-02 09:36:44 -03:00
|
|
|
if (PyUnicode_Check(pystr)) {
|
2008-05-08 11:29:10 -03:00
|
|
|
rval = ascii_escape_unicode(pystr);
|
|
|
|
}
|
|
|
|
else {
|
2009-05-02 09:36:44 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"first argument must be a string, not %.80s",
|
2008-05-08 11:29:10 -03:00
|
|
|
Py_TYPE(pystr)->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2015-01-11 11:41:01 -04:00
|
|
|
|
|
|
|
PyDoc_STRVAR(pydoc_encode_basestring,
|
|
|
|
"encode_basestring(string) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Return a JSON representation of a Python string"
|
|
|
|
);
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
py_encode_basestring(PyObject* self UNUSED, PyObject *pystr)
|
|
|
|
{
|
|
|
|
PyObject *rval;
|
|
|
|
/* Return a JSON representation of a Python string */
|
|
|
|
/* METH_O */
|
|
|
|
if (PyUnicode_Check(pystr)) {
|
|
|
|
rval = escape_unicode(pystr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"first argument must be a string, not %.80s",
|
|
|
|
Py_TYPE(pystr)->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
static void
|
|
|
|
scanner_dealloc(PyObject *self)
|
|
|
|
{
|
|
|
|
/* Deallocate scanner object */
|
|
|
|
scanner_clear(self);
|
|
|
|
Py_TYPE(self)->tp_free(self);
|
|
|
|
}
|
2008-05-08 11:29:10 -03:00
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
static int
|
|
|
|
scanner_traverse(PyObject *self, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
PyScannerObject *s;
|
|
|
|
assert(PyScanner_Check(self));
|
|
|
|
s = (PyScannerObject *)self;
|
|
|
|
Py_VISIT(s->strict);
|
|
|
|
Py_VISIT(s->object_hook);
|
|
|
|
Py_VISIT(s->object_pairs_hook);
|
|
|
|
Py_VISIT(s->parse_float);
|
|
|
|
Py_VISIT(s->parse_int);
|
|
|
|
Py_VISIT(s->parse_constant);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-11 02:26:20 -03:00
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
static int
|
|
|
|
scanner_clear(PyObject *self)
|
2008-05-08 11:29:10 -03:00
|
|
|
{
|
2009-05-02 09:36:44 -03:00
|
|
|
PyScannerObject *s;
|
|
|
|
assert(PyScanner_Check(self));
|
|
|
|
s = (PyScannerObject *)self;
|
|
|
|
Py_CLEAR(s->strict);
|
|
|
|
Py_CLEAR(s->object_hook);
|
|
|
|
Py_CLEAR(s->object_pairs_hook);
|
|
|
|
Py_CLEAR(s->parse_float);
|
|
|
|
Py_CLEAR(s->parse_int);
|
|
|
|
Py_CLEAR(s->parse_constant);
|
2010-09-04 17:16:53 -03:00
|
|
|
Py_CLEAR(s->memo);
|
2009-05-02 09:36:44 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
|
|
|
|
/* Read a JSON object from PyUnicode pystr.
|
|
|
|
idx is the index of the first character after the opening curly brace.
|
|
|
|
*next_idx_ptr is a return-by-reference index to the first character after
|
|
|
|
the closing curly brace.
|
|
|
|
|
|
|
|
Returns a new PyObject (usually a dict, but object_hook can change that)
|
|
|
|
*/
|
2011-09-28 02:41:54 -03:00
|
|
|
void *str;
|
|
|
|
int kind;
|
|
|
|
Py_ssize_t end_idx;
|
2009-05-02 09:36:44 -03:00
|
|
|
PyObject *val = NULL;
|
2010-09-04 17:16:53 -03:00
|
|
|
PyObject *rval = NULL;
|
2009-05-02 09:36:44 -03:00
|
|
|
PyObject *key = NULL;
|
|
|
|
int strict = PyObject_IsTrue(s->strict);
|
2010-09-04 17:16:53 -03:00
|
|
|
int has_pairs_hook = (s->object_pairs_hook != Py_None);
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_ssize_t next_idx;
|
2010-09-04 17:16:53 -03:00
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READY(pystr) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
str = PyUnicode_DATA(pystr);
|
|
|
|
kind = PyUnicode_KIND(pystr);
|
|
|
|
end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
|
|
|
|
|
2010-09-04 17:16:53 -03:00
|
|
|
if (has_pairs_hook)
|
|
|
|
rval = PyList_New(0);
|
|
|
|
else
|
|
|
|
rval = PyDict_New();
|
2009-05-02 09:36:44 -03:00
|
|
|
if (rval == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* skip whitespace after { */
|
2011-09-28 02:41:54 -03:00
|
|
|
while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++;
|
2009-05-02 09:36:44 -03:00
|
|
|
|
|
|
|
/* only loop if the object is non-empty */
|
2013-01-03 02:44:15 -04:00
|
|
|
if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') {
|
|
|
|
while (1) {
|
2010-09-04 17:16:53 -03:00
|
|
|
PyObject *memokey;
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
/* read key */
|
2013-01-03 02:44:15 -04:00
|
|
|
if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '"') {
|
2012-06-28 20:58:26 -03:00
|
|
|
raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx);
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
|
|
|
|
if (key == NULL)
|
|
|
|
goto bail;
|
2010-09-04 17:16:53 -03:00
|
|
|
memokey = PyDict_GetItem(s->memo, key);
|
|
|
|
if (memokey != NULL) {
|
|
|
|
Py_INCREF(memokey);
|
|
|
|
Py_DECREF(key);
|
|
|
|
key = memokey;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (PyDict_SetItem(s->memo, key, key) < 0)
|
|
|
|
goto bail;
|
|
|
|
}
|
2009-05-02 09:36:44 -03:00
|
|
|
idx = next_idx;
|
|
|
|
|
|
|
|
/* skip whitespace between key and : delimiter, read :, skip whitespace */
|
2011-09-28 02:41:54 -03:00
|
|
|
while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
|
|
|
|
if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
|
2012-06-28 20:58:26 -03:00
|
|
|
raise_errmsg("Expecting ':' delimiter", pystr, idx);
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
idx++;
|
2011-09-28 02:41:54 -03:00
|
|
|
while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
|
2009-05-02 09:36:44 -03:00
|
|
|
|
|
|
|
/* read any JSON term */
|
|
|
|
val = scan_once_unicode(s, pystr, idx, &next_idx);
|
|
|
|
if (val == NULL)
|
|
|
|
goto bail;
|
|
|
|
|
2010-09-04 17:16:53 -03:00
|
|
|
if (has_pairs_hook) {
|
|
|
|
PyObject *item = PyTuple_Pack(2, key, val);
|
|
|
|
if (item == NULL)
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
2010-09-04 17:16:53 -03:00
|
|
|
Py_CLEAR(key);
|
|
|
|
Py_CLEAR(val);
|
|
|
|
if (PyList_Append(rval, item) == -1) {
|
|
|
|
Py_DECREF(item);
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
}
|
2010-09-04 17:16:53 -03:00
|
|
|
Py_DECREF(item);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (PyDict_SetItem(rval, key, val) < 0)
|
|
|
|
goto bail;
|
|
|
|
Py_CLEAR(key);
|
|
|
|
Py_CLEAR(val);
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
idx = next_idx;
|
|
|
|
|
|
|
|
/* skip whitespace before } or , */
|
2011-09-28 02:41:54 -03:00
|
|
|
while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
|
2009-05-02 09:36:44 -03:00
|
|
|
|
|
|
|
/* bail if the object is closed or we didn't get the , delimiter */
|
2013-01-03 02:44:15 -04:00
|
|
|
if (idx <= end_idx && PyUnicode_READ(kind, str, idx) == '}')
|
2009-05-02 09:36:44 -03:00
|
|
|
break;
|
2013-01-03 02:44:15 -04:00
|
|
|
if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ',') {
|
2012-06-28 20:58:26 -03:00
|
|
|
raise_errmsg("Expecting ',' delimiter", pystr, idx);
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
/* skip whitespace after , delimiter */
|
2011-09-28 02:41:54 -03:00
|
|
|
while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*next_idx_ptr = idx + 1;
|
|
|
|
|
2010-09-04 17:16:53 -03:00
|
|
|
if (has_pairs_hook) {
|
2009-05-02 09:36:44 -03:00
|
|
|
val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
|
|
|
|
Py_DECREF(rval);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if object_hook is not None: rval = object_hook(rval) */
|
|
|
|
if (s->object_hook != Py_None) {
|
|
|
|
val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
|
|
|
|
Py_DECREF(rval);
|
2010-09-04 17:16:53 -03:00
|
|
|
return val;
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
return rval;
|
|
|
|
bail:
|
|
|
|
Py_XDECREF(key);
|
|
|
|
Py_XDECREF(val);
|
2010-09-04 17:16:53 -03:00
|
|
|
Py_XDECREF(rval);
|
2009-05-02 09:36:44 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
|
2015-02-17 04:14:30 -04:00
|
|
|
/* Read a JSON array from PyUnicode pystr.
|
2009-05-02 09:36:44 -03:00
|
|
|
idx is the index of the first character after the opening brace.
|
|
|
|
*next_idx_ptr is a return-by-reference index to the first character after
|
|
|
|
the closing brace.
|
|
|
|
|
|
|
|
Returns a new PyList
|
|
|
|
*/
|
2011-09-28 02:41:54 -03:00
|
|
|
void *str;
|
|
|
|
int kind;
|
|
|
|
Py_ssize_t end_idx;
|
2009-05-02 09:36:44 -03:00
|
|
|
PyObject *val = NULL;
|
|
|
|
PyObject *rval = PyList_New(0);
|
|
|
|
Py_ssize_t next_idx;
|
|
|
|
if (rval == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READY(pystr) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
str = PyUnicode_DATA(pystr);
|
|
|
|
kind = PyUnicode_KIND(pystr);
|
|
|
|
end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
/* skip whitespace after [ */
|
2011-09-28 02:41:54 -03:00
|
|
|
while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
|
2009-05-02 09:36:44 -03:00
|
|
|
|
|
|
|
/* only loop if the array is non-empty */
|
2013-01-03 02:44:15 -04:00
|
|
|
if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
|
|
|
|
while (1) {
|
2009-05-02 09:36:44 -03:00
|
|
|
|
|
|
|
/* read any JSON term */
|
|
|
|
val = scan_once_unicode(s, pystr, idx, &next_idx);
|
|
|
|
if (val == NULL)
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
if (PyList_Append(rval, val) == -1)
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
Py_CLEAR(val);
|
|
|
|
idx = next_idx;
|
|
|
|
|
|
|
|
/* skip whitespace between term and , */
|
2011-09-28 02:41:54 -03:00
|
|
|
while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
|
2009-05-02 09:36:44 -03:00
|
|
|
|
|
|
|
/* bail if the array is closed or we didn't get the , delimiter */
|
2013-01-03 02:44:15 -04:00
|
|
|
if (idx <= end_idx && PyUnicode_READ(kind, str, idx) == ']')
|
2009-05-02 09:36:44 -03:00
|
|
|
break;
|
2013-01-03 02:44:15 -04:00
|
|
|
if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ',') {
|
2012-06-28 20:58:26 -03:00
|
|
|
raise_errmsg("Expecting ',' delimiter", pystr, idx);
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
/* skip whitespace after , */
|
2011-09-28 02:41:54 -03:00
|
|
|
while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
/* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */
|
|
|
|
if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
|
2013-01-03 02:44:15 -04:00
|
|
|
raise_errmsg("Expecting value", pystr, end_idx);
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
*next_idx_ptr = idx + 1;
|
|
|
|
return rval;
|
|
|
|
bail:
|
|
|
|
Py_XDECREF(val);
|
|
|
|
Py_DECREF(rval);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2015-02-17 04:14:30 -04:00
|
|
|
_parse_constant(PyScannerObject *s, const char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
|
|
|
|
/* Read a JSON constant.
|
2009-05-02 09:36:44 -03:00
|
|
|
constant is the constant string that was found
|
|
|
|
("NaN", "Infinity", "-Infinity").
|
|
|
|
idx is the index of the first character of the constant
|
|
|
|
*next_idx_ptr is a return-by-reference index to the first character after
|
|
|
|
the constant.
|
|
|
|
|
|
|
|
Returns the result of parse_constant
|
|
|
|
*/
|
|
|
|
PyObject *cstr;
|
|
|
|
PyObject *rval;
|
|
|
|
/* constant is "NaN", "Infinity", or "-Infinity" */
|
|
|
|
cstr = PyUnicode_InternFromString(constant);
|
|
|
|
if (cstr == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* rval = parse_constant(constant) */
|
|
|
|
rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
|
2011-10-11 17:11:42 -03:00
|
|
|
idx += PyUnicode_GET_LENGTH(cstr);
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_DECREF(cstr);
|
|
|
|
*next_idx_ptr = idx;
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
|
|
|
|
/* Read a JSON number from PyUnicode pystr.
|
|
|
|
idx is the index of the first character of the number
|
|
|
|
*next_idx_ptr is a return-by-reference index to the first character after
|
|
|
|
the number.
|
|
|
|
|
|
|
|
Returns a new PyObject representation of that number:
|
2015-02-17 04:14:30 -04:00
|
|
|
PyLong, or PyFloat.
|
2009-05-02 09:36:44 -03:00
|
|
|
May return other types if parse_int or parse_float are set
|
|
|
|
*/
|
2011-09-28 02:41:54 -03:00
|
|
|
void *str;
|
|
|
|
int kind;
|
|
|
|
Py_ssize_t end_idx;
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_ssize_t idx = start;
|
|
|
|
int is_float = 0;
|
|
|
|
PyObject *rval;
|
2011-04-25 14:16:06 -03:00
|
|
|
PyObject *numstr = NULL;
|
|
|
|
PyObject *custom_func;
|
2009-05-02 09:36:44 -03:00
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READY(pystr) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
str = PyUnicode_DATA(pystr);
|
|
|
|
kind = PyUnicode_KIND(pystr);
|
|
|
|
end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
/* read a sign if it's there, make sure it's not the end of the string */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READ(kind, str, idx) == '-') {
|
2009-05-02 09:36:44 -03:00
|
|
|
idx++;
|
|
|
|
if (idx > end_idx) {
|
2013-01-03 02:44:15 -04:00
|
|
|
raise_stop_iteration(start);
|
2009-05-02 09:36:44 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read as many integer digits as we find as long as it doesn't start with 0 */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') {
|
2009-05-02 09:36:44 -03:00
|
|
|
idx++;
|
2011-09-28 02:41:54 -03:00
|
|
|
while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
/* if it starts with 0 we only expect one integer digit */
|
2011-09-28 02:41:54 -03:00
|
|
|
else if (PyUnicode_READ(kind, str, idx) == '0') {
|
2009-05-02 09:36:44 -03:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
/* no integer digits, error */
|
|
|
|
else {
|
2013-01-03 02:44:15 -04:00
|
|
|
raise_stop_iteration(start);
|
2009-05-02 09:36:44 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the next char is '.' followed by a digit then read all float digits */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (idx < end_idx && PyUnicode_READ(kind, str, idx) == '.' && PyUnicode_READ(kind, str, idx + 1) >= '0' && PyUnicode_READ(kind, str, idx + 1) <= '9') {
|
2009-05-02 09:36:44 -03:00
|
|
|
is_float = 1;
|
|
|
|
idx += 2;
|
2011-09-28 02:41:54 -03:00
|
|
|
while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) {
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_ssize_t e_start = idx;
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
/* read an exponent sign if present */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++;
|
2009-05-02 09:36:44 -03:00
|
|
|
|
|
|
|
/* read all digits */
|
2011-09-28 02:41:54 -03:00
|
|
|
while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
|
2009-05-02 09:36:44 -03:00
|
|
|
|
|
|
|
/* if we got a digit, then parse as float. if not, backtrack */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') {
|
2009-05-02 09:36:44 -03:00
|
|
|
is_float = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
idx = e_start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-25 14:16:06 -03:00
|
|
|
if (is_float && s->parse_float != (PyObject *)&PyFloat_Type)
|
|
|
|
custom_func = s->parse_float;
|
|
|
|
else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type)
|
|
|
|
custom_func = s->parse_int;
|
|
|
|
else
|
|
|
|
custom_func = NULL;
|
|
|
|
|
|
|
|
if (custom_func) {
|
|
|
|
/* copy the section we determined to be a number */
|
2011-09-28 02:41:54 -03:00
|
|
|
numstr = PyUnicode_FromKindAndData(kind,
|
2011-10-07 15:55:35 -03:00
|
|
|
(char*)str + kind * start,
|
2011-09-28 02:41:54 -03:00
|
|
|
idx - start);
|
2011-04-25 14:16:06 -03:00
|
|
|
if (numstr == NULL)
|
|
|
|
return NULL;
|
|
|
|
rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
else {
|
2011-04-25 14:16:06 -03:00
|
|
|
Py_ssize_t i, n;
|
|
|
|
char *buf;
|
|
|
|
/* Straight conversion to ASCII, to avoid costly conversion of
|
|
|
|
decimal unicode digits (which cannot appear here) */
|
|
|
|
n = idx - start;
|
|
|
|
numstr = PyBytes_FromStringAndSize(NULL, n);
|
|
|
|
if (numstr == NULL)
|
|
|
|
return NULL;
|
|
|
|
buf = PyBytes_AS_STRING(numstr);
|
|
|
|
for (i = 0; i < n; i++) {
|
2011-09-28 02:41:54 -03:00
|
|
|
buf[i] = (char) PyUnicode_READ(kind, str, i + start);
|
2011-04-25 14:16:06 -03:00
|
|
|
}
|
|
|
|
if (is_float)
|
|
|
|
rval = PyFloat_FromString(numstr);
|
|
|
|
else
|
|
|
|
rval = PyLong_FromString(buf, NULL, 10);
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
Py_DECREF(numstr);
|
|
|
|
*next_idx_ptr = idx;
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
|
|
|
|
{
|
|
|
|
/* Read one JSON term (of any kind) from PyUnicode pystr.
|
|
|
|
idx is the index of the first character of the term
|
|
|
|
*next_idx_ptr is a return-by-reference index to the first character after
|
|
|
|
the number.
|
|
|
|
|
|
|
|
Returns a new PyObject representation of the term.
|
|
|
|
*/
|
2011-05-07 11:58:09 -03:00
|
|
|
PyObject *res;
|
2011-09-28 02:41:54 -03:00
|
|
|
void *str;
|
|
|
|
int kind;
|
|
|
|
Py_ssize_t length;
|
|
|
|
|
|
|
|
if (PyUnicode_READY(pystr) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
str = PyUnicode_DATA(pystr);
|
|
|
|
kind = PyUnicode_KIND(pystr);
|
|
|
|
length = PyUnicode_GET_LENGTH(pystr);
|
|
|
|
|
2014-04-14 12:45:21 -03:00
|
|
|
if (idx < 0) {
|
2014-04-14 12:46:51 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "idx cannot be negative");
|
2014-04-14 12:45:21 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (idx >= length) {
|
2013-01-03 02:44:15 -04:00
|
|
|
raise_stop_iteration(idx);
|
2009-05-02 09:36:44 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-28 02:41:54 -03:00
|
|
|
|
|
|
|
switch (PyUnicode_READ(kind, str, idx)) {
|
2009-05-02 09:36:44 -03:00
|
|
|
case '"':
|
|
|
|
/* string */
|
|
|
|
return scanstring_unicode(pystr, idx + 1,
|
|
|
|
PyObject_IsTrue(s->strict),
|
|
|
|
next_idx_ptr);
|
|
|
|
case '{':
|
|
|
|
/* object */
|
2011-05-07 11:58:09 -03:00
|
|
|
if (Py_EnterRecursiveCall(" while decoding a JSON object "
|
|
|
|
"from a unicode string"))
|
|
|
|
return NULL;
|
|
|
|
res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
|
|
|
|
Py_LeaveRecursiveCall();
|
|
|
|
return res;
|
2009-05-02 09:36:44 -03:00
|
|
|
case '[':
|
|
|
|
/* array */
|
2011-05-07 11:58:09 -03:00
|
|
|
if (Py_EnterRecursiveCall(" while decoding a JSON array "
|
|
|
|
"from a unicode string"))
|
|
|
|
return NULL;
|
|
|
|
res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
|
|
|
|
Py_LeaveRecursiveCall();
|
|
|
|
return res;
|
2009-05-02 09:36:44 -03:00
|
|
|
case 'n':
|
|
|
|
/* null */
|
2011-09-28 02:41:54 -03:00
|
|
|
if ((idx + 3 < length) && PyUnicode_READ(kind, str, idx + 1) == 'u' && PyUnicode_READ(kind, str, idx + 2) == 'l' && PyUnicode_READ(kind, str, idx + 3) == 'l') {
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
*next_idx_ptr = idx + 4;
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
/* true */
|
2011-09-28 02:41:54 -03:00
|
|
|
if ((idx + 3 < length) && PyUnicode_READ(kind, str, idx + 1) == 'r' && PyUnicode_READ(kind, str, idx + 2) == 'u' && PyUnicode_READ(kind, str, idx + 3) == 'e') {
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_INCREF(Py_True);
|
|
|
|
*next_idx_ptr = idx + 4;
|
|
|
|
return Py_True;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
/* false */
|
2011-10-11 16:56:19 -03:00
|
|
|
if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
|
|
|
|
PyUnicode_READ(kind, str, idx + 2) == 'l' &&
|
|
|
|
PyUnicode_READ(kind, str, idx + 3) == 's' &&
|
2011-09-28 02:41:54 -03:00
|
|
|
PyUnicode_READ(kind, str, idx + 4) == 'e') {
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_INCREF(Py_False);
|
|
|
|
*next_idx_ptr = idx + 5;
|
|
|
|
return Py_False;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
/* NaN */
|
2011-10-11 16:56:19 -03:00
|
|
|
if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
|
2011-09-28 02:41:54 -03:00
|
|
|
PyUnicode_READ(kind, str, idx + 2) == 'N') {
|
2009-05-02 09:36:44 -03:00
|
|
|
return _parse_constant(s, "NaN", idx, next_idx_ptr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
/* Infinity */
|
2011-10-11 16:56:19 -03:00
|
|
|
if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
|
|
|
|
PyUnicode_READ(kind, str, idx + 2) == 'f' &&
|
|
|
|
PyUnicode_READ(kind, str, idx + 3) == 'i' &&
|
2011-09-28 02:41:54 -03:00
|
|
|
PyUnicode_READ(kind, str, idx + 4) == 'n' &&
|
2011-10-11 16:56:19 -03:00
|
|
|
PyUnicode_READ(kind, str, idx + 5) == 'i' &&
|
|
|
|
PyUnicode_READ(kind, str, idx + 6) == 't' &&
|
2011-09-28 02:41:54 -03:00
|
|
|
PyUnicode_READ(kind, str, idx + 7) == 'y') {
|
2009-05-02 09:36:44 -03:00
|
|
|
return _parse_constant(s, "Infinity", idx, next_idx_ptr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
/* -Infinity */
|
2011-10-11 16:56:19 -03:00
|
|
|
if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
|
2011-09-28 02:41:54 -03:00
|
|
|
PyUnicode_READ(kind, str, idx + 2) == 'n' &&
|
|
|
|
PyUnicode_READ(kind, str, idx + 3) == 'f' &&
|
2011-10-11 16:56:19 -03:00
|
|
|
PyUnicode_READ(kind, str, idx + 4) == 'i' &&
|
2011-09-28 02:41:54 -03:00
|
|
|
PyUnicode_READ(kind, str, idx + 5) == 'n' &&
|
2011-10-11 16:56:19 -03:00
|
|
|
PyUnicode_READ(kind, str, idx + 6) == 'i' &&
|
|
|
|
PyUnicode_READ(kind, str, idx + 7) == 't' &&
|
2011-09-28 02:41:54 -03:00
|
|
|
PyUnicode_READ(kind, str, idx + 8) == 'y') {
|
2009-05-02 09:36:44 -03:00
|
|
|
return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Didn't find a string, object, array, or named constant. Look for a number. */
|
|
|
|
return _match_number_unicode(s, pystr, idx, next_idx_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
/* Python callable interface to scan_once_{str,unicode} */
|
|
|
|
PyObject *pystr;
|
|
|
|
PyObject *rval;
|
|
|
|
Py_ssize_t idx;
|
|
|
|
Py_ssize_t next_idx = -1;
|
|
|
|
static char *kwlist[] = {"string", "idx", NULL};
|
|
|
|
PyScannerObject *s;
|
|
|
|
assert(PyScanner_Check(self));
|
|
|
|
s = (PyScannerObject *)self;
|
2012-12-01 14:34:16 -04:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:scan_once", kwlist, &pystr, &idx))
|
2009-05-02 09:36:44 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (PyUnicode_Check(pystr)) {
|
|
|
|
rval = scan_once_unicode(s, pystr, idx, &next_idx);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"first argument must be a string, not %.80s",
|
|
|
|
Py_TYPE(pystr)->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-09-04 17:16:53 -03:00
|
|
|
PyDict_Clear(s->memo);
|
|
|
|
if (rval == NULL)
|
|
|
|
return NULL;
|
2009-05-02 09:36:44 -03:00
|
|
|
return _build_rval_index_tuple(rval, next_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
PyScannerObject *s;
|
|
|
|
s = (PyScannerObject *)type->tp_alloc(type, 0);
|
|
|
|
if (s != NULL) {
|
|
|
|
s->strict = NULL;
|
|
|
|
s->object_hook = NULL;
|
|
|
|
s->object_pairs_hook = NULL;
|
|
|
|
s->parse_float = NULL;
|
|
|
|
s->parse_int = NULL;
|
|
|
|
s->parse_constant = NULL;
|
|
|
|
}
|
|
|
|
return (PyObject *)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
/* Initialize Scanner object */
|
|
|
|
PyObject *ctx;
|
|
|
|
static char *kwlist[] = {"context", NULL};
|
|
|
|
PyScannerObject *s;
|
|
|
|
|
|
|
|
assert(PyScanner_Check(self));
|
|
|
|
s = (PyScannerObject *)self;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
|
|
|
|
return -1;
|
|
|
|
|
2010-09-04 17:16:53 -03:00
|
|
|
if (s->memo == NULL) {
|
|
|
|
s->memo = PyDict_New();
|
|
|
|
if (s->memo == NULL)
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
/* All of these will fail "gracefully" so we don't need to verify them */
|
|
|
|
s->strict = PyObject_GetAttrString(ctx, "strict");
|
|
|
|
if (s->strict == NULL)
|
|
|
|
goto bail;
|
|
|
|
s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
|
|
|
|
if (s->object_hook == NULL)
|
|
|
|
goto bail;
|
|
|
|
s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
|
|
|
|
if (s->object_pairs_hook == NULL)
|
|
|
|
goto bail;
|
|
|
|
s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
|
|
|
|
if (s->parse_float == NULL)
|
|
|
|
goto bail;
|
|
|
|
s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
|
|
|
|
if (s->parse_int == NULL)
|
|
|
|
goto bail;
|
|
|
|
s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
|
|
|
|
if (s->parse_constant == NULL)
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bail:
|
|
|
|
Py_CLEAR(s->strict);
|
|
|
|
Py_CLEAR(s->object_hook);
|
|
|
|
Py_CLEAR(s->object_pairs_hook);
|
|
|
|
Py_CLEAR(s->parse_float);
|
|
|
|
Py_CLEAR(s->parse_int);
|
|
|
|
Py_CLEAR(s->parse_constant);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(scanner_doc, "JSON scanner object");
|
|
|
|
|
|
|
|
static
|
|
|
|
PyTypeObject PyScannerType = {
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
"_json.Scanner", /* tp_name */
|
|
|
|
sizeof(PyScannerObject), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
scanner_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
scanner_call, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
|
|
|
|
0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
|
|
|
scanner_doc, /* tp_doc */
|
|
|
|
scanner_traverse, /* tp_traverse */
|
|
|
|
scanner_clear, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
scanner_members, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
scanner_init, /* tp_init */
|
|
|
|
0,/* PyType_GenericAlloc, */ /* tp_alloc */
|
|
|
|
scanner_new, /* tp_new */
|
|
|
|
0,/* PyObject_GC_Del, */ /* tp_free */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
PyEncoderObject *s;
|
|
|
|
s = (PyEncoderObject *)type->tp_alloc(type, 0);
|
|
|
|
if (s != NULL) {
|
|
|
|
s->markers = NULL;
|
|
|
|
s->defaultfn = NULL;
|
|
|
|
s->encoder = NULL;
|
|
|
|
s->indent = NULL;
|
|
|
|
s->key_separator = NULL;
|
|
|
|
s->item_separator = NULL;
|
|
|
|
s->sort_keys = NULL;
|
|
|
|
s->skipkeys = NULL;
|
|
|
|
}
|
|
|
|
return (PyObject *)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
/* initialize Encoder object */
|
|
|
|
static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
|
|
|
|
|
|
|
|
PyEncoderObject *s;
|
2009-12-08 11:57:31 -04:00
|
|
|
PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
|
|
|
|
PyObject *item_separator, *sort_keys, *skipkeys, *allow_nan;
|
2009-05-02 09:36:44 -03:00
|
|
|
|
|
|
|
assert(PyEncoder_Check(self));
|
|
|
|
s = (PyEncoderObject *)self;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO:make_encoder", kwlist,
|
2009-12-08 11:57:31 -04:00
|
|
|
&markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
|
|
|
|
&sort_keys, &skipkeys, &allow_nan))
|
2009-05-02 09:36:44 -03:00
|
|
|
return -1;
|
|
|
|
|
2009-12-08 11:57:31 -04:00
|
|
|
s->markers = markers;
|
|
|
|
s->defaultfn = defaultfn;
|
|
|
|
s->encoder = encoder;
|
|
|
|
s->indent = indent;
|
|
|
|
s->key_separator = key_separator;
|
|
|
|
s->item_separator = item_separator;
|
|
|
|
s->sort_keys = sort_keys;
|
|
|
|
s->skipkeys = skipkeys;
|
2015-01-11 11:41:01 -04:00
|
|
|
s->fast_encode = NULL;
|
|
|
|
if (PyCFunction_Check(s->encoder)) {
|
|
|
|
PyCFunction f = PyCFunction_GetFunction(s->encoder);
|
|
|
|
if (f == (PyCFunction)py_encode_basestring_ascii ||
|
|
|
|
f == (PyCFunction)py_encode_basestring) {
|
|
|
|
s->fast_encode = f;
|
|
|
|
}
|
|
|
|
}
|
2009-12-08 11:57:31 -04:00
|
|
|
s->allow_nan = PyObject_IsTrue(allow_nan);
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_INCREF(s->markers);
|
|
|
|
Py_INCREF(s->defaultfn);
|
|
|
|
Py_INCREF(s->encoder);
|
|
|
|
Py_INCREF(s->indent);
|
|
|
|
Py_INCREF(s->key_separator);
|
|
|
|
Py_INCREF(s->item_separator);
|
|
|
|
Py_INCREF(s->sort_keys);
|
|
|
|
Py_INCREF(s->skipkeys);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
/* Python callable interface to encode_listencode_obj */
|
|
|
|
static char *kwlist[] = {"obj", "_current_indent_level", NULL};
|
|
|
|
PyObject *obj;
|
|
|
|
Py_ssize_t indent_level;
|
|
|
|
PyEncoderObject *s;
|
2011-10-06 14:09:51 -03:00
|
|
|
_PyAccu acc;
|
2011-08-19 13:03:14 -03:00
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
assert(PyEncoder_Check(self));
|
|
|
|
s = (PyEncoderObject *)self;
|
2012-12-01 14:34:16 -04:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:_iterencode", kwlist,
|
|
|
|
&obj, &indent_level))
|
2009-05-02 09:36:44 -03:00
|
|
|
return NULL;
|
2011-10-06 14:09:51 -03:00
|
|
|
if (_PyAccu_Init(&acc))
|
2009-05-02 09:36:44 -03:00
|
|
|
return NULL;
|
2011-08-19 13:03:14 -03:00
|
|
|
if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
|
2011-10-06 14:09:51 -03:00
|
|
|
_PyAccu_Destroy(&acc);
|
2009-05-02 09:36:44 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-06 14:09:51 -03:00
|
|
|
return _PyAccu_FinishAsList(&acc);
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_encoded_const(PyObject *obj)
|
|
|
|
{
|
|
|
|
/* Return the JSON string representation of None, True, False */
|
|
|
|
if (obj == Py_None) {
|
|
|
|
static PyObject *s_null = NULL;
|
|
|
|
if (s_null == NULL) {
|
|
|
|
s_null = PyUnicode_InternFromString("null");
|
|
|
|
}
|
|
|
|
Py_INCREF(s_null);
|
|
|
|
return s_null;
|
|
|
|
}
|
|
|
|
else if (obj == Py_True) {
|
|
|
|
static PyObject *s_true = NULL;
|
|
|
|
if (s_true == NULL) {
|
|
|
|
s_true = PyUnicode_InternFromString("true");
|
|
|
|
}
|
|
|
|
Py_INCREF(s_true);
|
|
|
|
return s_true;
|
|
|
|
}
|
|
|
|
else if (obj == Py_False) {
|
|
|
|
static PyObject *s_false = NULL;
|
|
|
|
if (s_false == NULL) {
|
|
|
|
s_false = PyUnicode_InternFromString("false");
|
|
|
|
}
|
|
|
|
Py_INCREF(s_false);
|
|
|
|
return s_false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "not a const");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-10 17:01:45 -03:00
|
|
|
static PyObject *
|
|
|
|
encoder_encode_long(PyEncoderObject* s UNUSED, PyObject *obj)
|
|
|
|
{
|
|
|
|
/* Return the JSON representation of a PyLong and PyLong subclasses.
|
|
|
|
Calls int() on PyLong subclasses in case the str() was changed.
|
|
|
|
Added specifically to deal with IntEnum. See Issue18264. */
|
|
|
|
PyObject *encoded, *longobj;
|
|
|
|
if (PyLong_CheckExact(obj)) {
|
|
|
|
encoded = PyObject_Str(obj);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
longobj = PyNumber_Long(obj);
|
|
|
|
if (longobj == NULL) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_ValueError,
|
|
|
|
"Unable to coerce int subclass to int"
|
|
|
|
);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
encoded = PyObject_Str(longobj);
|
|
|
|
Py_DECREF(longobj);
|
|
|
|
}
|
|
|
|
return encoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
static PyObject *
|
|
|
|
encoder_encode_float(PyEncoderObject *s, PyObject *obj)
|
|
|
|
{
|
2013-08-10 17:01:45 -03:00
|
|
|
/* Return the JSON representation of a PyFloat.
|
|
|
|
Modified to call float() on float subclasses in case the subclass
|
|
|
|
changes the repr. See Issue18264. */
|
|
|
|
PyObject *encoded, *floatobj;
|
2009-05-02 09:36:44 -03:00
|
|
|
double i = PyFloat_AS_DOUBLE(obj);
|
|
|
|
if (!Py_IS_FINITE(i)) {
|
|
|
|
if (!s->allow_nan) {
|
2013-08-10 17:01:45 -03:00
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_ValueError,
|
|
|
|
"Out of range float values are not JSON compliant"
|
|
|
|
);
|
2009-05-02 09:36:44 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (i > 0) {
|
|
|
|
return PyUnicode_FromString("Infinity");
|
|
|
|
}
|
|
|
|
else if (i < 0) {
|
|
|
|
return PyUnicode_FromString("-Infinity");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return PyUnicode_FromString("NaN");
|
|
|
|
}
|
|
|
|
}
|
2013-08-10 17:01:45 -03:00
|
|
|
/* coerce float subclasses to float (primarily for Enum) */
|
|
|
|
if (PyFloat_CheckExact(obj)) {
|
|
|
|
/* Use a better float format here? */
|
|
|
|
encoded = PyObject_Repr(obj);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
floatobj = PyNumber_Float(obj);
|
|
|
|
if (floatobj == NULL) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_ValueError,
|
|
|
|
"Unable to coerce float subclass to float"
|
|
|
|
);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
encoded = PyObject_Repr(floatobj);
|
|
|
|
Py_DECREF(floatobj);
|
|
|
|
}
|
|
|
|
return encoded;
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
encoder_encode_string(PyEncoderObject *s, PyObject *obj)
|
|
|
|
{
|
|
|
|
/* Return the JSON representation of a string */
|
|
|
|
if (s->fast_encode)
|
2015-01-11 11:41:01 -04:00
|
|
|
return s->fast_encode(NULL, obj);
|
2009-05-02 09:36:44 -03:00
|
|
|
else
|
|
|
|
return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-10-06 14:09:51 -03:00
|
|
|
_steal_accumulate(_PyAccu *acc, PyObject *stolen)
|
2009-05-02 09:36:44 -03:00
|
|
|
{
|
|
|
|
/* Append stolen and then decrement its reference count */
|
2011-10-06 14:09:51 -03:00
|
|
|
int rval = _PyAccu_Accumulate(acc, stolen);
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_DECREF(stolen);
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-10-06 14:09:51 -03:00
|
|
|
encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
|
2011-08-19 13:03:14 -03:00
|
|
|
PyObject *obj, Py_ssize_t indent_level)
|
2009-05-02 09:36:44 -03:00
|
|
|
{
|
2011-08-19 13:03:14 -03:00
|
|
|
/* Encode Python object obj to a JSON term */
|
2009-05-02 09:36:44 -03:00
|
|
|
PyObject *newobj;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (obj == Py_None || obj == Py_True || obj == Py_False) {
|
|
|
|
PyObject *cstr = _encoded_const(obj);
|
|
|
|
if (cstr == NULL)
|
|
|
|
return -1;
|
2011-08-19 13:03:14 -03:00
|
|
|
return _steal_accumulate(acc, cstr);
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
else if (PyUnicode_Check(obj))
|
|
|
|
{
|
|
|
|
PyObject *encoded = encoder_encode_string(s, obj);
|
|
|
|
if (encoded == NULL)
|
|
|
|
return -1;
|
2011-08-19 13:03:14 -03:00
|
|
|
return _steal_accumulate(acc, encoded);
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
else if (PyLong_Check(obj)) {
|
2013-08-10 17:01:45 -03:00
|
|
|
PyObject *encoded = encoder_encode_long(s, obj);
|
2009-05-02 09:36:44 -03:00
|
|
|
if (encoded == NULL)
|
|
|
|
return -1;
|
2011-08-19 13:03:14 -03:00
|
|
|
return _steal_accumulate(acc, encoded);
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
else if (PyFloat_Check(obj)) {
|
|
|
|
PyObject *encoded = encoder_encode_float(s, obj);
|
|
|
|
if (encoded == NULL)
|
|
|
|
return -1;
|
2011-08-19 13:03:14 -03:00
|
|
|
return _steal_accumulate(acc, encoded);
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
else if (PyList_Check(obj) || PyTuple_Check(obj)) {
|
2011-05-10 19:02:56 -03:00
|
|
|
if (Py_EnterRecursiveCall(" while encoding a JSON object"))
|
|
|
|
return -1;
|
2011-08-19 13:03:14 -03:00
|
|
|
rv = encoder_listencode_list(s, acc, obj, indent_level);
|
2011-05-10 19:02:56 -03:00
|
|
|
Py_LeaveRecursiveCall();
|
|
|
|
return rv;
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
else if (PyDict_Check(obj)) {
|
2011-05-10 19:02:56 -03:00
|
|
|
if (Py_EnterRecursiveCall(" while encoding a JSON object"))
|
|
|
|
return -1;
|
2011-08-19 13:03:14 -03:00
|
|
|
rv = encoder_listencode_dict(s, acc, obj, indent_level);
|
2011-05-10 19:02:56 -03:00
|
|
|
Py_LeaveRecursiveCall();
|
|
|
|
return rv;
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyObject *ident = NULL;
|
|
|
|
if (s->markers != Py_None) {
|
|
|
|
int has_key;
|
|
|
|
ident = PyLong_FromVoidPtr(obj);
|
|
|
|
if (ident == NULL)
|
|
|
|
return -1;
|
|
|
|
has_key = PyDict_Contains(s->markers, ident);
|
|
|
|
if (has_key) {
|
|
|
|
if (has_key != -1)
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Circular reference detected");
|
|
|
|
Py_DECREF(ident);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PyDict_SetItem(s->markers, ident, obj)) {
|
|
|
|
Py_DECREF(ident);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
|
|
|
|
if (newobj == NULL) {
|
|
|
|
Py_XDECREF(ident);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-05-10 19:02:56 -03:00
|
|
|
|
|
|
|
if (Py_EnterRecursiveCall(" while encoding a JSON object"))
|
|
|
|
return -1;
|
2011-08-19 13:03:14 -03:00
|
|
|
rv = encoder_listencode_obj(s, acc, newobj, indent_level);
|
2011-05-10 19:02:56 -03:00
|
|
|
Py_LeaveRecursiveCall();
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_DECREF(newobj);
|
|
|
|
if (rv) {
|
|
|
|
Py_XDECREF(ident);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ident != NULL) {
|
|
|
|
if (PyDict_DelItem(s->markers, ident)) {
|
|
|
|
Py_XDECREF(ident);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_XDECREF(ident);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-10-06 14:09:51 -03:00
|
|
|
encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
|
2011-08-19 13:03:14 -03:00
|
|
|
PyObject *dct, Py_ssize_t indent_level)
|
2009-05-02 09:36:44 -03:00
|
|
|
{
|
2011-08-19 13:03:14 -03:00
|
|
|
/* Encode Python dict dct a JSON term */
|
2009-05-02 09:36:44 -03:00
|
|
|
static PyObject *open_dict = NULL;
|
|
|
|
static PyObject *close_dict = NULL;
|
|
|
|
static PyObject *empty_dict = NULL;
|
|
|
|
PyObject *kstr = NULL;
|
|
|
|
PyObject *ident = NULL;
|
2009-05-27 03:50:31 -03:00
|
|
|
PyObject *it = NULL;
|
2009-05-27 06:58:34 -03:00
|
|
|
PyObject *items;
|
|
|
|
PyObject *item = NULL;
|
2009-05-02 09:36:44 -03:00
|
|
|
int skipkeys;
|
|
|
|
Py_ssize_t idx;
|
|
|
|
|
|
|
|
if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
|
|
|
|
open_dict = PyUnicode_InternFromString("{");
|
|
|
|
close_dict = PyUnicode_InternFromString("}");
|
|
|
|
empty_dict = PyUnicode_InternFromString("{}");
|
|
|
|
if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
2009-05-27 03:50:31 -03:00
|
|
|
if (Py_SIZE(dct) == 0)
|
2011-10-06 14:09:51 -03:00
|
|
|
return _PyAccu_Accumulate(acc, empty_dict);
|
2009-05-02 09:36:44 -03:00
|
|
|
|
|
|
|
if (s->markers != Py_None) {
|
|
|
|
int has_key;
|
|
|
|
ident = PyLong_FromVoidPtr(dct);
|
|
|
|
if (ident == NULL)
|
|
|
|
goto bail;
|
|
|
|
has_key = PyDict_Contains(s->markers, ident);
|
|
|
|
if (has_key) {
|
|
|
|
if (has_key != -1)
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Circular reference detected");
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (PyDict_SetItem(s->markers, ident, dct)) {
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-06 14:09:51 -03:00
|
|
|
if (_PyAccu_Accumulate(acc, open_dict))
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
|
|
|
|
if (s->indent != Py_None) {
|
|
|
|
/* TODO: DOES NOT RUN */
|
|
|
|
indent_level += 1;
|
|
|
|
/*
|
|
|
|
newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
|
|
|
|
separator = _item_separator + newline_indent
|
|
|
|
buf += newline_indent
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2015-05-02 23:28:04 -03:00
|
|
|
items = PyMapping_Items(dct);
|
2010-11-04 13:51:32 -03:00
|
|
|
if (items == NULL)
|
2009-05-27 08:19:02 -03:00
|
|
|
goto bail;
|
2015-05-02 23:28:04 -03:00
|
|
|
if (PyObject_IsTrue(s->sort_keys) && PyList_Sort(items) < 0)
|
|
|
|
goto bail;
|
2009-05-27 06:58:34 -03:00
|
|
|
it = PyObject_GetIter(items);
|
2010-11-04 13:51:32 -03:00
|
|
|
Py_DECREF(items);
|
|
|
|
if (it == NULL)
|
2009-05-27 03:50:31 -03:00
|
|
|
goto bail;
|
2009-05-02 09:36:44 -03:00
|
|
|
skipkeys = PyObject_IsTrue(s->skipkeys);
|
|
|
|
idx = 0;
|
2009-05-27 06:58:34 -03:00
|
|
|
while ((item = PyIter_Next(it)) != NULL) {
|
|
|
|
PyObject *encoded, *key, *value;
|
|
|
|
if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
key = PyTuple_GET_ITEM(item, 0);
|
2009-05-02 09:36:44 -03:00
|
|
|
if (PyUnicode_Check(key)) {
|
|
|
|
Py_INCREF(key);
|
|
|
|
kstr = key;
|
|
|
|
}
|
|
|
|
else if (PyFloat_Check(key)) {
|
|
|
|
kstr = encoder_encode_float(s, key);
|
|
|
|
if (kstr == NULL)
|
|
|
|
goto bail;
|
|
|
|
}
|
2009-05-27 06:58:34 -03:00
|
|
|
else if (key == Py_True || key == Py_False || key == Py_None) {
|
2010-05-09 12:52:27 -03:00
|
|
|
/* This must come before the PyLong_Check because
|
|
|
|
True and False are also 1 and 0.*/
|
2009-05-27 06:58:34 -03:00
|
|
|
kstr = _encoded_const(key);
|
2009-05-02 09:36:44 -03:00
|
|
|
if (kstr == NULL)
|
|
|
|
goto bail;
|
|
|
|
}
|
2009-05-27 06:58:34 -03:00
|
|
|
else if (PyLong_Check(key)) {
|
2013-08-10 17:01:45 -03:00
|
|
|
kstr = encoder_encode_long(s, key);
|
|
|
|
if (kstr == NULL) {
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
2013-08-10 17:01:45 -03:00
|
|
|
}
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
else if (skipkeys) {
|
2009-05-27 06:58:34 -03:00
|
|
|
Py_DECREF(item);
|
2009-05-02 09:36:44 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* TODO: include repr of key */
|
2010-07-21 09:29:04 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "keys must be a string");
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx) {
|
2011-10-06 14:09:51 -03:00
|
|
|
if (_PyAccu_Accumulate(acc, s->item_separator))
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
encoded = encoder_encode_string(s, kstr);
|
|
|
|
Py_CLEAR(kstr);
|
|
|
|
if (encoded == NULL)
|
|
|
|
goto bail;
|
2011-10-06 14:09:51 -03:00
|
|
|
if (_PyAccu_Accumulate(acc, encoded)) {
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_DECREF(encoded);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
Py_DECREF(encoded);
|
2011-10-06 14:09:51 -03:00
|
|
|
if (_PyAccu_Accumulate(acc, s->key_separator))
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
2009-05-27 03:50:31 -03:00
|
|
|
|
2009-05-27 06:58:34 -03:00
|
|
|
value = PyTuple_GET_ITEM(item, 1);
|
2011-08-19 13:03:14 -03:00
|
|
|
if (encoder_listencode_obj(s, acc, value, indent_level))
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
idx += 1;
|
2009-05-27 06:58:34 -03:00
|
|
|
Py_DECREF(item);
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
2009-05-27 03:50:31 -03:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
goto bail;
|
|
|
|
Py_CLEAR(it);
|
|
|
|
|
2009-05-02 09:36:44 -03:00
|
|
|
if (ident != NULL) {
|
|
|
|
if (PyDict_DelItem(s->markers, ident))
|
|
|
|
goto bail;
|
|
|
|
Py_CLEAR(ident);
|
|
|
|
}
|
2011-02-22 16:15:44 -04:00
|
|
|
/* TODO DOES NOT RUN; dead code
|
2009-05-02 09:36:44 -03:00
|
|
|
if (s->indent != Py_None) {
|
|
|
|
indent_level -= 1;
|
2011-02-22 16:15:44 -04:00
|
|
|
|
|
|
|
yield '\n' + (' ' * (_indent * _current_indent_level))
|
|
|
|
}*/
|
2011-10-06 14:09:51 -03:00
|
|
|
if (_PyAccu_Accumulate(acc, close_dict))
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bail:
|
2009-05-27 03:50:31 -03:00
|
|
|
Py_XDECREF(it);
|
2009-05-27 06:58:34 -03:00
|
|
|
Py_XDECREF(item);
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_XDECREF(kstr);
|
|
|
|
Py_XDECREF(ident);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2011-10-06 14:09:51 -03:00
|
|
|
encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
|
2011-08-19 13:03:14 -03:00
|
|
|
PyObject *seq, Py_ssize_t indent_level)
|
2009-05-02 09:36:44 -03:00
|
|
|
{
|
2011-08-19 13:03:14 -03:00
|
|
|
/* Encode Python list seq to a JSON term */
|
2009-05-02 09:36:44 -03:00
|
|
|
static PyObject *open_array = NULL;
|
|
|
|
static PyObject *close_array = NULL;
|
|
|
|
static PyObject *empty_array = NULL;
|
|
|
|
PyObject *ident = NULL;
|
|
|
|
PyObject *s_fast = NULL;
|
|
|
|
Py_ssize_t i;
|
|
|
|
|
|
|
|
if (open_array == NULL || close_array == NULL || empty_array == NULL) {
|
|
|
|
open_array = PyUnicode_InternFromString("[");
|
|
|
|
close_array = PyUnicode_InternFromString("]");
|
|
|
|
empty_array = PyUnicode_InternFromString("[]");
|
|
|
|
if (open_array == NULL || close_array == NULL || empty_array == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ident = NULL;
|
|
|
|
s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
|
|
|
|
if (s_fast == NULL)
|
|
|
|
return -1;
|
2012-11-01 15:52:06 -03:00
|
|
|
if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
|
2009-05-02 09:36:44 -03:00
|
|
|
Py_DECREF(s_fast);
|
2011-10-06 14:09:51 -03:00
|
|
|
return _PyAccu_Accumulate(acc, empty_array);
|
2009-05-02 09:36:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->markers != Py_None) {
|
|
|
|
int has_key;
|
|
|
|
ident = PyLong_FromVoidPtr(seq);
|
|
|
|
if (ident == NULL)
|
|
|
|
goto bail;
|
|
|
|
has_key = PyDict_Contains(s->markers, ident);
|
|
|
|
if (has_key) {
|
|
|
|
if (has_key != -1)
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Circular reference detected");
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (PyDict_SetItem(s->markers, ident, seq)) {
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-06 14:09:51 -03:00
|
|
|
if (_PyAccu_Accumulate(acc, open_array))
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
if (s->indent != Py_None) {
|
|
|
|
/* TODO: DOES NOT RUN */
|
|
|
|
indent_level += 1;
|
|
|
|
/*
|
|
|
|
newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
|
|
|
|
separator = _item_separator + newline_indent
|
|
|
|
buf += newline_indent
|
|
|
|
*/
|
|
|
|
}
|
2012-11-01 15:52:06 -03:00
|
|
|
for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
|
|
|
|
PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
|
2009-05-02 09:36:44 -03:00
|
|
|
if (i) {
|
2011-10-06 14:09:51 -03:00
|
|
|
if (_PyAccu_Accumulate(acc, s->item_separator))
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
}
|
2011-08-19 13:03:14 -03:00
|
|
|
if (encoder_listencode_obj(s, acc, obj, indent_level))
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (ident != NULL) {
|
|
|
|
if (PyDict_DelItem(s->markers, ident))
|
|
|
|
goto bail;
|
|
|
|
Py_CLEAR(ident);
|
|
|
|
}
|
2011-02-22 16:15:44 -04:00
|
|
|
|
|
|
|
/* TODO: DOES NOT RUN
|
2009-05-02 09:36:44 -03:00
|
|
|
if (s->indent != Py_None) {
|
|
|
|
indent_level -= 1;
|
2011-02-22 16:15:44 -04:00
|
|
|
|
|
|
|
yield '\n' + (' ' * (_indent * _current_indent_level))
|
|
|
|
}*/
|
2011-10-06 14:09:51 -03:00
|
|
|
if (_PyAccu_Accumulate(acc, close_array))
|
2009-05-02 09:36:44 -03:00
|
|
|
goto bail;
|
|
|
|
Py_DECREF(s_fast);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bail:
|
|
|
|
Py_XDECREF(ident);
|
|
|
|
Py_DECREF(s_fast);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
encoder_dealloc(PyObject *self)
|
|
|
|
{
|
|
|
|
/* Deallocate Encoder */
|
|
|
|
encoder_clear(self);
|
|
|
|
Py_TYPE(self)->tp_free(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
encoder_traverse(PyObject *self, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
PyEncoderObject *s;
|
|
|
|
assert(PyEncoder_Check(self));
|
|
|
|
s = (PyEncoderObject *)self;
|
|
|
|
Py_VISIT(s->markers);
|
|
|
|
Py_VISIT(s->defaultfn);
|
|
|
|
Py_VISIT(s->encoder);
|
|
|
|
Py_VISIT(s->indent);
|
|
|
|
Py_VISIT(s->key_separator);
|
|
|
|
Py_VISIT(s->item_separator);
|
|
|
|
Py_VISIT(s->sort_keys);
|
|
|
|
Py_VISIT(s->skipkeys);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
encoder_clear(PyObject *self)
|
|
|
|
{
|
|
|
|
/* Deallocate Encoder */
|
|
|
|
PyEncoderObject *s;
|
|
|
|
assert(PyEncoder_Check(self));
|
|
|
|
s = (PyEncoderObject *)self;
|
|
|
|
Py_CLEAR(s->markers);
|
|
|
|
Py_CLEAR(s->defaultfn);
|
|
|
|
Py_CLEAR(s->encoder);
|
|
|
|
Py_CLEAR(s->indent);
|
|
|
|
Py_CLEAR(s->key_separator);
|
|
|
|
Py_CLEAR(s->item_separator);
|
|
|
|
Py_CLEAR(s->sort_keys);
|
|
|
|
Py_CLEAR(s->skipkeys);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
|
|
|
|
|
|
|
|
static
|
|
|
|
PyTypeObject PyEncoderType = {
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
"_json.Encoder", /* tp_name */
|
|
|
|
sizeof(PyEncoderObject), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
encoder_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
encoder_call, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
|
|
|
encoder_doc, /* tp_doc */
|
|
|
|
encoder_traverse, /* tp_traverse */
|
|
|
|
encoder_clear, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
encoder_members, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
encoder_init, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
encoder_new, /* tp_new */
|
|
|
|
0, /* tp_free */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyMethodDef speedups_methods[] = {
|
|
|
|
{"encode_basestring_ascii",
|
|
|
|
(PyCFunction)py_encode_basestring_ascii,
|
|
|
|
METH_O,
|
|
|
|
pydoc_encode_basestring_ascii},
|
2015-01-11 11:41:01 -04:00
|
|
|
{"encode_basestring",
|
|
|
|
(PyCFunction)py_encode_basestring,
|
|
|
|
METH_O,
|
|
|
|
pydoc_encode_basestring},
|
2009-05-02 09:36:44 -03:00
|
|
|
{"scanstring",
|
|
|
|
(PyCFunction)py_scanstring,
|
|
|
|
METH_VARARGS,
|
|
|
|
pydoc_scanstring},
|
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(module_doc,
|
|
|
|
"json speedups\n");
|
|
|
|
|
|
|
|
static struct PyModuleDef jsonmodule = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"_json",
|
|
|
|
module_doc,
|
|
|
|
-1,
|
|
|
|
speedups_methods,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
2009-05-02 09:36:44 -03:00
|
|
|
};
|
|
|
|
|
2015-03-17 13:48:27 -03:00
|
|
|
PyMODINIT_FUNC
|
2009-05-02 09:36:44 -03:00
|
|
|
PyInit__json(void)
|
|
|
|
{
|
|
|
|
PyObject *m = PyModule_Create(&jsonmodule);
|
|
|
|
if (!m)
|
|
|
|
return NULL;
|
|
|
|
PyScannerType.tp_new = PyType_GenericNew;
|
|
|
|
if (PyType_Ready(&PyScannerType) < 0)
|
|
|
|
goto fail;
|
|
|
|
PyEncoderType.tp_new = PyType_GenericNew;
|
|
|
|
if (PyType_Ready(&PyEncoderType) < 0)
|
|
|
|
goto fail;
|
|
|
|
Py_INCREF((PyObject*)&PyScannerType);
|
|
|
|
if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
|
|
|
|
Py_DECREF((PyObject*)&PyScannerType);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
Py_INCREF((PyObject*)&PyEncoderType);
|
|
|
|
if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
|
|
|
|
Py_DECREF((PyObject*)&PyEncoderType);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
fail:
|
|
|
|
Py_DECREF(m);
|
|
|
|
return NULL;
|
2008-05-08 11:29:10 -03:00
|
|
|
}
|