@@ -1590,6 +1590,42 @@ def testfunc(n):
15901590 # __init__ resolution allows promotion of range to constant
15911591 self .assertNotIn ("_LOAD_GLOBAL_BUILTINS" , uops )
15921592
1593+ def test_init_guards_removed (self ):
1594+ class MyPoint :
1595+ def __init__ (self , x , y ):
1596+ return None
1597+
1598+ def testfunc (n ):
1599+ point_local = MyPoint
1600+ for _ in range (n ):
1601+ p = point_local (1.0 , 2.0 )
1602+ p = point_local (1.0 , 2.0 )
1603+
1604+ _ , ex = self ._run_with_optimizer (testfunc , TIER2_THRESHOLD )
1605+ self .assertIsNotNone (ex )
1606+ # The __init__ call should be traced through via _PUSH_FRAME
1607+ count = count_ops (ex , "_CREATE_INIT_FRAME" )
1608+ self .assertEqual (count , 2 )
1609+ # __init__ resolution allows promotion of range to constant
1610+ count = count_ops (ex , "_CHECK_OBJECT" )
1611+ self .assertEqual (count , 1 )
1612+
1613+ def test_init_guards_removed_global (self ):
1614+
1615+ def testfunc (n ):
1616+ for _ in range (n ):
1617+ p = MyGlobalPoint (1.0 , 2.0 )
1618+ p = MyGlobalPoint (1.0 , 2.0 )
1619+
1620+ _ , ex = self ._run_with_optimizer (testfunc , TIER2_THRESHOLD )
1621+ self .assertIsNotNone (ex )
1622+ # The __init__ call should be traced through via _PUSH_FRAME
1623+ count = count_ops (ex , "_CREATE_INIT_FRAME" )
1624+ self .assertEqual (count , 2 )
1625+ # __init__ resolution allows promotion of range to constant
1626+ count = count_ops (ex , "_CHECK_OBJECT" )
1627+ self .assertEqual (count , 0 )
1628+
15931629 def test_guard_type_version_locked_propagates (self ):
15941630 """
15951631 _GUARD_TYPE_VERSION_LOCKED should set the type version on the
@@ -5216,5 +5252,9 @@ def test(self, *args, **kwargs):
52165252test_object = TestObject ()
52175253test_bound_method = TestObject .test .__get__ (test_object )
52185254
5255+ class MyGlobalPoint :
5256+ def __init__ (self , x , y ):
5257+ return None
5258+
52195259if __name__ == "__main__" :
52205260 unittest .main ()
0 commit comments