Skip to content

Commit 48267a3

Browse files
committed
Switch to PyUnicodeWriter
1 parent c066338 commit 48267a3

2 files changed

Lines changed: 28 additions & 40 deletions

File tree

Parser/asdl_c.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ def visitModule(self, mod):
877877
* Format the names in the set 'missing' into a natural language list,
878878
* sorted in the order in which they appear in 'fields'.
879879
*
880-
* Similar to format_missing from 'Python/ceval.c'.
880+
* Similar to format_missing() from 'Python/ceval.c'.
881881
*
882882
* Parameters
883883
*
@@ -893,20 +893,21 @@ def visitModule(self, mod):
893893
return NULL;
894894
}
895895
num_total = num_left = PySet_GET_SIZE(missing);
896-
PyObject *name_str = PyUnicode_FromString("");
896+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
897+
if (writer == NULL) {
898+
goto error;
899+
}
897900
// Iterate all AST node fields in order so that the missing positional
898901
// arguments are rendered in the order in which __init__ expects them.
899902
for (Py_ssize_t i = 0; i < num_fields; i++) {
900903
PyObject *name = PySequence_GetItem(fields, i);
901-
if (!name) {
902-
Py_DECREF(name_str);
903-
return NULL;
904+
if (name == NULL) {
905+
goto error;
904906
}
905907
int contains = PySet_Contains(missing, name);
906908
if (contains == -1) {
907-
Py_DECREF(name_str);
908909
Py_DECREF(name);
909-
return NULL;
910+
goto error;
910911
}
911912
else if (contains == 1) {
912913
const char* fmt = NULL;
@@ -923,24 +924,17 @@ def visitModule(self, mod):
923924
fmt = "'%U', ";
924925
}
925926
num_left--;
926-
PyObject *tail = PyUnicode_FromFormat(fmt, name);
927-
if (!tail) {
928-
Py_DECREF(name_str);
927+
if (PyUnicodeWriter_Format(writer, fmt, name) < 0) {
929928
Py_DECREF(name);
930-
return NULL;
931-
}
932-
PyObject *tmp = PyUnicode_Concat(name_str, tail);
933-
Py_DECREF(name_str);
934-
Py_DECREF(tail);
935-
if (!tmp) {
936-
Py_DECREF(name);
937-
return NULL;
929+
goto error;
938930
}
939-
name_str = tmp;
940931
}
941932
Py_DECREF(name);
942933
}
943-
return name_str;
934+
return PyUnicodeWriter_Finish(writer);
935+
error:
936+
PyUnicodeWriter_Discard(writer);
937+
return NULL;
944938
}
945939
946940
static int

Python/Python-ast.c

Lines changed: 14 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)