@@ -2858,6 +2858,85 @@ def func():
28582858 names = ["func" , "outer" , "outer" , "inner" , "inner" , "outer" , "inner" ]
28592859 self .do_test (func , names )
28602860
2861+ def test_jit_frame (self ):
2862+ def fakefunc ():
2863+ pass
2864+
2865+ def f ():
2866+ return sys ._getframe (1 )
2867+
2868+ res = _testinternalcapi .call_with_jit_frame (fakefunc , f , ())
2869+
2870+ def test_jit_frame_globals (self ):
2871+ """jit executable can fill in globals when accessed"""
2872+ def fakefunc ():
2873+ pass
2874+
2875+ fake_globals = {"abc" :42 }
2876+ def callback ():
2877+ return {"globals" : fake_globals }
2878+
2879+ res = _testinternalcapi .call_with_jit_frame (fakefunc , globals , (), callback )
2880+ self .assertEqual (res , fake_globals )
2881+
2882+ def test_jit_frame_builtins (self ):
2883+ """jit executable can fill in builtins when accessed"""
2884+ def fakefunc ():
2885+ pass
2886+
2887+ fake_builtins = {"abc" :42 }
2888+ def callback ():
2889+ return {"builtins" : fake_builtins }
2890+
2891+ res = _testinternalcapi .call_with_jit_frame (fakefunc , _testlimitedcapi .eval_getbuiltins , (), callback )
2892+ self .assertEqual (res , fake_builtins )
2893+
2894+ def test_jit_frame_instr_ptr (self ):
2895+ """jit executable can fill in the instr ptr each time the frame is queried"""
2896+ def fakefunc ():
2897+ pass
2898+ pass
2899+ pass
2900+ pass
2901+
2902+ offset = 0
2903+ linenos = []
2904+ def test ():
2905+ for op in dis .get_instructions (fakefunc ):
2906+ if op .opname in ("RESUME" , "NOP" , "RETURN_VALUE" ):
2907+ nonlocal offset
2908+ offset = op .offset // 2
2909+ linenos .append (sys ._getframe (1 ).f_lineno )
2910+
2911+ def callback ():
2912+ return {"instr_ptr" : offset }
2913+
2914+ _testinternalcapi .call_with_jit_frame (fakefunc , test , (), callback )
2915+ base = fakefunc .__code__ .co_firstlineno
2916+ self .assertEqual (linenos , [base , base + 1 , base + 2 , base + 3 , base + 4 ])
2917+
2918+ def test_jit_frame_code (self ):
2919+ """internal C api checks the for a code executor"""
2920+ def fakefunc ():
2921+ pass
2922+
2923+ def callback ():
2924+ return _testinternalcapi .iframe_getcode (sys ._getframe (1 ))
2925+
2926+ res = _testinternalcapi .call_with_jit_frame (fakefunc , callback , ())
2927+ self .assertEqual (res , fakefunc .__code__ )
2928+
2929+ def test_jit_frame_line (self ):
2930+ """internal C api checks the for a code executor"""
2931+ def fakefunc ():
2932+ pass
2933+
2934+ def callback ():
2935+ return _testinternalcapi .iframe_getline (sys ._getframe (1 ))
2936+
2937+ res = _testinternalcapi .call_with_jit_frame (fakefunc , callback , ())
2938+ self .assertEqual (res , fakefunc .__code__ .co_firstlineno )
2939+
28612940
28622941@unittest .skipUnless (support .Py_GIL_DISABLED , 'need Py_GIL_DISABLED' )
28632942class TestPyThreadId (unittest .TestCase ):
0 commit comments