Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 43 additions & 62 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1522,52 +1522,46 @@ encoder_listencode_obj(PyEncoderObject *s, PyUnicodeWriter *writer,
return rv;
}
else {
PyObject *ident = NULL;
int rc;

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)
rc = Py_ReprEnter(obj);
if (rc != 0) {
if (rc > 0) {
PyErr_SetString(PyExc_ValueError, "Circular reference detected");
Py_DECREF(ident);
return -1;
}
if (PyDict_SetItem(s->markers, ident, obj)) {
Py_DECREF(ident);
return -1;
}
goto bail;
}
}

newobj = PyObject_CallOneArg(s->defaultfn, obj);
if (newobj == NULL) {
Py_XDECREF(ident);
return -1;
goto bail;
}

if (_Py_EnterRecursiveCall(" while encoding a JSON object")) {
Py_DECREF(newobj);
Py_XDECREF(ident);
return -1;
goto bail;
}
rv = encoder_listencode_obj(s, writer, newobj, indent_level, indent_cache);
_Py_LeaveRecursiveCall();

Py_DECREF(newobj);
if (rv) {
_PyErr_FormatNote("when serializing %T object", obj);
Py_XDECREF(ident);
return -1;
Py_DECREF(newobj);
goto bail;
}
if (ident != NULL) {
if (PyDict_DelItem(s->markers, ident)) {
Py_XDECREF(ident);
return -1;
}
Py_XDECREF(ident);

if (s->markers != Py_None) {
Py_ReprLeave(obj);
}
Py_DECREF(newobj);
return rv;
bail:
if (s->markers != Py_None) {
Py_ReprLeave(obj);
}
return -1;
}
}

Expand Down Expand Up @@ -1649,28 +1643,22 @@ encoder_listencode_dict(PyEncoderObject *s, PyUnicodeWriter *writer,
Py_ssize_t indent_level, PyObject *indent_cache)
{
/* Encode Python dict dct a JSON term */
PyObject *ident = NULL;
PyObject *items = NULL;
PyObject *key, *value;
bool first = true;
int rc;

if (PyDict_GET_SIZE(dct) == 0) {
/* Fast path */
return PyUnicodeWriter_WriteASCII(writer, "{}", 2);
}

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)
rc = Py_ReprEnter(dct);
if (rc != 0) {
if (rc > 0) {
PyErr_SetString(PyExc_ValueError, "Circular reference detected");
goto bail;
}
if (PyDict_SetItem(s->markers, ident, dct)) {
}
goto bail;
}
}
Expand Down Expand Up @@ -1719,11 +1707,6 @@ encoder_listencode_dict(PyEncoderObject *s, PyUnicodeWriter *writer,
}
}

if (ident != NULL) {
if (PyDict_DelItem(s->markers, ident))
goto bail;
Py_CLEAR(ident);
}
if (s->indent != Py_None && !first) {
indent_level--;
if (write_newline_indent(writer, indent_level, indent_cache) < 0) {
Expand All @@ -1734,11 +1717,16 @@ encoder_listencode_dict(PyEncoderObject *s, PyUnicodeWriter *writer,
if (PyUnicodeWriter_WriteChar(writer, '}')) {
goto bail;
}
if (s->markers != Py_None) {
Py_ReprLeave(dct);
}
return 0;

bail:
if (s->markers != Py_None) {
Py_ReprLeave(dct);
}
Py_XDECREF(items);
Py_XDECREF(ident);
return -1;
}

Expand All @@ -1747,11 +1735,10 @@ encoder_listencode_list(PyEncoderObject *s, PyUnicodeWriter *writer,
PyObject *seq,
Py_ssize_t indent_level, PyObject *indent_cache)
{
PyObject *ident = NULL;
PyObject *s_fast = NULL;
Py_ssize_t i;
int rc;

ident = NULL;
s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
if (s_fast == NULL)
return -1;
Expand All @@ -1761,17 +1748,11 @@ encoder_listencode_list(PyEncoderObject *s, PyUnicodeWriter *writer,
}

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)
rc = Py_ReprEnter(seq);
if (rc != 0) {
if (rc > 0) {
PyErr_SetString(PyExc_ValueError, "Circular reference detected");
goto bail;
}
if (PyDict_SetItem(s->markers, ident, seq)) {
}
goto bail;
}
}
Expand Down Expand Up @@ -1801,11 +1782,6 @@ encoder_listencode_list(PyEncoderObject *s, PyUnicodeWriter *writer,
goto bail;
}
}
if (ident != NULL) {
if (PyDict_DelItem(s->markers, ident))
goto bail;
Py_CLEAR(ident);
}

if (s->indent != Py_None) {
indent_level--;
Expand All @@ -1817,11 +1793,16 @@ encoder_listencode_list(PyEncoderObject *s, PyUnicodeWriter *writer,
if (PyUnicodeWriter_WriteChar(writer, ']')) {
goto bail;
}
if (s->markers != Py_None) {
Py_ReprLeave(seq);
}
Py_DECREF(s_fast);
return 0;

bail:
Py_XDECREF(ident);
if (s->markers != Py_None) {
Py_ReprLeave(seq);
}
Py_DECREF(s_fast);
return -1;
}
Expand Down
Loading