U
    {h                     @   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ZzddlmZ dZ	W n$ e
k
rl   ddlmZ dZ	Y nX zddlZddlZW n$ e
k
r   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dddZdd ZdddZ dd Z!dd Z"dddZ#G dd dZ$G dd dZ%G d d! d!e%Z&d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7hZ'd8d9 Z(G d:d; d;Z)G d<d= d=Z*G d>d? d?ej+Z,G d@dA dAe,Z-G dBdC dCe-Z.G dDdE dEej/Z0G dFdG dGe0Z1G dHdI dIe0Z2G dJdK dKe0Z3G dLdM dMZ4e4 Z5G dNdO dOej6e)Z7G dPdQ dQe7Z8G dRdS dSe7Z9G dTdU dUe7Z:G dVdW dWe)ej;Z<G dXdY dYe7ej=Z>G dZd[ d[e>ej?Z@G d\d] d]e@ZAG d^d_ d_e>ZBG d`da dae>ZCG dbdc dce>ZDG ddde dee7ZEG dfdg dgeEZFG dhdi die7ZGG djdk dke7ZHG dldm dme7ZIG dndo doe7ZJdpdq ZKG drds dse7ZLG dtdu dueLZMG dvdw dwZNG dxdy dye7ejOeNZPG dzd{ d{e7ZQG d|d} d}ejRe)ZSG d~d deSZTG dd dejRe)ZUG dd dejRe)eNZVe< ZWe8X ZYeYeW_Ydd ZZeZ  dS )z)
GDB extension that adds Cython support.
    N)etreeT)ElementTreeFz,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-fhl22ezh/Cython/Debugger/libcython.pywrapper/   s    z>default_selected_gdb_frame.<locals>.decorator.<locals>.wrapper)N	functoolswrapsr   r   r   r   r   	decorator.   s    
z-default_selected_gdb_frame.<locals>.decoratorr   )r   r   r   r   r   default_selected_gdb_frame-   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   ?   s    

z%require_cython_frame.<locals>.wrapper)r   r   require_running_programr   r   r   r   require_cython_frame>   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   L   s    z5dispatch_on_frame.<locals>.decorator.<locals>.wrapperr   r   r(   r)   r   r   r   K   s    z$dispatch_on_frame.<locals>.decoratorr   )r(   r)   r   r   r*   r   dispatch_on_frameJ   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   `   s
    z(require_running_program.<locals>.wrapperr   r   r   r   r   r#   _   s    r#   c                    s   t   fdd}|S )Nc                    s&   t |tjr| } | |f||S N)
isinstancer
   Valuestring)r   r/   r   r   r   r   r   r   l   s    z.gdb_function_value_to_unicode.<locals>.wrapperr   r   r   r   r   gdb_function_value_to_unicodek   s    r0   c              
      s   ddl }t|jj}r0fdd|D n| d}|r^tt } fdd|D }dfdd	}d	j	j
d
d|d |df|d|S )a7  Prints out all instance variables needed to recreate an object.

    Following the python convention for __repr__, this function prints all the
    information stored in an instance as opposed to its class. The working
    assumption is that most initialization arguments are stored as a property
    using the same name.

    The object contents are displayed as the initialization call followed by,
    optionally, the value of each of the instance's properties in the form:
    ```
    ClassName(
            init_arg_1 = "repr of some example str",
            ...
        )
    self.state_based_property = ...
    ```

    Function arguments:
    self        Instance to be represented

    renamed     Dictionary of initialization arguments that are stored under a
                different property name in the form { argument: property }

    state       Boolean representing whether properties outside the
                initialization parameters should be printed (self.prop = ...).
                Using `False` may make the class more amenable to recursive repr
    r   Nc                    s   g | ]}  ||qS r   )r!   ).0arg)renamedr   r   
<listcomp>   s     zsimple_repr.<locals>.<listcomp>r   c                    s   g | ]}| kr|qS r   r   )r1   attr)
init_attrsr   r   r4      s      c                 3   sF   t ||p
|D ]2\}}tt |dd}|  | d| V  qd S )N

		z = )zipreprgetattrreplace)prefixattrsr   r5   r2   paramr   r   r   names_and_values   s    z%simple_repr.<locals>.names_and_values (,r8   z
	)z
self.)N)inspecttuple	signature__init__
parameterssortedvarskeysjoin	__class____qualname__)r   r3   staterE   Zinit_arg_namesZ
state_reprZinstance_attrsrA   r   )r6   r3   r   r   simple_reprx   s&     rQ   c                   @   s   e Zd Zd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_namerS   rT   r   r   r   rH      s    zCythonModule.__init__c                 C   s   t | ddiddS )NrY   r   F)r3   rP   rQ   r@   r   r   r   __repr__   s    zCythonModule.__repr__N__name__
__module__rO   rH   r[   r   r   r   r   rR      s   rR   c                   @   s   e Zd Zdd Zdd ZdS )CythonVariablec                 C   s&   || _ || _|| _|| _t|| _d S r,   )r   cnamequalified_nametypeintlineno)r   r   r`   ra   rb   rd   r   r   r   rH      s
    zCythonVariable.__init__c                 C   s   t | S r,   rZ   r@   r   r   r   r[      s    zCythonVariable.__repr__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	           	         sB   t  ||||| || _|| _|dk| _i | _g | _t | _d S )NTrue)	superrH   modulepf_cnameis_initmodule_functionlocals	argumentssetstep_into_functions)	r   ri   r   r`   rj   ra   rd   rb   rk   rN   r   r   rH      s    	
zCythonFunction.__init__)r]   r^   rO   r   rH   __classcell__r   r   rp   r   re      s   re   zFrame.is_validz
Frame.namezFrame.architecturez
Frame.typezFrame.pczFrame.blockzFrame.functionzFrame.olderzFrame.newerzFrame.find_salzFrame.selectzFrame.static_linkzFrame.levelzFrame.languagezSymbol.is_validzSymbol.valuezSymtab_and_line.is_validzSymtab.is_validzSymtab.fullnamezSymtab.global_blockzSymtab.static_blockzSymtab.linetablec                 C   s   |  d}t t| D ]}|dr&qt| |}t|rH|jtkrH| }t|tj	tj
tjfkrvt|ddd}|| dt|tr|dkr|ddn| d 7 }q|S )a  Returns a string representing the internal state of a provided GDB frame
    https://sourceware.org/gdb/current/onlinedocs/gdb.html/Frames-In-Python.html

    Created to serve as GDB.Frame.__repr__ for debugging purposes. GDB has many
    layers of abstraction separating the state of the debugger from the
    corresponding source code. This prints a tree of instance properties,
    expanding the values for Symtab_and_line, Symbol, and Symtab.

    Most of these properties require computation to determine, meaning much of
    relevant info is behind a monad, a subset of which are evaluated.

    Arguments
    frame       The GDB.Frame instance to be represented as a string
    r7   __z
	z: linex)rJ   dir
startswithr;   callablerO   frame_repr_whitelistrb   r
   ZSymtab_and_lineZSymbolZSymtab
frame_reprrstripr<   r-   rc   )r   res	attributevaluer   r   r   ry      s    



ry   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   r   r   r   r   get_c_function_name(  s    zCythonBase.get_c_function_namec                 C   s
   |  jS r,   )find_salrs   r   r   r   r   get_c_lineno,  s    zCythonBase.get_c_linenoc                 C   s$   | j j| }|d kr t |S r,   )r   r   r!   r   NoCythonFunctionInFrameError)r   r   resultr   r   r   get_cython_function0  s    zCythonBase.get_cython_functionc                 C   s    |  |}|jj| |dS )z
        Get the current Cython line number. Returns ("<no filename>", 0) if there is no
        correspondence between the C and Cython code.
        )z<no filename>r   )r   ri   rW   r!   r   )r   r   cyfuncr   r   r   get_cython_lineno8  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"   r   ri   rS   r   AssertionErrorpygmentsZlexersZCythonLexerr%   r   r   r   r
   r   Zcurrent_line_numZPythonLexerr   Zsymtabfullnamers   ZCLexerSourceFileDescriptor)r   r   rS   rd   lexerZfilename_and_linenoZpyframeobjectZsymbol_and_line_objr   r   r   get_source_descA  s6    




zCythonBase.get_source_descc                 C   s   |   \}}||S r,   )r   
get_source)r   r   source_descrd   r   r   r   get_source_lined  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%   r   ro   )r   r   r   Zolder_framecython_funcr   r   r   r'   i  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|| 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_cr   c                    s   j jj|  dS )Nr   )r   	cy_cvalueinvoke)r2   r   r   r   r   <lambda>      z-CythonBase.print_stackframe.<locals>.<lambda>r   z, c                 s   s   | ]\}}d ||f V  qdS )z%s=%sNr   )r1   r   valr   r   r   	<genexpr>  s     z.CythonBase.print_stackframe.<locals>.<genexpr>z#%-2d 0x%016x in %s(%s)z	 at %s:%sr7       ) r
   r   selectr   r   printr%   r   r   r   r   print_stackframeco_namer"   r   r   r`   parse_and_evalr   addressr-   rc   strbytesr   rM   sysstdoutwriterS   r   r   )r   r   indexr   r   r   rd   r   	func_nameZ
