U
    eʵ                     @   s  d Z ddlmZ zeZW n ek
r,   Y nX ddlZddlZddlZddl	Z	ddl
Z
ddlZzeZeZW n ek
r   eZeZY nX zddlmZ dZW n ek
r6   dZzddlmZ W nn ek
r0   zddlmZ W nF ek
r*   zddlZW n" ek
r$   ddlmZ Y nX Y nX Y nX Y nX zddlZddlZW n& ek
rr   dZejd	 Y nX eed
rddlmZ nddl m!Z ddl"m#Z# dZ$dZ%e&e$e%dZ'e( pdZ)dnddZ*dd Z+doddZ,dd Z-dd Z.G dd de/Z0G dd de/Z1G d d! d!e1Z2G d"d# d#e/Z3G d$d% d%e/Z4G d&d' d'ej5Z6G d(d) d)e6Z7G d*d+ d+e7Z8G d,d- d-ej9Z:G d.d/ d/e:Z;G d0d1 d1e:Z<G d2d3 d3e:Z=G d4d5 d5e/Z>e> Z?G d6d7 d7ej@e3ZAG d8d9 d9eAZBG d:d; d;eAZCG d<d= d=eAZDG d>d? d?e3e#jEZFG d@dA dAeAe#jGZHG dBdC dCeHe#jIZJG dDdE dEeJZKG dFdG dGeHZLG dHdI dIeHZMG dJdK dKeHZNG dLdM dMeAZOG dNdO dOeOZPG dPdQ dQeAZQG dRdS dSeAZRG dTdU dUeAZSG dVdW dWeAZTdXdY ZUG dZd[ d[eAZVG d\d] d]eVZWG d^d_ d_e/ZXG d`da daeAe#jYeXZZG dbdc dceAZ[G ddde deej\e3Z]G dfdg dge]Z^G dhdi diej\e3Z_G djdk dkej\e3eXZ`eF ZaeBb Zcecea_cdldm Zded  dS )pz)
GDB extension that adds Cython support.
    )print_functionN)etreeTF)cElementTree)ElementTreez,Install pygments for colorized source code.
string_to_argv)r   )split)	libpythonCObjectPythonObject)r	   r
   zUTF-8c                    s    fdd}|S )Nc                    s   t  d fdd	}|S )Nc                    sX   z|pt  }W n tk
r.   t dY nX  rF| d krFt | |f||S NzNo frame is currently selected.)gdbselected_frameRuntimeErrorGdbErrornameNoFunctionNameInFrameError)selfframeargskwargs)errfunction =/tmp/pip-unpacked-wheel-7k3cqui1/Cython/Debugger/libcython.pywrapperI   s    z>default_selected_gdb_frame.<locals>.decorator.<locals>.wrapper)N	functoolswrapsr   r   r   r   r   	decoratorH   s    
z-default_selected_gdb_frame.<locals>.decoratorr   )r   r!   r   r   r   default_selected_gdb_frameG   s    r"   c                    s   t  t fdd}|S )Nc                    s6   | dpt }| |s&td | f||S )Nr   zHSelected frame does not correspond with a Cython function we know about.)getr   r   is_cython_functionr   )r   r   r   r   r    r   r   r   Y   s    

