
    GJj                         d Z ddlZddlZddlZddlmZmZ ddlmZ ddl	m
Z
mZ d Zd Zd Zd	 Z G d
 de      Zedk(  r ej$                          yy)aB  
Regression test for issue #5611 / PR #10482.

CFGraph._find_topo_order must be stack-based (not recursive) so that
functions with large control-flow graphs do not exhaust the Python call
stack.

The test is run in a subprocess so that sys.setrecursionlimit manipulation
is completely isolated from the rest of the suite.
    N)CFGraphControlFlowAnalysis)TestCase)FunctionIdentityByteCodec                     dg}t        |       D ]/  }|j                  d| d       |j                  d| d| d       1 |j                  d       dj                  |      S )	z/ Source for function with unusually large CFG. zdef _large_cfg_func(x):z    dep_z = 1 if True else 0z    val_z = x if dep_z else 0z    return x
)rangeappendjoin)countlinesis      f/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/numba/tests/test_controlflow.py_generate_large_cfg_sourcer      sf    &'E5\xs"567xs,qc9:  
LL 99U    c                     t        j                  |       }t        |      }t        |      }|j	                          |j
                  S )z;Create a CFGraph from a Python function for direct testing.)r   from_functionr   r   rungraph)funcfunc_idbccfas       r   _create_cfg_from_functionr   #   s9    ,,T2G	'	B
b
!CGGI99r   c                 *    d}|r| nd}d}|r| }| S d}| S )z7 Test function for probing stack depth during compile.    r    )xdep_0val_0dep_1val_1s        r   _complex_funcr$   ,   s-    EAAEEAEH EHr   c                      dgt        t              } t        j                  fd}|t        _        	 | j                          t        _        d   S # t        _        w xY w)zj
    Return the Python frame depth at CFGraph._find_topo_order during a trivial
    njit compilation.
    r   c                     t        j                         }d}||dz  }|j                  }||d   kD  r|d<    |       S Nr   r   sys	_getframef_back)selffddepth_sampleoriginals      r   _probing_topo_orderz;_measure_trivial_compile_depth.<locals>._probing_topo_order>   sR    MMOmFAA m |ALO~r   )r   r$   r   _find_topo_orderprocess)cfgr1   r/   r0   s     @@r   _measure_trivial_compile_depthr5   5   s]    
 3L
#M
2C''H  3G#"? #s   A A$c                   0    e Zd Zej                  d        Zy)TestCFGTopoOrderNonRecursivec                 P   t        t               dz         }d }t         |             }t        d      }i }t	        t        |dd      |       t        |d         }t        j                         }t        j                  ||z          d}	 |j                          t        j                  |       |ydd	j                  t        j                  |j                              z   }
| j!                  |
       y# t        $ r}	|	}Y d}	~	kd}	~	ww xY w# t        j                  |       w xY w)
a
  
        Regression tests for PR #10482: _find_topo_order must not use recursive
        Python calls, so that large CFGs cannot exhaust the stack.

        Steps
        -----
        1.  Measure the frame depth at CFGraph.topo_order() during a trivial
            njit compilation by patching the method to walk the frame
            chain once; call this bsize. Add 2 for headroom.
        1a. Determine current frame_depth
        2.  Lower sys.setrecursionlimit to bsize + frame_depth
        3.  Attempt to compile a function with 100 ternary-expression
            pairs.
        4.  Catch any RecursionError.
        5.  Restore sys.setrecursionlimit unconditionally.
        6.  Fail the test and print the traceback if a RecursionError was
            raised.
           c               3   b   K   t        j                  d      } | | j                  } d | y y wr'   r(   )r-   s    r   frameszVTestCFGTopoOrderNonRecursive.test_topo_order_no_recursion_in_large_cfg.<locals>.framesp   s.     a A-HH -s   *//d   z<generated_large_cfg>exec_large_cfg_funcNzeUnexpected RecursionError.Potential regression related to finding toplogical order.

Full traceback:
 )intr5   sumr   r=   compiler   r)   getrecursionlimitsetrecursionlimitr2   RecursionErrorr   	traceback	format_tb__traceback__fail)r,   bsizer;   frame_depthfunenvr4   original_limit
caught_excexcmsgs              r   )test_topo_order_no_recursion_in_large_cfgzFTestCFGTopoOrderNonRecursive.test_topo_order_no_recursion_in_large_cfgX   s"   , 24q89	 &(m(-'
 	
 (,=(>? ..0ek12 
	2  "
 !!.1 $ '')--j.F.FGHI 
 IIcN#  	J	 !!.1s*   C6 6	D?DD DD D%N)__name__
__module____qualname__r   run_test_in_subprocessrR   r   r   r   r7   r7   V   s    $$D %Dr   r7   __main__)__doc__r)   rF   unittestnumba.core.controlflowr   r   numba.tests.supportr   numba.core.bytecoder   r   r   r   r$   r5   r7   rS   mainr   r   r   <module>r^      s\   	    ? ( :BG8 GT zHMMO r   