func_cname	func_argsr   fZ	gdb_valueZfunc_addressar   r   r   r   z  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_dict  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_ptrrn   	iteritemsZproxyval)r   Zremote_dictZpyobject_dictr   seenkvr   r   r   get_cython_globals_dict  s    z"CythonBase.get_cython_globals_dictNrB   c                 C   sT   t |rd}nd|jf }|d kr:td||||f  ntd|||||f  d S )NrB   z(%s) z%s%s = %s%sz%s%-*s = %s%s)r   Zpretty_printer_lookuprb   r   )r   r   r}   max_name_lengthr=   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 )Nr   z->TF)	rl   r   r`   rd   rb   r   rc   r
   r   )r   r   Z
local_namecyvarZ
cur_linenor   r   r   is_initialized  s    



zCythonBase.is_initialized)F)NrB   )r]   r^   rO   r    r"   r%   r   r   r   r   r   r   r'   r   r   r   r   r   r   r   r   r   r~     s0   







"

A
r~   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 )r   Nc                 C   s   || _ || _|| _d S r,   )rS   r   	formatter)r   rS   r   r   r   r   r   rH     s    zSourceFileDescriptor.__init__c                 C   s
   | j d k	S r,   )rS   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)
r   r   rI   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 )Nr   > z%s %4d    %s)	openrS   r   read
splitlines	itertoolsislice	enumeraterz   )r   startstop
lex_source	mark_line
lex_entirer   sliceidxrs   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 coder   r7   )r
   r   rS   maxrM   r   OSError)r   r   r   r   r   r   excr   r   r   r     s    

zSourceFileDescriptor.get_source)N)NTr   F)r]   r^   rO   rH   r   r   r   r   r   r   r   r   r     s   
  r   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 j|  d S r,   )msgrh   rH   )r   r   rp   r   r   rH   7  s    zCyGDBError.__init__)r]   r^   rO   __doc__rH   rq   r   r   rp   r   r   2  s   r   c                   @   s   e Zd ZdZdZdS )r   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^   rO   r   r   r   r   r   r   r   <  s   r   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   D  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                    s2   | j j | _| _t ||| |d k	r.|| _d S r,   )rN   r   Zshow_docZset_docrh   rH   r}   )r   r   command_classZparameter_classdefaultrp   r   r   rH   T  s    