z%require_cython_frame.<locals>.wrapper)r   r   require_running_programr   r   r    r   require_cython_frameX   s    r&   c                    s    fdd}|S )Nc                    s   t   fdd}|S )Nc                    s`   |   }|  }|s|r.s.| f|| n.|r>t n|  rRt  n
tdd S )NzFNot a function cygdb knows about. Use the normal GDB commands instead.)r$   is_python_functionr   executeis_relevant_functionr   )r   r   r   Zis_cyZis_py)	c_commandr   python_commandr   r   r   f   s    z5dispatch_on_frame.<locals>.decorator.<locals>.wrapperr   r   r*   r+   r    r   r!   e   s    z$dispatch_on_frame.<locals>.decoratorr   )r*   r+   r!   r   r,   r   dispatch_on_framed   s    r-   c                    s   t   fdd}|S )Nc                     s6   zt   W n tk
r*   t dY nX  | |S r   )r   r   r   r   )r   r   r    r   r   r   z   s
    z(require_running_program.<locals>.wrapperr   r   r   r    r   r%   y   s    r%   c                    s   t   fdd}|S )Nc                    s&   t |tjr| } | |f||S N)
isinstancer   Valuestring)r   r1   r   r   r    r   r   r      s    z.gdb_function_value_to_unicode.<locals>.wrapperr   r   r   r    r   gdb_function_value_to_unicode   s    r2   c                   @   s   e Zd Zdd ZdS )CythonModulec                 C   s.   || _ || _|| _i | _i | _i | _i | _d S r.   )r   filename
c_filenameglobalslineno_cy2clineno_c2cy	functions)r   module_namer4   r5   r   r   r   __init__   s    zCythonModule.__init__N__name__
__module____qualname__r;   r   r   r   r   r3      s   r3   c                   @   s   e Zd Zdd ZdS )CythonVariablec                 C   s&   || _ || _|| _|| _t|| _d S r.   )r   cnamequalified_nametypeintlineno)r   r   rA   rB   rC   rE   r   r   r   r;      s
    zCythonVariable.__init__Nr<   r   r   r   r   r@      s   r@   c                       s"   e Zd Zedf fdd	Z  ZS )CythonFunctionFalsec	           	         sF   t t| ||||| || _|| _|dk| _i | _g | _t | _	d S )NTrue)
superrF   r;   modulepf_cnameis_initmodule_functionlocals	argumentssetstep_into_functions)	r   rJ   r   rA   rK   rB   rE   rC   rL   	__class__r   r   r;      s    	
zCythonFunction.__init__)r=   r>   r?   r	   r;   __classcell__r   r   rQ   r   rF      s   rF   c                   @   s   e Zd Zedddd Zeddd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 dd Zeddd!ddZdd Zdd Zd"ddZdd  ZdS )#
CythonBaseFr   c                 C   s   |  | jjkS r.   )r   cyfunctions_by_cnamer   r   r   r   r   r$      s    zCythonBase.is_cython_functionc                 C   s,   |  dkr(t| }|o&|  S dS )z
        Tells if a frame is associated with a Python function.
        If we can't read the Python frame information, don't regard it as such.
        PyEval_EvalFrameExF)r   r   Frameget_pyopis_optimized_out)r   r   pyframer   r   r   r'      s    zCythonBase.is_python_functionc                 C   s   |  S r.   )r   rW   r   r   r   get_c_function_name   s    zCythonBase.get_c_function_namec                 C   s
   |  jS r.   )find_sallinerW   r   r   r   get_c_lineno   s    zCythonBase.get_c_linenoc                 C   s$   | j j| }|d kr t |S r.   )rU   rV   r#   r   NoCythonFunctionInFrameError)r   r   resultr   r   r   get_cython_function   s    zCythonBase.get_cython_functionc                 C   s    |  |}|jj| |dS )z
        Get the current Cython line number. Returns 0 if there is no
        correspondence between the C and Cython code.
        r   )rc   rJ   r8   r#   r`   )r   r   cyfuncr   r   r   get_cython_lineno   s    
zCythonBase.get_cython_linenoc                 C   s   d  } }}|  |rZ| |jj}| |}||d ks>t|d }trtjjdd}n| 	|rt
| }|std| }| }trtjjdd}n>| }|r|jsd }d}n"|j }|j}trtjjdd}t|||fS )Nr      F)Zstripallz*Unable to read information on python frame)r$   rc   rJ   r4   re   AssertionErrorpygmentsZlexersZCythonLexerr'   r   rY   rZ   r   r   Zcurrent_line_numZPythonLexerr^   Zsymtabfullnamer_   ZCLexerSourceFileDescriptor)r   r   r4   rE   lexerZfilename_and_linenoZpyframeobjectZsymbol_and_line_objr   r   r   get_source_desc   s6    




zCythonBase.get_source_descc                 C   s   |   \}}||S r.   )rl   
get_source)r   r   source_descrE   r   r   r   get_source_line  s    zCythonBase.get_source_linec                 C   sN   |  }| }| |s$| |r(dS |rJ| |rJ| |}||jkS dS )zl
        returns whether we care about a frame on the user-level when debugging
        Cython code
        TF)r   olderr$   r'   rc   rP   )r   r   r   Zolder_framecython_funcr   r   r   r)     s    

zCythonBase.is_relevant_functionc                    s  t  }   z \}}W n" tk
rD   td|  Y dS X |s rt 	 }|dksr|
 rj |ddS |j}d}	g }
nR rƈ } fdd}|j}|j}	g }
n \}}  }|}	g }
zt |	}W n tk
r   d}Y n>X |j}t|tsJt|ttfs8t|}t| d d}d	d
d |
D }tjd||||f  |jdk	rtjd|j|f  tjd ztjd||  W n t jk
r   Y nX |  dS )zk
        Print a C, Cython or Python stack frame and the line of source code
        if available.
        z%#%-2d Unknown Frame (compile with -g)NT)is_crX   c                    s   j jj|  dS )Nr   )rU   	cy_cvalueinvoke)argr   r   r   r   <lambda>A      z-CythonBase.print_stackframe.<locals>.<lambda>r   z, c                 s   s   | ]\}}d ||f V  qdS )z%s=%sNr   ).0r   valr   r   r   	<genexpr>X  s     z.CythonBase.print_stackframe.<locals>.<genexpr>z#%-2d 0x%016x in %s(%s)z	 at %s:%s
    ) r   r   selectrl   r   printr'   r   rY   rZ   r[   print_stackframeco_namer$   rc   r   rA   parse_and_evalr   addressr/   rD   strbytesr   joinsysstdoutwriter4   rm   r   )r   r   indexrr   r   rn   rE   r\   	func_nameZ
func_cname	func_argsrd   fZ	gdb_valueZfunc_addressar   rw   r   r   %  sV    


