U
    9%e                     @   s   d 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
 z$edd dZe
 Zejd	ZW n ek
r   dZdZY nX d
ZeeeZdZeeeZzddlZdZW n ek
r   dZY nX dZeeeZG dd deZdS )z'Helpers for running gdb related testing    N)config)_confirm_gdb)collect_gdbinfoF)Zneed_ptrace_attachT3z;functioning gdb with correct ptrace permissions is requiredz gdb with python 3 support neededzpexpect module needed for testc                   @   s   e Zd ZdZd0ddZdd Zd	d
 Zdd Zdd Zdd Z	dd Z
d1ddZdd Zdd Zdd Zdd Zdd Zd2d d!Zd3d"d#Zd4d&d'Zd5d(d)Zd6d*d+Zd,d- Zd.d/ ZdS )7GdbMIDriverzs
    Driver class for the GDB machine interface:
    https://sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI.html
    Fx   Nc                 C   sT   t sd}t|ts d}t|tj| _tj| _|| _	|| _
|| _|| _|   d S )Nz[This driver requires the pexpect module. This can be obtained via:

$ conda install pexpectzWThis driver requires a gdb binary. This can be obtained via the system package manager.)_HAVE_PEXPECTRuntimeError	_HAVE_GDBr   Z
GDB_BINARY_gdb_binarysys
executable_python_debug
_file_name_timeout
_init_cmds_drive)self	file_namedebugtimeoutZ	init_cmdsmsg r   V/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/numba/tests/gdb_support.py__init__+   s    zGdbMIDriver.__init__c                 C   sp   t j| jst| jddg}| jdk	r6|t| j7 }|d| j| jg7 }t	
d|| _| jrltjj| j_dS )z0This function sets up the caputured gdb instancez--interpretermiNz--args )ospathisfiler   AssertionErrorr   r   listr   pexpectspawnjoin	_capturedr   r   stdoutbufferZlogfiler   cmdr   r   r   r   <   s    
zGdbMIDriver._drivec                 C   s   d|   kS )z_Returns True if the underlying gdb implementation has python support
           False otherwisepython)list_featuresr   r   r   r   supports_pythonG   s    zGdbMIDriver.supports_pythonc                 C   s,   |   sdS d}| d| d| jj kS )zReturns True if the underlying gdb implementation has NumPy support
           (and by extension Python support) False otherwiseFzGpython from __future__ import print_function;import numpy; print(numpy)consolezmodule 'numpy' from)r.   interpreter_execr&   beforedecoder)   r   r   r   supports_numpyL   s
    zGdbMIDriver.supports_numpyc              
   C   sX   z| j j|| jd W n< tjjk
rR } zd| d}t||W 5 d }~X Y nX d S )N)r   zExpected value did not arrive: .)r&   expectr   r#   
exceptionsTIMEOUT
ValueError)r   r5   er   r   r   r   _captured_expectW   s
    zGdbMIDriver._captured_expectc                 C   s4   | j j}|d}||ks0td| d| ddS )z=Asserts that the current output string contains the expected.utf-8decoded=

expected=)N)r&   afterr2   r!   )r   expectedoutputdecodedr   r   r   assert_output^   s    
zGdbMIDriver.assert_outputc                 C   sH   | j j}|d}| d }t||}|sDtd| d| ddS )zKAsserts that the current output string contains the expected
        regex.r;   r   r<   r=   r>   N)r&   r?   r2   
splitlinesrematchr!   )r   r@   rA   rB   Zdone_strfoundr   r   r   assert_regex_outputd   s
    
