U
    {hJ                     @   s2  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ZddlZddlZddl	Z	ddl
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 ddlmZ dge_d	d
 ZG dd deZG dd de	jedZG dd deZG dd deZG dd de	jZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"G d d! d!eZ#G d"d# d#eZ$G d$d% d%eZ%d&Z&d'Z'G d(d) d)eZ(G d*d+ d+eZ)G d,d- d-eZ*G d.d/ d/eZ+G d0d1 d1eZ,G d2d3 d3eZ-ej./d4Z0e0re1d5d6Z2d7d8 Z3d9d: Z4d;d< Z5d@d>d?Z6dS )Az
Tests that run inside GDB.

Note: debug information is already imported by the file generated by
Cython.Debugger.Cygdb.make_command_file()
    N   )	libcython)	libpython   )TestLibCythongdbc                    s   t   fdd}|S )Nc                    sN   t t| j j z | f||W S  tk
rH   t dt   Y nX d S )NzAn exception occurred:)_debugtype__name__	Exception	traceback
format_exc)selfargskwargsfunc O/tmp/pip-unpacked-wheel-fhl22ezh/Cython/Debugger/Tests/test_libcython_in_gdb.pywrapper$   s    z(print_on_call_decorator.<locals>.wrapper)	functoolswraps)r   r   r   r   r   print_on_call_decorator#   s    	r   c                   @   s   e Zd Zdd ZdS )TraceMethodCallMetac                 C   s0   |  D ]"\}}t|rt| |t| qd S N)itemsinspect
isfunctionsetattrr   )r   namebasesdict	func_namer   r   r   r   __init__2   s    
zTraceMethodCallMeta.__init__N)r
   
__module____qualname__r#   r   r   r   r   r   0   s   r   c                       sL   e Zd ZdZ fddZdddZdd Zdd	d
Zdd Zdd Z	  Z
S )DebugTestCasezf
    Base class for test cases. On teardown it kills the inferior and unsets
    all breakpoints.
    c                    sV   t  | tj| _tjjd | _tjjd \| _| _tjj	d | _
tjj	d | _d S )Ncodefilespamzcodefile.hamzcodefile.eggs)superr#   r   cyZcython_namespacemoduleZfunctions_by_name	spam_func	spam_methZfunctions_by_qualified_nameham_func	eggs_func)r   r   	__class__r   r   r#   >   s    zDebugTestCase.__init__Nc                 C   s   t d| }|r||}|S )Nz$cy_cvalue("%s"))r   Zparse_and_eval)r   varnamecast_toresultr   r   r   read_varH   s    zDebugTestCase.read_varc                 C   s   t jdddS )Nzinfo localsTZ	to_stringr   executer   r   r   r   
local_infoO   s    zDebugTestCase.local_infoc                 C   s2   |d k	rt j| }t }| tj|| d S r   )test_libcythonsource_to_linenor   selected_frameassertEqualr   Zcython_infolineno)r   source_liner?   framer   r   r   lineno_equalsR   s    
zDebugTestCase.lineno_equalsc                 C   s.   t j| }tjd| dd tjddd d S )Nzcy break codefile:%dTr6   run)r;   r<   r   r8   )r   r@   Zbreak_linenor   r   r   break_and_runX   s    
zDebugTestCase.break_and_runc                 C   sD   t jddd zt jddd W n tk
r4   Y nX t d d S )Nzdelete breakpointsTr6   zkill inferior 1zset args -c "import codefile")r   r8   RuntimeErrorr9   r   r   r   tearDown]   s    zDebugTestCase.tearDown)N)NN)r
   r$   r%   __doc__r#   r5   r:   rB   rD   rF   __classcell__r   r   r0   r   r&   8   s   