zCythonBase.print_stackframec                 C   sT   t d}zt d}W n$ tk
r<   t tdY nX || }|d S )NZ__pyx_mPyModuleObjectzy                Unable to lookup type PyModuleObject, did you compile python
                with debugging support (-g)?Zmd_dict)	r   r   Zlookup_typer   r   textwrapdedentcastpointer)r   mr   r   r   r   get_remote_cython_globals_dictg  s    
z)CythonBase.get_remote_cython_globals_dictc                 C   sB   |   }tj|}i }t }| D ]\}}||||< q&|S )zk
        Get the Cython globals dict where the remote names are turned into
        local strings.
        )r   r   ZPyObjectPtrZfrom_pyobject_ptrrO   	iteritemsZproxyval)r   Zremote_dictZpyobject_dictrb   seenkvr   r   r   get_cython_globals_dictu  s    z"CythonBase.get_cython_globals_dictN c                 C   sT   t |rd}nd|jf }|d kr:td||||f  ntd|||||f  d S )Nr   z(%s) z%s%s = %s%sz%s%-*s = %s%s)r   Zpretty_printer_lookuprC   r   )r   r   valuemax_name_lengthprefixtypenamer   r   r   print_gdb_value  s    
zCythonBase.print_gdb_valuec                 C   sV   |j | }|  d }d|jkrL||jkrH|jtkrDtt|jS dS dS ||jkS )Nrf   z->TF)	rM   re   rA   rE   rC   r
   rD   r   r   )r   rq   Z
local_namecyvarZ
cur_linenor   r   r   is_initialized  s    



zCythonBase.is_initialized)F)Nr   )r=   r>   r?   r"   r$   r'   r]   r`   rc   re   rl   ro   r)   r   r   r   r   r   r   r   r   r   rT      s0   







"

A
rT   c                   @   s8   e Zd ZdddZdd Zdd Zdd	 ZdddZdS )rj   Nc                 C   s   || _ || _|| _d S r.   )r4   rk   	formatter)r   r4   rk   r   r   r   r   r;     s    zSourceFileDescriptor.__init__c                 C   s
   | j d k	S r.   )r4   r   r   r   r   valid  s    zSourceFileDescriptor.validc                 C   sL   t rH| jrHtjrHtjj}| jd kr2t jj|d}n| j}t 	|| j|S |S )N)bg)