zGdbMIDriver.assert_regex_output c                 C   s   | j | | | d S )N)r&   Zsendliner:   )r   commandr5   r   r   r   _run_commandm   s    zGdbMIDriver._run_commandc                 C   s   | j ddd dS )zgdb command ~= 'run'z	-exec-run\^running.*\r\nr5   NrK   r-   r   r   r   runq   s    zGdbMIDriver.runc                 C   s   | j ddd dS )zgdb command ~= 'continue'z-exec-continuerL   rM   NrN   r-   r   r   r   contu   s    zGdbMIDriver.contc                 C   s   | j ddd | j  dS )zgdb command ~= 'quit'z	-gdb-exitrM   N)rK   r&   	terminater-   r   r   r   quity   s    zGdbMIDriver.quitc                 C   s   | j ddd dS )zgdb command ~= 'next'z
-exec-next\*stopped,.*\r\nrM   NrN   r-   r   r   r   next~   s    zGdbMIDriver.nextc                 C   s   | j ddd dS )zgdb command ~= 'step'z
-exec-steprS   rM   NrN   r-   r   r   r   step   s    zGdbMIDriver.stepc                 C   s   |dk	r|dk	rt dd}|dk	r4|d| d7 }|dk	rbt|tsJt|d| j d| d7 }|dk	rt|tsxt|d| d7 }| j|d	d
 dS )zgdb command ~= 'break'Nz%Can only supply one of line or symbolz-break-insert z-c "z" z-f :r   z\^donerM   )r8   
isinstanceintr!   r   strrK   )r   linesymbol	conditionbpr   r   r   set_breakpoint   s    zGdbMIDriver.set_breakpointc                 C   sh   |  d | d |dk	r<t|ts*t| d| d |dk	rdt|tsRt| d| d dS )z%Checks that a breakpoint has been hitrS   z!*stopped,reason="breakpoint-hit",Nzbkptno=""zline=")r:   rC   rW   rX   r!   )r   numberrZ   r   r   r   check_hit_breakpoint   s    

z GdbMIDriver.check_hit_breakpoint   r   c                 C   sN   |||fD ]}t |tr |dks
tq
d| d| d| }| j|dd dS )zgdb command ~= 'info args'r   rb      z-stack-list-arguments r   \^done,.*\r\nrM   NrW   rX   r!   rK   )r   print_valuesZ	low_frameZ
high_framexr*   r   r   r   stack_list_arguments   s    z GdbMIDriver.stack_list_argumentsc                 C   s2   t |tr|dkstd| }| j|dd dS )zgdb command ~= 'info locals'rc   z-stack-list-variables re   rM   Nrf   )r   rg   r*   r   r   r   stack_list_variables   s    
z GdbMIDriver.stack_list_variablesc                 C   sD   |dkrt d|dkr t dd| d| d}| j|dd dS )	z!gdb command ~= 'interpreter-exec'Nzinterpreter cannot be Nonezcommand cannot be Nonez-interpreter-exec z "r_   z\^(done|error).*\r\nrM   )r8   rK   )r   interpreterrJ   r*   r   r   r   r0      s    zGdbMIDriver.interpreter_execc                 C   s   d}| j |dd d S )Nz-list-featuresre   rM   rN   r)   r   r   r   _list_features_raw   s    zGdbMIDriver._list_features_rawc                 C   sh   |    | jj}|d}td|}|dk	s6td| }t|dksRtd|d 	dd	
d
S )zXNo equivalent gdb command? Returns a list of supported gdb
           features.
        r;   z.*features=\[(.*)\].*Nz"No match found for features stringrb   z$Invalid number of match groups foundr   r_   rI   ,)rl   r&   r?   r2   rE   rF   r!   groupslenreplacesplit)r   rA   rB   mgr   r   r   r,      s    
zGdbMIDriver.list_features)Fr   N)rI   )NNN)NN)rb   r   r   )rb   )NN)__name__
__module____qualname____doc__r   r   r.   r3   r:   rC   rH   rK   rO   rP   rR   rT   rU   r^   ra   ri   rj   r0   rl   r,   r   r   r   r   r   &   s*   
	





	r   )rw   r   rE   r   ZunittestZ
numba.corer   Znumba.misc.gdb_hookr   Znumba.misc.numba_gdbinfor   r
   Z	_gdb_infoZpy_ver
startswithZ_GDB_HAS_PY3	ExceptionZ_msgZ
skipUnlessZ	needs_gdbZneeds_gdb_py3r#   r   ImportErrorZskip_unless_pexpectobjectr   r   r   r   r   <module>   s6   


