U
    ,d                     @   s  d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZmZmZmZ ddlmZ ddlZejZed	ped
pdekZeedZeedZe  Z!e!dkZ"ee"dZ#ej$%dddkZ&ee&dZ'edZ(e	ddZ)edddZ*dd Z+dd Z,dd Z-e#eG dd deZ.e#eeG dd  d eZ/e/0  e#eeG d!d" d"eZ1eG d#d$ d$eZ2e3d%kre4  dS )&z
Tests gdb bindings
    N)permutations)njitgdbgdb_initgdb_breakpointprange)errors)jit)TestCasecaptured_stdouttagskip_parfors_unsupported)	needs_gdblinuxdarwinZbsdzunix-like OS is requiredznon unix-like OS is required>   armv7laarch64ztesting disabled on ARMGDB_TEST1zneeds gdb harnesslong_runningT)debug)forceobjr   c                 C   s>   t dddddd | d }| d }| ||f}t| ||| d S N-exset confirm offcq   Q@)r   printabr   d r$   A/tmp/pip-unpacked-wheel-eu7e0c37/numba/tests/test_gdb_bindings.pyimpl_gdb_call+   s
    
r&   c                 C   sD   t dddddd | d }| d }| ||f}t  t| ||| d S r   )r   r   r   r    r$   r$   r%   impl_gdb_call_w_bp3   s    
r'   c                 C   sZ   t dddddd | d7 } tdD ]2}| d }| d }| ||f}t  t| ||| q"d S )	Nr   r   r   r         r   r   )r   r   r   r   )r!   ir"   r   r#   r$   r$   r%   (impl_gdb_split_init_and_break_w_parallel<   s    
r+   c                   @   s   e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	edd Z
eedd Zeedd Zeedd ZdS )TestGdbBindImplsa  
    Contains unit test implementations for gdb binding testing. Test must be
    decorated with `@needs_gdb_harness` to prevent their running under normal
    test conditions, the test methods must also end with `_impl` to be
    considered for execution. The tests themselves are invoked by the
    `TestGdbBinding` test class through the parsing of this class for test
    methods and then running the discovered tests in a separate process. Test
    names not including the word `quick` will be tagged as @tag('long_running')
    c              	   C   s   t   td W 5 Q R X d S N
   )r   r&   selfr$   r$   r%   $test_gdb_cmd_lang_cpython_quick_implT   s    z5TestGdbBindImpls.test_gdb_cmd_lang_cpython_quick_implc              	   C   s"   t   ttd W 5 Q R X d S r-   )r   	_dbg_njitr&   r/   r$   r$   r%   %test_gdb_cmd_lang_nopython_quick_implY   s    z6TestGdbBindImpls.test_gdb_cmd_lang_nopython_quick_implc              	   C   s"   t   ttd W 5 Q R X d S r-   )r   _dbg_jitr&   r/   r$   r$   r%   $test_gdb_cmd_lang_objmode_quick_impl^   s    z5TestGdbBindImpls.test_gdb_cmd_lang_objmode_quick_implc              	   C   s   t   td W 5 Q R X d S r-   )r   r'   r/   r$   r$   r%   *test_gdb_split_init_and_break_cpython_implc   s    z;TestGdbBindImpls.test_gdb_split_init_and_break_cpython_implc              	   C   s"   t   ttd W 5 Q R X d S r-   )r   r2   r'   r/   r$   r$   r%   +test_gdb_split_init_and_break_nopython_implh   s    z<TestGdbBindImpls.test_gdb_split_init_and_break_nopython_implc              	   C   s"   t   ttd W 5 Q R X d S r-   )r   r4   r'   r/   r$   r$   r%   *test_gdb_split_init_and_break_objmode_implm   s    z;TestGdbBindImpls.test_gdb_split_init_and_break_objmode_implc              	   C   s   t   td W 5 Q R X d S r-   )r   r+   r/   r$   r$   r%   5test_gdb_split_init_and_break_w_parallel_cpython_implr   s    zFTestGdbBindImpls.test_gdb_split_init_and_break_w_parallel_cpython_implc              	   C   s"   t   ttd W 5 Q R X d S r-   )r   r2   r+   r/   r$   r$   r%   6test_gdb_split_init_and_break_w_parallel_nopython_implx   s    zGTestGdbBindImpls.test_gdb_split_init_and_break_w_parallel_nopython_implc              	   C   s"   t   ttd W 5 Q R X d S r-   )r   r4   r+   r/   r$   r$   r%   5test_gdb_split_init_and_break_w_parallel_objmode_impl~   s    zFTestGdbBindImpls.test_gdb_split_init_and_break_w_parallel_objmode_implN)__name__
__module____qualname____doc__needs_gdb_harnessr1   r3   r5   r6   r7   r8   r   r9   r:   r;   r$   r$   r$   r%   r,   G   s,   