rh   rk   
parameterscolorize_codeterminal_backgroundr   r   
formattersZTerminalFormatterZ	highlight)r   coder   r   r   r   r   lex  s    
zSourceFileDescriptor.lexc              	   c   s   t | j}|r&|r&| |  }t||d |d }t|D ]H\}}	|| |kr^d}
nd}
|rt|st| |	}	d|
|| |	 f V  qDW 5 Q R X d S )Nrf   > z%s %4d    %s)	openr4   r   read
splitlines	itertoolsislice	enumeraterstrip)r   startstop
lex_source	mark_line
lex_entirer   sliceidxr_   r   r   r   r   _get_source  s    
z SourceFileDescriptor._get_sourceTr   Fc              	   C   sf   t d}| js|t|d}|d kr.|d }zd| |||||W S  tk
r`   |Y nX d S )NzUnable to retrieve source coderf   r}   )r   r   r4   maxr   r   IOError)r   r   r   r   r   r   excr   r   r   rm     s    

zSourceFileDescriptor.get_source)N)NTr   F)r=   r>   r?   r;   r   r   r   rm   r   r   r   r   rj     s   
  rj   c                       s    e Zd ZdZ fddZ  ZS )
CyGDBErrorz6
    Base class for Cython-command related errors
    c                    s    |p
| j f}tt| j|  d S r.   )msgrI   r   r;   )r   r   rQ   r   r   r;     s    zCyGDBError.__init__)r=   r>   r?   __doc__r;   rS   r   r   rQ   r   r     s   r   c                   @   s   e Zd ZdZdZdS )ra   z]
    raised when the user requests the current cython function, which is
    unavailable
    z7Current function is a function cygdb doesn't know aboutNr=   r>   r?   r   r   r   r   r   r   ra     s   ra   c                   @   s   e Zd ZdZdZdS )r   zi
    raised when the name of the C function could not be determined
    in the current C stack frame
    zDC function name could not be determined in the current C stack frameNr   r   r   r   r   r     s   r   c                       s.   e Zd ZdZd fdd	Zdd ZeZ  ZS )CythonParameterz*
    Base class for cython parameters
    Nc                    s6   | j j | _| _tt| ||| |d k	r2|| _d S r.   )rR   r   Zshow_docZset_docrI   r   r;   r   )r   r   command_classZparameter_classdefaultrQ   r   r   r;     s    zCythonParameter.__init__c                 C   s
   t | jS r.   )boolr   r   r   r   r   __bool__  s    zCythonParameter.__bool__)N)r=   r>   r?   r   r;   r   __nonzero__rS   r   r   rQ   r   r     s   r   c                   @   s   e Zd ZdZdS ) CompleteUnqualifiedFunctionNameszH
    Have 'cy break' complete unqualified function or method names.
    Nr=   r>   r?   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdS )ColorizeSourceCodez5
    Tell cygdb whether to colorize source code.
    Nr   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdS )TerminalBackgroundzJ
    Tell cygdb about the user's terminal background (light or dark).
    Nr   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdd ZdS )CythonParametersz
    Simple container class that might get more functionality in the distant
    future (mostly to remind us that we're dealing with parameters).
    c                 C   s@   t dtjtjd| _tdtjtjd| _tdtjtj	d| _
d S )NZcy_complete_unqualifiedTZcy_colorize_codeZcy_terminal_background_colorZdark)r   r   COMMAND_BREAKPOINTSZPARAM_BOOLEANcomplete_unqualifiedr   COMMAND_FILESr   r   ZPARAM_STRINGr   r   r   r   r   r;   %  s$    zCythonParameters.__init__N)r=   r>   r?   r   r;   r   r   r   r   r     s   r   c                   @   s.   e Zd ZdZejZedd Zedd Z	dS )CythonCommandz(
    Base class for Cython commands
    c                 C   s:   t | ds| || jf||S | || j| jf||S d S )Ncompleter_class)hasattrr   r   )clsZclsnamer   r   r   r   r   	_registerB  s    
zCythonCommand._registerc                 O   s0   t | dd }|r | | j|| | | j||S )Nalias)getattrr   r   r   )r   r   r   r   r   r   r   registerJ  s    zCythonCommand.registerN)
r=   r>   r?   r   r   COMMAND_NONEr   classmethodr   r   r   r   r   r   r   ;  s   
r   c                       s0   e Zd ZdZdZejZejZ	 fddZ
  ZS )CyCyaa  
    Invoke a Cython command. Available commands are:

        cy import
        cy break
        cy step
        cy next
        cy run
        cy cont
        cy finish
        cy up
        cy down
        cy select
        cy bt / cy backtrace
        cy list
        cy print
        cy set
        cy locals
        cy globals
        cy exec
    rU   c                    s   t t| j|||dd tt t t t t	 t
 t t t t t t t t t tddt t tdtdtdtdd	}| D ]\}}| |_t| || q| | _i | _i | _ i | _!t"#t$| _%d S )
NT)r   zcy exec-cy-execcy_cnamert   	cy_linenocy_eval)import_Zbreak_stepnextruncontfinishupdownr   Zbtlistprint_rM   r6   exec__execrO   r   rt   r   r   )&rI   r   r;   dictCyImportr   CyBreakCyStepCyNextCyRunCyContCyFinishCyUpCyDownCySelectCyBacktraceCyListCyPrintCyLocals	CyGlobalsr   ZFixGdbCommandCyExecCySetCyCNameCyCValueCyLineCyEvalitemsrU   setattrcython_namespacefunctions_by_qualified_namerV   collectionsdefaultdictr   functions_by_name)r   r   r   r   commandscommand_namecommandrQ   r   r   r;   n  sH     
zCyCy.__init__)r=   r>   r?   r   r   r   r   r   ZCOMPLETE_COMMANDr   r;   rS   r   r   rQ   r   r   S  s
   r   c                   @   s.   e Zd ZdZdZejZejZ	e
jdd ZdS )r   zb
    Import debug information outputted by the Cython compiler
    Example: cy import FILE...
    z	cy importc                 C   s  t |tr|t}t|D ]}zt|}W n: tk
rh } ztd||j	d f W 5 d }~X Y nX t
|}| D ]}tf |j}|| jj|j< |dD ]}	|	j}
tf |
|j|
d < q|dD ]}tf d|i|j}|j}|j}| jj| | || jj|j< || jj|j< | }
|j|< |dD ] }|j}
tf |
|j|
d < q:|dD ]}|j}
|j|
d  qf|j !d	d
 |dD  q|dD ]b}t"|jd }|jd }t#t$t"|jd % }t&||j'||f< |D ]}||f|j(|< qqq|qd S )NzUnable to open file %r: %srf   ZGlobalsr   Z	FunctionsrJ   ZLocalsZStepIntoFunctionsc                 s   s   | ]}|j V  qd S r.   )tag)rz   Zfuncargr   r   r   r|     s    z"CyImport.invoke.<locals>.<genexpr>	ArgumentsZLineNumberMapping
src_linenosrc_path	c_linenos))r/   BYTESdecode_filesystemencodingr   r   OSErrorr   r   r   r   parsegetrootr3   attribrU   r  r   findr@   r6   rF   rB   r  appendr  rV   rA   r9   rM   rP   addrN   extendrD   r   mapr   minr7   r8   )r   r   from_ttyrv   r   etrJ   cython_modulevariabledr   cython_functionr   qnamelocalZstep_into_funcmarkerr  r  r  c_linenor   r   r   ru     s\    

*



zCyImport.invokeN)r=   r>   r?   r   r   r   ZCOMMAND_STATUSr   ZCOMPLETE_FILENAMEr   r   dont_suppress_errorsru   r   r   r   r   r     s   r   c                   @   sF   e Zd ZdZdZejZdd Zdd Z	e
jdd Ze
jd	d
 ZdS )r   a  
    Set a breakpoint for Cython code using Cython qualified name notation, e.g.:

        cy break cython_modulename.ClassName.method_name...

    or normal notation:

        cy break function_or_method_name...

    or for a line number:

        cy break cython_module:lineno...

    Set a Python breakpoint:
        Break on any function or method named 'func' in module 'modname'

            cy break -p modname.func...

        Break on any function or method named 'func'

            cy break -p func...
    zcy breakc                 C   s   | d\}}}t|}|r*| jj| }n
|  j}|j|f|jkrr|j|j|f }d|j|f }t	
d|  n
t	dd S )N:z%s:%sbreak z5Not a valid line number. Does it contain actual code?)	partitionrD   rU   r  rc   rJ   r4   r7   r5   r   r(   r   )r   r   
modulename_rE   r$  r+  
breakpointr   r   r   
_break_pyx  s    
zCyBreak._break_pyxc                 C   sr  | j j|}|r|jrd }|g}|s:| j j|p8g }dd |D }|s^td|  d S t|dkr0td t	|D ]\}}td||j
f  q|ztd}W n tk
r   Y d S X | dkrd S | d	kr|}q:q| r$d
t|  krt|k r$n n|t| g}q:qtd qn
|d
 g}|D ].}td|j  |jr>td|j  q>d S )Nc                 S   s   g | ]}|j s|qS r   rL   rz   r   r   r   r   
<listcomp>  s      z+CyBreak._break_funcname.<locals>.<listcomp>r.  rf   z"There are multiple such functions:z%3d) %szMSelect a function, press 'a' for all functions or press 'q' or '^D' to quit: qr   r   zNot understood...zbreak %s)rU   r  r#   rL   r  r   r(   lenr   r   rB   inputEOFErrorlowerisdigitrD   rA   rK   )r   funcnamefuncZbreak_funcsfuncsr   rb   r   r   r   _break_funcname  sN    

 

zCyBreak._break_funcnamec                 C   s|   t |tr|t}t|}|dr8|dd  }d}nd}|D ]6}|rXtd|  q@d|krl| | q@| 	| q@d S )Nz-prf   TFzpy-break %sr-  )
r/   r  r  r  r   
startswithr   r(   r3  r@  )r   Zfunction_namesr!  argvZpython_breakpointsr=  r   r   r   ru   B  s    


zCyBreak.invokec                    s   dd | j j D }dd | j j D }tjr@t||}n|}| 	 }|r`d|d krt
|d t  	 fdd|D S |d   fdd|D }t tkrt t fdd|D }|S )	Nc                 S   s&   g | ]\}}t d d |D r|qS )c                 s   s   | ]}|j  V  qd S r.   r4  r5  r   r   r   r|   Z  s     z.CyBreak.complete.<locals>.<listcomp>.<genexpr>)any)rz   nLr   r   r   r6  Y  s    z$CyBreak.complete.<locals>.<listcomp>c                 S   s   g | ]\}}|j s|qS r   r4  )rz   rD  r   r   r   r   r6  [  s    .c                    s"   g | ]}| r| kr|qS r   rA  rz   rD  )r   wordr   r   r6  g  s    
 c                    s   g | ]}|  r|qS r   rH  rI  )lastwordr   r   r6  l  s     
 c                    s   g | ]}| d  qS r.   r   rI  )strip_prefix_lengthr   r   r6  r  s     )rU   r  r  r  r   r   r   chainstripr   rO   r8  )r   textrJ  namesqnamesZ	all_nameswordsZcomplr   )rK  r   rL  rJ  r   completeU  s    zCyBreak.completeN)r=   r>   r?   r   r   r   r   r   r3  r@  r   r,  ru   rS  r   r   r   r   r     s   1
