o
    wi                     @   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 V/home/ubuntu/sommelier/.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   is_cy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   r2   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   r3   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   )r4   attr)
init_attrsr   r   r7      r8   c                 3   sH    t ||p|D ]\}}tt |dd}|  | d| V  qd S )N

		z = )zipreprgetattrreplace)prefixattrsr   r9   r5   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   r6   staterI   init_arg_names
state_reprinstance_attrsrE   r   )r:   r6   r   r   simple_reprx   s$   rX   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_namer[   r\   r   r   r   rL      s   
zCythonModule.__init__c                 C   s   t | ddiddS )Nra   r   F)r6   rT   rX   rD   r   r   r   __repr__   s   zCythonModule.__repr__N__name__
__module__rS   rL   rc   r   r   r   r   rZ      s    rZ   c                   @   rY   )CythonVariablec                 C   s&   || _ || _|| _|| _t|| _d S r   )r   cnamequalified_nametypeintlineno)r   r   rh   ri   rj   rl   r   r   r   rL      s
   zCythonVariable.__init__c                 C   s   t | S r   rb   rD   r   r   r   rc      s   zCythonVariable.__repr__Nrd   r   r   r   r   rg      s    rg   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)	superrL   modulepf_cnameis_initmodule_functionlocals	argumentssetstep_into_functions)	r   rq   r   rh   rr   ri   rl   rj   rs   rR   r   r   rL      s   	
zCythonFunction.__init__)re   rf   rS   r   rL   __classcell__r   r   rx   r   rm      s    rm   >   Frame.pc
Frame.name
Frame.typeFrame.blockFrame.levelFrame.newerFrame.olderFrame.selectSymbol.valueFrame.find_salFrame.functionFrame.is_validFrame.languageSymbol.is_validSymtab.fullnameSymtab.is_validSymtab.linetableFrame.static_linkFrame.architectureSymtab.global_blockSymtab.static_blockSymtab_and_line.is_validc                 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
    r;   __z
	z: linex)rN   dir
startswithr?   callablerS   frame_repr_whitelistrj   r
   Symtab_and_lineSymbolSymtab
frame_reprrstripr@   r0   rk   )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_salr   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   rq   r_   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   rq   r[   r   pygmentslexersCythonLexerr&   r   r   r   r
   r   current_line_numPythonLexerr   symtabfullnamer   CLexerSourceFileDescriptor)r   r   r[   rl   lexerfilename_and_linenopyframeobject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_descrl   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   rw   )r   r   r   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)r5   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   )r4   r   valr   r   r   	<genexpr>  s    z.CythonBase.print_stackframe.<locals>.<genexpr>z#%-2d 0x%016x in %s(%s)z	 at %s:%sr;       ) r
   r   selectr   r   printr&   r   r   r   r   print_stackframeco_namer#   r   r   rh   parse_and_evalr   addressr0   rk   strbytesr   rQ   sysstdoutwriter[   r   r   )r   r   indexr   r   r   rl   r   	func_name
func_cname	func_argsr   f	gdb_value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 )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_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   PyObjectPtrfrom_pyobject_ptrrv   	iteritemsproxyval)r   remote_dictpyobject_dictr   seenkvr   r   r   get_cython_globals_dict  s   z"CythonBase.get_cython_globals_dictNrF   c                 C   sV   t |rd}nd|jf }|d u rtd||||f  d S td|||||f  d S )NrF   z(%s) z%s%s = %s%sz%s%-*s = %s%s)r   pretty_printer_lookuprj   r   )r   r   r   max_name_lengthrA   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)	rt   r   rh   rl   rj   r   rk   r
   r   )r   r   
local_namecyvar
cur_linenor   r   r   is_initialized  s   




zCythonBase.is_initialized)F)NrF   )re   rf   rS   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   )r[   r   	formatter)r   r[   r   r   r   r   r   rL     s   
zSourceFileDescriptor.__init__c                 C   s
   | j d uS r   )r[   rD   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   rM   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 )Nr   > z%s %4d    %s)	openr[   r  read
splitlines	itertoolsislice	enumerater   )r   startstop
lex_source	mark_line
lex_entirer   sliceidxr   rA   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   r;   )r
   r   r[   maxrQ   r  OSError)r   r  r  r  r  r  excr   r   r   r     s   

zSourceFileDescriptor.get_sourcer   )NTr   F)re   rf   rS   rL   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   )msgrp   rL   )r   r   rx   r   r   rL   7  s   zCyGDBError.__init__)re   rf   rS   __doc__rL   ry   r   r   rx   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re   rf   rS   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   )rR   r  show_docset_docrp   rL   r   )r   r   command_classparameter_classdefaultrx   r   r   rL   T  s   

zCythonParameter.__init__c                 C   s
   t | jS r   )boolr   rD   r   r   r   __bool__[  r   zCythonParameter.__bool__r   )re   rf   rS   r  rL   r)  __nonzero__ry   r   r   rx   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re   rf   rS   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   r0  n  r.  r0  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  r0  PARAM_STRINGr  rD   r   r   r   rL   z  s$   
zCythonParameters.__init__N)re   rf   rS   r  rL   r   r   r   r   r1  t  s    r1  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   	_register  s   
zCythonCommand._registerc                 O   s0   t | dd }|r| | j|| | | j||S )Nalias)r?   r@  rA  r   )r>  r   r   rA  r   r   r   register  s   zCythonCommand.registerN)
re   rf   rS   r  r
   COMMAND_NONEr%  classmethodr@  rB  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)rA   import_break_stepnextruncontfinishupdownr   btlistprint_rt   r]   exec_zcy exec-cy-exec_execrv   cy_cnamer   	cy_linenocy_evalr   )&rp   r;  rL   dictCyImportrB  CyBreakCyStepCyNextCyRunCyContCyFinishCyUpCyDownCySelectCyBacktraceCyListCyPrintCyLocals	CyGlobalsr   FixGdbCommandCyExecCySetCyCNameCyCValueCyLineCyEvalitemsr   setattrcython_namespacefunctions_by_qualified_namer   collectionsdefaultdictrP  functions_by_name)r   r   r%  r<  commandscommand_namecommandrx   r   r   rL     sp   	
zCyCy.__init__)re   rf   rS   r  r   r
   rC  r%  COMPLETE_COMMANDr<  rL   ry   r   r   rx   r   rE    s    rE  c                   @   .   e Zd ZdZdZejZejZ	e
jdd ZdS )rY  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   Globalsr   	Functionsrq   LocalsStepIntoFunctionsc                 s   s    | ]}|j V  qd S r   )tag)r4   funcargr   r   r   r   /  s    
z"CyImport.invoke.<locals>.<genexpr>	ArgumentsLineNumberMapping
src_linenosrc_path	c_linenosr   ))r0   r   decode_filesystemencodingr   r  r  r
   r   r   r   parsegetrootrZ   attribr   rq  r   findrg   r]   rm   ri   ru  appendrr  r   rh   r`   rt   rw   addru   extendrk   rP  mapr   minr^   r_   )r   r   from_ttyr5   r   etrq   cython_modulevariabledr   cython_functionr   qnamelocalstep_into_funcmarkerr  r  r  c_linenor   r   r   r     sh   





zCyImport.invokeN)re   rf   rS   r  r   r
   COMMAND_STATUSr%  COMPLETE_FILENAMEr<  r   dont_suppress_errorsr   r   r   r   r   rY    s    rY  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 )rZ  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?)	partitionrk   r   rq  r   rq   r[   r^   r\   r
   r'   r   )r   r   
modulename_rl   r  r  
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   rs   r4   r   r   r   r   r7   p  s    z+CyBreak._break_funcname.<locals>.<listcomp>r  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   rr  r"   rs   ru  r
   r'   lenr   r  ri   inputEOFErrorlowerisdigitrk   rh   rr   )r   funcnamefunc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 %sr  )
r0   r   r  r  r   r   r
   r'   r  r  )r   function_namesr  argv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 )
NrF   c                 S   s&   g | ]\}}t d d |D r|qS )c                 s   s    | ]}|j  V  qd S r   r  r  r   r   r   r     s    z.CyBreak.complete.<locals>.<listcomp>.<genexpr>)any)r4   nLr   r   r   r7     s    
z$CyBreak.complete.<locals>.<listcomp>c                 S   s   g | ]	\}}|j s|qS r   r  )r4   r  r   r   r   r   r7     s    
.c                    s"   g | ]}| r| vr|qS r   r   r4   r  )r   wordr   r   r7     s    c                    s   g | ]	}|  r|qS r   r  r  )lastwordr   r   r7     s    c                    s   g | ]}| d  qS r   r   r  )strip_prefix_lengthr   r   r7     r8   )r   ru  ro  rr  rM   r8  r  chainstripr   rv   r  )r   textr  namesqnames	all_nameswordscomplr   )r  r   r  r  r   complete  s    zCyBreak.completeN)re   rf   rS   r  r   r
   r6  r%  r  r  r   r  r   r  r   r   r   r   rZ  ;  s    1
rZ  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   rp   rl   r   rx   r   r   rl     s   
zCythonInfo.linenoc                    s4   zt  |}W n tjy   Y d S w | pd S r   )rp   r   r
   r   r  )r   r   r   rx   r   r   r     s   zCythonInfo.get_source_linec                    s   | j r	t |S d S r   )r&   rp   exc_infor   rx   r   r   r    s   zCythonInfo.exc_infoc                 C   s   |   r	|  jS dS )Nr   )r#   r   rw   rD   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)
re   rf   rS   r  rl   r   r  r  r  ry   r   r   rx   r   r    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   rB    s   z&CythonExecutionControlCommand.registerN)re   rf   rS   rD  rB  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 )NrH  rI  T	to_string)stepinto)r&   python_stepr  r#   finish_executingr
   r'   rH  )r   r   r  rx  r   r   r   r     s   zCyStep.invokeN)	re   rf   rS   r  r   r  r   r  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)re   rf   rS   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)
re   rf   rS   r  r   r   r  r  rJ  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)
re   rf   rS   r  r   r   r  r  rK  r   r   r   r   r   r^  "  s    r^  c                   @   r  )r_  z-
    Execute until the function returns.
    z	cy finishN)
re   rf   rS   r  r   r   r  r  rL  r   r   r   r   r   r_  ,  s    r_  c                   @   r  )r`  z5
    Go up a Cython, Python or relevant C frame.
    zcy uprM  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   r  r   r   r   r   r   r   <  s"   zCyUp.invokeN)	re   rf   rS   r  r   r  r   r  r   r   r   r   r   r`  5  s    r`  c                   @   r  )ra  z7
    Go down a Cython, Python or relevant C frame.
    zcy downrN  N)re   rf   rS   r  r   r  r   r   r   r   ra  N  s    ra  c                   @   s"   e Zd ZdZdZejdd ZdS )rb  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   )rk   
ValueErrorr
   r   r   newerr   
stackdepthr'   r   r   )r   stacknor  r   r  r  r   r   r   r   _  s    
zCySelect.invokeN)re   rf   rS   r  r   r   r  r   r   r   r   r   rb  W  s
    rb  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 )rc  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   r  r   	print_allr   is_relevantr   r   r   r   z  s"   zCyBacktrace.invokeN)re   rf   rS   r  r   rA  r
   COMMAND_STACKr%  COMPLETE_NONEr<  r   r  r$   r   r   r   r   r   rc  r  s    rc  c                   @   rz  )rd  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   r  r  sdrl   sourcer   r   r   r     s
   zCyList.invokeN)re   rf   rS   r  r   r
   r9  r%  r  r<  r   r  r   r   r   r   r   rd    s    rd  c                   @   s0   e Zd ZdZdZejZej	dd Z
dd ZdS )re  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   rq   r]   get_truncated_reprr   MAX_OUTPUT_LENr   rh   r
   r   r   r   r   r&   r'   r#   r   r   r   lstripdereference)r   r   r  global_python_dictmodule_globalsr   rh   cr   r   r   r     s2   

