@@ -1454,14 +1454,20 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
1454
1454
1455
1455
if (bytes_found ) {
1456
1456
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 ++ ) {
1458
1464
expr_ty elem = asdl_seq_GET (strings , i );
1459
1465
PyBytes_Concat (& res , elem -> v .Constant .value );
1460
1466
}
1461
1467
if (_PyArena_AddPyObject (arena , res ) < 0 ) {
1462
1468
return NULL ;
1463
1469
}
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 );
1465
1471
}
1466
1472
1467
1473
if (!f_string_found && len == 1 ) {
@@ -1519,6 +1525,13 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
1519
1525
&& asdl_seq_GET (flattened , i + 1 )-> kind == Constant_kind ) {
1520
1526
expr_ty first_elem = elem ;
1521
1527
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
+
1522
1535
_PyUnicodeWriter_Init (& writer );
1523
1536
expr_ty last_elem = elem ;
1524
1537
for (j = i ; j < n_flattened_elements ; j ++ ) {
@@ -1541,7 +1554,7 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
1541
1554
return NULL ;
1542
1555
}
1543
1556
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 ,
1545
1558
last_elem -> end_lineno , last_elem -> end_col_offset , p -> arena );
1546
1559
if (elem == NULL ) {
1547
1560
Py_DECREF (concat_str );
0 commit comments