r&   )	metaclassc                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestDebugInformationClassesc                 C   s.   |  | jjd d}t|| jjs*tdS )zDtest that debug information was parsed properly into data structuresr'   )c_var
python_varr
   __builtins__rG   __file__N)r>   r+   r   setissubsetglobalsAssertionError)r   global_varsr   r   r   test_CythonModulei   s    z-TestDebugInformationClasses.test_CythonModulec                 C   sJ   | j j}|d }|d }| |jtj | |jtj | |jd d S )NrK   rL   zcodefile.c_var)r+   rQ   r>   r	   r   CObjectZPythonObjectqualified_name)r   module_globalsrK   rL   r   r   r   test_CythonVariablep   s    z/TestDebugInformationClasses.test_CythonVariablec                 C   s   |  | jjd |  | jjd |  | jj| j | jjsLt| j| jjf| jjrXt| jjrdt| jjrpt|  | jj	t
j |  | jj	t
j |  | jjdg |  | jjddh tjd }|  | jj| |  t| jjtd d S )Nzcodefile.spamzcodefile.SomeClass.spamaZputssome_c_functiondef spam(a=0):Zabcd)r>   r,   rV   r-   r+   r/   Zpf_cnamerR   r.   r	   r   rU   	argumentsZstep_into_functionsr;   r<   r?   sortedlocalslist)r   Zexpected_linenor   r   r   test_CythonFunctionx   s$    


z/TestDebugInformationClasses.test_CythonFunctionN)r
   r$   r%   rT   rX   r`   r   r   r   r   rJ   g   s   rJ   c                   @   s   e Zd Zdd Zdd ZdS )TestReprMethodsc                    sj   t j}tt|j}td  fddt|D }|| }t	dt
| }| t|t| d S )Nr   c                 3   s   | ]}  d V  qdS )   N)Z	randbytes).0_Zlorem_ipsumr   r   	<genexpr>   s     z3TestReprMethods.test_simple_repr.<locals>.<genexpr>z
libcython.)r   ZCythonModulelenr   	signature
parametersrandomRandomrangeevalreprr>   pickledumps)r   Z
test_classnum_argsZfiller_argsinstanceZ	recreatedr   re   r   test_simple_repr   s    
z TestReprMethods.test_simple_reprc                 C   s2   d}|  | t }| t|t| d S )N	import os)rD   r   r=   r>   r   Z
frame_repr)r   	beginlinerA   r   r   r   test_frame_repr   s    
zTestReprMethods.test_frame_reprN)r
   r$   r%   rs   rv   r   r   r   r   ra      s   	ra   c                   @   s   e Zd Zdd ZdS )TestParametersc                 C   s0   t d tjjstt d tjjr,td S )Nzset cy_colorize_code onzset cy_colorize_code off)r   r8   r   ri   colorize_coderR   r9   r   r   r   test_parameters   s    

zTestParameters.test_parametersN)r
   r$   r%   ry   r   r   r   r   rw      s   rw   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
	TestBreakc                 C   sn   t t pd}td | t t |d  t d }| |jtj | jj|j	ks`t
|jsjt
d S )Nr   cy break codefile.spamr   )rg   r   Zbreakpointsr8   r>   r	   ZBP_BREAKPOINTr,   cnamelocationrR   Zenabled)r   Zbreakpoint_amountZbpr   r   r   
test_break   s    
zTestBreak.test_breakc                 C   s$   t d dt jdddks td S )Nzcy break -p joinz	def join(cy runTr6   )r   r8   rR   r9   r   r   r   test_python_break   s    
zTestBreak.test_python_breakc                 C   sJ   d}d}|  | | | tjddd}| | | |sFtd S )Nrt   zcdef int c_var = 12cy stepTr6   )rD   rB   r   r8   rstripendswithrR   )r   ru   ZnextlineZstep_resultr   r   r   test_break_lineno   s    


zTestBreak.test_break_linenoc                 C   sD   t jjj}| d|dd | d|dd  | d|dd  d S )Nr(   zcodefile.SomeClass.sszcodefile.SomeClass.Zpamz
codefile.s)r   r*   Zbreak_ZcompleteZassertIn)r   Z	completerr   r   r   test_break_completion   s    
zTestBreak.test_break_completionN)r
   r$   r%   r   r   r   r   r   r   r   r   rz      s   

