From 9939e510f17a8834180afa9fd9dc176c37dd7b30 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Wed, 1 Oct 2025 23:59:45 +0000 Subject: [PATCH 1/4] [mypyc] feat: support constant folding in `translate_ord` --- mypyc/irbuild/specialize.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mypyc/irbuild/specialize.py b/mypyc/irbuild/specialize.py index 84807a7fdb53..fb83b26cb3bf 100644 --- a/mypyc/irbuild/specialize.py +++ b/mypyc/irbuild/specialize.py @@ -1060,8 +1060,9 @@ def translate_ord(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS: return None arg = expr.args[0] - if isinstance(arg, (StrExpr, BytesExpr)) and len(arg.value) == 1: - return Integer(ord(arg.value)) + folded_arg = constant_fold_expr(builder, arg) + if isinstance(folded_arg, (str, bytes)) and len(folded_arg) == 1: + return Integer(ord(folded_arg)) return None From 984ca5f4b70a1f0b27ec60a10af3a9fdf403e904 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 2 Oct 2025 00:05:25 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypyc/irbuild/specialize.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mypyc/irbuild/specialize.py b/mypyc/irbuild/specialize.py index fb83b26cb3bf..f353a7022e9e 100644 --- a/mypyc/irbuild/specialize.py +++ b/mypyc/irbuild/specialize.py @@ -19,7 +19,6 @@ from mypy.nodes import ( ARG_NAMED, ARG_POS, - BytesExpr, CallExpr, DictExpr, Expression, From d30e0fbea5ddaf1fed133f4bdbfc198be68bd543 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Wed, 1 Oct 2025 20:07:50 -0400 Subject: [PATCH 3/4] Update specialize.py --- mypyc/irbuild/specialize.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mypyc/irbuild/specialize.py b/mypyc/irbuild/specialize.py index f353a7022e9e..23d223884a0f 100644 --- a/mypyc/irbuild/specialize.py +++ b/mypyc/irbuild/specialize.py @@ -77,6 +77,7 @@ uint8_rprimitive, ) from mypyc.irbuild.builder import IRBuilder +from mypyc.irbuild.constant_fold import constant_fold_expr from mypyc.irbuild.for_helpers import ( comprehension_helper, sequence_from_generator_preallocate_helper, From 59c1826fd6a9f582a43395abec0f2821da4b3072 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Wed, 1 Oct 2025 20:35:30 -0400 Subject: [PATCH 4/4] Update specialize.py --- mypyc/irbuild/specialize.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mypyc/irbuild/specialize.py b/mypyc/irbuild/specialize.py index 23d223884a0f..d2ec181e8b65 100644 --- a/mypyc/irbuild/specialize.py +++ b/mypyc/irbuild/specialize.py @@ -1059,10 +1059,9 @@ def translate_float(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Valu def translate_ord(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None: if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS: return None - arg = expr.args[0] - folded_arg = constant_fold_expr(builder, arg) - if isinstance(folded_arg, (str, bytes)) and len(folded_arg) == 1: - return Integer(ord(folded_arg)) + arg = constant_fold_expr(builder, expr.args[0]) + if isinstance(arg, (str, bytes)) and len(arg) == 1: + return Integer(ord(arg)) return None