Skip to content

Commit 2194f4f

Browse files
committed
add test for const string multiply
1 parent c4d0a7b commit 2194f4f

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Lib/test/test_compile.py

Lines changed: 5 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 with_multiplied_string():
911+
"not docstring" * 4
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['with_multiplied_string'].__doc__)
925929

926930
# Run AST parse first, then compile and run it
927931
for opt in [0, 1, 2]:
@@ -940,6 +944,7 @@ def with_const_expression():
940944
self.assertIsNone(ns['two_strings'].__doc__)
941945
self.assertIsNone(ns['with_fstring'].__doc__)
942946
self.assertIsNone(ns['with_const_expression'].__doc__)
947+
self.assertIsNone(ns['with_multiplied_string'].__doc__)
943948

944949
@support.cpython_only
945950
def test_docstring_interactive_mode(self):

Python/ast_opt.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,12 @@ fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
500500
newval = PyNumber_Subtract(lv, rv);
501501
break;
502502
case Mult:
503+
if ((state->optimize == 1) && (
504+
(PyUnicode_CheckExact(lv) && PyLong_CheckExact(rv)) ||
505+
(PyUnicode_CheckExact(rv) && PyLong_Check(lv))))
506+
{
507+
return 1;
508+
}
503509
newval = safe_multiply(lv, rv);
504510
break;
505511
case Div:

0 commit comments

Comments
 (0)