U
    {h                   	   @   s.  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Zd dlZd dlm  mZ d dlmZ e je jeZe jedZe jedZeeZdd eedD Z W 5 Q R X da!d	d
 Z"G dd dej#Z$G dd de$Z%G dd de%Z&e'dkr*e(  dS )    N)	ccompiler)Cygdbcodefilecfuncs.cc                 C   s   i | ]\}}|  |qS  )strip).0iliner   r   G/tmp/pip-unpacked-wheel-fhl22ezh/Cython/Debugger/Tests/TestLibCython.py
<dictcomp>   s      r      c               	   C   sB  t d k	rt S da ztjdddgtjd} W n tk
rB   d }Y n&X |  \}}d}t||dd}|r.t	t
t| }|d	d
gkr.da tjdd}|d |  tjddd|jgtjd} |  \}}z0t	t
t|dd }|d
d	gk rda W n tk
r"   da Y nX W 5 Q R X t s>td t S )NFgdbz-nxz	--version)stdoutzGNU gdb [^\d]*(\d+)\.(\d+)asciiignore      Tzw+)modez8python import sys; print("%s %s" % sys.version_info[:2])-batch-xz6Skipping gdb tests, need gdb >= 7.2 with Python >= 2.7)have_gdb
subprocessPopenPIPEOSErrorcommunicaterematchdecodelistmapintgroupstempfileNamedTemporaryFilewriteflushnamesplit
ValueErrorwarningswarn)pZgdb_versionr   _regexZgdb_version_numberZpython_version_scriptZinternal_python_versionr   r   r   test_gdb   s@    

r0   c                   @   s   e Zd Zdd Zdd ZdS )DebuggerTestCasec                 C   s  t  s
dS t | _tj| jd| _tj| jdd| _tj| jd| _	t
 | _z(t| j tt| j tt| j	d  ttdd| j	d  t }|jdgd	d
gd t| jd| jd}t }tjf | jdttd|}ttjd}tj}|t_|  zN|j"f | jddd	| jdd| |j#|d |d |d dddgid W 5 |   |t_|!  X W n   t| j  Y nX dS )z
        Run gdb and have cygdb import the debug information from the code
        defined in TestParseTreeTransforms's setUp method
        Nzcodefile.pyxZcython_debugZcython_debug_info_codefileZcfuncsz.cz.hr   Tz-fPIC)debugZextra_postargsr   )test_directorymodulemodule_pathF)workdirZcleanup_workdirtagsw)Z	gdb_debug
output_dir)Z	targetdirincdirZannotateZextra_compile_optionsr3   r4   extra_objectszcfuncs.o)r3   r4   r6   r:   Zextra_extension_args)$r0   r$   mkdtemptempdirospathjoindestfileZ
debug_destZcfuncs_destfilegetcwdcwdchdirshutilcopyr   cfuncs_filereplacer   Znew_compilercompiledict	build_extZOptimizationruntestsZCythonCompileTestCaseZ
parse_tagsopendevnullsysstderrZdisable_optimizationZrestore_statecloseZ
run_cythonZrun_distutils)selfcompileroptsZoptimization_disablerZcython_compile_testcaseZ
new_stderrrP   r   r   r   setUpG   sx    


	
zDebuggerTestCase.setUpc                 C   s&   t  s
d S t| j t| j d S N)r0   r>   rD   rC   rE   rmtreer=   rR   r   r   r   tearDown   s    zDebuggerTestCase.tearDownN)__name__
__module____qualname__rU   rY   r   r   r   r   r1   E   s   Zr1   c                       s(   e Zd Z fddZ fddZ  ZS )GdbDebuggerTestCasec              	      s   t  s
d S t   td}tdtjd d f }t| j	|| _
t| j
d}|| W 5 Q R X ddd| j
dd	tjd
dg	}g }tjd}|r|| |tjtjtjtj ttjtj|d}tj|tjtj|d| _d S )Na              python

            import os
            import sys
            import traceback

            def excepthook(type, value, tb):
                traceback.print_exception(type, value, tb)
                sys.stderr.flush()
                sys.stdout.flush()
                os._exit(1)

            sys.excepthook = excepthook

            # Have tracebacks end up on sys.stderr (gdb replaces sys.stderr
            # with an object that calls gdb.write())
            sys.stderr = sys.__stderr__

            end
            z            python

            from Cython.Debugger.Tests import test_libcython_in_gdb
            test_libcython_in_gdb.main(version=%r)

            end
            r   ar   r   r   z-nz--argsz-czimport codefile
PYTHONPATH)r_   )r   rP   env)r0   superrU   textwrapdedentrO   version_infocygdbZmake_command_filer=   gdb_command_filerM   r&   
executabler>   environgetappendr?   dirnameabspathCython__file__rJ   pathsepr@   r   r   r   r-   )rR   Zprefix_codecodefargspathsr?   r`   	__class__r   r   rU      s>    

	  
zGdbDebuggerTestCase.setUpc              	      sx   t  s
d S zZt   | jrbz| jj  W n   Y nX z| jj	  W n   Y nX | j
  W 5 t| j X d S rV   )r0   r>   removerf   ra   rY   r-   r   rQ   rP   waitrX   rt   r   r   rY      s    
    zGdbDebuggerTestCase.tearDown)rZ   r[   r\   rU   rY   __classcell__r   r   rt   r   r]      s   <r]   c                   @   s   e Zd Zdd ZdS )TestAllc           	      C   s   t  s
d S | j \}}|d}|d}| jj}|dkrVtj| tj| nN|dkrd}d||f }d||f }d||f }d|||||f }tj| d S )	NzUTF-8r   r   z******************************z%s   v INSIDE GDB v   %sz%s   v STDERR v   %sz%s   ^ INSIDE GDB ^   %sz
%s
%s%s
%s%s)r0   r-   r   r   
returncoderO   rP   r&   )	rR   outerrexit_statusZborderstartrP   enderrmsgr   r   r   test_all   s     

zTestAll.test_allN)rZ   r[   r\   r   r   r   r   r   ry      s   ry   __main__))r>   r   rO   rE   r+   rb   Zunittestr$   r   	distutilsr   rL   ZCython.Distutils.extensionrm   ZCython.Distutils.old_build_extZ	DistutilsZold_build_extrK   ZCython.Debuggerr   re   r?   rk   rl   rn   rootr@   r   rG   rM   rq   	enumerateZsource_to_linenor   r0   ZTestCaser1   r]   ry   rZ   mainr   r   r   r   <module>   s2   
)cN