zCythonParameter.__init__c                 C   s
   t | jS r,   )boolr}   r@   r   r   r   __bool__[  s    zCythonParameter.__bool__)N)r]   r^   rO   r   rH   r   __nonzero__rq   r   r   rp   r   r   O  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^   rO   r   r   r   r   r   r   b  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   h  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   n  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   rH   z  s$    zCythonParameters.__init__N)r]   r^   rO   r   rH   r   r   r   r   r   t  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   	_register  s    
zCythonCommand._registerc                 O   s0   t | dd }|r | | j|| | | j||S )Nalias)r;   r   r   r   )r   r   r   r   r   r   r   register  s    zCythonCommand.registerN)
r]   r^   rO   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
    r   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_cnamer   	cy_linenocy_eval)import_Zbreak_stepnextruncontfinishupdownr   Zbtlistprint_rl   rU   exec__execrn   r   r   r   r   )&rh   r   rH   dictCyImportr   CyBreakCyStepCyNextCyRunCyContCyFinishCyUpCyDownCySelectCyBacktraceCyListCyPrintCyLocals	CyGlobalsr   ZFixGdbCommandCyExecCySetCyCNameCyCValueCyLineCyEvalitemsr   setattrcython_namespacefunctions_by_qualified_namer   collectionsdefaultdictr	  functions_by_name)r   r   r   r   commandscommand_namecommandrp   r   r   rH     sH     
zCyCy.__init__)r]   r^   rO   r   r   r
   r   r   ZCOMPLETE_COMMANDr   rH   rq   r   r   rp   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  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: %sr   ZGlobalsr   Z	Functionsri   ZLocalsZStepIntoFunctionsc                 s   s   | ]}|j V  qd S r,   )tag)r1   Zfuncargr   r   r   r   /  s    z"CyImport.invoke.<locals>.<genexpr>	ArgumentsZLineNumberMapping
src_linenosrc_path	c_linenos))r-   r   decode_filesystemencodingr   r   r   r
   r   r   r   parsegetrootrR   attribr   r%  r   findr_   rU   re   ra   r)  appendr&  r   r`   rX   rl   ro   addrm   extendrc   r	  mapr   minrV   rW   )r   r   from_ttyr2   r   etri   cython_modulevariabledr   cython_functionr   qnamelocalZstep_into_funcmarkerr/  r0  r1  c_linenor   r   r   r     s\    

*



zCyImport.invokeN)r]   r^   rO   r   r   r
   ZCOMMAND_STATUSr   ZCOMPLETE_FILENAMEr   r   dont_suppress_errorsr   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?)	partitionrc   r   r%  r   ri   rS   rV   rT   r
   r&   r   )r   r   
modulename_rd   r@  rG  
breakpointr   r   r   
_break_pyxV  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   rk   r1   r   r   r   r   r4   p  s      z+CyBreak._break_funcname.<locals>.<listcomp>rJ  r   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)r   r&  r!   rk   r)  r
   r&   lenr   r   ra   inputEOFErrorlowerisdigitrc   r`   rj   )r   funcnamefuncZbreak_funcsfuncsr   r   r   r   r   _break_funcnamef  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-pr   TFzpy-break %srI  )
r-   r   r2  r3  r   rv   r
   r&   rO  r[  )r   Zfunction_namesr=  argvZpython_breakpointsrX  r   r   r   r     s    


