|
19 | 19 | from mypy.nodes import ( |
20 | 20 | ARG_NAMED, |
21 | 21 | ARG_POS, |
22 | | - BytesExpr, |
23 | 22 | CallExpr, |
24 | 23 | DictExpr, |
25 | 24 | Expression, |
|
78 | 77 | uint8_rprimitive, |
79 | 78 | ) |
80 | 79 | from mypyc.irbuild.builder import IRBuilder |
| 80 | +from mypyc.irbuild.constant_fold import constant_fold_expr |
81 | 81 | from mypyc.irbuild.for_helpers import ( |
82 | 82 | comprehension_helper, |
83 | 83 | sequence_from_generator_preallocate_helper, |
@@ -716,21 +716,18 @@ def translate_dict_setdefault(builder: IRBuilder, expr: CallExpr, callee: RefExp |
716 | 716 |
|
717 | 717 | @specialize_function("format", str_rprimitive) |
718 | 718 | 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) |
| 719 | + if isinstance(callee, MemberExpr): |
| 720 | + folded_callee = constant_fold_expr(builder, callee.expr) |
| 721 | + if isinstance(folded_callee, str) and expr.arg_kinds.count(ARG_POS) == len(expr.arg_kinds): |
| 722 | + tokens = tokenizer_format_call(folded_callee) |
| 723 | + if tokens is None: |
| 724 | + return None |
| 725 | + literals, format_ops = tokens |
| 726 | + # Convert variables to strings |
| 727 | + substitutions = convert_format_expr_to_str(builder, format_ops, expr.args, expr.line) |
| 728 | + if substitutions is None: |
| 729 | + return None |
| 730 | + return join_formatted_strings(builder, literals, substitutions, expr.line) |
734 | 731 | return None |
735 | 732 |
|
736 | 733 |
|
@@ -1059,9 +1056,9 @@ def translate_float(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Valu |
1059 | 1056 | def translate_ord(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None: |
1060 | 1057 | if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS: |
1061 | 1058 | return None |
1062 | | - arg = expr.args[0] |
1063 | | - if isinstance(arg, (StrExpr, BytesExpr)) and len(arg.value) == 1: |
1064 | | - return Integer(ord(arg.value)) |
| 1059 | + arg = constant_fold_expr(builder, expr.args[0]) |
| 1060 | + if isinstance(arg, (str, bytes)) and len(arg) == 1: |
| 1061 | + return Integer(ord(arg)) |
1065 | 1062 | return None |
1066 | 1063 |
|
1067 | 1064 |
|
|
0 commit comments