Skip to content

Commit 88f6acf

Browse files
committed
add const string multiply test
1 parent 1b1fcae commit 88f6acf

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Lib/test/test_compile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,9 @@ def with_fstring():
906906
907907
def with_const_expression():
908908
"also" + " not docstring"
909+
910+
def multiple_const_strings():
911+
"not docstring " * 3
909912
""")
910913

911914
for opt in [0, 1, 2]:
@@ -922,6 +925,7 @@ def with_const_expression():
922925
self.assertIsNone(ns['two_strings'].__doc__)
923926
self.assertIsNone(ns['with_fstring'].__doc__)
924927
self.assertIsNone(ns['with_const_expression'].__doc__)
928+
self.assertIsNone(ns['multiple_const_strings'].__doc__)
925929

926930
@support.cpython_only
927931
def test_docstring_interactive_mode(self):

Python/ast_opt.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,23 @@ static int astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimize
676676
static int
677677
astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state)
678678
{
679+
int docstring = _PyAST_GetDocString(stmts) != NULL;
679680
CALL_SEQ(astfold_stmt, stmt, stmts);
681+
if (!docstring && _PyAST_GetDocString(stmts) != NULL) {
682+
stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
683+
asdl_expr_seq *values = _Py_asdl_expr_seq_new(1, ctx_);
684+
if (!values) {
685+
return 0;
686+
}
687+
asdl_seq_SET(values, 0, st->v.Expr.value);
688+
expr_ty expr = _PyAST_JoinedStr(values, st->lineno, st->col_offset,
689+
st->end_lineno, st->end_col_offset,
690+
ctx_);
691+
if (!expr) {
692+
return 0;
693+
}
694+
st->v.Expr.value = expr;
695+
}
680696
return 1;
681697
}
682698

0 commit comments

Comments
 (0)