B
    v9aJ                 @   s   d dl Z d dlZd dlT d dlmZmZ d dlmZ d dlm	Z	 d dl
mZ G dd de jZG d	d
 d
ZG dd de	ZG dd dZedkrd dlmZ edddd dS )    N)*)Frame	Scrollbar)macosx)ScrolledList)ListedToplevelc               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )Idbc             C   s   || _ tj|  d S )N)guibdbBdb__init__)selfr	    r   /usr/lib/python3.7/debugger.pyr      s    zIdb.__init__c             C   sL   |  |r|   d S | |}y| j|| W n tk
rF   Y nX d S )N)in_rpc_codeset_step_Idb__frame2messager	   interactionZTclError)r   framemessager   r   r   	user_line   s    

zIdb.user_linec             C   s4   |  |r|   d S | |}| j||| d S )N)r   r   r   r	   r   )r   r   infor   r   r   r   user_exception   s
    

zIdb.user_exceptionc             C   sB   |j jdrdS |j}|j j}d|kr4d|kr4dS | |S d S )Nzrpc.pyTidlelibZdebuggerF)f_codeco_filenamecountf_backr   )r   r   Z
prev_frameZ	prev_namer   r   r   r   #   s    zIdb.in_rpc_codec             C   sF   |j }|j}|j}tj|}d||f }|jdkrBd||jf }|S )Nz%s:%s?z%s: %s())r   r   f_linenoospathbasenameco_name)r   r   codefilenamelinenor"   r   r   r   r   Z__frame2message/   s    
zIdb.__frame2messageN)__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r      s
   
r   c               @   s   e Zd Zd Z Z ZZd1ddZdd Zd2ddZ	dd	 Z
d3d
dZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdZdd Zdd Zd d! ZdZdZd"d# Zd$d% Zd4d'd(Zd)d* Zd+d, Zd-d. Zd/d0 Z dS )5DebuggerNc             C   s:   |d krt | }|| _|| _d | _|   d| _d| _d S )Nr   )r   pyshellidbr   make_guiinteractingnesting_level)r   r+   r,   r   r   r   r   >   s    zDebugger.__init__c                sN   j dkr.  jd fdd d S zd_jj  S d_X d S )Nr   d   c                  s
   j   S )N)runr   )argsr   r   r   <lambda>h       zDebugger.run.<locals>.<lambda>   )r/   
abort_looprootZafterr.   r,   r1   )r   r2   r   )r2   r   r   r1   H   s    
zDebugger.runc             C   sd   y|    W n tk
r    Y nX | jr6| j  d S | jrL| j  d | _| j  | j	  d S )N)
quit	Exceptionr.   topZbellstackviewercloser+   Zclose_debuggerdestroy)r   eventr   r   r   r<   p   s    

 
zDebugger.closec             C   sF  | j }|j| _|j | _}t| | _}| jd | jd |d| j | j	d| j t
| | _}| jjdd g  | _}t|d| jd | _}|| t|d	| jd | _}|| t|d
| jd | _}|| t|d| jd | _}|| t|d| jd | _}|| x&|D ]}|jdd |jdd q*W t
| | _}| jjdd | jst|| j_| jd t|d| j | jd| _!| j!j"ddd | j#st|| j_#t|d| j$| j#d| _%| j%j"ddd | j&st|| j_&| j&d t|d| j'| j&d| _(| j(j"ddd | j)sBt|| j_)t|d| j*| j)d| _+| j+j"ddd t,|dd| _-| j-jdd t,|dd| _.| j.jddd | j./d| _0t
|dd| _1| j1jddd t
|| _2| j2jddd t
|dd| _3| j3jddd | j4 r|    | j&4 r.| '  | j)4 rB| *  d S )NzDebug ControlZDebugZWM_DELETE_WINDOWz<Escape>w)anchorZGo)textcommandZStepZOverZOutZQuitdisabled)stateleft)sider5   ZStack)rA   rB   variabler   )rowcolumnZSourceLocalsGlobalsx)r@   fill
background)heightboth)expandrM   )5r+   flistr7   r   r:   Zwm_titleZwm_iconnameZwm_protocolr<   Zbindr   bframepackbuttonsZButtoncontZbcontappendstepZbstepnextZbnextretZbretr8   	configurecframevstackZ
BooleanVar	__class__setZCheckbutton
show_stackZbstackgridvsourceshow_sourceZbsourcevlocalsshow_localsZblocalsvglobalsshow_globalsZbglobalsLabelstatuserrorZcgeterrorbgfstackflocalsfglobalsget)r   r+   r7   r:   rS   Zblbr\   r   r   r   r-      s    