zCyPrint.invokec                 C   s(   |   r|  }tt|j|jS g S r   )r#   r   rP  r  r  rt   r]   )r   r   r   r   r   r    s   zCyPrint.completeN)re   rf   rS   r  r   r
   COMMAND_DATAr%  r   r  r   r  r   r   r   r   re    s    
"re  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 )	rf  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keyrF   )r   rs   r   r]   r   rt   r  r  rN   ro  sortkeyr   r   r
   r   rh   r   r   )	r   r   r  r  local_cython_varsr   r   r   r   r   r   r   r     s   
zCyLocals.invokeNre   rf   rS   r  r   r
   r  r%  r  r<  r   r  r.   r   r   r   r   r   rf        
rf  c                   @   r  )	rg  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   rq   r]   r  r  rv   r   rN   ro  r  r  r   r  r  r
   r   rh   r   r   r   r   )r   r   r  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   rg    r  rg  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   rt   ro  rj   r   r   r
   r   rh   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_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   FetchAndRestoreErrorr
   r   r  pointervalueevalcoder  )r   r  r  
input_typeglobal_dict
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&   _evalcode_pythonr  )r   r  r  r   r  r   r   r   r  i  s
   
z#EvaluateOrExecuteCodeMixin.evalcodeN)re   rf   rS   r  r  r  r  r  r   r   r   r   r  $  s     r  c                   @   rz  )ri  zD
    Execute Python code in the nearest Python or Cython frame.
    rS  c                 C   s.   |  |\}}t }|| ||j d S r   )readcoder   r  r  r  Py_file_input)r   exprr  r  r  r   r   r   r   ~  s   zCyExec.invokeN)re   rf   rS   r  r   r
   r  r%  r  r<  r   r  r   r   r   r   r   ri  u  s    ri  c                   @   s2   e Zd ZdZdZejZejZ	e
jedd ZdS )rj  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   r  r
   r   r   rU  r   r  r'   )r   r  r  name_and_exprvarnamerh   r   r   r   r     s   
zCySet.invokeN)re   rf   rS   r  r   r
   r  r%  r  r<  r   r  r%   r   r   r   r   r   rj    s    rj  c                   @   s(   e Zd ZdZejeedddZdS )rk  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   rt   rh   rq   r]   r   r`   r   rr  r"   r   )r   cynamer   rh   r  r  r   r   r   r     s    


zCyCName.invoker   )	re   rf   rS   r  r   r  r%   r3   r   r   r   r   r   rk    s    	rk  c                       s0   e Zd ZdZejeed fdd	Z  Z	S )rl  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   rp   r   r
   r   _gdbvalr   )r   r  r   globals_dictr  rh   rx   r   r   r     s   


zCyCValue.invoker   )
re   rf   rS   r  r   r  r%   r3   r   ry   r   r   rx   r   rl    s    rl  c                   @   "   e Zd ZdZejedd ZdS )rm  z&
    Get the current Cython line.
    c                 C   s   |   d S r  )r   rD   r   r   r   r     s   zCyLine.invokeN)re   rf   rS   r  r   r  r%   r   r   r   r   r   rm    
    rm  c                   @   r  )rn  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)re   rf   rS   r  r   r  r3   r   r   r   r   r   rn    r  rn  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   )NT)[r  r   r   r   r  rs  r
   lxmlr   	have_lxmlImportError	xml.etreer   pygments.lexersr   pygments.formattersstderrr   r=  r   shlexr   Cython.Debuggerr   r   r   rX  _data_typesgetfilesystemencodingr  r!   r%   r.   r$   r3   rX   rZ   rg   rm   r   r   r   r   r   r  r   r   	Parameterr"  r,  r/  r0  r1  rM   Commandr;  rE  rY  rZ  
PythonInfor  ExecutionControlCommandBaser  PythonStepperMixinr[  r\  r]  r^  r_  r`  ra  rb  rc  rd  re  r  rf  rg  r  PyExecri  rj  Functionrk  rl  rm  rn  r  rB  r   r  r   r   r   r   <module>   s    



1 ]?
QB %
		 3-Q%
