https://github.com/python/cpython/commit/853dafe23a138459be544065251d0150df680a2c
commit: 853dafe23a138459be544065251d0150df680a2c
branch: main
author: Donghee Na <[email protected]>
committer: corona10 <[email protected]>
date: 2026-04-05T00:40:12+09:00
summary:

gh-148083: Prevent constant folding when lhs is container types (gh-148090)

files:
M Include/internal/pycore_optimizer.h
M Python/optimizer_analysis.c
M Python/optimizer_bytecodes.c
M Python/optimizer_cases.c.h
M Python/optimizer_symbols.c

diff --git a/Include/internal/pycore_optimizer.h 
b/Include/internal/pycore_optimizer.h
index 2986afb142b5d1..cf01c620476ff7 100644
--- a/Include/internal/pycore_optimizer.h
+++ b/Include/internal/pycore_optimizer.h
@@ -394,6 +394,7 @@ extern JitOptRef _Py_uop_sym_new_type(
 extern JitOptRef _Py_uop_sym_new_const(JitOptContext *ctx, PyObject 
*const_val);
 extern JitOptRef _Py_uop_sym_new_const_steal(JitOptContext *ctx, PyObject 
*const_val);
 bool _Py_uop_sym_is_safe_const(JitOptContext *ctx, JitOptRef sym);
+bool _Py_uop_sym_is_not_container(JitOptRef sym);
 _PyStackRef _Py_uop_sym_get_const_as_stackref(JitOptContext *ctx, JitOptRef 
sym);
 extern JitOptRef _Py_uop_sym_new_null(JitOptContext *ctx);
 extern bool _Py_uop_sym_has_type(JitOptRef sym);
diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c
index 2953311b392600..92e1c081d524db 100644
--- a/Python/optimizer_analysis.c
+++ b/Python/optimizer_analysis.c
@@ -252,6 +252,7 @@ add_op(JitOptContext *ctx, _PyUOpInstruction *this_instr,
 #define sym_is_not_null _Py_uop_sym_is_not_null
 #define sym_is_const _Py_uop_sym_is_const
 #define sym_is_safe_const _Py_uop_sym_is_safe_const
+#define sym_is_not_container _Py_uop_sym_is_not_container
 #define sym_get_const _Py_uop_sym_get_const
 #define sym_new_const_steal _Py_uop_sym_new_const_steal
 #define sym_get_const_as_stackref _Py_uop_sym_get_const_as_stackref
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index 6e9a34384ba531..f2645553513f3d 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -515,7 +515,8 @@ dummy_func(void) {
         res = sym_new_not_null(ctx);
         ds = dict_st;
         ss = sub_st;
-        if (sym_matches_type(dict_st, &PyFrozenDict_Type)) {
+        if (sym_is_not_container(sub_st) &&
+            sym_matches_type(dict_st, &PyFrozenDict_Type)) {
             REPLACE_OPCODE_IF_EVALUATES_PURE(dict_st, sub_st, res);
         }
     }
@@ -706,7 +707,8 @@ dummy_func(void) {
         b = sym_new_type(ctx, &PyBool_Type);
         l = left;
         r = right;
-        if (sym_matches_type(right, &PyFrozenSet_Type)) {
+        if (sym_is_not_container(left) &&
+            sym_matches_type(right, &PyFrozenSet_Type)) {
             REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, b);
         }
     }
diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h
index dc00b6bc1397f5..fb3ec39a42eabc 100644
--- a/Python/optimizer_cases.c.h
+++ b/Python/optimizer_cases.c.h
@@ -1462,7 +1462,8 @@
             res = sym_new_not_null(ctx);
             ds = dict_st;
             ss = sub_st;
-            if (sym_matches_type(dict_st, &PyFrozenDict_Type)) {
+            if (sym_is_not_container(sub_st) &&
+                sym_matches_type(dict_st, &PyFrozenDict_Type)) {
                 if (
                     sym_is_safe_const(ctx, dict_st) &&
                     sym_is_safe_const(ctx, sub_st)
@@ -2993,7 +2994,8 @@
             b = sym_new_type(ctx, &PyBool_Type);
             l = left;
             r = right;
-            if (sym_matches_type(right, &PyFrozenSet_Type)) {
+            if (sym_is_not_container(left) &&
+                sym_matches_type(right, &PyFrozenSet_Type)) {
                 if (
                     sym_is_safe_const(ctx, left) &&
                     sym_is_safe_const(ctx, right)
diff --git a/Python/optimizer_symbols.c b/Python/optimizer_symbols.c
index 2614bcd430a2c5..6230b8948697e2 100644
--- a/Python/optimizer_symbols.c
+++ b/Python/optimizer_symbols.c
@@ -287,6 +287,20 @@ _Py_uop_sym_is_safe_const(JitOptContext *ctx, JitOptRef 
sym)
            (typ == &PyFrozenSet_Type);
 }
 
+bool
+_Py_uop_sym_is_not_container(JitOptRef sym)
+{
+    PyTypeObject *typ = _Py_uop_sym_get_type(sym);
+    if (typ == NULL) {
+        return false;
+    }
+    return (typ == &PyLong_Type) ||
+           (typ == &PyFloat_Type) ||
+           (typ == &PyUnicode_Type) ||
+           (typ == &_PyNone_Type) ||
+           (typ == &PyBool_Type);
+}
+
 void
 _Py_uop_sym_set_type(JitOptContext *ctx, JitOptRef ref, PyTypeObject *typ)
 {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to