mirror of
https://github.com/python/cpython.git
synced 2025-11-01 14:11:41 +00:00
Co-authored-by: Dave Peck <davepeck@gmail.com> Co-authored-by: sobolevn <mail@sobolevn.me>
This commit is contained in:
parent
0d87bb66d3
commit
af8d1f56f5
13 changed files with 2098 additions and 2134 deletions
|
|
@ -663,27 +663,15 @@ build_ftstring_body(asdl_expr_seq *values, bool is_format_spec)
|
|||
}
|
||||
|
||||
static int
|
||||
_write_values_subarray(PyUnicodeWriter *writer, asdl_expr_seq *values, Py_ssize_t first_idx,
|
||||
Py_ssize_t last_idx, char prefix, PyArena *arena)
|
||||
append_templatestr(PyUnicodeWriter *writer, expr_ty e)
|
||||
{
|
||||
int result = -1;
|
||||
|
||||
asdl_expr_seq *new_values = _Py_asdl_expr_seq_new(last_idx - first_idx + 1, arena);
|
||||
if (!new_values) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Py_ssize_t j = 0;
|
||||
for (Py_ssize_t i = first_idx; i <= last_idx; ++i) {
|
||||
asdl_seq_SET(new_values, j++, asdl_seq_GET(values, i));
|
||||
}
|
||||
|
||||
PyObject *body = build_ftstring_body(new_values, false);
|
||||
PyObject *body = build_ftstring_body(e->v.TemplateStr.values, false);
|
||||
if (!body) {
|
||||
return result;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (-1 != append_char(writer, prefix) &&
|
||||
if (-1 != append_charp(writer, "t") &&
|
||||
-1 != append_repr(writer, body))
|
||||
{
|
||||
result = 0;
|
||||
|
|
@ -692,72 +680,6 @@ _write_values_subarray(PyUnicodeWriter *writer, asdl_expr_seq *values, Py_ssize_
|
|||
return result;
|
||||
}
|
||||
|
||||
static int
|
||||
append_templatestr(PyUnicodeWriter *writer, expr_ty e)
|
||||
{
|
||||
PyArena *arena = _PyArena_New();
|
||||
if (!arena) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
Py_ssize_t last_idx = 0;
|
||||
Py_ssize_t len = asdl_seq_LEN(e->v.TemplateStr.values);
|
||||
if (len == 0) {
|
||||
int result = _write_values_subarray(writer, e->v.TemplateStr.values,
|
||||
0, len - 1, 't', arena);
|
||||
_PyArena_Free(arena);
|
||||
return result;
|
||||
}
|
||||
|
||||
for (Py_ssize_t i = 0; i < len; i++) {
|
||||
expr_ty value = asdl_seq_GET(e->v.TemplateStr.values, i);
|
||||
|
||||
// Handle implicit concat of t-strings with f-strings
|
||||
if (value->kind == FormattedValue_kind) {
|
||||
if (i > last_idx) {
|
||||
// Create a new TemplateStr with the values between last_idx and i
|
||||
// and append it to the writer.
|
||||
if (_write_values_subarray(writer, e->v.TemplateStr.values,
|
||||
last_idx, i - 1, 't', arena) == -1) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (append_charp(writer, " ") == -1) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
// Append the FormattedValue to the writer.
|
||||
if (_write_values_subarray(writer, e->v.TemplateStr.values,
|
||||
i, i, 'f', arena) == -1) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (i + 1 < len) {
|
||||
if (append_charp(writer, " ") == -1) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
last_idx = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (last_idx < len) {
|
||||
if (_write_values_subarray(writer, e->v.TemplateStr.values,
|
||||
last_idx, len - 1, 't', arena) == -1) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
_PyArena_Free(arena);
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
_PyArena_Free(arena);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
append_joinedstr(PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4070,16 +4070,6 @@ codegen_template_str(compiler *c, expr_ty e)
|
|||
}
|
||||
else {
|
||||
VISIT(c, expr, value);
|
||||
Py_ssize_t j;
|
||||
for (j = i + 1; j < value_count; j++) {
|
||||
value = asdl_seq_GET(e->v.TemplateStr.values, j);
|
||||
if (value->kind == Interpolation_kind) {
|
||||
break;
|
||||
}
|
||||
VISIT(c, expr, value);
|
||||
ADDOP_INPLACE(c, loc, Add);
|
||||
}
|
||||
i = j - 1;
|
||||
stringslen++;
|
||||
last_was_interpolation = 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue