o
    j                     @   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
y5   ddlmZ dZ	Y nw z
ddlZddlZW n e
yQ   dZejd Y nw eedr^d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 pydZdqddZdd ZdrddZ dd Z!dd Z"dsddZ#G dd dZ$G dd dZ%G d d! d!e%Z&h d"Z'd#d$ Z(G d%d& d&Z)G d'd( d(Z*G d)d* d*ej+Z,G d+d, d,e,Z-G d-d. d.e-Z.G d/d0 d0ej/Z0G d1d2 d2e0Z1G d3d4 d4e0Z2G d5d6 d6e0Z3G d7d8 d8Z4e4 Z5G d9d: d:ej6e)Z7G d;d< d<e7Z8G d=d> d>e7Z9G d?d@ d@e7Z:G dAdB dBe)ej;Z<G dCdD dDe7ej=Z>G dEdF dFe>ej?Z@G dGdH dHe@ZAG dIdJ dJe>ZBG dKdL dLe>ZCG dMdN dNe>ZDG dOdP dPe7ZEG dQdR dReEZFG dSdT dTe7ZGG dUdV dVe7ZHG dWdX dXe7ZIG dYdZ dZe7ZJd[d\ ZKG d]d^ d^e7ZLG d_d` d`eLZMG dadb dbZNG dcdd dde7ejOeNZPG dedf dfe7ZQG dgdh dhejRe)ZSG didj djeSZTG dkdl dlejRe)ZUG dmdn dnejRe)eNZVe< ZWe8X ZYeYeW_Ydodp ZZeZ  dS )tz)
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y   t dw  r | d u r t | |g|R i |S NzNo frame is currently selected.)gdbselected_frameRuntimeErrorGdbErrornameNoFunctionNameInFrameError)selfframeargskwargs)errfunction Z/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/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                    s<   | dpt }| |std | g|R i |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                    sh   |   }|  }|s|rs| g|R i | d S |r$t d S |  r/t  d S td)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                       t   fdd}|S )Nc                     s4   zt   W n ty   t dw  | i |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                    r-   )Nc                    s,   t |tjr
| } | |g|R i |S r   )
isinstancer
   Valuestring)r   r0   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   r1   c              
      s   ddl }t|jj}rfdd|D n| d}|r/tt } fdd|D }dfdd	}d	j	j
d
d|d |dg|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>       zsimple_repr.<locals>.<listcomp>r   c                    s   g | ]}| vr|qS r   r   )r2   attr)
init_attrsr   r   r5      r6   c                 3   sH    t ||p|D ]\}}tt |dd}|  | d| V  qd S )N

		z = )zipreprgetattrreplace)prefixattrsr   r7   r3   paramr   r   r   names_and_values   s
   z%simple_repr.<locals>.names_and_values (,r:   z
	)z
self.r   )inspecttuple	signature__init__
parameterssortedvarskeysjoin	__class____qualname__)r   r4   staterG   Zinit_arg_namesZ
state_reprZinstance_attrsrC   r   )r8   r4   r   r   simple_reprx   s$   rS   c                   @      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_namerV   rW   r   r   r   rJ      s   
zCythonModule.__init__c                 C   s   t | ddiddS )Nr\   r   F)r4   rR   rS   rB   r   r   r   __repr__   s   zCythonModule.__repr__N__name__
__module__rQ   rJ   r^   r   r   r   r   rU      s    rU   c                   @   rT   )CythonVariablec                 C   s&   || _ || _|| _|| _t|| _d S r   )r   cnamequalified_nametypeintlineno)r   r   rc   rd   re   rg   r   r   r   rJ      s
   zCythonVariable.__init__c                 C   s   t | S r   r]   rB   r   r   r   r^      s   zCythonVariable.__repr__Nr_   r   r   r   r   rb      s    rb   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)	superrJ   modulepf_cnameis_initmodule_functionlocals	argumentssetstep_into_functions)	r   rl   r   rc   rm   rd   rg   re   rn   rP   r   r   rJ      s   	
