https://github.com/python/cpython/commit/6e63c4736beebdf912acd391fc437672ee9d362e
commit: 6e63c4736beebdf912acd391fc437672ee9d362e
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2025-01-24T11:38:13+01:00
summary:

gh-119182: Use public PyUnicodeWriter in _json.c (#129249)

Replace the private _PyUnicodeWriter API with the public
PyUnicodeWriter API.

files:
M Modules/_json.c

diff --git a/Modules/_json.c b/Modules/_json.c
index a99abbe72bf7a0..091bcbfdced42b 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -353,6 +353,13 @@ _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)
 {
@@ -371,9 +378,10 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int 
strict, Py_ssize_t *next
     const void *buf;
     int kind;
 
-    _PyUnicodeWriter writer;
-    _PyUnicodeWriter_Init(&writer);
-    writer.overallocate = 1;
+    PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
+    if (writer == NULL) {
+        goto bail;
+    }
 
     len = PyUnicode_GET_LENGTH(pystr);
     buf = PyUnicode_DATA(pystr);
@@ -404,7 +412,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int 
strict, Py_ssize_t *next
 
         if (c == '"') {
             // Fast path for simple case.
-            if (writer.buffer == NULL) {
+            if (_PyUnicodeWriter_IsEmpty(writer)) {
                 PyObject *ret = PyUnicode_Substring(pystr, end, next);
                 if (ret == NULL) {
                     goto bail;
@@ -420,7 +428,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int 
strict, Py_ssize_t *next
 
         /* Pick up this chunk if it's not zero length */
         if (next != end) {
-            if (_PyUnicodeWriter_WriteSubstring(&writer, pystr, end, next) < 
0) {
+            if (PyUnicodeWriter_WriteSubstring(writer, pystr, end, next) < 0) {
                 goto bail;
             }
         }
@@ -511,18 +519,18 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int 
strict, Py_ssize_t *next
                     end -= 6;
             }
         }
-        if (_PyUnicodeWriter_WriteChar(&writer, c) < 0) {
+        if (PyUnicodeWriter_WriteChar(writer, c) < 0) {
             goto bail;
         }
     }
 
-    rval = _PyUnicodeWriter_Finish(&writer);
+    rval = PyUnicodeWriter_Finish(writer);
     *next_end_ptr = end;
     return rval;
 
 bail:
     *next_end_ptr = -1;
-    _PyUnicodeWriter_Dealloc(&writer);
+    PyUnicodeWriter_Discard(writer);
     return NULL;
 }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to