rz   c                   @   s   e Zd ZdddZdS )DebugStepperTestCaseNc                 C   sB   t | j |D ] \}}| | |||   q| || d S r   )r   r8   commandr>   r5   r:   rB   )r   Zvarnames_and_valuesr@   r?   r2   valuer   r   r   step   s    zDebugStepperTestCase.step)NN)r
   r$   r%   r   r   r   r   r   r      s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	TestStepzb
    Test stepping. Stepping happens in the code found in
    Cython/Debugger/Tests/codefile.
    c                 C   s   t d t jddd | d t jddd | d d| _| jdgd	d
 | jddgdd
 | jdgdd
 | jg dd
 t jddd | tt  d | t  d jd d S )Nr{   rC   Tr6   r[   r   zb = c = d = 0)br   zb = 1)r@   )r   r   )cr   c = 2)r   r   int(10)puts("spam")Zcontr   r   )	r   r8   rB   r   r   r>   rg   Z	inferiorspidr9   r   r   r   test_cython_step   s    


zTestStep.test_cython_stepc                 C   s0   |  d tjddd | t  d d S )Nsome_c_function()r   Tr6   rZ   )rD   r   r8   r>   r=   r   r9   r   r   r   test_c_step   s    
zTestStep.test_c_stepc                 C   sp   |  d tjddd}t }| | d t| }|j	
t }| |d td|slt|d S )Nos.path.join("foo", "bar")r   Tr6   ZPyEval_EvalFrameExjoinz\d+    def join\()rD   r   r8   r=   r>   r   r   ZFrameZget_pyopco_nameZproxyvalrO   rematchrR   )r   r4   ZcurframeZpyframeZ
frame_namer   r   r   test_python_step   s    
zTestStep.test_python_stepN)r
   r$   r%   rG   r   r   r   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd ZdS )TestNextc                 C   s0   |  d d}|D ]}td | | qd S )Nr   )r   r   r   r   zcy next)rD   r   r8   rB   )r   linesliner   r   r   test_cython_next  s
    

zTestNext.test_cython_nextN)r
   r$   r%   r   r   r   r   r   r     s   r   c                   @   s   e Zd Zdd Zdd ZdS )TestLocalsGlobalsc                 C   sL   |  d tjddd}ds(tt|ds4t|d|ksHtt|d S )Nr   	cy localsTr6   za = 0zb = (int) 1zc = (int) 2rD   r   r8   rR   rn   r   r4   r   r   r   test_locals"  s
    
zTestLocalsGlobals.test_localsc                 C   s   |  d tjddd}d|ks,tt|d|ks@tt|d|ksTtt|d|kshtt|d	|ks|tt|d S )
Nr   z
cy globalsTr6   z	__name__ z__doc__ zos zc_var zpython_var r   r   r   r   r   test_globals*  s    
zTestLocalsGlobals.test_globalsN)r
   r$   r%   r   r   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd ZdS )TestBacktracec                 C   sL   dt jj_| d dd }tjddd}|| tjddd}|| d S )	NFr   c                 S   s(   t d| st| d| ks$t| d S )Nz-\#\d+ *0x.* in spam\(\) at .*codefile\.pyx:22r   )r   searchrR   )r4   r   r   r   match_backtrace_output<  s    z<TestBacktrace.test_backtrace.<locals>.match_backtrace_outputzcy btTr6   zcy bt -a)r   ri   rx   r   rD   r   r8   )r   r   r4   r   r   r   test_backtrace7  s    

zTestBacktrace.test_backtraceN)r
   r$   r%   r   r   r   r   r   r   5  s   r   c                   @   s   e Zd Zdd ZdS )TestFunctionsc                 C   sz   |  d tjddd}td|s,t|tjddd}tjd }t||ks\t||ftjddd}d|ksvtd S )	Nr   zprint $cy_cname("b")Tr6   z	__pyx_.*bzprint $cy_lineno()zprint $cy_cvalue("b")z= 1)	rD   r   r8   r   r   rR   r;   r<   str)r   r4   Zsupposed_linenor   r   r   test_functionsM  s    

