Ë
    pGìj‹  ã                   ó¨   — 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      ú`C:\Users\Stabi\Downloads\virtual-tryon(1)\venv\Lib\site-packages\numba/tests/test_controlflow.pyÚ_generate_large_cfg_sourcer      sf   € à&Ð'€EÜ�5Ž\ˆØ�‰�x ˜sÐ"5Ð6Ô7Ø�‰�x ˜s ,¨q¨c°Ð9Õ:ð ð 
‡L�L�Ô Ø�9‰9�UÓÐó    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Ó2€GÜ	�'Ó	€BÜ
˜bÓ
!€CØ‡G�G„IØ�9‰9Ð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Ù‰A˜A€EØ€EÙˆA€EØ€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   ø€ Ü�M‰M‹OˆØˆØˆmØ�‰FˆAØ—‘ˆAð ‰mð ˆ|˜A‰ÒØˆL˜‰OÙ˜‹~Ðr   )r   r$   r   Ú_find_topo_orderÚprocess)Úcfgr1   r/   r0   s     @@r   Ú_measure_trivial_compile_depthr5   5   s]   ù€ ð
 �3€LÜ
#¤MÓ
2€CÜ×'Ñ'€Hõð  3„GÔð#Ø×ÑÔà"ŒŒà˜‰?Ðøð #Œ�ú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Ø�-Ø—H‘H�Ø’ð “-ù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Ó4°qÑ8Ó9ˆò	ô ™&›(“mˆä(¨Ó-ˆØˆÜÜØØ'Øóð
 ô	
ô (¨Ð,=Ñ(>Ó?ˆô ×.Ñ.Ó0ˆÜ×Ñ˜e kÑ1Ô2ð ˆ
ð	2Ø× Ñ Ô"ô
 ×!Ñ! .Ô1ð Ðàð$ð —'‘'œ)×-Ñ-¨j×.FÑ.FÓGÓHñIð ð
 �I‰I�c�Nøô# ò 	Ø�Jûð	ûô ×!Ñ! .Õ1ús*   Â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ÒØ€H‡M�M…Oð r   