zCythonFunction.__init__)r`   ra   rQ   r   rJ   __classcell__r   r   rs   r   rh      s    rh   >   z
Frame.typezFrame.architecturezSymtab.static_blockzFrame.olderzSymtab_and_line.is_validzSymtab.is_validz
Frame.namezFrame.newerzSymtab.linetablezSymbol.is_validzFrame.languagezFrame.pczFrame.is_validzFrame.functionzFrame.selectzFrame.levelzSymtab.global_blockzFrame.static_linkzSymbol.valuezFrame.find_salzFrame.blockzSymtab.fullnamec                 C   s   |  d}t t| D ]K}|drqt| |}t|r$|jtv r$| }t|tj	tj
tjfv r;t|ddd}|| dt|trO|dkrO|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
    r9   __z
	z: linex)rL   dir
startswithr=   callablerQ   frame_repr_whitelistre   r
   ZSymtab_and_lineSymbolZSymtab
frame_reprrstripr>   r.   rf   )r   res	attributevaluer   r   r   r}      s   



r}   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v 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_salrv   r   r   r   r   get_c_lineno,  s   
zCythonBase.get_c_linenoc                 C   s$   | j j| }|d u 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   rl   rZ   r"   r   )r   r   cyfuncr   r   r   get_cython_lineno8  s   
zCythonBase.get_cython_linenoc                 C   s   d  } }}|  |r-| |jj}| |}||d ksJ |d }tr,tjjdd}nD| |rRt	
| }|s@td| }| }trQtjjdd}n| }|r[|js`d }d}n|j }|j}trqtjjdd}t|||fS )Nr      F)stripallz*Unable to read information on python frame)r#   r   rl   rV   r   pygmentslexersCythonLexerr&   r   r   r   r
   r   Zcurrent_line_numPythonLexerr   Zsymtabfullnamerv   CLexerSourceFileDescriptor)r   r   rV   rg   lexerZfilename_and_linenoZpyframeobjectZsymbol_and_line_objr   r   r   get_source_descA  s:   




zCythonBase.get_source_descc                 C   s   |   \}}||S r   )r   
get_source)r   r   source_descrg   r   r   r   get_source_lined  s   
zCythonBase.get_source_linec                 C   sN   |  }| }| |s| |rdS |r%| |r%| |}||jv S dS )zl
        returns whether we care about a frame on the user-level when debugging
        Cython code
        TF)r   olderr#   r&   r   rr   )r   r   r   Zolder_framecython_funcr   r   r   r(   i  s   

zCythonBase.is_relevant_functionc                    s  t  }   z	 \}}W n ty!   td|  Y dS w |sH rHt 	 }|du s8|
 r@j |ddS |j}d}	g }
n) rb } fdd}|j}|j}	g }
n \}}  }|}	g }
zt |	}W n ty   d}Y nw |j}t|tst|ttfst|}t| d d}d	d
d |
D }tjd||||f  |jdurtjd|j|f  tjd ztjd|| d W n
 t jy   Y nw |  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)r3   r   r   r   r   <lambda>  s    z-CythonBase.print_stackframe.<locals>.<lambda>r   z, c                 s   s     | ]\}}d ||f V  qdS )z%s=%sNr   )r2   r   valr   r   r   	<genexpr>  s    z.CythonBase.print_stackframe.<locals>.<genexpr>z#%-2d 0x%016x in %s(%s)z	 at %s:%sr9       ) r
   r   selectr   r   printr&   r   r   r   r   print_stackframeco_namer#   r   r   rc   parse_and_evalr   addressr.   rf   strbytesr   rO   sysstdoutwriterV   r   r   )r   r   indexr   r   r   rg   r   	func_nameZ
func_cnameZ	func_argsr   fZ	gdb_valueZfunc_addressar   r   r   r   z  s\   



zCythonBase.print_stackframec                 C   sN   t d}zt d}W n ty   t tdw || }|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_ptrrq   	iteritemsZproxyval)r   Zremote_dictZpyobject_dictr   seenkvr   r   r   get_cython_globals_dict  s   z"CythonBase.get_cython_globals_dictNrD   c                 C   sV   t |rd}nd|jf }|d u rtd||||f  d S td|||||f  d S )NrD   z(%s) z%s%s = %s%sz%s%-*s = %s%s)r   Zpretty_printer_lookupre   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v r&||jkr$|jtkr"tt|jS dS dS ||jkS )Nr   z->TF)	ro   r   rc   rg   re   r   rf   r
   r   )r   r   Z
local_namecyvarZ
cur_linenor   r   r   is_initialized  s   




zCythonBase.is_initialized)F)NrD   )r`   ra   rQ   r!   r#   r&   r   r   r   r   r   r   r(   r   r   r   r   r   r   r   r   r   r     s2    







"

A
r   c                   @   s<   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   )rV   r   	formatter)r   rV   r   r   r   r   r   rJ     s   
zSourceFileDescriptor.__init__c                 C   s
   | j d uS r   )rV   rB   r   r   r   valid     
zSourceFileDescriptor.validc                 C   sL   t r$| jr$tjr$tjj}| jd u rt jj|d}n| j}t 	|| j|S |S )N)bg)
r   r   rK   colorize_codeterminal_backgroundr   r   
formattersZTerminalFormatter	highlight)r   coder   r   r   r   r   lex  s   
zSourceFileDescriptor.lexc                 c   s    t | jJ}|r|r| |  }t||d |d }t|D ]$\}}	|| |kr0d}
nd}
|r;|s;| |	}	d|
|| |	 f V  q#W d    d S 1 sSw   Y  d S )Nr   > z%s %4d    %s)	openrV   r   read
splitlines	itertoolsislice	enumerater~   )r   startstop
lex_source	mark_line
lex_entirer   sliceidxrv   r?   r   r   r   _get_source  s   
"z SourceFileDescriptor._get_sourceTr   Fc              	   C   s\   t d}| js
|t|d}|d u r|d }zd| |||||W S  ty-   |w )NzUnable to retrieve source coder   r9   )r
   r   rV   maxrO   r   OSError)r   r   r   r   r   r   excr   r   r   r     s   

zSourceFileDescriptor.get_sourcer   )NTr   F)r`   ra   rQ   rJ   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   )msgrk   rJ   )r   r   rs   r   r   rJ   7  s   zCyGDBError.__init__)r`   ra   rQ   __doc__rJ   rt   r   r   rs   r   r   2  s    r   c                   @      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`   ra   rQ   r   r   r   r   r   r   r   <      r   c                   @   r   )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  r   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 ||| |d ur|| _d S d S r   )rP   r   Zshow_docZset_docrk   rJ   r   )r   r   command_classZparameter_classdefaultrs   r   r   rJ   T  s   

zCythonParameter.__init__c                 C   s
   t | jS r   )boolr   rB   r   r   r   __bool__[  r   zCythonParameter.__bool__r   )r`   ra   rQ   r   rJ   r   __nonzero__rt   r   r   rs   r   r   O  s
    r   c                   @      e Zd ZdZdS ) CompleteUnqualifiedFunctionNameszH
    Have 'cy break' complete unqualified function or method names.
    Nr`   ra   rQ   r   r   r   r   r   r   b      r   c                   @   r   )ColorizeSourceCodez5
    Tell cygdb whether to colorize source code.
    Nr   r   r   r   r   r   h  r   r   c                   @   r   )TerminalBackgroundzJ
    Tell cygdb about the user's terminal background (light or dark).
    Nr   r   r   r   r   r   n  r   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   rB   r   r   r   rJ   z  s$   
zCythonParameters.__init__N)r`   ra   rQ   r   rJ   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   sB   t | ds| || jg|R i |S | || j| jg|R i |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`   ra   rQ   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                    sN  t t| j|||dd tdi dt dt dt dt dt	 dt
 d	t d
t dt dt dt dt dt dt dt dtdddt dt dtddtddtddtd}| D ]\}}| |_t| || q| | _i | _i | _ i | _!t"#t$| _%d S )NT)r?   import_Zbreak_stepnextruncontfinishupdownr   Zbtlistprint_ro   rX   Zexec_zcy exec-cy-exec_execrq   cy_cnamer   Z	cy_linenoZcy_evalr   )&rk   r   rJ   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   commandsZcommand_namecommandrs   r   r   rJ     sp   	
zCyCy.__init__)r`   ra   rQ   r   r   r
   r  r   ZCOMPLETE_COMMANDr   rJ   rt   r   r   rs   r   r    s    r  c                   @   .   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y. } ztd||j	d f d }~ww t
|}| D ]}tdi |j}|| jj|j< |dD ]}	|	j}
tdi |
|j|
d < qN|dD ]h}tdd|i|j}|j}|j}| jj| | || jj|j< || jj|j< | }
|j|< |dD ]}|j}
tdi |
|j|
d < q|dD ]}|j}
|j|
d  q|j !d	d
 |dD  qe|dD ]/}t"|jd }|jd }t#t$t"|jd % }t&||j'||f< |D ]	}||f|j(|< qqq8qd S )NzUnable to open file %r: %sr   ZGlobalsr   Z	Functionsrl   ZLocalsZStepIntoFunctionsc                 s   s    | ]}|j V  qd S r   )tag)r2   Zfuncargr   r   r   r   /  s    
z"CyImport.invoke.<locals>.<genexpr>	ArgumentsZLineNumberMapping
src_linenosrc_path	c_linenosr   ))r.   r   decode_filesystemencodingr   r   r   r
   r   r   r   parseZgetrootrU   Zattribr   r.  r   findrb   rX   rh   rd   r2  appendr/  r   rc   r[   ro   rr   addrp   extendrf   r  mapr   minrY   rZ   )r   r   from_ttyr3   r   etrl   cython_modulevariabledr   cython_functionr   qnamelocalZstep_into_funcmarkerr8  r9  r:  c_linenor   r   r   r     sh   





zCyImport.invokeN)r`   ra   rQ   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v r:|j|j|f }d|j|f }t	
d|  d S t	d)N:z%s:%sbreak z5Not a valid line number. Does it contain actual code?)	partitionrf   r   r.  r   rl   rV   rY   rW   r
   r'   r   )r   r   
