o
    fʵ                     @   s  d Z ddlmZ zeZW n	 ey   Y nw 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yA   eZeZY nw z
ddlmZ dZW nE ey   dZzddlmZ W n1 ey   zddlmZ W n ey   zddlZW n ey   ddlmZ Y nw Y nw Y nw Y nw z
ddlZddlZW n ey   dZejd	 Y nw 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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 U/var/www/html/mig_web/myenv/lib/python3.10/site-packages/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                    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   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                    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   is_cy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   r0   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   z   s   
z(require_running_program.<locals>.wrapperr   r   r   r!   r   r&   y   s   r&   c                    r1   )Nc                    s,   t |tjr
| } | |g|R i |S r   )
isinstancer   Valuestring)r   r4   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   r5   c                   @      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_namer8   r9   r   r   r   __init__   s   
zCythonModule.__init__N__name__
__module____qualname__r?   r   r   r   r   r7      s    r7   c                   @   r6   )CythonVariablec                 C   s&   || _ || _|| _|| _t|| _d S r   )r   cnamequalified_nametypeintlineno)r   r   rE   rF   rG   rI   r   r   r   r?      s
   zCythonVariable.__init__Nr@   r   r   r   r   rD      s    rD   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)
superrJ   r?   modulepf_cnameis_initmodule_functionlocals	argumentssetstep_into_functions)	r   rN   r   rE   rO   rF   rI   rG   rP   	__class__r   r   r?      s   	
zCythonFunction.__init__)rA   rB   rC   r	   r?   __classcell__r   r   rU   r   rJ      s    rJ   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_salliner[   r   r   r   get_c_lineno   s   
zCythonBase.get_c_linenoc                 C   s$   | j j| }|d u rt |S r   )rY   rZ   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   )rg   rN   r<   r$   rd   )r   r   cyfuncr   r   r   get_cython_lineno   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%   rg   rN   r8   ri   pygmentslexersCythonLexerr(   r   r]   r^   r   r   current_line_numPythonLexerrb   symtabfullnamerc   CLexerSourceFileDescriptor)r   r   r8   rI   lexerfilename_and_linenopyframeobjectsymbol_and_line_objr   r   r   get_source_desc   s:   




