https://github.com/python/cpython/commit/49918f5b0ceb1950c3222fd4fd6be872d2e15c6f
commit: 49918f5b0ceb1950c3222fd4fd6be872d2e15c6f
branch: main
author: Neko Asakura <[email protected]>
committer: savannahostrowski <[email protected]>
date: 2026-05-07T23:02:04Z
summary:

gh-149481: skip `FOR_ITER` inline specialization for Python `__next__` (#149491)

Co-authored-by: Savannah Ostrowski <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>

files:
M Include/internal/pycore_typeobject.h
M Lib/test/test_capi/test_opt.py
M Objects/typeobject.c
M Python/optimizer_bytecodes.c
M Python/optimizer_cases.c.h

diff --git a/Include/internal/pycore_typeobject.h 
b/Include/internal/pycore_typeobject.h
index 8d48cf6605ca7e..785b77d3e3be81 100644
--- a/Include/internal/pycore_typeobject.h
+++ b/Include/internal/pycore_typeobject.h
@@ -122,6 +122,8 @@ extern PyObject* _Py_BaseObject_RichCompare(PyObject* self, 
PyObject* other, int
 extern PyObject* _Py_slot_tp_getattro(PyObject *self, PyObject *name);
 extern PyObject* _Py_slot_tp_getattr_hook(PyObject *self, PyObject *name);
 
+extern int _PyType_HasSlotTpIternext(PyTypeObject *type);
+
 extern PyTypeObject _PyBufferWrapper_Type;
 
 PyAPI_FUNC(PyObject*) _PySuper_Lookup(PyTypeObject *su_type, PyObject *su_obj,
diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py
index d80fec9a8a0d2b..aaa5050208ced9 100644
--- a/Lib/test/test_capi/test_opt.py
+++ b/Lib/test/test_capi/test_opt.py
@@ -598,7 +598,8 @@ def testfunc(n, m):
         ex = get_first_executor(testfunc)
         self.assertIsNotNone(ex)
         uops = get_opnames(ex)
-        self.assertIn("_ITER_NEXT_INLINE", uops)
+        self.assertIn("_FOR_ITER_TIER_TWO", uops)
+        self.assertNotIn("_ITER_NEXT_INLINE", uops)
 
 
 @requires_specialization
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 4f43747ba83fd9..9a18ca72516da7 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -11079,6 +11079,12 @@ slot_tp_iternext(PyObject *self)
     return vectorcall_method(&_Py_ID(__next__), stack, 1);
 }
 
+int
+_PyType_HasSlotTpIternext(PyTypeObject *type)
+{
+    return type->tp_iternext == slot_tp_iternext;
+}
+
 static PyObject *
 slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
 {
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index e10a096baa3318..39cc36ae79fead 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -2,6 +2,7 @@
 #include "pycore_long.h"
 #include "pycore_opcode_utils.h"
 #include "pycore_optimizer.h"
+#include "pycore_typeobject.h"
 #include "pycore_uops.h"
 #include "pycore_uop_ids.h"
 #include "internal/pycore_moduleobject.h"
@@ -1459,7 +1460,8 @@ dummy_func(void) {
             type = sym_get_probable_type(iter);
             definite = false;
         }
-        if (type != NULL && type != &PyGen_Type && type->tp_iternext != NULL) {
+        if (type != NULL && type != &PyGen_Type && type->tp_iternext != NULL
+            && !_PyType_HasSlotTpIternext(type)) {
             PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
             _Py_BloomFilter_Add(dependencies, type);
             if (!definite) {
diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h
index 01ecb3790aa2cd..db3dcbb97b2645 100644
--- a/Python/optimizer_cases.c.h
+++ b/Python/optimizer_cases.c.h
@@ -3706,7 +3706,8 @@
                 type = sym_get_probable_type(iter);
                 definite = false;
             }
-            if (type != NULL && type != &PyGen_Type && type->tp_iternext != 
NULL) {
+            if (type != NULL && type != &PyGen_Type && type->tp_iternext != 
NULL
+                && !_PyType_HasSlotTpIternext(type)) {
                 PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
                 _Py_BloomFilter_Add(dependencies, type);
                 if (!definite) {

_______________________________________________
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