modulename_rg   rG  rN  
breakpointr   r   r   
_break_pyxV  s   

zCyBreak._break_pyxc                 C   s\  | j j|}|r|jrd }|g}|s| j j|pg }dd |D }|s.td|  d S t|dkrtd t	|D ]\}}td||j
f  q<	 ztd}W n
 ty[   Y d S w | d	krdd S | d
krm|}n!| rdt|  krt|k rn n|t| g}ntd qKn|d g}|D ]}td|j  |jrtd|j  qd S )Nc                 S   s   g | ]}|j s|qS r   rn   r2   r   r   r   r   r5   p  s    z+CyBreak._break_funcname.<locals>.<listcomp>rQ  r   z"There are multiple such functions:z%3d) %sTzMSelect 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"   rn   r2  r
   r'   lenr   r   rd   inputEOFErrorlowerisdigitrf   rc   rm   )r   funcnamefuncZbreak_funcsfuncsr   r   r   r   r   _break_funcnamef  sR   
 
zCyBreak._break_funcnamec                 C   s|   t |tr
|t}t|}|dr|dd  }d}nd}|D ]}|r,td|  q d|v r6| | q | 	| q d S )Nz-pr   TFzpy-break %srP  )
r.   r   r;  r<  r   ry   r
   r'   rV  rb  )r   Zfunction_namesrD  argvZpython_breakpointsr_  r   r   r   r     s   


zCyBreak.invokec                    s   pddd | j j D }dd | j j D }tjr$t||}n|}| 	 }|r4d|d vrKt
|d t  	 fdd|D S |d   fdd|D }t tkrqt t fd	d|D }|S )
NrD   c                 S   s&   g | ]\}}t d d |D r|qS )c                 s   s    | ]}|j  V  qd S r   rW  rX  r   r   r   r     s    z.CyBreak.complete.<locals>.<listcomp>.<genexpr>)any)r2   nLr   r   r   r5     s    
z$CyBreak.complete.<locals>.<listcomp>c                 S   s   g | ]	\}}|j s|qS r   rW  )r2   re  r   r   r   r   r5     s    
.c                    s"   g | ]}| r| vr|qS r   ry   r2   re  )r   wordr   r   r5     s    c                    s   g | ]	}|  r|qS r   ri  rj  )lastwordr   r   r5     s    c                    s   g | ]}| d  qS r   r   rj  )strip_prefix_lengthr   r   r5     r6   )r   r2  r,  r/  rK   r   r   chainstripr   rq   rZ  )r   textrk  namesZqnamesZ	all_nameswordsZcomplr   )rl  r   rm  rk  r   complete  s    zCyBreak.completeN)r`   ra   rQ   r   r   r
   r   r   rV  rb  r   rO  r   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 |S Nr   )r#   r   rk   rg   r   rs   r   r   rg     s   
zCythonInfo.linenoc                    s4   zt  |}W n tjy   Y d S w | pd S r   )rk   r   r
   r   ro  )r   r   rv   rs   r   r   r     s   zCythonInfo.get_source_linec                    s   | j r	t |S d S r   )r&   rk   exc_infor   rs   r   r   rv    s   zCythonInfo.exc_infoc                 C   s   |   r	|  jS dS )Nr   )r#   r   rr   rB   r   r   r   runtime_break_functions  s   