zCythonBase.get_source_descc                 C   s   |   \}}||S r   )ry   
get_source)r   r   source_descrI   r   r   r   get_source_line  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(   rg   rT   )r   r   r   older_framecython_funcr   r   r   r*     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||  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   )rY   	cy_cvalueinvoke)argr   r   r   r   <lambda>A  s    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   selectry   r   printr(   r   r]   r^   r_   print_stackframeco_namer%   rg   r   rE   parse_and_evalr   addressr2   rH   strbytesr   joinsysstdoutwriter8   rz   r   )r   r   indexr   r   r{   rI   r`   	func_name
func_cname	func_argsrh   f	gdb_valuefunc_addressar   r   r   r   %  s\   



zCythonBase.print_stackframec                 C   sN   t d}zt d}W n ty   t tdw || }|d S )N__pyx_mPyModuleObjectzy                Unable to lookup type PyModuleObject, did you compile python
                with debugging support (-g)?md_dict)	r   r   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   PyObjectPtrfrom_pyobject_ptrrS   	iteritemsproxyval)r   remote_dictpyobject_dictrf   seenkvr   r   r   get_cython_globals_dictu  s   z"CythonBase.get_cython_globals_dictN c                 C   sV   t |rd}nd|jf }|d u rtd||||f  d S td|||||f  d S )Nr   z(%s) z%s%s = %s%sz%s%-*s = %s%s)r   pretty_printer_lookuprG   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v r&||jkr$|jtkr"tt|jS dS dS ||jkS )Nrj   z->TF)	rQ   ri   rE   rI   rG   r
   rH   r   r   )r   r   
local_namecyvar
cur_linenor   r   r   is_initialized  s   




zCythonBase.is_initialized)F)Nr   )rA   rB   rC   r#   r%   r(   ra   rd   rg   ri   ry   r|   r*   r   r   r   r   r   r   r   r   r   rX      s2    







"

A
rX   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 )rt   Nc                 C   s   || _ || _|| _d S r   )r8   ru   	formatter)r   r8   ru   r   r   r   r   r?     s   
zSourceFileDescriptor.__init__c                 C   s
   | j d uS r   )r8   r   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)
rl   ru   
parameterscolorize_codeterminal_backgroundr   r   
formatters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 )Nrj   > z%s %4d    %s)	openr8   r   read
splitlines	itertoolsislice	enumeraterstrip)r   startstop
lex_source	mark_line
lex_entirer   sliceidxrc   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 coderj   r   )r   r   r8   maxr   r   IOError)r   r   r   r   r   r   excr   r   r   rz     s   

zSourceFileDescriptor.get_sourcer   )NTr   F)rA   rB   rC   r?   r   r   r   rz   r   r   r   r   rt     s    
rt   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   )msgrM   r   r?   )r   r   rU   r   r   r?     s   zCyGDBError.__init__)rA   rB   rC   __doc__r?   rW   r   r   rU   r   r     s    r   c                   @      e Zd ZdZdZdS )re   z]
    raised when the user requests the current cython function, which is
    unavailable
    z7Current function is a function cygdb doesn't know aboutNrA   rB   rC   r   r   r   r   r   r   re         re   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     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                    s:   | j j | _| _tt| ||| |d ur|| _d S d S r   )rV   r   show_docset_docrM   r   r?   r   )r   r   command_classparameter_classdefaultrU   r   r   r?     s   
zCythonParameter.__init__c                 C   s
   t | jS r   )boolr   r   r   r   r   __bool__  r   zCythonParameter.__bool__r   )rA   rB   rC   r   r?   r   __nonzero__rW   r   r   rU   r   r     s
    r   c                   @      e Zd ZdZdS ) CompleteUnqualifiedFunctionNameszH
    Have 'cy break' complete unqualified function or method names.
    NrA   rB   rC   r   r   r   r   r   r         r   c                   @   r   )ColorizeSourceCodez5
    Tell cygdb whether to colorize source code.
    Nr   r   r   r   r   r     r   r   c                   @   r   )TerminalBackgroundzJ
    Tell cygdb about the user's terminal background (light or dark).
    Nr   r   r   r   r   r     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 )Ncy_complete_unqualifiedTcy_colorize_codecy_terminal_background_colordark)r   r   COMMAND_BREAKPOINTSPARAM_BOOLEANcomplete_unqualifiedr   COMMAND_FILESr   r   PARAM_STRINGr   r   r   r   r   r?   %  s$   
zCythonParameters.__init__N)rA   rB   rC   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   sB   t | ds| || jg|R i |S | || j| jg|R i |S )Ncompleter_class)hasattrr   r   )cls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)
rA   rB   rC   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
    rY   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_break_stepnextruncontfinishupdownr   btlistprint_rQ   r:   exec_zcy exec-cy-exec_execrS   cy_cnamer   	cy_linenocy_evalr   )&rM   r   r?   dictCyImportr  CyBreakCyStepCyNextCyRunCyContCyFinishCyUpCyDownCySelectCyBacktraceCyListCyPrintCyLocals	CyGlobalsr   FixGdbCommandCyExecCySetCyCNameCyCValueCyLineCyEvalitemsrY   setattrcython_namespacefunctions_by_qualified_namerZ   collectionsdefaultdictr  functions_by_name)r   r   r   r   commandscommand_namecommandrU   r   r   r?   n  sp   	
zCyCy.__init__)rA   rB   rC   r   r   r   r  r   COMPLETE_COMMANDr   r?   rW   r   r   rU   r   r
  S  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: %srj   Globalsr   	FunctionsrN   LocalsStepIntoFunctionsc                 s   s    | ]}|j V  qd S r   )tag)r   funcargr   r   r   r     s    
z"CyImport.invoke.<locals>.<genexpr>	ArgumentsLineNumberMapping
src_linenosrc_path	c_linenosr   ))r2   BYTESdecode_filesystemencodingr   r   OSErrorr   r   r   r   parsegetrootr7   attribrY   r6  r   findrD   r:   rJ   rF   r:  appendr7  rZ   rE   r=   rQ   rT   addrR   extendrH   r  mapr   minr;   r<   )r   r   from_ttyr   r   etrN   cython_modulevariabledr   cython_functionr   qnamelocalstep_into_funcmarkerrH  rI  rJ  c_linenor   r   r   r     sh   





zCyImport.invokeN)rA   rB   rC   r   r   r   COMMAND_STATUSr   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?)	partitionrH   rY   r6  rg   rN   r8   r;   r9   r   r)   r   )r   r   
modulename_rI   r[  rc  
breakpointr   r   r   
_break_pyx  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   rP   r   r   r   r   r   
<listcomp>  s    z+CyBreak._break_funcname.<locals>.<listcomp>rh  rj   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)rY   r7  r$   rP   r:  r   r)   lenr   r   rF   inputEOFErrorlowerisdigitrH   rE   rO   )r   funcnamefuncbreak_funcsfuncsr   rf   r   r   r   _break_funcname  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-prj   TFzpy-break %srg  )
r2   rK  rL  rM  r   
startswithr   r)   rm  r{  )r   function_namesrX  argvpython_breakpointsrw  r   r   r   r   B  s   


zCyBreak.invokec                    s   dd | j j D }dd | j j D }tjr t||}n|}| 	 }|r0d|d vrGt
|d t  	 fdd|D S |d   fdd|D }t tkrmt 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   rn  ro  r   r   r   r   Z  s    z.CyBreak.complete.<locals>.<listcomp>.<genexpr>)any)r   nLr   r   r   rp  Y  s    
z$CyBreak.complete.<locals>.<listcomp>c                 S   s   g | ]	\}}|j s|qS r   rn  )r   r  r   r   r   r   rp  [  s    
.c                    s"   g | ]}| r| vr|qS r   r|  r   r  )r   wordr   r   rp  g  s    c                    s   g | ]	}|  r|qS r   r  r  )lastwordr   r   rp  l  s    c                    s   g | ]}| d  qS r   r   r  )strip_prefix_lengthr   r   rp  r  s    )rY   r:  r4  r7  r   r   r   chainstripr   rS   rr  )r   textr  namesqnames	all_nameswordscomplr   )r  r   r  r  r   completeU  s   zCyBreak.completeN)rA   rB   rC   r   r   r   r   r   rm  r{  r   rf  r   r  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 Nrj   )r%   ri   rM   r  rI   r[   rU   r   r   rI   |  s   
zCythonInfo.linenoc                    s8   z
t t| |}W n tjy   Y d S w | pd S r   )rM   r  r|   r   r   r  )r   r   rc   rU   r   r   r|     s   zCythonInfo.get_source_linec                    s   | j rtt| |S d S r   )r(   rM   r  exc_infor[   rU   r   r   r    s   zCythonInfo.exc_infoc                 C   s   |   r	|  jS dS )Nr   )r%   rg   rT   r   r   r   r   runtime_break_functions  s   
z"CythonInfo.runtime_break_functionsc                 C   s   dg}| | jj |S )Nr\   )rU  rY   rZ   )r   rf   r   r   r   static_break_functions  s   z!CythonInfo.static_break_functions)
rA   rB   rC   r   rI   r|   r  r  r  rW   r   r   rU   r   r  w  s    	r  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)rA   rB   rC   r	  r  r   r   r   r   r    s    r  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	to_string)stepinto)r(   python_stepr  r%   finish_executingr   r)   r  )r   r   rX  r=  r   r   r   r     s   zCyStep.invokeN)	rA   rB   rC   r   r   r  r   rf  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)rA   rB   rC   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)
rA   rB   rC   r   r   r   rf  r  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)
rA   rB   rC   r   r   r   rf  r  r  r   r   r   r   r   r#    s    r#  c                   @   r  )r$  z-
    Execute until the function returns.
    z	cy finishN)
rA   rB   rC   r   r   r   rf  r  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   rj   )r   )
r   r)   _commandr*   r   r   r   r   r}   r   )r   r   rY  r   r   r   r   r   r     s"   zCyUp.invokeN)	rA   rB   rC   r   r   r  r   rf  r   r   r   r   r   r%    s    r%  c                   @   r  )r&  z7
    Go down a Cython, Python or relevant C frame.
    zcy downr  N)rA   rB   rC   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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 %drj   )rH   
ValueErrorr   r   r   newerr   
stackdepthr)   r   r   )r   stacknorX  r   r  rY  r   r   r   r     s    
zCySelect.invokeN)rA   rB   rC   r   r   r   rf  r   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| }| s|dk}d}|rAz| |}W n ty*   d}Y nw |s/|r5| || |d7 }| }|sd S d S )Nz-ar   Frj   )r   r   r}   r*   r   r   r  )r   r   rX  r   	print_allr   is_relevantr   r   r   r   #  s"   zCyBacktrace.invokeN)rA   rB   rC   r   r   r  r   COMMAND_STACKr   COMPLETE_NONEr   r   rf  r&   r   r   r   r   r   r(    s    r(  c                   @   r?  )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   )ry   rz   r   )r   rk  rX  sdrI   sourcer   r   r   r   E  s
   zCyList.invokeN)rA   rB   rC   r   r   r   r   r   r  r   r   rf  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   rg   rN   r:   get_truncated_reprr   MAX_OUTPUT_LENr   rE   r   r   r   r_   r   r(   r)   r%   rY   r   r   lstripdereference)r   r   rX  global_python_dictmodule_globalsr   rE   cr   r   r   r   V  s2   

zCyPrint.invokec                 C   s(   |   r|  }tt|j|jS g S r   )r%   rg   r  r   r  rQ   r:   )r   r   r   r   r   r  y  s   zCyPrint.completeN)rA   rB   rC   r   r   r   COMMAND_DATAr   r   rf  r   r  r   r   r   r   r*  N  s    
"r*  c                 C   s   | d   S )Nr   )ru  )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keyr   )rg   rP   rY   r:   r   rQ   rr  r   sortedr4  sortkeyr   r   r   r   rE   r_   r   )	r   r   rX  r^  local_cython_varsr   r   r   r   r   r   r   r     s   
zCyLocals.invokeNrA   rB   rC   r   r   r   r  r   r  r   r   rf  r0   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   rg   rN   r:   rr  r   rS   r   r  r4  r  r  r   r  rT  r   r   rE   r   r_   r   r   )r   r   rX  r  r  max_globals_len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)rg   rQ   r4  rG   r
   r   r   r   rE   r   r_   alloc_pystringr   xdecref)	r   executorlocal_dict_pointerr   r   r   r   	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_frame  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   FetchAndRestoreErrorr   r   r  pointervalueevalcoder  )r   r  r   
input_typeglobal_dict
local_dictrf   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`.
        )r  r   PythonCodeExecutorr(   _evalcode_pythonr  )r   r   r  r   r  r   r   r   r    s
   