r,   c                   @   sB   e Zd ZdZdZdZdddZdd Zedd	 Z	ed
d Z
dS )TestGdbBindingz
    This test class is used to generate tests which will run the test cases
    defined in TestGdbBindImpls in isolated subprocesses, this is for safety
    in case something goes awry.
    FTc           	         s   t j|t jt j|dd  fdd}td|}zP|    \}} j}|dkrnt	d||
 |
 f |
 |
 fW S |  X dS )	NT)stdoutstderrenvshellc                      s     j    j     d S N)rB   flushrC   killr$   popenr$   r%   rH      s    

z$TestGdbBinding.run_cmd.<locals>.killg      4@r   z9process failed with code %s: stderr follows
%s
stdout :%s)NN)
subprocessPopenPIPE	threadingTimercancelstartcommunicate
returncodeAssertionErrordecode)	r0   cmdlinerD   Z
kill_is_okrH   timeoutouterrretcoder$   rI   r%   run_cmd   s*    
zTestGdbBinding.run_cmdc                 K   s>   t j }d|d< d|d< tjdd|g}| jd||f|S )Nr   Z	NUMBA_OPTr   z-mznumba.runtests )osenvironcopysys
executabler[   join)r0   testkwargsZenv_copyrV   r$   r$   r%   run_test_in_separate_process   s
    
z+TestGdbBinding.run_test_in_separate_processc                    sj   t j}t j}|dst|dd}d|||f   fdd}d|krVt| || nt| |t| d S )N_impl z%s.%s.%sc                    sd   |   \}}d| d| d}| jd||d | jd||d | jd||d | jd||d d S )	Nz
STDOUT=z
STDERR=
zGNU gdb)msgOKFAILERROR)re   assertInZassertNotIn)r0   oeZdbgmsgZinjected_methodr$   r%   test_template   s    z-TestGdbBinding._inject.<locals>.test_templateZquick)r,   r=   r<   endswithrT   replacesetattrr   )clsnameZthemodZtheclsZmethnamerq   r$   rp   r%   _inject   s    zTestGdbBinding._injectc                 C   s&   t tD ]}|dr| | qd S )NZtest_gdb)dirr,   
startswithrw   )ru   rv   r$   r$   r%   generate   s    
zTestGdbBinding.generateN)F)r<   r=   r>   r?   Z_numba_parallel_test_Z_DEBUGr[   re   classmethodrw   rz   r$   r$   r$   r%   rA      s   
	
rA   c                   @   s   e Zd Zedd ZdS )TestGdbMiscc                    sZ   dd }d  fdd}t ttgD ]\}}|||}|| q&tdd }|| d S )Nc                    s   t  fdd}|S )Nc                     s   d}    d}  | | S Nr      r$   r!   r"   f1f2r$   r%   impl   s
    z:TestGdbMisc.test_call_gdb_twice.<locals>.gen.<locals>.impl)r   )r   r   r   r$   r   r%   gen   s    z,TestGdbMisc.test_call_gdb_twice.<locals>.genz8Calling either numba.gdb() or numba.gdb_init() more thanc              	      s4    tj}|   W 5 Q R X  t|j d S rF   )assertRaisesr   ZUnsupportedErrorrm   str	exception)funcraisesZmsg_headr0   r$   r%   check   s    z.TestGdbMisc.test_call_gdb_twice.<locals>.checkc                  S   s   d} t   d}t  | | S r}   )r   r   r   r$   r$   r%   use_globals   s
    z4TestGdbMisc.test_call_gdb_twice.<locals>.use_globals)r   r   r   r   )r0   r   r   Zg1Zg2r   r   r$   r   r%   test_call_gdb_twice   s    



zTestGdbMisc.test_call_gdb_twiceN)r<   r=   r>   r   r   r$   r$   r$   r%   r|      s   r|   c                   @   s   e Zd Zdd ZdS )TestGdbExceptionsc                    st   dd }|t ddtfD ]V}ttfD ]H  fdd}| tj}||  W 5 Q R X d}| |t|j	 q$qd S )Nc                 S   s   | S rF   r$   )xr$   r$   r%   nop_compiler  s    z5TestGdbExceptions.test_call_gdb.<locals>.nop_compilerT)r   c                      s
      d S rF   r$   r$   methr$   r%   python_func  s    z4TestGdbExceptions.test_call_gdb.<locals>.python_funcz2gdb support is only available on unix-like systems)
r	   r   r   r   r   r   ZTypingErrorrm   r   r   )r0   r   compilerr   r   ri   r$   r   r%   test_call_gdb  s    zTestGdbExceptions.test_call_gdbN)r<   r=   r>   r   r$   r$   r$   r%   r      s   r   __main__)5r?   r]   platformrK   r`   rN   	itertoolsr   Znumbar   r   r   r   r   Z
numba.corer   r	   Znumba.tests.supportr
   r   r   r   Znumba.tests.gdb_supportr   Zunittest	_platformry   Z
_unix_likeZ
skipUnlessZ	unix_onlyZskipIfZnot_unixmachineZ
_arch_nameZ_is_armZnot_armr^   getZ	_gdb_condr@   r   r2   r4   r&   r'   r+   r,   rA   rz   r|   r   r<   mainr$   r$   r$   r%   <module>   s^   

	<L$