zDebugger.make_guic             C   s|  || _ | jj|d |r~|\}}}y
|j}W n  tk
rL   dt| }Y nX |d k	rxyd|t|f }W n   Y nX d}nd}d }| j}| jj||d | j}	|	r| j	
| j |\}
}|	|
| | d | j r|   x| jD ]}|jdd	 qW | j  |  jd7  _| jjd
d |  jd8  _x| jD ]}|jdd	 q<W | jjdd | jjd| jd d | _ d S )N)rA   z%sz%s: %sZyellow )rA   rN   r5   Znormal)rD   Zvwaitz::idledebugwaitrC   )r   ri   r[   r'   AttributeErrorstrrk   rj   r;   r,   	get_stack
load_stackshow_variablesrb   ro   sync_source_linerU   r:   wakeupr/   r7   tkcall)r   r   r   r   typevaluetbZm1bgsvstackirp   r   r   r   r      sH    




zDebugger.interactionc             C   sV   | j }|sd S | |\}}|d d |dd   dkrRtj|rR| j|| d S )Nr5   z<>)r   _Debugger__frame2fileliner    r!   existsrR   Zgotofileline)r   r   r%   r&   r   r   r   rw      s    (zDebugger.sync_source_linec             C   s   |j }|j}|j}||fS )N)r   r   r   )r   r   r$   r%   r&   r   r   r   Z__frame2fileline  s    zDebugger.__frame2filelinec             C   s   | j   |   d S )N)r,   Zset_continuer6   )r   r   r   r   rV     s    
zDebugger.contc             C   s   | j   |   d S )N)r,   r   r6   )r   r   r   r   rX     s    
zDebugger.stepc             C   s   | j | j |   d S )N)r,   Zset_nextr   r6   )r   r   r   r   rY     s    zDebugger.nextc             C   s   | j | j |   d S )N)r,   Z
set_returnr   r6   )r   r   r   r   rZ     s    zDebugger.retc             C   s   | j   |   d S )N)r,   Zset_quitr6   )r   r   r   r   r8     s    
zDebugger.quitc             C   s   | j jddd d S )Nr_   z::idledebugwait1)r7   ry   rz   )r   r   r   r   r6     s    zDebugger.abort_loopc             C   s~   | j sN| j rNt| j| j|  | _ }| jrz| j| jd \}}|	|| n,| j }|rp| j spd | _ |
  d| jd< d S )Nr5   rO   )r;   r]   ro   StackViewerrl   rR   r   r,   rt   ru   r<   )r   r   r   r   r   r   r   r`   $  s    zDebugger.show_stackc             C   s   | j  r|   d S )N)rb   ro   rw   )r   r   r   r   rc   1  s    
zDebugger.show_sourcec             C   s   |d | _ |   d S )Nr   )r   rv   )r   Z	stackitemr   r   r   
show_frame5  s    
zDebugger.show_framec             C   sL   | j }| j r$|s@t| jd| _ n|r@d | _ |  d| jd< |   d S )NrJ   r5   rO   )localsviewerrd   ro   NamespaceViewerrm   r<   rv   )r   lvr   r   r   re   <  s    

zDebugger.show_localsc             C   sL   | j }| j r$|s@t| jd| _ n|r@d | _ |  d| jd< |   d S )NrK   r5   rO   )globalsviewerrf   ro   r   rn   r<   rv   )r   gvr   r   r   rg   H  s    

zDebugger.show_globalsr   c             C   st   | j }| j}| j}|s d  }}n |j}|j}|r@|r@||kr@d }|rX|||| jjj |rp|||| jjj d S )N)	r   r   r   f_locals	f_globals	load_dictr+   ZinterpZrpcclt)r   forcer   r   r   ZldictZgdictr   r   r   rv   T  s    
zDebugger.show_variablesc             C   s   | j || d S )N)r,   Z	set_break)r   r%   r&   r   r   r   set_breakpoint_hered  s    zDebugger.set_breakpoint_herec             C   s   | j || d S )N)r,   Zclear_break)r   r%   r&   r   r   r   clear_breakpoint_hereg  s    zDebugger.clear_breakpoint_herec             C   s   | j | d S )N)r,   Zclear_all_file_breaks)r   r%   r   r   r   clear_file_breaksj  s    zDebugger.clear_file_breaksc          	   C   sX   xR| j jjD ]D}|jj}y x|jD ]}| || q"W W q tk
rN   wY qX qW d S )N)r+   rR   Zinversedictior%   Zbreakpointsr   rr   )r   Zeditwinr%   r&   r   r   r   load_breakpointsm  s    zDebugger.load_breakpoints)N)N)N)r   )!r'   r(   r)   r]   rb   rd   rf   r   r1   r<   r-   r   rw   r   rV   rX   rY   rZ   r8   r6   r;   r`   rc   r   r   r   re   rg   rv   r   r   r   r   r   r   r   r   r*   :   s6   

