Skip to content

Commit ef1cd5c

Browse files
committed
Clean up ast_unparse.c append_tstring()
1 parent 3ebdd97 commit ef1cd5c

File tree

1 file changed

+3
-54
lines changed

1 file changed

+3
-54
lines changed

Python/ast_unparse.c

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -700,62 +700,11 @@ append_templatestr(PyUnicodeWriter *writer, expr_ty e)
700700
return -1;
701701
}
702702

703-
Py_ssize_t last_idx = 0;
704703
Py_ssize_t len = asdl_seq_LEN(e->v.TemplateStr.values);
705-
if (len == 0) {
706-
int result = _write_values_subarray(writer, e->v.TemplateStr.values,
707-
0, len - 1, 't', arena);
708-
_PyArena_Free(arena);
709-
return result;
710-
}
711-
712-
for (Py_ssize_t i = 0; i < len; i++) {
713-
expr_ty value = asdl_seq_GET(e->v.TemplateStr.values, i);
714-
715-
// Handle implicit concat of t-strings with f-strings
716-
if (value->kind == FormattedValue_kind) {
717-
if (i > last_idx) {
718-
// Create a new TemplateStr with the values between last_idx and i
719-
// and append it to the writer.
720-
if (_write_values_subarray(writer, e->v.TemplateStr.values,
721-
last_idx, i - 1, 't', arena) == -1) {
722-
goto error;
723-
}
724-
725-
if (append_charp(writer, " ") == -1) {
726-
goto error;
727-
}
728-
}
729-
730-
// Append the FormattedValue to the writer.
731-
if (_write_values_subarray(writer, e->v.TemplateStr.values,
732-
i, i, 'f', arena) == -1) {
733-
goto error;
734-
}
735-
736-
if (i + 1 < len) {
737-
if (append_charp(writer, " ") == -1) {
738-
goto error;
739-
}
740-
}
741-
742-
last_idx = i + 1;
743-
}
744-
}
745-
746-
if (last_idx < len) {
747-
if (_write_values_subarray(writer, e->v.TemplateStr.values,
748-
last_idx, len - 1, 't', arena) == -1) {
749-
goto error;
750-
}
751-
}
752-
_PyArena_Free(arena);
753-
754-
return 0;
755-
756-
error:
704+
int result = _write_values_subarray(writer, e->v.TemplateStr.values,
705+
0, len - 1, 't', arena);
757706
_PyArena_Free(arena);
758-
return -1;
707+
return result;
759708
}
760709

761710
static int

0 commit comments

Comments
 (0)