z"CythonInfo.runtime_break_functionsc                 C   s   dg}| | jj |S )Nr   )rA  r   r   )r   r   r   r   r   static_break_functions  s   z!CythonInfo.static_break_functions)
r`   ra   rQ   r   rg   r   rv  rw  rx  rt   r   r   rs   r   rt    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`   ra   rQ   r  r  r   r   r   r   ry    s    ry  c                   @   &   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   sZ   |   r| | j d S |  s$| jrd}nd}| tj|dd d S | j| jd d S )Nr
  r  TZ	to_string)stepinto)r&   Zpython_stepr}  r#   Zfinish_executingr
   r'   r
  )r   r   rD  r4  r   r   r   r     s   zCyStep.invokeN)	r`   ra   rQ   r   r   r}  r   rO  r   r   r   r   r   r    s    r  c                   @      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`   ra   rQ   r   r   r}  r   r   r   r   r    s    r  c                   @       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`   ra   rQ   r   r   r   rO  ry  r  r   r   r   r   r   r    s    r  c                   @   r  )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`   ra   rQ   r   r   r   rO  ry  r  r   r   r   r   r   r  "  s    r  c                   @   r  )r  z-
    Execute until the function returns.
    z	cy finishN)
r`   ra   rQ   r   r   r   rO  ry  r  r   r   r   r   r   r  ,  s    r  c                   @   r{  )r  z5
    Go up a Cython, Python or relevant C frame.
    zcy upr  c              
   G   s   z t j| jdd | t  st j| jdd | t  rW n ty2 } zt j|j d }~ww t  }d}|rE| }|d7 }|s;| j	|d d d S )NTr|  r   r   )r   )
r
   r'   _commandr(   r   r   r   r   r   r   )r   r   rE  r   r   r   r   r   r   <  s"   zCyUp.invokeN)	r`   ra   rQ   r   r   r  r   rO  r   r   r   r   r   r  5  s    r  c                   @   r~  )r  z7
    Go down a Cython, Python or relevant C frame.
    zcy downr  N)r`   ra   rQ   r   r   r  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y   td|f w t }| r&| }| st|}ztd|| d f  W d S  t	yL } ztj|j
 d }~ww )NzNot a valid number: %rz	select %dr   )rf   
ValueErrorr
   r   r   newerr   
stackdepthr'   r   r   )r   ZstacknorD  r   r  rE  r   r   r   r   _  s    
zCySelect.invokeN)r`   ra   rQ   r   r   r   rO  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| }| s|dk}d}|rAz| |}W n ty*   d}Y nw |s/|r5| || |d7 }| }|sd S d S )Nz-ar   Fr   )r
   r   r   r(   r   r   r  )r   r   rD  r   Z	print_allr   Zis_relevantr   r   r   r   z  s"   zCyBacktrace.invokeN)r`   ra   rQ   r   r   r  r
   COMMAND_STACKr   COMPLETE_NONEr   r   rO  r$   r   r   r   r   r   r!  r  s    r!  c                   @   r5  )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   rT  rD  sdrg   sourcer   r   r   r     s
   zCyList.invokeN)r`   ra   rQ   r   r   r
   r   r   r  r   r   rO  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}||v r || tj}td||f  d S ||v rT|| j}zt	
|}W n ty@   td|  Y d S w |jsL| || d S td|  d S |  r_t	d| S |  r| jj|d}|D ]}|dkrz| }qo | || d S t	d|  d S )Nz%s = %szunable to get value of %sz%s is optimized outz	py-print *zprint )r   r   rl   rX   get_truncated_reprr   MAX_OUTPUT_LENr   rc   r
   r   r   r   r   r&   r'   r#   r   r   r   lstripdereference)r   r   rD  global_python_dictmodule_globalsr   rc   cr   r   r   r     s2   

zCyPrint.invokec                 C   s(   |   r|  }tt|j|jS g S r   )r#   r   r  r   rn  ro   rX   )r   r   r   r   r   rs    s   zCyPrint.completeN)r`   ra   rQ   r   r   r
   COMMAND_DATAr   r   rO  r   rs  r   r   r   r   r#    s    
"r#  c                 C   s   | d   S )Nr   )r]  )itemr   r   r   r     s    r   c                   @   :   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rCt|j}|jsC| |j||d q$d S )NkeyrD   )r   rn   r   rX   r   ro   rZ  r   rL   r,  sortkeyr   r   r
   r   rc   r   r   )	r   r   rD  rJ  Zlocal_cython_varsr   r   r   r   r   r   r   r     s   