r   c                       sH   e Zd ZdZ fddZ fddZ fddZdd	 Zd
d Z  Z	S )
CythonInfozM
    Implementation of the interface dictated by libpython.LanguageInfo.
    c                    s(   |  |r| |d S tt| |S Nrf   )r$   re   rI   rT  rE   rW   rQ   r   r   rE   |  s    
zCythonInfo.linenoc                    s>   zt t| |}W n tjk
r,   Y d S X | p8d S d S r.   )rI   rT  ro   r   r   rN  )r   r   r_   rQ   r   r   ro     s
    zCythonInfo.get_source_linec                    s   | j rtt| |S d S r.   )r'   rI   rT  exc_inforW   rQ   r   r   rV    s    zCythonInfo.exc_infoc                 C   s   |   r|  jS dS )Nr   )r$   rc   rP   r   r   r   r   runtime_break_functions  s    
z"CythonInfo.runtime_break_functionsc                 C   s   dg}| | jj |S )NrX   )r  rU   rV   )r   rb   r   r   r   static_break_functions  s    z!CythonInfo.static_break_functions)
r=   r>   r?   r   rE   ro   rV  rW  rX  rS   r   r   rQ   r   rT  w  s   	rT  c                   @   s   e Zd Zedd ZdS )CythonExecutionControlCommandc                 C   s   | | j tS r.   )r   cython_info)r   r   r   r   r     s    z&CythonExecutionControlCommand.registerN)r=   r>   r?   r   r   r   r   r   r   rY    s   rY  c                   @   s&   e Zd ZdZdZdZejdd ZdS )r   z&Step through Cython, Python or C code.zcy -stepTc                 C   sV   |   r| | j n<|  sD| jr*d}nd}| tj|dd n| j| jd d S )Nr   r   TZ	to_string)stepinto)r'   Zpython_stepr\  r$   Zfinish_executingr   r(   r   )r   r   r!  r  r   r   r   ru     s    zCyStep.invokeN)	r=   r>   r?   r   r   r\  r   r,  ru   r   r   r   r   r     s
   r   c                   @   s   e Zd ZdZdZdZdS )r   z#Step-over Cython, Python or C code.zcy -nextFN)r=   r>   r?   r   r   r\  r   r   r   r   r     s   r   c                   @   s    e Zd ZdZdZeejZ	dS )r   z
    Run a Cython program. This is like the 'run' command, except that it
    displays Cython or Python source lines as well
    zcy runN)