z#EvaluateOrExecuteCodeMixin.evalcodeN)rA   rB   rC   r   r  r  r  r  r   r   r   r   r    s     r  c                   @   r?  )r.  zD
    Execute Python code in the nearest Python or Cython frame.
    r  c                 C   s.   |  |\}}t }|| ||j d S r   )readcoder   r  r  r  Py_file_input)r   exprrX  r  r  r   r   r   r   '  s   zCyExec.invokeN)rA   rB   rC   r   r   r   r  r   r  r   r   rf  r   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=rj      z,Invalid expression. Use 'cy set var = expr'.zset %s = %s)	r   rr  r   r   rY   r  r   r  r)   )r   r  rX  name_and_exprvarnamerE   r   r   r   r   >  s   
zCySet.invokeN)rA   rB   rC   r   r   r   r  r   r  r   r   rf  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 )r0  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%   rg   rQ   rE   rN   r:   r   r=   rY   r7  r$   r   )r   cynamer   rE   r^  r_  r   r   r   r   V  s    


zCyCName.invoker   )	rA   rB   rC   r   r   rf  r'   r5   r   r   r   r   r   r0  L  s    	r0  c                       s0   e Zd ZdZejeed fdd	Z  Z	S )r1  z-
    Get the value of a Cython variable.
    Nc                    s\   |   }| |}| ||rtt| j||d}t|S ||v r'|| jS t	d| )Nr   zVariable %s is not initialized.)