(
K
1
r*   c               @   sV   e Zd Zdd ZdddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd ZdS )r   c             C   s<   t  rt| | ntj| |dd || _|| _g | _d S )NP   )width)r   ZisAquaTkr   r   rR   r	   r   )r   masterrR   r	   r   r   r   r   y  s    zStackViewer.__init__Nc             C   s   || _ |   xtt|D ]}|| \}}y|jd }W n   d}Y nX |j}|j}|j}	dd l}
|
	||}|
 }|	dkrd|||f }nd||	||f }||krd| }| | qW |d k	r| | d S )Nr'   r   r   )r   rq   Nz%s, line %d: %sz%s.%s(), line %d: %sz> )r   clearrangelenr   r   r   r#   	linecachegetlinestriprW   Zselect)r   r   indexr   r   r&   modnamer$   r%   funcnamer   Z
sourcelineitemr   r   r   ru     s.    

zStackViewer.load_stackc             C   s   | j rt| |S d S )N)r   r   popup_event)r   r>   r   r   r   r     s    zStackViewer.popup_eventc             C   s*   | j }|jd| jd |jd| jd d S )NzGo to source line)labelrB   zShow stack frame)menuZadd_commandgoto_source_lineshow_stack_frame)r   r   r   r   r   	fill_menu  s
    
zStackViewer.fill_menuc             C   s4   d|  krt | jk r0n n| j| j|  d S )Nr   )r   r   r	   r   )r   r   r   r   r   	on_select  s    zStackViewer.on_selectc             C   s   |  | d S )N)rc   )r   r   r   r   r   	on_double  s    zStackViewer.on_doublec             C   s   | j d}| | d S )Nactive)listboxr   rc   )r   r   r   r   r   r     s    zStackViewer.goto_source_linec             C   s@   | j d}d|  kr&t| jk r<n n| j| j|  d S )Nr   r   )r   r   r   r   r	   r   )r   r   r   r   r   r     s    zStackViewer.show_stack_framec             C   sd   d|  krt | jk s n d S | j| \}}|j}|j}tj|r`| j|}|r`|	| d S )Nr   )
r   r   r   r   r    r!   isfilerR   openZgotoline)r   r   r   r&   r$   r%   Zeditr   r   r   rc     s    zStackViewer.show_source)N)r'   r(   r)   r   ru   r   r   r   r   r   r   rc   r   r   r   r   r   w  s   
r   c               @   s,   e Zd Zd
ddZdZdddZdd	 ZdS )r   Nc             C   s   d}d}|rdt | }|| _|| _dd l}| | _d| j_d| j_t| | _	}| j	j
ddd t||dd	d
| _| jj
dd t|dd | _}|j
ddd t|tdtd|dd||fd | _}	|	j
dddd |	j|d< |j|	d< t|	 | _}
|	jdd|
dd| _| | d S )Nr   (      <   r5   rP   )rQ   rM      Zgroove)rA   borderwidthZreliefrL   )rM   vbar)namerighty)rF   rM   i,  )rO   scrollregionrE   )rF   rM   rQ   rB   Zyscrollcommandnw)Zwindowr@   )r   r   titlereprlibReprrepr	maxstringmaxotherr   r   rT   rh   r   r   r   ZCanvasminmaxcanvasZyviewr_   subframeZcreate_windowZsfidr   )r   r   r   dictr   rO   r   r   r   r   r   r   r   r   r     s2    


zNamespaceViewer.__init__r   r   c             C   sf  || j kr|sd S | j}| j}xt|j D ]}|  q.W d | _ |sdt|dd}|jddd n|	 }t
|}	d}
x~|	D ]v}|| }| j|}|r|dd }t||d}|j|
ddd t|ddd	}|d| |j|
ddd |
d }
q~W || _ |  | }| }| j}dd||f| jd
< |dkrNd|d< |jdd n||d< |jdd d S )NNone)rA   r   )rH   rI   r5   r   r   )rH   rI   Zsticky)r   r   r   i,  rO   )rQ   )r   r   r   listZchildrenvaluesr=   rh   ra   keyssortedr   ZEntryinsertZupdate_idletasksZwinfo_reqwidthZwinfo_reqheightr   rT   )r   r   r   Z
rpc_clientr   r   clZ	keys_listnamesrH   r   r|   Zsvaluer   rO   r   r   r   r   r     sF    

zNamespaceViewer.load_dictc             C   s   | j   d S )N)r   r=   )r   r   r   r   r<     s    zNamespaceViewer.close)N)r   N)r'   r(   r)   r   r   r   r<   r   r   r   r   r     s   

6r   __main__)mainzidlelib.idle_test.test_debuggerr   F)	verbosityexit)r
   r    ZtkinterZtkinter.ttkr   r   r   r   Zidlelib.scrolledlistr   Zidlelib.windowr   r   r   r*   r   r   r'   Zunittestr   r   r   r   r   <module>   s   .  ?SX