r=   r>   r?   r   r   r   r,  rY  r   ru   r   r   r   r   r     s   r   c                   @   s    e Zd ZdZdZeejZ	dS )r   z
    Continue a Cython program. This is like the 'run' command, except that it
    displays Cython or Python source lines as well.
    zcy contN)
r=   r>   r?   r   r   r   r,  rY  r   ru   r   r   r   r   r     s   r   c                   @   s    e Zd ZdZdZeejZ	dS )r   z-
    Execute until the function returns.
    z	cy finishN)
r=   r>   r?   r   r   r   r,  rY  r   ru   r   r   r   r   r     s   r   c                   @   s&   e Zd ZdZdZdZejdd ZdS )r   z5
    Go up a Cython, Python or relevant C frame.
    zcy upr   c              
   G   s   z4t j| jdd | t  s2t j| jdd qW n. tk
rb } zt j|j W 5 d }~X Y nX t  }d}|r| }|d7 }qp| j	|d d d S )NTr[  r   rf   )r   )
r   r(   _commandr)   r   r   r   r   rp   r   )r   r   r"  r   r   r   r   r   ru     s    
zCyUp.invokeN)	r=   r>   r?   r   r   r]  r   r,  ru   r   r   r   r   r     s
   r   c                   @   s   e Zd ZdZdZdZdS )r   z7
    Go down a Cython, Python or relevant C frame.
    zcy downr   N)r=   r>   r?   r   r   r]  r   r   r   r   r     s   r   c                   @   s"   e Zd ZdZdZejdd ZdS )r   z
    Select a frame. Use frame numbers as listed in `cy backtrace`.
    This command is useful because `cy backtrace` prints a reversed backtrace.
    z	cy selectc              
   C   s   zt |}W n$ tk
r0   td|f Y nX t }| rL| }q:t|}ztd|| d f  W n. t	k
r } ztj|j
 W 5 d }~X Y nX d S )NzNot a valid number: %rz	select %drf   )rD   
