summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2024-10-12 07:28:34 (GMT)
committerGitHub <noreply@github.com>2024-10-12 07:28:34 (GMT)
commit4a943c3251d1b3fdf50cfb9264ae74e5bc845c3c (patch)
treed9ef975abd23c6905d02555570b530da387ee096
parent5d8739e956cd20d3860133b384518a3c5c74e5ae (diff)
downloadcpython-4a943c3251d1b3fdf50cfb9264ae74e5bc845c3c.zip
cpython-4a943c3251d1b3fdf50cfb9264ae74e5bc845c3c.tar.gz
cpython-4a943c3251d1b3fdf50cfb9264ae74e5bc845c3c.tar.bz2
gh-125196: Use PyUnicodeWriter in parser (#125271)
Replace the private _PyUnicodeWriter API with the public PyUnicodeWriter API in _PyPegen_concatenate_strings().
-rw-r--r--Parser/action_helpers.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/Parser/action_helpers.c b/Parser/action_helpers.c
index 24b817c..cb21777 100644
--- a/Parser/action_helpers.c
+++ b/Parser/action_helpers.c
@@ -1615,7 +1615,6 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
}
/* build folded list */
- _PyUnicodeWriter writer;
current_pos = 0;
for (i = 0; i < n_flattened_elements; i++) {
expr_ty elem = asdl_seq_GET(flattened, i);
@@ -1635,14 +1634,17 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
"abc" u"abc" -> "abcabc" */
PyObject *kind = elem->v.Constant.kind;
- _PyUnicodeWriter_Init(&writer);
+ PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
+ if (writer == NULL) {
+ return NULL;
+ }
expr_ty last_elem = elem;
for (j = i; j < n_flattened_elements; j++) {
expr_ty current_elem = asdl_seq_GET(flattened, j);
if (current_elem->kind == Constant_kind) {
- if (_PyUnicodeWriter_WriteStr(
- &writer, current_elem->v.Constant.value)) {
- _PyUnicodeWriter_Dealloc(&writer);
+ if (PyUnicodeWriter_WriteStr(writer,
+ current_elem->v.Constant.value)) {
+ PyUnicodeWriter_Discard(writer);
return NULL;
}
last_elem = current_elem;
@@ -1652,9 +1654,8 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
}
i = j - 1;
- PyObject *concat_str = _PyUnicodeWriter_Finish(&writer);
+ PyObject *concat_str = PyUnicodeWriter_Finish(writer);
if (concat_str == NULL) {
- _PyUnicodeWriter_Dealloc(&writer);
return NULL;
}
if (_PyArena_AddPyObject(p->arena, concat_str) < 0) {