summaryrefslogtreecommitdiffstats
path: root/Modules/_json.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-07-23 17:02:54 (GMT)
committerGitHub <noreply@github.com>2024-07-23 17:02:54 (GMT)
commite6b25e9a09dbe09839b36f97b9174a30b1db2dbf (patch)
tree000493ed4293ecce1a6433e8892212fe9809c3d6 /Modules/_json.c
parentc908d1f87d287a4b3ec58c85b692a7eb617fa6ea (diff)
downloadcpython-e6b25e9a09dbe09839b36f97b9174a30b1db2dbf.zip
cpython-e6b25e9a09dbe09839b36f97b9174a30b1db2dbf.tar.gz
cpython-e6b25e9a09dbe09839b36f97b9174a30b1db2dbf.tar.bz2
gh-122163: Add notes for JSON serialization errors (GH-122165)
This allows to identify the source of the error.
Diffstat (limited to 'Modules/_json.c')
-rw-r--r--Modules/_json.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/Modules/_json.c b/Modules/_json.c
index c7fe156..9e29de0 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -11,6 +11,7 @@
#include "Python.h"
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
#include "pycore_runtime.h" // _PyRuntime
+#include "pycore_pyerrors.h" // _PyErr_FormatNote
#include "pycore_global_strings.h" // _Py_ID()
#include <stdbool.h> // bool
@@ -1461,6 +1462,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer,
Py_DECREF(newobj);
if (rv) {
+ _PyErr_FormatNote("when serializing %T object", obj);
Py_XDECREF(ident);
return -1;
}
@@ -1477,7 +1479,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer,
static int
encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *first,
- PyObject *key, PyObject *value,
+ PyObject *dct, PyObject *key, PyObject *value,
PyObject *newline_indent,
PyObject *item_separator)
{
@@ -1535,6 +1537,7 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir
return -1;
}
if (encoder_listencode_obj(s, writer, value, newline_indent) < 0) {
+ _PyErr_FormatNote("when serializing %T item %R", dct, key);
return -1;
}
return 0;
@@ -1606,7 +1609,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer,
key = PyTuple_GET_ITEM(item, 0);
value = PyTuple_GET_ITEM(item, 1);
- if (encoder_encode_key_value(s, writer, &first, key, value,
+ if (encoder_encode_key_value(s, writer, &first, dct, key, value,
new_newline_indent,
current_item_separator) < 0)
goto bail;
@@ -1616,7 +1619,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer,
} else {
Py_ssize_t pos = 0;
while (PyDict_Next(dct, &pos, &key, &value)) {
- if (encoder_encode_key_value(s, writer, &first, key, value,
+ if (encoder_encode_key_value(s, writer, &first, dct, key, value,
new_newline_indent,
current_item_separator) < 0)
goto bail;
@@ -1710,8 +1713,10 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer,
if (_PyUnicodeWriter_WriteStr(writer, separator) < 0)
goto bail;
}
- if (encoder_listencode_obj(s, writer, obj, new_newline_indent))
+ if (encoder_listencode_obj(s, writer, obj, new_newline_indent)) {
+ _PyErr_FormatNote("when serializing %T item %zd", seq, i);
goto bail;
+ }
}
if (ident != NULL) {
if (PyDict_DelItem(s->markers, ident))