Skip to content

Commit 6e63c47

Browse files
authored
gh-119182: Use public PyUnicodeWriter in _json.c (#129249)
Replace the private _PyUnicodeWriter API with the public PyUnicodeWriter API.
1 parent c005ea4 commit 6e63c47

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

Modules/_json.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,13 @@ _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
353353
return tpl;
354354
}
355355

356+
static inline int
357+
_PyUnicodeWriter_IsEmpty(PyUnicodeWriter *writer_pub)
358+
{
359+
_PyUnicodeWriter *writer = (_PyUnicodeWriter*)writer_pub;
360+
return (writer->pos == 0);
361+
}
362+
356363
static PyObject *
357364
scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
358365
{
@@ -371,9 +378,10 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
371378
const void *buf;
372379
int kind;
373380

374-
_PyUnicodeWriter writer;
375-
_PyUnicodeWriter_Init(&writer);
376-
writer.overallocate = 1;
381+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
382+
if (writer == NULL) {
383+
goto bail;
384+
}
377385

378386
len = PyUnicode_GET_LENGTH(pystr);
379387
buf = PyUnicode_DATA(pystr);
@@ -404,7 +412,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
404412

405413
if (c == '"') {
406414
// Fast path for simple case.
407-
if (writer.buffer == NULL) {
415+
if (_PyUnicodeWriter_IsEmpty(writer)) {
408416
PyObject *ret = PyUnicode_Substring(pystr, end, next);
409417
if (ret == NULL) {
410418
goto bail;
@@ -420,7 +428,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
420428

421429
/* Pick up this chunk if it's not zero length */
422430
if (next != end) {
423-
if (_PyUnicodeWriter_WriteSubstring(&writer, pystr, end, next) < 0) {
431+
if (PyUnicodeWriter_WriteSubstring(writer, pystr, end, next) < 0) {
424432
goto bail;
425433
}
426434
}
@@ -511,18 +519,18 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
511519
end -= 6;
512520
}
513521
}
514-
if (_PyUnicodeWriter_WriteChar(&writer, c) < 0) {
522+
if (PyUnicodeWriter_WriteChar(writer, c) < 0) {
515523
goto bail;
516524
}
517525
}
518526

519-
rval = _PyUnicodeWriter_Finish(&writer);
527+
rval = PyUnicodeWriter_Finish(writer);
520528
*next_end_ptr = end;
521529
return rval;
522530

523531
bail:
524532
*next_end_ptr = -1;
525-
_PyUnicodeWriter_Dealloc(&writer);
533+
PyUnicodeWriter_Discard(writer);
526534
return NULL;
527535
}
528536

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy