|
78 | 78 | uint8_rprimitive, |
79 | 79 | ) |
80 | 80 | from mypyc.irbuild.builder import IRBuilder |
| 81 | +from mypyc.irbuild.constant_fold import constant_fold_expr |
81 | 82 | from mypyc.irbuild.for_helpers import ( |
82 | 83 | comprehension_helper, |
83 | 84 | sequence_from_generator_preallocate_helper, |
@@ -716,21 +717,18 @@ def translate_dict_setdefault(builder: IRBuilder, expr: CallExpr, callee: RefExp |
716 | 717 |
|
717 | 718 | @specialize_function("format", str_rprimitive) |
718 | 719 | def translate_str_format(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None: |
719 | | - if ( |
720 | | - isinstance(callee, MemberExpr) |
721 | | - and isinstance(callee.expr, StrExpr) |
722 | | - and expr.arg_kinds.count(ARG_POS) == len(expr.arg_kinds) |
723 | | - ): |
724 | | - format_str = callee.expr.value |
725 | | - tokens = tokenizer_format_call(format_str) |
726 | | - if tokens is None: |
727 | | - return None |
728 | | - literals, format_ops = tokens |
729 | | - # Convert variables to strings |
730 | | - substitutions = convert_format_expr_to_str(builder, format_ops, expr.args, expr.line) |
731 | | - if substitutions is None: |
732 | | - return None |
733 | | - return join_formatted_strings(builder, literals, substitutions, expr.line) |
| 720 | + if isinstance(callee, MemberExpr): |
| 721 | + folded_callee = constant_fold_expr(builder, callee.expr) |
| 722 | + if isinstance(folded_callee, str) and expr.arg_kinds.count(ARG_POS) == len(expr.arg_kinds): |
| 723 | + tokens = tokenizer_format_call(folded_callee) |
| 724 | + if tokens is None: |
| 725 | + return None |
| 726 | + literals, format_ops = tokens |
| 727 | + # Convert variables to strings |
| 728 | + substitutions = convert_format_expr_to_str(builder, format_ops, expr.args, expr.line) |
| 729 | + if substitutions is None: |
| 730 | + return None |
| 731 | + return join_formatted_strings(builder, literals, substitutions, expr.line) |
734 | 732 | return None |
735 | 733 |
|
736 | 734 |
|
|
0 commit comments