ValueErrorr   r   r   newerr   
stackdepthr(   r   r   )r   Zstacknor!  r   r`  r"  r   r   r   ru     s    

zCySelect.invokeN)r=   r>   r?   r   r   r   r,  ru   r   r   r   r   r      s   r   c                   @   s6   e Zd ZdZdZdZejZej	Z
ejedd ZdS )r   zPrint the Cython stackzcy btzcy backtracec                 C   s|   t  }| r| }q|dk}d}|rxz| |}W n tk
rP   d}Y nX |sZ|rf| || |d7 }| }q&d S )Nz-ar   Frf   )r   r   rp   r)   r   r   r_  )r   r   r!  r   Z	print_allr   Zis_relevantr   r   r   ru   #  s    

zCyBacktrace.invokeN)r=   r>   r?   r   r   r   r   COMMAND_STACKr   COMPLETE_NONEr   r   r,  r%   ru   r   r   r   r   r     s   r   c                   @   s.   e Zd ZdZdZejZejZ	e
jdd ZdS )r   za
    List Cython source code. To disable to customize colouring see the cy_*
    parameters.
    zcy listc                 C   s2   |   \}}|j|d |d |dd}t| d S )N   T)r   r   )rl   rm   r   )r   r1  r!  sdrE   sourcer   r   r   ru   E  s
    zCyList.invokeN)r=   r>   r?   r   r   r   r   r   rb  r   r   r,  ru   r   r   r   r   r   ;  s   r   c                   @   s0   e Zd ZdZdZejZej	dd Z
dd ZdS )r   zT
    Print a Cython variable using 'cy-print x' or 'cy-print module.function.x'
    zcy printc                 C   s  |   }|  jj}||kr>|| tj}td||f  n||kr|| j}zt	
|}W n  tk
r~   td|  Y n"X |js| || ntd|  np|  rt	d| S |  r| jj|d}|D ]}|dkr| }q qq| || nt	d|  d S )Nz%s = %szunable to get value of %sz%s is optimized outz	py-print *zprint )r   rc   rJ   r6   get_truncated_reprr   MAX_OUTPUT_LENr   rA   r   r   r   r[   r   r'   r(   r$   rU   rt   ru   lstripdereference)r   r   r!  global_python_dictmodule_globalsr   rA   cr   r   r   ru   V  s0    


zCyPrint.invokec                 C   s,   |   r$|  }tt|j|jS g S d S r.   )r$   rc   r   r   rM  rM   r6   )r   r   r   r   r   rS  y  s    zCyPrint.completeN)r=   r>   r?   r   r   r   COMMAND_DATAr   r   r,  ru   rS  r   r   r   r   r   N  s   
"r   c                 C   s   | d   S )Nr   )r;  )itemr   r   r   rx     ry   rx   c                   @   s:   e Zd ZdZdZejZejZ	e
jeddddd ZdS )	r   z8
    List the locals from the current Cython frame.
    z	cy localszinfo localsz	py-localsr,   c           	      C   s   |   }|jr"| jj|| d S |j}tt|td}t|	 t
dD ]>\}}| |   |jrHt|j}|jsH| |j||d qHd S )Nkeyr   )rc   rL   rU   r6   ru   rM   r8  r   sortedr  sortkeyr   r   r   r   rA   r[   r   )	r   r   r!  r'  Zlocal_cython_varsr   r   r   r   r   r   r   ru     s    
 zCyLocals.invokeNr=   r>   r?   r   r   r   ra  r   rb  r   r   r,  r-   ru   r   r   r   r   r     s   
r   c                   @   s:   e Zd ZdZdZejZejZ	e
jeddddd ZdS )	r   z:
    List the globals from the current Cython module.
    z
cy globalszinfo variablesz
py-globalsr,   c              	   C   s  |   }|  jj}d}d}|r0tt|td}|r@tt|}t||}t }td t|	 t
dD ]0\}	}
|
tj}
||	 td||	|
f  qhtd t|	 t
dD ]N\}}||krzt|j}W n tk
r   Y qX |js| |j||d qd S )Nr   rp  zPython globals:z    %-*s = %sz
C globals:r~   )r   rc   rJ   r6   r8  r   rO   r   rr  r  rs  rg  r   rh  r  r   r   rA   r   r[   r   r   )r   r   r!  rk  rl  Zmax_globals_lenZmax_globals_dict_lenr   r   r   r   r   r   r   r   r   r   ru     s6    


 zCyGlobals.invokeNrt  r   r   r   r   r     s   
r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )EvaluateOrExecuteCodeMixinz
    Evaluate or execute Python code in a Cython or Python frame. The 'evalcode'
    method evaluations Python code, prints a traceback if an exception went
    uncaught, and returns any return value as a gdb.Value (NULL on exception).
    c           	   
   C   s   |   }|j D ]\}}|jtkr| ||rzt|j}W n t	k
rX   Y qY n
X |j
rbq||}d|||jf }z&t|dk rtd tdW 5 || X qdS )zBFill a remotely allocated dict with values from the Cython C stackz
                    (PyObject *) PyDict_SetItem(
                        (PyObject *) %d,
                        (PyObject *) %d,
                        (PyObject *) %s)
                r   zPyErr_Print()zUnable to execute Python code.N)rc   rM   r  rC   r
   r   r   r   rA   r   r[   Zalloc_pystringxdecrefr   )	r   executorZlocal_dict_pointerrq   r   r   r{   Z	pystringpr   r   r   r   _fill_locals_dict  s(    





z,EvaluateOrExecuteCodeMixin._fill_locals_dictc                 C   sD   t  }|r6| |s | |r,|  |S | }qt dd S )Nz0There is no Cython or Python frame on the stack.)r   r   r$   r'   r   rp   r   rW   r   r   r   "_find_first_cython_or_python_frame  s    

z=EvaluateOrExecuteCodeMixin._find_first_cython_or_python_framec                 C   sf   t  T td}td}z&| |t | |||||}W 5 |t | X W 5 Q R X |S )Nz&(PyObject *) PyModule_GetDict(__pyx_m)z(PyObject *) PyDict_New())r   ZFetchAndRestoreErrorr   r   rv  Zpointervaluerx  evalcode)r   rw  r   
input_typeZglobal_dictZ
local_dictrb   r   r   r   _evalcode_cython   s    


z+EvaluateOrExecuteCodeMixin._evalcode_cythonc                 C   s6   |   }t }| |r(t|||S | |||S )zi
        Evaluate `code` in a Python or Cython stack frame using the given
        `input_type`.
        )ry  r   PythonCodeExecutorr'   Z_evalcode_pythonr|  )r   r   r{  r   rw  r   r   r   rz    s
    
z#EvaluateOrExecuteCodeMixin.evalcodeN)r=   r>   r?   r   rx  ry  r|  rz  r   r   r   r   ru    s
    ru  c                   @   s.   e Zd ZdZdZejZejZ	e
jdd ZdS )r   zD
    Execute Python code in the nearest Python or Cython frame.
    r   c                 C   s.   |  |\}}t }|| ||j d S r.   )Zreadcoder   r}  rv  rz  ZPy_file_input)r   exprr!  r{  rw  r   r   r   ru   '  s    zCyExec.invokeN)r=   r>   r?   r   r   r   ra  r   rb  r   r   r,  ru   r   r   r   r   r     s   r   c                   @   s2   e Zd ZdZdZejZejZ	e
jedd ZdS )r   z
    Set a Cython variable to a certain value

        cy set my_cython_c_variable = 10
        cy set my_cython_py_variable = $cy_eval("{'doner': 'kebab'}")

    This is equivalent to

        set $cy_value("my_cython_variable") = 10
    zcy setc                 C   sR   | dd}t|dkr"td|\}}| jj| }td||f  d S )N=rf      z,Invalid expression. Use 'cy set var = expr'.zset %s = %s)	r   r8  r   r   rU   r   ru   rN  r(   )r   r~  r!  Zname_and_exprvarnamerA   r   r   r   ru   >  s    
zCySet.invokeN)r=   r>   r?   r   r   r   rn  r   rb  r   r   r,  r&   ru   r   r   r   r   r   .  s   r   c                   @   s(   e Zd ZdZejeedddZdS )r  z
    Get the C name of a Cython variable in the current context.
    Examples:

        print $cy_cname("function")
        print $cy_cname("Class.method")
        print $cy_cname("module.function")
    Nc                 C   s   |p
t  }d }| |r| |}||jkr<|j| j}nF||jjkrX|jj| j}n*d|jj|f }||jj	kr|jj	| j}|s| j
j|}|st d| |S )Nz%s.%szNo such Cython variable: %s)r   r   r$   rc   rM   rA   rJ   r6   r   r9   rU   r  r#   r   )r   cynamer   rA   r'  r(  r   r   r   ru   V  s     


zCyCName.invoke)N)	r=   r>   r?   r   r   r,  r&   r2   ru   r   r   r   r   r  L  s
   	r  c                       s0   e Zd ZdZejeed fdd	Z  Z	S )r  z-
    Get the value of a Cython variable.
    Nc                    s`   |   }| |}| ||r<tt| j||d}t|S ||krN|| jS t	d| d S )Nrs   zVariable %s is not initialized.)