zTestFunctions.test_functionsN)r
   r$   r%   r   r   r   r   r   r   K  s   r   c                   @   s   e Zd Zdd ZdS )	TestPrintc                 C   s\   |  d tjddd}| d| tjddd}| d| tjddd}| d	| d S )
Nr   z
cy print bTr6   zb = (int) 1
zcy print python_varzpython_var = 13
zcy print c_varzc_var = (int) 12
)rD   r   r8   r>   r   r   r   r   
test_print\  s    
zTestPrint.test_printN)r
   r$   r%   r   r   r   r   r   r   Z  s   r   z    14            int b, c
    15
    16        b = c = d = 0
    17
    18        b = 1
>   19        c = 2
    20        int(10)
    21        puts("spam")
    22        os.path.join("foo", "bar")
    23        some_c_function()
z     5        void some_c_function()
     6
     7    import os
     8
     9    cdef int c_var = 12
>   10    python_var = 13
    11
    12    def spam(a=0):
    13        cdef:
    14            int b, c
c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestListc                 C   sP   d}t dD ]0}t|dk r2t|dkr2|d7 }||d 7 }q|d d }d S )N 

   r   z    r|   )$correct_result_test_list_inside_funcsplitrg   )r   Zcorrect_result_wrong_whitespaceZcorrect_resultr   r   r   r   #workaround_for_coding_style_checker  s    z,TestList.workaround_for_coding_style_checkerc                 C   sB   |  d tjddd}ddd |dD }| t| d S )Nr   cy listTr6   r   c                 S   s   g | ]}|  qS r   r   rc   r   r   r   r   
<listcomp>  s     z2TestList.test_list_inside_func.<locals>.<listcomp>)rD   r   r8   r   r   r>   r   r   r   r   r   test_list_inside_func  s    
zTestList.test_list_inside_funcc                 C   sB   |  d tjddd}ddd |dD }| t| d S )Nzpython_var = 13r   Tr6   r   c                 S   s   g | ]}|  qS r   r   r   r   r   r   r     s     z3TestList.test_list_outside_func.<locals>.<listcomp>)rD   r   r8   r   r   r>   %correct_result_test_list_outside_funcr   r   r   r   test_list_outside_func  s    
zTestList.test_list_outside_funcN)r
   r$   r%   r   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd Zdd ZdS )
TestUpDownc                 C   sN   |  d td | ttjd tjddd}d|ks>td|ksJtd S )Nr   r   zcy downzcy upTr6   zspam())rD   r   r8   assertRaisesrE   rR   r   r   r   r   test_updown  s    

zTestUpDown.test_updownN)r
   r$   r%   r   r   r   r   r   r     s   r   c                       s@   e Zd Z fddZ fddZdd Zdd Zd	d
 Z  ZS )TestExecc                    s.   t    t \| _| _t| jd| _d S )Nzr+)	r)   setUptempfilemkstempfdtmpfilenameosfdopentmpfiler9   r0   r   r   r     s    
zTestExec.setUpc              	      s,   t    z| j  W 5 t| j X d S r   )r)   rF   r   remover   r   closer9   r0   r   r   rF     s    
zTestExec.tearDownc                 C   s"   t d| j|f  | j  S )Nz$cy exec open(%r, "w").write(str(%s)))r   r8   r   r   readstrip)r   r   r   r   r   eval_command  s    zTestExec.eval_commandc                 C   s    |  d | d| d d S )Nr   z[0]z[a]zX            cy exec
            pass

            "nothing"
            end
            r   )
rD   r>   r   r   r8   textwrapdedentr   r   r   r   r   r   r   test_cython_exec  s    
  zTestExec.test_cython_execc                 C   s4   |  d td td | d| d d S )Nr   r   zcy exec some_random_var = 14Z14Zsome_random_var)rD   r   r8   r>   r   r9   r   r   r   test_python_exec  s    


