From c1cfcf523078bc1d296508d915ae1d97bfad05c3 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 18 Apr 2024 20:44:49 +0200 Subject: [PATCH 01/24] Fix docstring of _json.Encoder --- Modules/_json.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_json.c b/Modules/_json.c index c55299899e77fe..757591b835a7f7 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1721,7 +1721,7 @@ encoder_clear(PyEncoderObject *self) return 0; } -PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable"); +PyDoc_STRVAR(encoder_doc, "Encoder(markers, default, encoder, indent, key_separator, item_separator, sort_keys, skipkeys, allow_nan)"); static PyType_Slot PyEncoderType_slots[] = { {Py_tp_doc, (void *)encoder_doc}, From 30f2e7213ac8560c2b221d5fa61d5e0e01a793e6 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 18 Apr 2024 23:37:16 +0200 Subject: [PATCH 02/24] Implement indent for _json --- Lib/json/encoder.py | 7 +-- Modules/_json.c | 119 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 99 insertions(+), 27 deletions(-) diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py index 597849eca0524a..4d64827e7181ef 100644 --- a/Lib/json/encoder.py +++ b/Lib/json/encoder.py @@ -244,10 +244,11 @@ def floatstr(o, allow_nan=self.allow_nan, return text - if (_one_shot and c_make_encoder is not None - and self.indent is None): + if (_one_shot and c_make_encoder is not None): + #and self.indent is None): + indent_str = self.indent if isinstance(self.indent, (str, type(None))) else ' ' * self.indent _iterencode = c_make_encoder( - markers, self.default, _encoder, self.indent, + markers, self.default, _encoder, indent_str, self.key_separator, self.item_separator, self.sort_keys, self.skipkeys, self.allow_nan) else: diff --git a/Modules/_json.c b/Modules/_json.c index 757591b835a7f7..a9f77364a5b893 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1356,6 +1356,29 @@ _steal_accumulate(_PyUnicodeWriter *writer, PyObject *stolen) return rval; } +PyObject* _create_newline_indent(PyObject* indent, Py_ssize_t indent_level) +{ + PyObject* start = PyUnicode_FromString("\n"); + if (start == 0) { + goto end; + } + PyObject* mul_name = PyUnicode_FromString("__mul__"); + if (mul_name == 0) { + goto end; + } + PyObject* _current_indent = PyObject_CallMethodOneArg(indent, mul_name, PyLong_FromLongLong(indent_level)); + if (_current_indent == 0) { + goto end; + } + + PyObject* newline_indent = PyUnicode_Concat(start, _current_indent); +end: + Py_XDECREF(start); + Py_XDECREF(mul_name); + Py_XDECREF(_current_indent); + return newline_indent; +} + static int encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *obj, Py_ssize_t indent_level) @@ -1489,16 +1512,34 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir return -1; } + PyObject* current_item_separator = s->item_separator; + if (s->indent != Py_None) { + PyObject *newline_indent = _create_newline_indent(s->indent, indent_level); + if (newline_indent == 0) { + Py_DECREF(keystr); + return -1; + } + current_item_separator = PyUnicode_Concat(current_item_separator, newline_indent); + if (current_item_separator == 0) { + Py_DECREF(newline_indent); + Py_DECREF(keystr); + return -1; + } + + } + if (*first) { *first = false; } else { - if (_PyUnicodeWriter_WriteStr(writer, s->item_separator) < 0) { + if (_PyUnicodeWriter_WriteStr(writer, current_item_separator) < 0) { Py_DECREF(keystr); return -1; } } - + if (s->indent != Py_None) { + Py_DECREF(current_item_separator); + } encoded = encoder_encode_string(s, keystr); Py_DECREF(keystr); if (encoded == NULL) { @@ -1549,14 +1590,17 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, if (_PyUnicodeWriter_WriteChar(writer, '{')) goto bail; + PyObject* newline_indent; 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 - */ + newline_indent = _create_newline_indent(s->indent, indent_level); + if (newline_indent == 0) { + goto bail; + } + + if (_PyUnicodeWriter_WriteStr(writer, newline_indent)) { + goto bail; + } } if (s->sort_keys || !PyDict_CheckExact(dct)) { @@ -1592,12 +1636,19 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, goto bail; Py_CLEAR(ident); } - /* TODO DOES NOT RUN; dead code if (s->indent != Py_None) { - indent_level -= 1; + indent_level--; + newline_indent = _create_newline_indent(s->indent, indent_level); + if (newline_indent == 0) { + goto bail; + } + + if (_PyUnicodeWriter_WriteStr(writer, newline_indent)) { + goto bail; + } + Py_DECREF(newline_indent); + } - yield '\n' + (' ' * (_indent * _current_indent_level)) - }*/ if (_PyUnicodeWriter_WriteChar(writer, '}')) goto bail; return 0; @@ -1608,6 +1659,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, return -1; } + static int encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *seq, Py_ssize_t indent_level) @@ -1643,19 +1695,30 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, if (_PyUnicodeWriter_WriteChar(writer, '[')) goto bail; + + PyObject* separator = s->item_separator; + PyObject* newline_indent = 0; 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 - */ + indent_level++; + newline_indent = _create_newline_indent(s->indent, indent_level); + if (newline_indent == 0) { + goto bail; + } + + if (_PyUnicodeWriter_WriteStr(writer, newline_indent)) { + goto bail; + } + + separator = PyUnicode_Concat(separator, newline_indent); + if (separator == 0) { + goto bail; + } + Py_DECREF(newline_indent); } for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) { PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i); if (i) { - if (_PyUnicodeWriter_WriteStr(writer, s->item_separator)) + if (_PyUnicodeWriter_WriteStr(writer, separator)) goto bail; } if (encoder_listencode_obj(s, writer, obj, indent_level)) @@ -1667,12 +1730,20 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_CLEAR(ident); } - /* TODO: DOES NOT RUN if (s->indent != Py_None) { - indent_level -= 1; + indent_level--; + Py_DECREF(separator); + newline_indent = _create_newline_indent(s->indent, indent_level); + if (newline_indent == 0) { + goto bail; + } + + if (_PyUnicodeWriter_WriteStr(writer, newline_indent)) { + goto bail; + } + Py_DECREF(newline_indent); + } - yield '\n' + (' ' * (_indent * _current_indent_level)) - }*/ if (_PyUnicodeWriter_WriteChar(writer, ']')) goto bail; Py_DECREF(s_fast); From 1da39f39a40264c63a785c9e15f5046013385de4 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 18 Apr 2024 23:47:26 +0200 Subject: [PATCH 03/24] refactor --- Modules/_json.c | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index a9f77364a5b893..f6a999382e6b1e 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1356,6 +1356,7 @@ _steal_accumulate(_PyUnicodeWriter *writer, PyObject *stolen) return rval; } +// TODO: this method should be cached PyObject* _create_newline_indent(PyObject* indent, Py_ssize_t indent_level) { PyObject* start = PyUnicode_FromString("\n"); @@ -1479,7 +1480,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, static int encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *first, - PyObject *key, PyObject *value, Py_ssize_t indent_level) + PyObject *key, PyObject *value, Py_ssize_t indent_level, PyObject *current_item_separator) { PyObject *keystr = NULL; PyObject *encoded; @@ -1512,22 +1513,6 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir return -1; } - PyObject* current_item_separator = s->item_separator; - if (s->indent != Py_None) { - PyObject *newline_indent = _create_newline_indent(s->indent, indent_level); - if (newline_indent == 0) { - Py_DECREF(keystr); - return -1; - } - current_item_separator = PyUnicode_Concat(current_item_separator, newline_indent); - if (current_item_separator == 0) { - Py_DECREF(newline_indent); - Py_DECREF(keystr); - return -1; - } - - } - if (*first) { *first = false; } @@ -1537,9 +1522,7 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir return -1; } } - if (s->indent != Py_None) { - Py_DECREF(current_item_separator); - } + encoded = encoder_encode_string(s, keystr); Py_DECREF(keystr); if (encoded == NULL) { @@ -1591,12 +1574,18 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, goto bail; PyObject* newline_indent; + PyObject* current_item_separator = s->item_separator; if (s->indent != Py_None) { indent_level += 1; newline_indent = _create_newline_indent(s->indent, indent_level); if (newline_indent == 0) { goto bail; } + current_item_separator = PyUnicode_Concat(current_item_separator, newline_indent); + if (current_item_separator == 0) { + Py_DECREF(newline_indent); + goto bail; + } if (_PyUnicodeWriter_WriteStr(writer, newline_indent)) { goto bail; @@ -1618,7 +1607,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, indent_level) < 0) + if (encoder_encode_key_value(s, writer, &first, key, value, indent_level, current_item_separator) < 0) goto bail; } Py_CLEAR(items); @@ -1626,7 +1615,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, indent_level) < 0) + if (encoder_encode_key_value(s, writer, &first, key, value, indent_level, current_item_separator) < 0) goto bail; } } @@ -1637,6 +1626,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_CLEAR(ident); } if (s->indent != Py_None) { + Py_DECREF(current_item_separator); indent_level--; newline_indent = _create_newline_indent(s->indent, indent_level); if (newline_indent == 0) { From 0d855518c958c5d1d99e8992e848a89f192e9a0a Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 19 Apr 2024 09:56:38 +0200 Subject: [PATCH 04/24] update comments --- Modules/_json.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/_json.c b/Modules/_json.c index f6a999382e6b1e..5cf48451ff8b44 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1356,13 +1356,14 @@ _steal_accumulate(_PyUnicodeWriter *writer, PyObject *stolen) return rval; } -// TODO: this method should be cached +// TODO: this method could be cached PyObject* _create_newline_indent(PyObject* indent, Py_ssize_t indent_level) { PyObject* start = PyUnicode_FromString("\n"); if (start == 0) { goto end; } + // there is no public PyUnicode_Repeat? PyObject* mul_name = PyUnicode_FromString("__mul__"); if (mul_name == 0) { goto end; From cc02a136decf6f296b50936b604781383449097c Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 19 Apr 2024 17:14:34 +0200 Subject: [PATCH 05/24] fix decrefs --- Lib/json/encoder.py | 8 +++++--- Modules/_json.c | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py index 4d64827e7181ef..909fead434f61e 100644 --- a/Lib/json/encoder.py +++ b/Lib/json/encoder.py @@ -245,10 +245,12 @@ def floatstr(o, allow_nan=self.allow_nan, if (_one_shot and c_make_encoder is not None): - #and self.indent is None): - indent_str = self.indent if isinstance(self.indent, (str, type(None))) else ' ' * self.indent + if self.indent is not None and not isinstance(self.indent, str): + indent = ' ' * self.indent + else: + indent = self.indent _iterencode = c_make_encoder( - markers, self.default, _encoder, indent_str, + markers, self.default, _encoder, indent, self.key_separator, self.item_separator, self.sort_keys, self.skipkeys, self.allow_nan) else: diff --git a/Modules/_json.c b/Modules/_json.c index 5cf48451ff8b44..e6c9bb2285f07a 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1574,7 +1574,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, if (_PyUnicodeWriter_WriteChar(writer, '{')) goto bail; - PyObject* newline_indent; + PyObject* newline_indent = 0; PyObject* current_item_separator = s->item_separator; if (s->indent != Py_None) { indent_level += 1; @@ -1589,6 +1589,8 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, } if (_PyUnicodeWriter_WriteStr(writer, newline_indent)) { + Py_DECREF(current_item_separator); + Py_DECREF(newline_indent); goto bail; } } @@ -1635,6 +1637,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, } if (_PyUnicodeWriter_WriteStr(writer, newline_indent)) { + Py_DECREF(newline_indent); goto bail; } Py_DECREF(newline_indent); @@ -1650,7 +1653,6 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, return -1; } - static int encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *seq, Py_ssize_t indent_level) From a444701b7b9de31c2174b60770e145c1b2795a0e Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sat, 20 Apr 2024 08:58:26 +0200 Subject: [PATCH 06/24] Apply suggestions from code review Co-authored-by: Nice Zombies --- Modules/_json.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/_json.c b/Modules/_json.c index e6c9bb2285f07a..a0ac0ff9b58721 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1360,6 +1360,7 @@ _steal_accumulate(_PyUnicodeWriter *writer, PyObject *stolen) PyObject* _create_newline_indent(PyObject* indent, Py_ssize_t indent_level) { PyObject* start = PyUnicode_FromString("\n"); + PyObject* newline_indent = NULL; if (start == 0) { goto end; } @@ -1373,7 +1374,7 @@ PyObject* _create_newline_indent(PyObject* indent, Py_ssize_t indent_level) goto end; } - PyObject* newline_indent = PyUnicode_Concat(start, _current_indent); + newline_indent = PyUnicode_Concat(start, _current_indent); end: Py_XDECREF(start); Py_XDECREF(mul_name); From ed989b835eaeca28e96eb61e57f142f0f42ed07e Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sun, 21 Apr 2024 20:58:15 +0200 Subject: [PATCH 07/24] review comments --- Modules/_json.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index a0ac0ff9b58721..471fb1023387aa 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1356,29 +1356,24 @@ _steal_accumulate(_PyUnicodeWriter *writer, PyObject *stolen) return rval; } -// TODO: this method could be cached + PyObject* _create_newline_indent(PyObject* indent, Py_ssize_t indent_level) { - PyObject* start = PyUnicode_FromString("\n"); PyObject* newline_indent = NULL; - if (start == 0) { - goto end; - } - // there is no public PyUnicode_Repeat? - PyObject* mul_name = PyUnicode_FromString("__mul__"); - if (mul_name == 0) { + + PyObject* _current_indent = PySequence_Repeat(indent, indent_level); + if (_current_indent == NULL) { goto end; } - PyObject* _current_indent = PyObject_CallMethodOneArg(indent, mul_name, PyLong_FromLongLong(indent_level)); - if (_current_indent == 0) { + PyObject* start = PyUnicode_FromString("\n"); + if (start == NULL) { goto end; } newline_indent = PyUnicode_Concat(start, _current_indent); end: - Py_XDECREF(start); - Py_XDECREF(mul_name); Py_XDECREF(_current_indent); + Py_XDECREF(start); return newline_indent; } @@ -1581,15 +1576,15 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, indent_level += 1; newline_indent = _create_newline_indent(s->indent, indent_level); if (newline_indent == 0) { - goto bail; + goto bail; } current_item_separator = PyUnicode_Concat(current_item_separator, newline_indent); if (current_item_separator == 0) { - Py_DECREF(newline_indent); - goto bail; + Py_DECREF(newline_indent); + goto bail; } - if (_PyUnicodeWriter_WriteStr(writer, newline_indent)) { + if (_PyUnicodeWriter_WriteStr(writer, newline_indent) < 0) { Py_DECREF(current_item_separator); Py_DECREF(newline_indent); goto bail; @@ -1637,7 +1632,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, goto bail; } - if (_PyUnicodeWriter_WriteStr(writer, newline_indent)) { + if (_PyUnicodeWriter_WriteStr(writer, newline_indent) < 0) { Py_DECREF(newline_indent); goto bail; } @@ -1690,7 +1685,7 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, if (_PyUnicodeWriter_WriteChar(writer, '[')) goto bail; - PyObject* separator = s->item_separator; + PyObject* separator = s->item_separator; // borrowed reference PyObject* newline_indent = 0; if (s->indent != Py_None) { indent_level++; @@ -1699,11 +1694,11 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, goto bail; } - if (_PyUnicodeWriter_WriteStr(writer, newline_indent)) { + if (_PyUnicodeWriter_WriteStr(writer, newline_indent) < 0) { goto bail; } - separator = PyUnicode_Concat(separator, newline_indent); + separator = PyUnicode_Concat(separator, newline_indent); // non-borrowed reference if (separator == 0) { goto bail; } @@ -1712,7 +1707,7 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) { PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i); if (i) { - if (_PyUnicodeWriter_WriteStr(writer, separator)) + if (_PyUnicodeWriter_WriteStr(writer, separator) < 0) goto bail; } if (encoder_listencode_obj(s, writer, obj, indent_level)) @@ -1732,7 +1727,7 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, goto bail; } - if (_PyUnicodeWriter_WriteStr(writer, newline_indent)) { + if (_PyUnicodeWriter_WriteStr(writer, newline_indent) < 0) { goto bail; } Py_DECREF(newline_indent); From 5df567b3f6f79b41d5f62d804bc087a36713b007 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sun, 21 Apr 2024 21:01:49 +0200 Subject: [PATCH 08/24] whitespace --- Modules/_json.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_json.c b/Modules/_json.c index 471fb1023387aa..7bc23d1ef0fc46 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1360,7 +1360,7 @@ _steal_accumulate(_PyUnicodeWriter *writer, PyObject *stolen) PyObject* _create_newline_indent(PyObject* indent, Py_ssize_t indent_level) { PyObject* newline_indent = NULL; - + PyObject* _current_indent = PySequence_Repeat(indent, indent_level); if (_current_indent == NULL) { goto end; From 5fdc2795e131e0b4df84cbf76c90e2d2cf129c56 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sun, 21 Apr 2024 21:46:40 +0200 Subject: [PATCH 09/24] whitespace --- Modules/_json.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_json.c b/Modules/_json.c index 7bc23d1ef0fc46..828c35c9c31d3a 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1360,7 +1360,7 @@ _steal_accumulate(_PyUnicodeWriter *writer, PyObject *stolen) PyObject* _create_newline_indent(PyObject* indent, Py_ssize_t indent_level) { PyObject* newline_indent = NULL; - + PyObject* _current_indent = PySequence_Repeat(indent, indent_level); if (_current_indent == NULL) { goto end; From 5e47a41545b7bef4ab49d7079d149962b5251940 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 22 Apr 2024 23:07:22 +0200 Subject: [PATCH 10/24] address review comments --- Lib/json/encoder.py | 4 +- Modules/_json.c | 140 +++++++++++++++++++++++--------------------- 2 files changed, 74 insertions(+), 70 deletions(-) diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py index 909fead434f61e..d6043864946cc2 100644 --- a/Lib/json/encoder.py +++ b/Lib/json/encoder.py @@ -244,9 +244,9 @@ def floatstr(o, allow_nan=self.allow_nan, return text - if (_one_shot and c_make_encoder is not None): + if _one_shot and c_make_encoder is not None: if self.indent is not None and not isinstance(self.indent, str): - indent = ' ' * self.indent + indent = ' ' * self.indent else: indent = self.indent _iterencode = c_make_encoder( diff --git a/Modules/_json.c b/Modules/_json.c index 828c35c9c31d3a..77c9fa69e9d0b6 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -85,11 +85,11 @@ encoder_dealloc(PyObject *self); static int encoder_clear(PyEncoderObject *self); static int -encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *seq, Py_ssize_t indent_level); +encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *seq, Py_ssize_t indent_level, PyObject* current_newline_indent); static int -encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *obj, Py_ssize_t indent_level); +encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *obj, Py_ssize_t indent_level, PyObject *current_newline_indent); static int -encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *dct, Py_ssize_t indent_level); +encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *dct, Py_ssize_t indent_level, PyObject* current_newline_indent); static PyObject * _encoded_const(PyObject *obj); static void @@ -1251,6 +1251,25 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return (PyObject *)s; } +static PyObject* +_create_newline_indent(PyObject* indent, Py_ssize_t indent_level) +{ + PyObject* current_indent = PySequence_Repeat(indent, indent_level); + if (current_indent == NULL) { + return NULL; + } + PyObject* start = PyUnicode_FromOrdinal(10); + if (start == NULL) { + Py_DECREF(current_indent); + return NULL; + } + + PyObject* newline_indent = PyUnicode_Concat(start, current_indent); + Py_DECREF(current_indent); + Py_DECREF(start); + return newline_indent; +} + static PyObject * encoder_call(PyEncoderObject *self, PyObject *args, PyObject *kwds) { @@ -1267,10 +1286,19 @@ encoder_call(PyEncoderObject *self, PyObject *args, PyObject *kwds) _PyUnicodeWriter_Init(&writer); writer.overallocate = 1; - if (encoder_listencode_obj(self, &writer, obj, indent_level)) { + PyObject * current_newline_indent = NULL; + if (self->indent != Py_None) { + current_newline_indent = _create_newline_indent(self->indent, indent_level); + if (current_newline_indent == NULL) { + _PyUnicodeWriter_Dealloc(&writer); + return NULL; + } + } + if (encoder_listencode_obj(self, &writer, obj, indent_level, current_newline_indent)) { _PyUnicodeWriter_Dealloc(&writer); return NULL; } + Py_XDECREF(current_newline_indent); result = PyTuple_New(1); if (result == NULL || @@ -1357,29 +1385,9 @@ _steal_accumulate(_PyUnicodeWriter *writer, PyObject *stolen) } -PyObject* _create_newline_indent(PyObject* indent, Py_ssize_t indent_level) -{ - PyObject* newline_indent = NULL; - - PyObject* _current_indent = PySequence_Repeat(indent, indent_level); - if (_current_indent == NULL) { - goto end; - } - PyObject* start = PyUnicode_FromString("\n"); - if (start == NULL) { - goto end; - } - - newline_indent = PyUnicode_Concat(start, _current_indent); -end: - Py_XDECREF(_current_indent); - Py_XDECREF(start); - return newline_indent; -} - static int encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, - PyObject *obj, Py_ssize_t indent_level) + PyObject *obj, Py_ssize_t indent_level, PyObject *current_newline_indent) { /* Encode Python object obj to a JSON term */ PyObject *newobj; @@ -1415,14 +1423,14 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, else if (PyList_Check(obj) || PyTuple_Check(obj)) { if (_Py_EnterRecursiveCall(" while encoding a JSON object")) return -1; - rv = encoder_listencode_list(s, writer, obj, indent_level); + rv = encoder_listencode_list(s, writer, obj, indent_level, current_newline_indent); _Py_LeaveRecursiveCall(); return rv; } else if (PyDict_Check(obj)) { if (_Py_EnterRecursiveCall(" while encoding a JSON object")) return -1; - rv = encoder_listencode_dict(s, writer, obj, indent_level); + rv = encoder_listencode_dict(s, writer, obj, indent_level, current_newline_indent); _Py_LeaveRecursiveCall(); return rv; } @@ -1456,7 +1464,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_XDECREF(ident); return -1; } - rv = encoder_listencode_obj(s, writer, newobj, indent_level); + rv = encoder_listencode_obj(s, writer, newobj, indent_level, current_newline_indent); _Py_LeaveRecursiveCall(); Py_DECREF(newobj); @@ -1477,7 +1485,8 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, static int encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *first, - PyObject *key, PyObject *value, Py_ssize_t indent_level, PyObject *current_item_separator) + PyObject *key, PyObject *value, Py_ssize_t indent_level, + PyObject *current_newline_indent, PyObject *current_item_separator) { PyObject *keystr = NULL; PyObject *encoded; @@ -1532,7 +1541,7 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir if (_PyUnicodeWriter_WriteStr(writer, s->key_separator) < 0) { return -1; } - if (encoder_listencode_obj(s, writer, value, indent_level) < 0) { + if (encoder_listencode_obj(s, writer, value, indent_level, current_newline_indent) < 0) { return -1; } return 0; @@ -1540,13 +1549,16 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir static int encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, - PyObject *dct, Py_ssize_t indent_level) + PyObject *dct, Py_ssize_t indent_level, + PyObject *current_newline_indent) { /* Encode Python dict dct a JSON term */ PyObject *ident = NULL; PyObject *items = NULL; PyObject *key, *value; bool first = true; + PyObject* newline_indent = NULL; + PyObject* separator_indent = NULL; if (PyDict_GET_SIZE(dct) == 0) /* Fast path */ return _PyUnicodeWriter_WriteASCIIString(writer, "{}", 2); @@ -1570,23 +1582,19 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, if (_PyUnicodeWriter_WriteChar(writer, '{')) goto bail; - PyObject* newline_indent = 0; - PyObject* current_item_separator = s->item_separator; + PyObject *current_item_separator = s->item_separator; // borrowed reference if (s->indent != Py_None) { indent_level += 1; - newline_indent = _create_newline_indent(s->indent, indent_level); - if (newline_indent == 0) { + newline_indent = PyUnicode_Concat(current_newline_indent, s->indent); + if (newline_indent == NULL) { goto bail; } - current_item_separator = PyUnicode_Concat(current_item_separator, newline_indent); - if (current_item_separator == 0) { - Py_DECREF(newline_indent); + separator_indent = PyUnicode_Concat(current_item_separator, newline_indent); + if (separator_indent == NULL) { goto bail; } - + current_item_separator = separator_indent; // update item separator with a borrowed reference if (_PyUnicodeWriter_WriteStr(writer, newline_indent) < 0) { - Py_DECREF(current_item_separator); - Py_DECREF(newline_indent); goto bail; } } @@ -1606,7 +1614,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, indent_level, current_item_separator) < 0) + if (encoder_encode_key_value(s, writer, &first, key, value, indent_level, newline_indent, current_item_separator) < 0) goto bail; } Py_CLEAR(items); @@ -1614,7 +1622,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, indent_level, current_item_separator) < 0) + if (encoder_encode_key_value(s, writer, &first, key, value, indent_level, newline_indent, current_item_separator) < 0) goto bail; } } @@ -1625,37 +1633,37 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_CLEAR(ident); } if (s->indent != Py_None) { - Py_DECREF(current_item_separator); + Py_DECREF(newline_indent); + Py_DECREF(separator_indent); indent_level--; - newline_indent = _create_newline_indent(s->indent, indent_level); - if (newline_indent == 0) { - goto bail; - } - if (_PyUnicodeWriter_WriteStr(writer, newline_indent) < 0) { - Py_DECREF(newline_indent); + if (_PyUnicodeWriter_WriteStr(writer, current_newline_indent) < 0) { goto bail; } - Py_DECREF(newline_indent); } if (_PyUnicodeWriter_WriteChar(writer, '}')) goto bail; + return 0; bail: Py_XDECREF(items); Py_XDECREF(ident); + Py_XDECREF(separator_indent); + Py_XDECREF(newline_indent); return -1; } static int encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, - PyObject *seq, Py_ssize_t indent_level) + PyObject *seq, Py_ssize_t indent_level, PyObject *current_newline_indent) { PyObject *ident = NULL; PyObject *s_fast = NULL; Py_ssize_t i; + PyObject* newline_indent = NULL; + PyObject* separator_indent = NULL; ident = NULL; s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence"); @@ -1685,12 +1693,11 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, if (_PyUnicodeWriter_WriteChar(writer, '[')) goto bail; - PyObject* separator = s->item_separator; // borrowed reference - PyObject* newline_indent = 0; + PyObject *separator = s->item_separator; // borrowed reference if (s->indent != Py_None) { indent_level++; - newline_indent = _create_newline_indent(s->indent, indent_level); - if (newline_indent == 0) { + newline_indent = PyUnicode_Concat(current_newline_indent, s->indent); + if (newline_indent == NULL) { goto bail; } @@ -1698,11 +1705,11 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, goto bail; } - separator = PyUnicode_Concat(separator, newline_indent); // non-borrowed reference - if (separator == 0) { + separator_indent = PyUnicode_Concat(separator, newline_indent); + if (separator_indent == NULL) { goto bail; } - Py_DECREF(newline_indent); + separator = separator_indent; // assign separator with borrowed reference } for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) { PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i); @@ -1710,7 +1717,7 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, if (_PyUnicodeWriter_WriteStr(writer, separator) < 0) goto bail; } - if (encoder_listencode_obj(s, writer, obj, indent_level)) + if (encoder_listencode_obj(s, writer, obj, indent_level, newline_indent)) goto bail; } if (ident != NULL) { @@ -1721,16 +1728,11 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, if (s->indent != Py_None) { indent_level--; - Py_DECREF(separator); - newline_indent = _create_newline_indent(s->indent, indent_level); - if (newline_indent == 0) { - goto bail; - } - - if (_PyUnicodeWriter_WriteStr(writer, newline_indent) < 0) { + Py_DECREF(newline_indent); + Py_DECREF(separator_indent); + if (_PyUnicodeWriter_WriteStr(writer, current_newline_indent) < 0) { goto bail; } - Py_DECREF(newline_indent); } if (_PyUnicodeWriter_WriteChar(writer, ']')) @@ -1741,6 +1743,8 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, bail: Py_XDECREF(ident); Py_DECREF(s_fast); + Py_XDECREF(separator_indent); + Py_XDECREF(newline_indent); return -1; } From a7f4bc61f395d737c27c08a8155dfcd39adc660d Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 22 Apr 2024 23:22:46 +0200 Subject: [PATCH 11/24] pep7 --- Modules/_json.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index 77c9fa69e9d0b6..982052dfdbff2a 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1251,7 +1251,7 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return (PyObject *)s; } -static PyObject* +static PyObject * _create_newline_indent(PyObject* indent, Py_ssize_t indent_level) { PyObject* current_indent = PySequence_Repeat(indent, indent_level); @@ -1557,8 +1557,8 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *items = NULL; PyObject *key, *value; bool first = true; - PyObject* newline_indent = NULL; - PyObject* separator_indent = NULL; + PyObject *newline_indent = NULL; + PyObject *separator_indent = NULL; if (PyDict_GET_SIZE(dct) == 0) /* Fast path */ return _PyUnicodeWriter_WriteASCIIString(writer, "{}", 2); From 35601c7c1f3c30235d460530e2d264f722bf630e Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 22 Apr 2024 23:29:52 +0200 Subject: [PATCH 12/24] pep7 --- Modules/_json.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index 982052dfdbff2a..321cf68797e036 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1662,8 +1662,8 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *ident = NULL; PyObject *s_fast = NULL; Py_ssize_t i; - PyObject* newline_indent = NULL; - PyObject* separator_indent = NULL; + PyObject *newline_indent = NULL; + PyObject *separator_indent = NULL; ident = NULL; s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence"); From 311b7dfb621d015f4680b30b4d6db18dfda69dad Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Wed, 24 Apr 2024 13:04:38 +0200 Subject: [PATCH 13/24] Update Modules/_json.c Co-authored-by: Erlend E. Aasland --- Modules/_json.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index 321cf68797e036..e5f1857cd4e8be 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1728,8 +1728,8 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, if (s->indent != Py_None) { indent_level--; - Py_DECREF(newline_indent); - Py_DECREF(separator_indent); + Py_CLEAR(newline_indent); + Py_CLEAR(separator_indent); if (_PyUnicodeWriter_WriteStr(writer, current_newline_indent) < 0) { goto bail; } From ed2c806b2694b569b5fe5bc3a1e01fc94a672ce1 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Wed, 24 Apr 2024 13:05:29 +0200 Subject: [PATCH 14/24] Update Modules/_json.c --- Modules/_json.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index e5f1857cd4e8be..e5c9dcb84ea7fe 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1633,8 +1633,8 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_CLEAR(ident); } if (s->indent != Py_None) { - Py_DECREF(newline_indent); - Py_DECREF(separator_indent); + Py_CLEAR(newline_indent); + Py_CLEAR(separator_indent); indent_level--; if (_PyUnicodeWriter_WriteStr(writer, current_newline_indent) < 0) { From a407b847285ab4c49ad9ccfd6a029ab3414f7382 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 25 Apr 2024 12:22:34 +0200 Subject: [PATCH 15/24] review comments --- Modules/_json.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index e5c9dcb84ea7fe..9393902bb94f07 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1258,7 +1258,7 @@ _create_newline_indent(PyObject* indent, Py_ssize_t indent_level) if (current_indent == NULL) { return NULL; } - PyObject* start = PyUnicode_FromOrdinal(10); + PyObject* start = PyUnicode_FromOrdinal('\n'); if (start == NULL) { Py_DECREF(current_indent); return NULL; @@ -1288,13 +1288,15 @@ encoder_call(PyEncoderObject *self, PyObject *args, PyObject *kwds) PyObject * current_newline_indent = NULL; if (self->indent != Py_None) { - current_newline_indent = _create_newline_indent(self->indent, indent_level); + current_newline_indent = _create_newline_indent(self->indent, + indent_level); if (current_newline_indent == NULL) { _PyUnicodeWriter_Dealloc(&writer); return NULL; } } - if (encoder_listencode_obj(self, &writer, obj, indent_level, current_newline_indent)) { + if (encoder_listencode_obj(self, &writer, obj, indent_level, + current_newline_indent)) { _PyUnicodeWriter_Dealloc(&writer); return NULL; } @@ -1384,10 +1386,10 @@ _steal_accumulate(_PyUnicodeWriter *writer, PyObject *stolen) return rval; } - static int encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, - PyObject *obj, Py_ssize_t indent_level, PyObject *current_newline_indent) + PyObject *obj, Py_ssize_t indent_level, + PyObject *current_newline_indent) { /* Encode Python object obj to a JSON term */ PyObject *newobj; @@ -1423,14 +1425,16 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, else if (PyList_Check(obj) || PyTuple_Check(obj)) { if (_Py_EnterRecursiveCall(" while encoding a JSON object")) return -1; - rv = encoder_listencode_list(s, writer, obj, indent_level, current_newline_indent); + rv = encoder_listencode_list(s, writer, obj, indent_level, + current_newline_indent); _Py_LeaveRecursiveCall(); return rv; } else if (PyDict_Check(obj)) { if (_Py_EnterRecursiveCall(" while encoding a JSON object")) return -1; - rv = encoder_listencode_dict(s, writer, obj, indent_level, current_newline_indent); + rv = encoder_listencode_dict(s, writer, obj, indent_level, + current_newline_indent); _Py_LeaveRecursiveCall(); return rv; } @@ -1464,7 +1468,8 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_XDECREF(ident); return -1; } - rv = encoder_listencode_obj(s, writer, newobj, indent_level, current_newline_indent); + rv = encoder_listencode_obj(s, writer, newobj, indent_level, + current_newline_indent); _Py_LeaveRecursiveCall(); Py_DECREF(newobj); @@ -1486,7 +1491,8 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, static int encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *first, PyObject *key, PyObject *value, Py_ssize_t indent_level, - PyObject *current_newline_indent, PyObject *current_item_separator) + PyObject *current_newline_indent, + PyObject *current_item_separator) { PyObject *keystr = NULL; PyObject *encoded; @@ -1541,7 +1547,8 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir if (_PyUnicodeWriter_WriteStr(writer, s->key_separator) < 0) { return -1; } - if (encoder_listencode_obj(s, writer, value, indent_level, current_newline_indent) < 0) { + if (encoder_listencode_obj(s, writer, value, indent_level, + current_newline_indent) < 0) { return -1; } return 0; @@ -1593,7 +1600,8 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, if (separator_indent == NULL) { goto bail; } - current_item_separator = separator_indent; // update item separator with a borrowed reference + // update item separator with a borrowed reference + current_item_separator = separator_indent; if (_PyUnicodeWriter_WriteStr(writer, newline_indent) < 0) { goto bail; } @@ -1614,7 +1622,9 @@ 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, indent_level, newline_indent, current_item_separator) < 0) + if (encoder_encode_key_value(s, writer, &first, key, value, + indent_level, newline_indent, + current_item_separator) < 0) goto bail; } Py_CLEAR(items); @@ -1622,7 +1632,9 @@ 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, indent_level, newline_indent, current_item_separator) < 0) + if (encoder_encode_key_value(s, writer, &first, key, value, + indent_level, newline_indent, + current_item_separator) < 0) goto bail; } } @@ -1644,7 +1656,6 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, if (_PyUnicodeWriter_WriteChar(writer, '}')) goto bail; - return 0; bail: @@ -1657,7 +1668,8 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, static int encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, - PyObject *seq, Py_ssize_t indent_level, PyObject *current_newline_indent) + PyObject *seq, Py_ssize_t indent_level, + PyObject *current_newline_indent) { PyObject *ident = NULL; PyObject *s_fast = NULL; From ac86ee4d6731c60371280535282447b3030dba49 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 25 Apr 2024 12:42:40 +0200 Subject: [PATCH 16/24] Update Modules/_json.c Co-authored-by: Nice Zombies --- Modules/_json.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_json.c b/Modules/_json.c index 9393902bb94f07..0a3dca3b8daf0f 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1296,7 +1296,7 @@ encoder_call(PyEncoderObject *self, PyObject *args, PyObject *kwds) } } if (encoder_listencode_obj(self, &writer, obj, indent_level, - current_newline_indent)) { + current_newline_indent)) { _PyUnicodeWriter_Dealloc(&writer); return NULL; } From 3b55d640ffd40da15c69e1d7881e4a8500b36ee0 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 25 Apr 2024 21:07:19 +0200 Subject: [PATCH 17/24] review comments --- Lib/json/encoder.py | 13 +++++------- Modules/_json.c | 51 ++++++++++++++++++--------------------------- 2 files changed, 25 insertions(+), 39 deletions(-) diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py index d6043864946cc2..323332f064edf8 100644 --- a/Lib/json/encoder.py +++ b/Lib/json/encoder.py @@ -244,18 +244,18 @@ def floatstr(o, allow_nan=self.allow_nan, return text + if self.indent is None or isinstance(self.indent, str): + indent = self.indent + else: + indent = ' ' * self.indent if _one_shot and c_make_encoder is not None: - if self.indent is not None and not isinstance(self.indent, str): - indent = ' ' * self.indent - else: - indent = self.indent _iterencode = c_make_encoder( markers, self.default, _encoder, indent, self.key_separator, self.item_separator, self.sort_keys, self.skipkeys, self.allow_nan) else: _iterencode = _make_iterencode( - markers, self.default, _encoder, self.indent, floatstr, + markers, self.default, _encoder, indent, floatstr, self.key_separator, self.item_separator, self.sort_keys, self.skipkeys, _one_shot) return _iterencode(o, 0) @@ -275,9 +275,6 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, _intstr=int.__repr__, ): - if _indent is not None and not isinstance(_indent, str): - _indent = ' ' * _indent - def _iterencode_list(lst, _current_indent_level): if not lst: yield '[]' diff --git a/Modules/_json.c b/Modules/_json.c index 0a3dca3b8daf0f..7849610376f2a0 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -85,11 +85,11 @@ encoder_dealloc(PyObject *self); static int encoder_clear(PyEncoderObject *self); static int -encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *seq, Py_ssize_t indent_level, PyObject* current_newline_indent); +encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *seq, PyObject* current_newline_indent); static int -encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *obj, Py_ssize_t indent_level, PyObject *current_newline_indent); +encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *obj, PyObject *current_newline_indent); static int -encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *dct, Py_ssize_t indent_level, PyObject* current_newline_indent); +encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *dct, PyObject* current_newline_indent); static PyObject * _encoded_const(PyObject *obj); static void @@ -1252,19 +1252,19 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } static PyObject * -_create_newline_indent(PyObject* indent, Py_ssize_t indent_level) +_create_newline_indent(PyObject *indent, Py_ssize_t indent_level) { - PyObject* current_indent = PySequence_Repeat(indent, indent_level); + PyObject *current_indent = PySequence_Repeat(indent, indent_level); if (current_indent == NULL) { return NULL; } - PyObject* start = PyUnicode_FromOrdinal('\n'); + PyObject *start = PyUnicode_FromOrdinal('\n'); if (start == NULL) { Py_DECREF(current_indent); return NULL; } - PyObject* newline_indent = PyUnicode_Concat(start, current_indent); + PyObject *newline_indent = PyUnicode_Concat(start, current_indent); Py_DECREF(current_indent); Py_DECREF(start); return newline_indent; @@ -1295,9 +1295,9 @@ encoder_call(PyEncoderObject *self, PyObject *args, PyObject *kwds) return NULL; } } - if (encoder_listencode_obj(self, &writer, obj, indent_level, - current_newline_indent)) { + if (encoder_listencode_obj(self, &writer, obj, current_newline_indent)) { _PyUnicodeWriter_Dealloc(&writer); + Py_XDECREF(current_newline_indent); return NULL; } Py_XDECREF(current_newline_indent); @@ -1388,8 +1388,7 @@ _steal_accumulate(_PyUnicodeWriter *writer, PyObject *stolen) static int encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, - PyObject *obj, Py_ssize_t indent_level, - PyObject *current_newline_indent) + PyObject *obj, PyObject *current_newline_indent) { /* Encode Python object obj to a JSON term */ PyObject *newobj; @@ -1425,16 +1424,14 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, else if (PyList_Check(obj) || PyTuple_Check(obj)) { if (_Py_EnterRecursiveCall(" while encoding a JSON object")) return -1; - rv = encoder_listencode_list(s, writer, obj, indent_level, - current_newline_indent); + rv = encoder_listencode_list(s, writer, obj, current_newline_indent); _Py_LeaveRecursiveCall(); return rv; } else if (PyDict_Check(obj)) { if (_Py_EnterRecursiveCall(" while encoding a JSON object")) return -1; - rv = encoder_listencode_dict(s, writer, obj, indent_level, - current_newline_indent); + rv = encoder_listencode_dict(s, writer, obj, current_newline_indent); _Py_LeaveRecursiveCall(); return rv; } @@ -1468,8 +1465,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_XDECREF(ident); return -1; } - rv = encoder_listencode_obj(s, writer, newobj, indent_level, - current_newline_indent); + rv = encoder_listencode_obj(s, writer, newobj, current_newline_indent); _Py_LeaveRecursiveCall(); Py_DECREF(newobj); @@ -1490,7 +1486,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, static int encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *first, - PyObject *key, PyObject *value, Py_ssize_t indent_level, + PyObject *key, PyObject *value, PyObject *current_newline_indent, PyObject *current_item_separator) { @@ -1547,8 +1543,7 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir if (_PyUnicodeWriter_WriteStr(writer, s->key_separator) < 0) { return -1; } - if (encoder_listencode_obj(s, writer, value, indent_level, - current_newline_indent) < 0) { + if (encoder_listencode_obj(s, writer, value, current_newline_indent) < 0) { return -1; } return 0; @@ -1556,8 +1551,7 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir static int encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, - PyObject *dct, Py_ssize_t indent_level, - PyObject *current_newline_indent) + PyObject *dct, PyObject *current_newline_indent) { /* Encode Python dict dct a JSON term */ PyObject *ident = NULL; @@ -1591,7 +1585,6 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *current_item_separator = s->item_separator; // borrowed reference if (s->indent != Py_None) { - indent_level += 1; newline_indent = PyUnicode_Concat(current_newline_indent, s->indent); if (newline_indent == NULL) { goto bail; @@ -1623,7 +1616,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, - indent_level, newline_indent, + newline_indent, current_item_separator) < 0) goto bail; } @@ -1633,7 +1626,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_ssize_t pos = 0; while (PyDict_Next(dct, &pos, &key, &value)) { if (encoder_encode_key_value(s, writer, &first, key, value, - indent_level, newline_indent, + newline_indent, current_item_separator) < 0) goto bail; } @@ -1647,7 +1640,6 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, if (s->indent != Py_None) { Py_CLEAR(newline_indent); Py_CLEAR(separator_indent); - indent_level--; if (_PyUnicodeWriter_WriteStr(writer, current_newline_indent) < 0) { goto bail; @@ -1668,8 +1660,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, static int encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, - PyObject *seq, Py_ssize_t indent_level, - PyObject *current_newline_indent) + PyObject *seq, PyObject *current_newline_indent) { PyObject *ident = NULL; PyObject *s_fast = NULL; @@ -1707,7 +1698,6 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *separator = s->item_separator; // borrowed reference if (s->indent != Py_None) { - indent_level++; newline_indent = PyUnicode_Concat(current_newline_indent, s->indent); if (newline_indent == NULL) { goto bail; @@ -1729,7 +1719,7 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, if (_PyUnicodeWriter_WriteStr(writer, separator) < 0) goto bail; } - if (encoder_listencode_obj(s, writer, obj, indent_level, newline_indent)) + if (encoder_listencode_obj(s, writer, obj, newline_indent)) goto bail; } if (ident != NULL) { @@ -1739,7 +1729,6 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, } if (s->indent != Py_None) { - indent_level--; Py_CLEAR(newline_indent); Py_CLEAR(separator_indent); if (_PyUnicodeWriter_WriteStr(writer, current_newline_indent) < 0) { From bb4ff43515d5307ce288a144e6f3cfd2565272a1 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 25 Apr 2024 21:14:28 +0200 Subject: [PATCH 18/24] rename newline_indent variables --- Modules/_json.c | 50 ++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index 7849610376f2a0..9724f43d9c8131 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1487,8 +1487,8 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, static int encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *first, PyObject *key, PyObject *value, - PyObject *current_newline_indent, - PyObject *current_item_separator) + PyObject *newline_indent, + PyObject *item_separator) { PyObject *keystr = NULL; PyObject *encoded; @@ -1525,7 +1525,7 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir *first = false; } else { - if (_PyUnicodeWriter_WriteStr(writer, current_item_separator) < 0) { + if (_PyUnicodeWriter_WriteStr(writer, item_separator) < 0) { Py_DECREF(keystr); return -1; } @@ -1543,7 +1543,7 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir if (_PyUnicodeWriter_WriteStr(writer, s->key_separator) < 0) { return -1; } - if (encoder_listencode_obj(s, writer, value, current_newline_indent) < 0) { + if (encoder_listencode_obj(s, writer, value, newline_indent) < 0) { return -1; } return 0; @@ -1551,14 +1551,14 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir static int encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, - PyObject *dct, PyObject *current_newline_indent) + PyObject *dct, PyObject *newline_indent) { /* Encode Python dict dct a JSON term */ PyObject *ident = NULL; PyObject *items = NULL; PyObject *key, *value; bool first = true; - PyObject *newline_indent = NULL; + PyObject *new_newline_indent = NULL; PyObject *separator_indent = NULL; if (PyDict_GET_SIZE(dct) == 0) /* Fast path */ @@ -1585,17 +1585,17 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *current_item_separator = s->item_separator; // borrowed reference if (s->indent != Py_None) { - newline_indent = PyUnicode_Concat(current_newline_indent, s->indent); - if (newline_indent == NULL) { + new_newline_indent = PyUnicode_Concat(newline_indent, s->indent); + if (new_newline_indent == NULL) { goto bail; } - separator_indent = PyUnicode_Concat(current_item_separator, newline_indent); + separator_indent = PyUnicode_Concat(current_item_separator, new_newline_indent); if (separator_indent == NULL) { goto bail; } // update item separator with a borrowed reference current_item_separator = separator_indent; - if (_PyUnicodeWriter_WriteStr(writer, newline_indent) < 0) { + if (_PyUnicodeWriter_WriteStr(writer, new_newline_indent) < 0) { goto bail; } } @@ -1616,7 +1616,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, - newline_indent, + new_newline_indent, current_item_separator) < 0) goto bail; } @@ -1626,7 +1626,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_ssize_t pos = 0; while (PyDict_Next(dct, &pos, &key, &value)) { if (encoder_encode_key_value(s, writer, &first, key, value, - newline_indent, + new_newline_indent, current_item_separator) < 0) goto bail; } @@ -1638,10 +1638,10 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_CLEAR(ident); } if (s->indent != Py_None) { - Py_CLEAR(newline_indent); + Py_CLEAR(new_newline_indent); Py_CLEAR(separator_indent); - if (_PyUnicodeWriter_WriteStr(writer, current_newline_indent) < 0) { + if (_PyUnicodeWriter_WriteStr(writer, newline_indent) < 0) { goto bail; } } @@ -1654,18 +1654,18 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_XDECREF(items); Py_XDECREF(ident); Py_XDECREF(separator_indent); - Py_XDECREF(newline_indent); + Py_XDECREF(new_newline_indent); return -1; } static int encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, - PyObject *seq, PyObject *current_newline_indent) + PyObject *seq, PyObject *newline_indent) { PyObject *ident = NULL; PyObject *s_fast = NULL; Py_ssize_t i; - PyObject *newline_indent = NULL; + PyObject *new_newline_indent = NULL; PyObject *separator_indent = NULL; ident = NULL; @@ -1698,16 +1698,16 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *separator = s->item_separator; // borrowed reference if (s->indent != Py_None) { - newline_indent = PyUnicode_Concat(current_newline_indent, s->indent); - if (newline_indent == NULL) { + new_newline_indent = PyUnicode_Concat(newline_indent, s->indent); + if (new_newline_indent == NULL) { goto bail; } - if (_PyUnicodeWriter_WriteStr(writer, newline_indent) < 0) { + if (_PyUnicodeWriter_WriteStr(writer, new_newline_indent) < 0) { goto bail; } - separator_indent = PyUnicode_Concat(separator, newline_indent); + separator_indent = PyUnicode_Concat(separator, new_newline_indent); if (separator_indent == NULL) { goto bail; } @@ -1719,7 +1719,7 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, if (_PyUnicodeWriter_WriteStr(writer, separator) < 0) goto bail; } - if (encoder_listencode_obj(s, writer, obj, newline_indent)) + if (encoder_listencode_obj(s, writer, obj, new_newline_indent)) goto bail; } if (ident != NULL) { @@ -1729,9 +1729,9 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, } if (s->indent != Py_None) { - Py_CLEAR(newline_indent); + Py_CLEAR(new_newline_indent); Py_CLEAR(separator_indent); - if (_PyUnicodeWriter_WriteStr(writer, current_newline_indent) < 0) { + if (_PyUnicodeWriter_WriteStr(writer, newline_indent) < 0) { goto bail; } } @@ -1745,7 +1745,7 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_XDECREF(ident); Py_DECREF(s_fast); Py_XDECREF(separator_indent); - Py_XDECREF(newline_indent); + Py_XDECREF(new_newline_indent); return -1; } From 9ef9332691479b16d44cc27a8d9ffbfe328e4928 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 25 Apr 2024 22:00:56 +0200 Subject: [PATCH 19/24] code style --- Modules/_json.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index 9724f43d9c8131..83cf5765b645ef 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -85,11 +85,11 @@ encoder_dealloc(PyObject *self); static int encoder_clear(PyEncoderObject *self); static int -encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *seq, PyObject* current_newline_indent); +encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *seq, PyObject *newline_indent); static int -encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *obj, PyObject *current_newline_indent); +encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *obj, PyObject *newline_indent); static int -encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *dct, PyObject* current_newline_indent); +encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, PyObject *dct, PyObject *newline_indent); static PyObject * _encoded_const(PyObject *obj); static void From ed029a6cbf1ee64fd01a2041ca0ea7147f0a6fc7 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 25 Apr 2024 22:05:37 +0200 Subject: [PATCH 20/24] rename variable --- Modules/_json.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index 83cf5765b645ef..1858a4a0c5241d 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1286,21 +1286,20 @@ encoder_call(PyEncoderObject *self, PyObject *args, PyObject *kwds) _PyUnicodeWriter_Init(&writer); writer.overallocate = 1; - PyObject * current_newline_indent = NULL; + PyObject *newline_indent = NULL; if (self->indent != Py_None) { - current_newline_indent = _create_newline_indent(self->indent, - indent_level); - if (current_newline_indent == NULL) { + newline_indent = _create_newline_indent(self->indent, indent_level); + if (newline_indent == NULL) { _PyUnicodeWriter_Dealloc(&writer); return NULL; } } - if (encoder_listencode_obj(self, &writer, obj, current_newline_indent)) { + if (encoder_listencode_obj(self, &writer, obj, newline_indent)) { _PyUnicodeWriter_Dealloc(&writer); - Py_XDECREF(current_newline_indent); + Py_XDECREF(newline_indent); return NULL; } - Py_XDECREF(current_newline_indent); + Py_XDECREF(newline_indent); result = PyTuple_New(1); if (result == NULL || @@ -1388,7 +1387,7 @@ _steal_accumulate(_PyUnicodeWriter *writer, PyObject *stolen) static int encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, - PyObject *obj, PyObject *current_newline_indent) + PyObject *obj, PyObject *newline_indent) { /* Encode Python object obj to a JSON term */ PyObject *newobj; @@ -1424,14 +1423,14 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, else if (PyList_Check(obj) || PyTuple_Check(obj)) { if (_Py_EnterRecursiveCall(" while encoding a JSON object")) return -1; - rv = encoder_listencode_list(s, writer, obj, current_newline_indent); + rv = encoder_listencode_list(s, writer, obj, newline_indent); _Py_LeaveRecursiveCall(); return rv; } else if (PyDict_Check(obj)) { if (_Py_EnterRecursiveCall(" while encoding a JSON object")) return -1; - rv = encoder_listencode_dict(s, writer, obj, current_newline_indent); + rv = encoder_listencode_dict(s, writer, obj, newline_indent); _Py_LeaveRecursiveCall(); return rv; } @@ -1465,7 +1464,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyUnicodeWriter *writer, Py_XDECREF(ident); return -1; } - rv = encoder_listencode_obj(s, writer, newobj, current_newline_indent); + rv = encoder_listencode_obj(s, writer, newobj, newline_indent); _Py_LeaveRecursiveCall(); Py_DECREF(newobj); From 36e3313d21a325dfc83a7e1bdcc905810b3665cd Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 3 May 2024 17:18:37 +0200 Subject: [PATCH 21/24] Update Modules/_json.c Rewrite _creat_newline_indent using PyUnicode_AppendAndDel Co-authored-by: Serhiy Storchaka --- Modules/_json.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index 1858a4a0c5241d..96a45a12900cec 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1254,19 +1254,11 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) static PyObject * _create_newline_indent(PyObject *indent, Py_ssize_t indent_level) { - PyObject *current_indent = PySequence_Repeat(indent, indent_level); - if (current_indent == NULL) { - return NULL; - } - PyObject *start = PyUnicode_FromOrdinal('\n'); - if (start == NULL) { - Py_DECREF(current_indent); - return NULL; + PyObject *newline_indent = PyUnicode_FromOrdinal('\n'); + if (newline_indent != NULL && indent_level) { + PyUnicode_AppendAndDel(&newline_indent, + PySequence_Repeat(indent, indent_level)); } - - PyObject *newline_indent = PyUnicode_Concat(start, current_indent); - Py_DECREF(current_indent); - Py_DECREF(start); return newline_indent; } From e78ff6ae226128318887309f9b0e8ecc91f574e6 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 3 May 2024 18:01:30 +0000 Subject: [PATCH 22/24] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst b/Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst new file mode 100644 index 00000000000000..21da3252874a6a --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst @@ -0,0 +1 @@ +Improve performance of :func:`json.dumps` and :func:`json.dump` when using the argument `indent`. From 5c40126cc2ed22cc4c02680016e8045ae48fe0b2 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 3 May 2024 20:55:27 +0200 Subject: [PATCH 23/24] Update Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst Co-authored-by: Jelle Zijlstra --- .../2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst b/Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst index 21da3252874a6a..c5459b22303aed 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst @@ -1 +1 @@ -Improve performance of :func:`json.dumps` and :func:`json.dump` when using the argument `indent`. +Improve performance of :func:`json.dumps` and :func:`json.dump` when using the argument *indent*. From a43f2f27630d68302ef2c396514158af2b367bd0 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 3 May 2024 23:50:30 +0200 Subject: [PATCH 24/24] update news entry --- .../2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst b/Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst index c5459b22303aed..097a663e3f5e24 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-05-03-18-01-26.gh-issue-95382.73FSEv.rst @@ -1 +1,2 @@ -Improve performance of :func:`json.dumps` and :func:`json.dump` when using the argument *indent*. +Improve performance of :func:`json.dumps` and :func:`json.dump` when using the argument *indent*. Depending on the data the encoding using +:func:`json.dumps` with *indent* can be up to 2 to 3 times faster.