r   rg   r   rM   r1  r   r   r   _gdbvalr   )r   r  r   globals_dictr^  rE   rU   r   r   r   v  s   


zCyCValue.invoker   )
rA   rB   rC   r   r   rf  r'   r5   r   rW   r   r   rU   r   r1  q  s    r1  c                   @   "   e Zd ZdZejedd ZdS )r2  z&
    Get the current Cython line.
    c                 C   s   |   d S r  )ri   r   r   r   r   r     s   zCyLine.invokeN)rA   rB   rC   r   r   rf  r'   r   r   r   r   r   r2    
    r2  c                   @   r  )r3  zO
    Evaluate Python code in the nearest Python or Cython frame and return
    c                 C   s   t jj}| ||S r   )r   r  Py_eval_inputr  )r   python_expressionr  r   r   r   r     s   zCyEval.invokeN)rA   rB   rC   r   r   rf  r5   r   r   r   r   r   r3    r  r3  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   source_gdb_scriptr   r   r   r   r!  r   r   r   r   register_defines  s   

r  )Tr   )er   
__future__r   	raw_inputrs  	NameErrorr   r   r   r   r8  r   unicodeUNICODEr   rK  r   lxmlr   	have_lxmlImportError	xml.etreer   r   elementtree.ElementTreepygments.lexersrl   pygments.formattersstderrr   r  r   shlexr   Cython.Debuggerr   r	   r
   r  _data_typesgetfilesystemencodingrM  r#   r'   r0   r&   r5   objectr7   rD   rJ   rX   rt   r   r   re   r   	Parameterr   r   r   r   r   r   Commandr   r
  r  r  
PythonInfor  ExecutionControlCommandBaser  PythonStepperMixinr   r!  r"  r#  r$  r%  r&  r'  r(  r)  r*  r  r+  r,  r  PyExecr.  r/  Functionr0  r1  r2  r3  r  r  rY   r  r   r   r   r   <module>   s    



 ]?
QB %
		 3-Q%