zCyLocals.invokeNr`   ra   rQ   r   r   r
   r  r   r  r   r   rO  r,   r   r   r   r   r   r$        
r$  c                   @   r  )	r%  z:
    List the globals from the current Cython module.
    z
cy globalszinfo variablesz
py-globalsr+   c              	   C   s  |   }|  jj}d}d}|rtt|td}|r tt|}t||}t }td t|	 t
dD ]\}	}
|
tj}
||	 td||	|
f  q4td t|	 t
dD ]&\}}||vrzt|j}W n	 tyr   Y qYw |js| |j||d qYd S )Nr   r  zPython globals:z    %-*s = %sz
C globals:r   )r   r   rl   rX   rZ  r   rq   r   rL   r,  r  r  r   r  r@  r
   r   rc   r   r   r   r   )r   r   rD  r  r  Zmax_globals_lenZmax_globals_dict_lenr   r   r   r   r   r   r   r   r   r   r      s:   


zCyGlobals.invokeNr  r   r   r   r   r%    r  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 ]Q\}}|jtkrZ| ||rZzt|j}W n	 t	y)   Y q	w |j
r.q	||}d|||jf }zt|dk rMtd tdW || q	|| w 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   ro   r,  re   r   r   r
   r   rc   r   r   Zalloc_pystringr   xdecref)	r   executorZlocal_dict_pointerr   r   r   r   Z	pystringpr   r   r   r   _fill_locals_dict+  s0   





z,EvaluateOrExecuteCodeMixin._fill_locals_dictc                 C   sB   t  }|r| |s| |r|  |S | }|st d)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   s   t  8 td}td}z| |t | |||||}W |t | n	|t | w W d    |S 1 s?w   Y  |S )Nz&(PyObject *) PyModule_GetDict(__pyx_m)z(PyObject *) PyDict_New())r   ZFetchAndRestoreErrorr
   r   r  Zpointervalueevalcoder  )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`   ra   rQ   r   r  r  r  r  r   r   r   r   r  $  s     r  c                   @   r5  )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   exprrD  r  r  r   r   r   r   ~  s   zCyExec.invokeN)r`   ra   rQ   r   r   r
   r  r   r  r   r   rO  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   rZ  r
   r   r   r  r   ro  r'   )r   r  rD  Zname_and_exprvarnamerc   r   r   r   r     s   
zCySet.invokeN)r`   ra   rQ   r   r   r
   r  r   r  r   r   rO  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 }| |rA| |}||jv r|j| j}n#||jjv r,|jj| j}nd|jj|f }||jj	v rA|jj	| j}|sJ| j
j|}|sSt d| |S )Nz%s.%szNo such Cython variable: %s)r
   r   r#   r   ro   rc   rl   rX   r   r[   r   r/  r"   r   )r   cynamer   rc   rJ  rK  r   r   r   r     s    


zCyCName.invoker   )	r`   ra   rQ   r   r   rO  r%   r1   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                    sX   |   }| |}| ||rt j||d}t|S ||v r%|| jS td| )Nr   zVariable %s is not initialized.)	r   r   r   rk   r   r
   r   Z_gdbvalr   )r   r  r   Zglobals_dictrJ  rc   rs   r   r   r     s   


zCyCValue.invoker   )
r`   ra   rQ   r   r   rO  r%   r1   r   rt   r   r   rs   r   r)    s    r)  c                   @   "   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  )r   rB   r   r   r   r     s   zCyLine.invokeN)r`   ra   rQ   r   r   rO  r%   r   r   r   r   r   r*    
    r*  c                   @   r  )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`   ra   rQ   r   r   rO  r1   r   r   r   r   r   r+    r  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  )Tr   )NT)[r   r   r   r   r   r0  r
   Z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getfilesystemencodingr<  r!   r%   r,   r$   r1   rS   rU   rb   rh   r{   r}   r   r   r   r   r   r   	Parameterr   r   r   r   r   rK   Commandr   r  r  r  Z
PythonInfort  ZExecutionControlCommandBasery  ZPythonStepperMixinr  r  r  r  r  r  r  r   r!  r"  r#  r  r$  r%  r  ZPyExecr&  r'  Functionr(  r)  r*  r+  rz  r  r   r  r   r   r   r   <module>   s    



1 ]?
QB %
		 3-Q%
