Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-133968: Use private unicode writer for json #133832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
Loading
from
Open
Changes from all commits
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
30 changes: 12 additions & 18 deletions 30 Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,6 @@ _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
return tpl;
}

static inline int
_PyUnicodeWriter_IsEmpty(PyUnicodeWriter *writer_pub)
{
_PyUnicodeWriter *writer = (_PyUnicodeWriter*)writer_pub;
return (writer->pos == 0);
}

static PyObject *
scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
{
Expand All @@ -385,10 +378,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
const void *buf;
int kind;

PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
if (writer == NULL) {
goto bail;
}
PyUnicodeWriter *writer = NULL;

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

if (c == '"') {
// Fast path for simple case.
if (_PyUnicodeWriter_IsEmpty(writer)) {
if (writer == NULL) {
PyObject *ret = PyUnicode_Substring(pystr, end, next);
if (ret == NULL) {
goto bail;
}
PyUnicodeWriter_Discard(writer);
*next_end_ptr = next + 1;;
return ret;
}
}
else if (c != '\\') {
raise_errmsg("Unterminated string starting at", pystr, begin);
goto bail;
} else if (writer == NULL) {
writer = PyUnicodeWriter_Create(0);
if (writer == NULL) {
goto bail;
}
}

/* Pick up this chunk if it's not zero length */
Expand Down Expand Up @@ -1476,13 +1470,13 @@ encoder_listencode_obj(PyEncoderObject *s, PyUnicodeWriter *writer,
int rv;

if (obj == Py_None) {
return PyUnicodeWriter_WriteUTF8(writer, "null", 4);
return _PyUnicodeWriter_WriteASCIIString((_PyUnicodeWriter *)writer, "null", 4);
}
else if (obj == Py_True) {
return PyUnicodeWriter_WriteUTF8(writer, "true", 4);
return _PyUnicodeWriter_WriteASCIIString((_PyUnicodeWriter *)writer, "true", 4);
}
else if (obj == Py_False) {
return PyUnicodeWriter_WriteUTF8(writer, "false", 5);
return _PyUnicodeWriter_WriteASCIIString((_PyUnicodeWriter *)writer, "false", 5);
}
else if (PyUnicode_Check(obj)) {
PyObject *encoded = encoder_encode_string(s, obj);
Expand Down Expand Up @@ -1649,7 +1643,7 @@ encoder_listencode_dict(PyEncoderObject *s, PyUnicodeWriter *writer,

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

if (s->markers != Py_None) {
Expand Down Expand Up @@ -1753,7 +1747,7 @@ encoder_listencode_list(PyEncoderObject *s, PyUnicodeWriter *writer,
return -1;
if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
Py_DECREF(s_fast);
return PyUnicodeWriter_WriteUTF8(writer, "[]", 2);
return _PyUnicodeWriter_WriteASCIIString((_PyUnicodeWriter *)writer, "[]", 2);
}

if (s->markers != Py_None) {
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.