r   rc   r   rI   r  ru   r   r   Z_gdbvalr   )r   r  r   Zglobals_dictr'  rA   rQ   r   r   ru   v  s    


zCyCValue.invoke)N)
r=   r>   r?   r   r   r,  r&   r2   ru   rS   r   r   rQ   r   r  q  s
   r  c                   @   s"   e Zd ZdZejedd ZdS )r  z&
    Get the current Cython line.
    c                 C   s   |   d S rU  )re   r   r   r   r   ru     s    zCyLine.invokeN)r=   r>   r?   r   r   r,  r&   ru   r   r   r   r   r    s   r  c                   @   s"   e Zd ZdZejedd ZdS )r  zO
    Evaluate Python code in the nearest Python or Cython frame and return
    c                 C   s   t jj}| ||S r.   )r   r}  ZPy_eval_inputrz  )r   Zpython_expressionr{  r   r   r   ru     s    zCyEval.invokeN)r=   r>   r?   r   r   r,  r2   ru   r   r   r   r   r    s   r  c                   C   s    t tdtjtjf  d S )Nz        define cy step
        cy -step
        end

        define cy next
        cy -next
        end

        document cy step
        %s
        end

        document cy next
        %s
        end
    )r   Zsource_gdb_scriptr   r   r   r   r   r   r   r   r   register_defines  s    
r  )T)N)er   
__future__r   	raw_inputr9  	NameErrorr   r   r   r   r	  r   unicodeUNICODEr   r  r   lxmlr   Z	have_lxmlImportErrorZ	xml.etreer   r   Zelementtree.ElementTreeZpygments.lexersrh   Zpygments.formattersstderrr   r   r   shlexr   ZCython.Debuggerr   r	   r
   r   Z_data_typesgetfilesystemencodingr  r"   r&   r-   r%   r2   objectr3   r@   rF   rT   rj   r   r   ra   r   	Parameterr   r   r   r   r   r   Commandr   r   r   r   Z
PythonInforT  ZExecutionControlCommandBaserY  ZPythonStepperMixinr   r   r   r   r   r   r   r   r   r   r   rs  r   r   ru  ZPyExecr   r   ZFunctionr  r  r  r  rZ  r   rU   r  r   r   r   r   <module>   s   
$


 ]?
QB %
		 3-Q%