zCyBreak.invokec                    s   pddd | j j D }dd | j j D }tjrHt||}n|}| 	 }|rhd|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 )
NrB   c                 S   s&   g | ]\}}t d d |D r|qS )c                 s   s   | ]}|j  V  qd S r,   rP  rQ  r   r   r   r     s     z.CyBreak.complete.<locals>.<listcomp>.<genexpr>)any)r1   nLr   r   r   r4     s    z$CyBreak.complete.<locals>.<listcomp>c                 S   s   g | ]\}}|j s|qS r   rP  )r1   r^  r   r   r   r   r4     s    .c                    s"   g | ]}| r| kr|qS r   rv   r1   r^  )r   wordr   r   r4     s    
 c                    s   g | ]}|  r|qS r   rb  rc  )lastwordr   r   r4     s     
 c                    s   g | ]}| d  qS r,   r   rc  )strip_prefix_lengthr   r   r4     s     )r   r)  r#  r&  rI   r   r   chainstripr   rn   rS  )r   textrd  namesqnamesZ	all_nameswordsZcomplr   )re  r   rf  rd  r   complete  s     zCyBreak.completeN)r]   r^   rO   r   r   r
   r   r   rO  r[  r   rH  r   rm  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 |S Nr   )r"   r   rh   rd   r   rp   r   r   rd     s    
zCythonInfo.linenoc                    s:   zt  |}W n tjk
r(   Y d S X | p4d S d S r,   )rh   r   r
   r   rh  )r   r   rs   rp   r   r   r     s
    zCythonInfo.get_source_linec                    s   | j rt |S d S r,   )r%   rh   exc_infor   rp   r   r   rp    s    zCythonInfo.exc_infoc                 C   s   |   r|  jS dS )Nr   )r"   r   ro   r@   r   r   r   runtime_break_functions  s    
z"CythonInfo.runtime_break_functionsc                 C   s   dg}| | jj |S )Nr   )r:  r   r   )r   r   r   r   r   static_break_functions  s    z!CythonInfo.static_break_functions)
r]   r^   rO   r   rd   r   rp  rq  rr  rq   r   r   rp   r   rn    s   	rn  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^   rO   r   r   r   r   r   r   rs    s   rs  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_steprv  r"   Zfinish_executingr
   r&   r  )r   r   r=  r,  r   r   r   r     s    zCyStep.invokeN)	r]   r^   rO   r   r   rv  r   rH  r   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^   rO   r   r   rv  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^   rO   r   r   r   rH  rs  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
    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^   rO   r   r   r   rH  rs  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-
    Execute until the function returns.
    z	cy finishN)
r]   r^   rO   r   r   r   rH  rs  r  r   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 )NTru  r   r   )r   )
r
   r&   _commandr'   r   r   r   r   r   r   )r   r   r>  r   r   r   r   r   r   <  s    
zCyUp.invokeN)	r]   r^   rO   r   r   rw  r   rH  r   r   r   r   r   r  5  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^   rO   r   r   rw  r   r   r   r   r  N  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 %dr   )rc   
ValueErrorr
   r   r   newerr   
stackdepthr&   r   r   )r   Zstacknor=  r   rz  r>  r   r   r   r   _  s    

zCySelect.invokeN)r]   r^   rO   r   r   r   rH  r   r   r   r   r   r  W  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   Fr   )r
   r   r   r'   r   r   ry  )r   r   r=  r   Z	print_allr   Zis_relevantr   r   r   r   z  s    

zCyBacktrace.invokeN)r]   r^   rO   r   r   r   r
   COMMAND_STACKr   COMPLETE_NONEr   r   rH  r#   r   r   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   )r   r   r   )r   rM  r=  sdrd   sourcer   r   r   r     s
    zCyList.invokeN)r]   r^   rO   r   r   r
   r   r   r|  r   r   rH  r   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   r   ri   rU   get_truncated_reprr   MAX_OUTPUT_LENr   r`   r
   r   r   r   r   r%   r&   r"   r   r   r   lstripdereference)r   r   r=  global_python_dictmodule_globalsr}   r`   cr   r   r   r     s0    


zCyPrint.invokec                 C   s,   |   r$|  }tt|j|jS g S d S r,   )r"   r   r	  r   rg  rl   rU   )r   r   r   r   r   rm    s    zCyPrint.completeN)r]   r^   rO   r   r   r
   COMMAND_DATAr   r   rH  r   rm  r   r   r   r   r    s   
