Skip to content

Commit c81446a

Browse files
authored
gh-133968: Create the Unicode writer on demand in json (#133832)
1 parent 379d0bc commit c81446a

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

Modules/_json.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,6 @@ _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
360360
return tpl;
361361
}
362362

363-
static inline int
364-
_PyUnicodeWriter_IsEmpty(PyUnicodeWriter *writer_pub)
365-
{
366-
_PyUnicodeWriter *writer = (_PyUnicodeWriter*)writer_pub;
367-
return (writer->pos == 0);
368-
}
369-
370363
static PyObject *
371364
scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
372365
{
@@ -385,10 +378,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
385378
const void *buf;
386379
int kind;
387380

388-
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
389-
if (writer == NULL) {
390-
goto bail;
391-
}
381+
PyUnicodeWriter *writer = NULL;
392382

393383
len = PyUnicode_GET_LENGTH(pystr);
394384
buf = PyUnicode_DATA(pystr);
@@ -419,19 +409,23 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
419409

420410
if (c == '"') {
421411
// Fast path for simple case.
422-
if (_PyUnicodeWriter_IsEmpty(writer)) {
412+
if (writer == NULL) {
423413
PyObject *ret = PyUnicode_Substring(pystr, end, next);
424414
if (ret == NULL) {
425415
goto bail;
426416
}
427-
PyUnicodeWriter_Discard(writer);
428417
*next_end_ptr = next + 1;;
429418
return ret;
430419
}
431420
}
432421
else if (c != '\\') {
433422
raise_errmsg("Unterminated string starting at", pystr, begin);
434423
goto bail;
424+
} else if (writer == NULL) {
425+
writer = PyUnicodeWriter_Create(0);
426+
if (writer == NULL) {
427+
goto bail;
428+
}
435429
}
436430

437431
/* Pick up this chunk if it's not zero length */

0 commit comments

Comments
 (0)