zTestExec.test_python_exec)	r
   r$   r%   r   rF   r   r   r   rH   r   r   r0   r   r     s
   r   c                   @   s   e Zd Zdd ZdS )CySetc                 C   s2   |  d td | jdtd}| |d d S )Nr   z!cy set a = $cy_eval("{None: []}")rY   )r3   z
{None: []})rD   r   r8   r5   r   r>   )r   Zstringvaluer   r   r   
test_cyset  s    

zCySet.test_cysetN)r
   r$   r%   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdd ZdS )
TestCyEvalz!Test the $cy_eval() gdb function.c                 C   sR   |  d tjddd}td|s,t|tjddd}td|sNt|d S )Nr   zprint $cy_eval("None")Tr6   z\$\d+ = None\nzprint $cy_eval("[a]")z\$\d+ = \[0\])rD   r   r8   r   r   rR   r   r   r   r   test_cy_eval  s
    
zTestCyEval.test_cy_evalN)r
   r$   r%   rG   r   r   r   r   r   r     s   r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestClosurec                 C   s   t d|  t d d S )Nz	cy break r   r7   )r   funcnamer   r   r   break_and_run_func  s    zTestClosure.break_and_run_funcc                 C   sb   |  d | dtjddd td | t| dd tjd	dd }| |d
 d S )Ninnerr   r   Tr6   r   rY   z'an object'
cy print aa = 'an object')r   r>   r   r8   r   r5   r   r   Zprint_resultr   r   r   
test_inner  s    

zTestClosure.test_innerc                 C   sl   |  d | dtjddd td | dtjddd td tjddd }| |d d S )	Nouterr   r   Tr6   r   r   r   )r   r>   r   r8   r   r   r   r   r   
test_outer  s    


zTestClosure.test_outerN)r
   r$   r%   r   r   r   r   r   r   r   r     s   r   Z	GDB_DEBUGz/dev/ttywc                  G   s@   t r<ttdjjdg| } td	dd | D d  d S )Nr   : c                 s   s   | ]}t |V  qd S r   )r   )rc   msgr   r   r   rf     s     z_debug.<locals>.<genexpr>r   )
	_do_debug	itertoolschainsys	_getframef_coder   _debug_filewriter   )messagesr   r   r   r     s
    r   c                    s   t t jds*d}t| td nVt| dgd}t	
|t	j}t  t fdd|D }tjdd|}| S d S )	NZsymbol_filezUnable to run tests, Python was not compiled with debugging information. Either compile python with -g or get a debug build (configure with --with-pydebug).r   r   )fromlistc                    s   g | ]\}}  |qS r   )ZloadTestsFromTestCase)rc   r   clsZtest_loaderr   r   r   &  s     z*run_unittest_in_module.<locals>.<listcomp>)	verbosity)hasattrr   Zselected_inferiorZ	progspacewarningswarnr   _exit
__import__r   
getmembersisclassunittestZ
TestLoaderZ	TestSuiteZTextTestRunnerrC   ZwasSuccessful)
modulenamer   mtestsZsuiter4   r   r   r   run_unittest_in_module  s    
r   c                  C   s4   ddl m}  tt}t| j}|r&|s0td dS )z~
    Run the libcython and libpython tests. Ensure that an appropriate status is
    returned to the parent test process.
    r   )test_libpython_in_gdbr   N)ZCython.Debugger.Testsr   r   r
   r   exit)r   Zsuccess_libcythonZsuccess_libpythonr   r   r   runtests+  s
    
r   Fc                 C   s:   | a |r0tjddtjtjtjgd}|t nt  d S )NFT)counttraceoutfileZ
ignoredirs)	Zinferior_python_versionr  ZTracer   stderrprefixexec_prefixZrunfuncr   )versionZ
trace_codeZtracerr   r   r   main8  s    
r  )F)7rG   r   r   r   r  ro   rj   r   r   r   r   r   r   r   r   r   r   r   r   r   r;   argvr   r	   r   ZTestCaser&   rJ   ra   rw   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   environgetr   openr   r   r   r   r  r   r   r   r   <module>   s`   /(	*
..