"r  c                 C   s   | d   S )Nr   )rV  )itemr   r   r   r     r   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  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keyrB   )r   rk   r   rU   r   rl   rS  r   rJ   r#  sortkeyr   r   r
   r   r`   r   r   )	r   r   r=  rC  Zlocal_cython_varsr   r   r   r}   r   r   r   r     s    
 zCyLocals.invokeNr]   r^   rO   r   r   r
   r{  r   r|  r   r   rH  r+   r   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   r  zPython globals:z    %-*s = %sz
C globals:r   )r   r   ri   rU   rS  r   rn   r   rJ   r#  r  r  r   r  r9  r
   r   r`   r   r   r   r   )r   r   r=  r  r  Zmax_globals_lenZmax_globals_dict_lenr   r   r   r   r   r   r}   r   r   r   r      s6    


 zCyGlobals.invokeNr  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)r   rl   r#  rb   r   r   r
   r   r`   r   r   Zalloc_pystringxdecrefr   )	r   executorZlocal_dict_pointerr   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   r   r   r   r   r   r   "_find_first_cython_or_python_frameK  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   r  Zpointervaluer  evalcode)r   r  r   
input_typeZglobal_dictZ
local_dictr   r   r   r   _evalcode_cythonW  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`.
        )r  r   PythonCodeExecutorr%   Z_evalcode_pythonr  )r   r   r  r   r  r   r   r   r  i  s
    
z#EvaluateOrExecuteCodeMixin.evalcodeN)r]   r^   rO   r   r  r  r  r  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  zD
    Execute Python code in the nearest Python or Cython frame.
    r   c                 C   s.   |  |\}}t }|| ||j d S r,   )Zreadcoder   r  r  r  ZPy_file_input)r   exprr=  r  r  r   r   r   r   ~  s    zCyExec.invokeN)r]   r^   rO   r   r   r
   r{  r   r|  r   r   rH  r   r   r   r   r   r  u  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=r      z,Invalid expression. Use 'cy set var = expr'.zset %s = %s)	r   rS  r
   r   r   r   r   rh  r&   )r   r  r=  Zname_and_exprvarnamer`   r   r   r   r     s    
zCySet.invokeN)r]   r^   rO   r   r   r
   r  r   r|  r   r   rH  r$   r   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"   r   rl   r`   ri   rU   r   rX   r   r&  r!   r   )r   cynamer   r`   rC  rD  r   r   r   r     s     


zCyCName.invoke)N)	r]   r^   rO   r   r   rH  r$   r0   r   r   r   r   r   r    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\   |   }| |}| ||r8t j||d}t|S ||krJ|| jS td| d S )Nr   zVariable %s is not initialized.)	r   r   r   rh   r   r
   r   Z_gdbvalr   )r   r  r   Zglobals_dictrC  r`   rp   r   r   r     s    


zCyCValue.invoke)N)
r]   r^   rO   r   r   rH  r$   r0   r   rq   r   r   rp   r   r     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 ro  )r   r@   r   r   r   r     s    zCyLine.invokeN)r]   r^   rO   r   r   rH  r$   r   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_inputr  )r   Zpython_expressionr  r   r   r   r     s    zCyEval.invokeN)r]   r^   rO   r   r   rH  r0   r   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)NT)[r   r   r   r   r   r'  r
   lxmlr   Z	have_lxmlImportErrorZ	xml.etreer   Zpygments.lexersr   Zpygments.formattersstderrr   r   r   shlexr   ZCython.Debuggerr   r   r   r  Z_data_typesgetfilesystemencodingr3  r    r$   r+   r#   r0   rQ   rR   r_   re   rx   ry   r~   r   r   r   r   r   	Parameterr   r   r   r   r   rI   Commandr   r   r  r  Z
PythonInforn  ZExecutionControlCommandBasers  ZPythonStepperMixinr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  ZPyExecr  r  ZFunctionr  r   r!  r"  rt  r   r   r  r   r   r   r   <module>   s   




1 ]?
QB %
		 3-Q%