Skip to content

Commit e8b6366

Browse files
authored
Merge pull request python#57 from isidentical/handle-constant-kinds
2 parents 30ea09f + 26e39c2 commit e8b6366

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Parser/action_helpers.c

+16-3
Original file line numberDiff line numberDiff line change
@@ -1454,14 +1454,20 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
14541454

14551455
if (bytes_found) {
14561456
PyObject* res = PyBytes_FromString("");
1457-
for (i = 0; i < len; i++) {
1457+
1458+
/* Bytes literals never get a kind, but just for consistency
1459+
since they are represented as Constant nodes, we'll mirror
1460+
the same behavior as unicode strings for determining the
1461+
kind. */
1462+
PyObject* kind = asdl_seq_GET(strings, 0)->v.Constant.kind;
1463+
for (i = 0; i < len; i++) {
14581464
expr_ty elem = asdl_seq_GET(strings, i);
14591465
PyBytes_Concat(&res, elem->v.Constant.value);
14601466
}
14611467
if (_PyArena_AddPyObject(arena, res) < 0) {
14621468
return NULL;
14631469
}
1464-
return _PyAST_Constant(res, NULL, lineno, col_offset, end_lineno, end_col_offset, p->arena);
1470+
return _PyAST_Constant(res, kind, lineno, col_offset, end_lineno, end_col_offset, p->arena);
14651471
}
14661472

14671473
if (!f_string_found && len == 1) {
@@ -1519,6 +1525,13 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
15191525
&& asdl_seq_GET(flattened, i+1)->kind == Constant_kind) {
15201526
expr_ty first_elem = elem;
15211527

1528+
/* When a string is getting concatenated, the kind of the string
1529+
is determined by the first string in the concatenation sequence.
1530+
1531+
u"abc" "def" -> u"abcdef"
1532+
"abc" u"abc" -> "abcabc" */
1533+
PyObject *kind = elem->v.Constant.kind;
1534+
15221535
_PyUnicodeWriter_Init(&writer);
15231536
expr_ty last_elem = elem;
15241537
for (j = i; j < n_flattened_elements; j++) {
@@ -1541,7 +1554,7 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
15411554
return NULL;
15421555
}
15431556

1544-
elem = _PyAST_Constant(concat_str, NULL, first_elem->lineno, first_elem->col_offset,
1557+
elem = _PyAST_Constant(concat_str, kind, first_elem->lineno, first_elem->col_offset,
15451558
last_elem->end_lineno, last_elem->end_col_offset, p->arena);
15461559
if (elem == NULL) {
15471560
Py_DECREF(concat_str);

0 commit comments

Comments
 (0)