
   V                       S r SSKrSSKrSSK7  SSKJrJr  SSKJr  SSK	J
r
  SSKJr   " S S	\R                  5      rS
 rS r " S S5      r " S S\
5      r " S S5      r\S:X  a  SSKJr  \" SSSS9  gg)a  Debug user code with a GUI interface to a subclass of bdb.Bdb.

The Idb instance 'idb' and Debugger instance 'gui' need references to each
other or to an rpc proxy for each other.

If IDLE is started with '-n', so that user code and idb both run in the
IDLE process, Debugger is called without an idb.  Debugger.__init__
calls Idb with its incomplete self.  Idb.__init__ stores gui and gui
then stores idb.

If IDLE is started normally, so that user code executes in a separate
process, debugger_r.start_remote_debugger is called, executing in the
IDLE process.  It calls 'start the debugger' in the remote process,
which calls Idb with a gui proxy.  Then Debugger is called in the IDLE
for more.
    N)*)Frame	Scrollbar)macosx)ScrolledList)ListedToplevelc                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )Idb   z6Supply user_line and user_exception functions for Bdb.c                 .   > Xl         [        TU ]	  5         g N)guisuper__init__)selfr   	__class__s     '/usr/lib/python3.13/idlelib/debugger.pyr   Idb.__init__    s        c                     [        U5      (       a  U R                  5         g[        U5      n U R                  R	                  X!5        g! [
         a     gf = f)z]Handle a user stopping or breaking at a line.

Convert frame to a string and send it to gui.
N)_in_rpc_codeset_step_frame2messager   interactionTclError)r   framemessages      r   	user_lineIdb.user_line$   sN    
 MMO '	HH  0 		s   A
 

AAc                     [        U5      (       a  U R                  5         g[        U5      nU R                  R	                  X1U5        g)z)Handle an the occurrence of an exception.N)r   r   r   r   r   )r   r   exc_infor   s       r   user_exceptionIdb.user_exception2   s7    MMO 'WX6r   )r   )
__name__
__module____qualname____firstlineno____doc__r   r   r"   __static_attributes____classcell__)r   s   @r   r
   r
      s    <7 7r   r
   c                     U R                   R                  R                  S5      (       a  gU R                  nUc  gUR                   R                  nSU;   a  SU;   a  g[	        U5      $ )z)Determine if debugger is within RPC code.zrpc.pyTFidlelibdebugger)f_codeco_filenamecountf_backr   )r   
prev_frame	prev_names      r   r   r   :   sb    ||%%h//\\
%%11		!jI&= J''r   c                     U R                   nUR                  nU R                  n[        R                  R                  U5      nU SU 3nUR                  S:w  a  U SUR                   S3nU$ )z"Return a message string for frame.:?: z())r.   r/   f_linenoospathbasenameco_name)r   codefilenamelinenor;   r   s         r   r   r   I   sj    <<DH^^Fww)H
!F8$G||sIR~R0Nr   c                       \ rS rSrSrSrSrSrSrSr	Sr
SrSS jrS rSS jrS rSS jrS	 rS
 rS rS rS rS rS rS rS rS rS rS rS rSS jrS rS r S r!S r"Sr#g)DebuggerU   zThe debugger interface.

This class handles the drawing of the debugger window and
the interactions with the underlying debugger session.
Nc                     Uc  [        U 5      nXl        X l        SU l        U R	                  5         SU l        SU l        g)zInstantiate and draw a debugger window.

:param pyshell: An instance of the PyShell Window
:type  pyshell: :class:`idlelib.pyshell.PyShell`

:param idb: An instance of the IDLE debugger (optional)
:type  idb: :class:`idlelib.debugger.Idb`
NFr   )r
   pyshellidbr   make_guiinteractingnesting_level)r   rD   rE   s      r   r   Debugger.__init__c   s<     ;d)C
 r   c                    ^ ^ T R                   S:  a2  T R                  5         T R                  R                  SUU 4S j5        g ST l        T R
                  R                  " T6 ST l        $ ! ST l        f = f)zRun the debugger.r   d   c                  "   > TR                   " T 6 $ r   )run)argsr   s   r   <lambda>Debugger.run.<locals>.<lambda>   s    4r   NTF)rH   
abort_looprootafterrG   rE   rM   )r   rN   s   ``r   rM   Debugger.runu   sa    > !OOIIOOC!89	%#D88<<&$DuDs   A- -	A6c                 j    U R                  5         U R                  (       a  U R                  R	                  5         gU R
                  (       a!  U R
                  R                  5         SU l        U R                  R                  5         U R                  R                  5         g! [         a     Nf = f)zClose the debugger and window.N)
quit	ExceptionrG   topbellstackviewercloserD   close_debuggerdestroyr   events     r   r[   Debugger.close   s    	IIK HHMMO""$d&6 	##%  		s   B% %
B21B2c                 
   U R                   nUR                  U l        UR                  =U l        n[        U5      =U l        nU R                  R                  S5        U R                  R                  S5        UR                  SU R                  5        U R                  R                  SU R                  5        [        U5      =U l        nU R                  R                  SS9  / =U l        n[        USU R                  S9=U l        nUR#                  U5        [        US	U R$                  S9=U l        nUR#                  U5        [        US
U R(                  S9=U l        nUR#                  U5        [        USU R,                  S9=U l        nUR#                  U5        [        USU R0                  S9=U l        nUR#                  U5        U H!  nUR3                  SS9  UR                  SS9  M#     [        U5      =U l        nU R4                  R                  SS9  U R6                  (       d5  [9        U5      U R:                  l        U R6                  R=                  S5        [?        USU R@                  U R6                  S9U l!        U RB                  RE                  SSS9  U RF                  (       d  [9        U5      U R:                  l#        [?        USU RH                  U RF                  S9U l%        U RJ                  RE                  SSS9  U RL                  (       d5  [9        U5      U R:                  l&        U RL                  R=                  S5        [?        USU RN                  U RL                  S9U l(        U RP                  RE                  SSS9  U RR                  (       d  [9        U5      U R:                  l)        [?        USU RT                  U RR                  S9U l+        U RV                  RE                  SSS9  [Y        USS9U l-        U RZ                  R                  SS9  [Y        USS9U l.        U R\                  R                  SSS9  U R\                  R_                  S5      U l0        [        USS9U l1        U Rb                  R                  SSS9  [        U5      U l2        U Rd                  R                  SSS9  [        USS9U l3        U Rf                  R                  SSS9  U R6                  Ri                  5       (       a  U RA                  5         U RL                  Ri                  5       (       a  U RO                  5         U RR                  Ri                  5       (       a  U RU                  5         gg) z$Draw the debugger gui on the screen.zDebug ControlDebugWM_DELETE_WINDOWz<Escape>w)anchorGo)textcommandStepOverOutQuitdisabledstateleft)side   Stack)rg   rh   variabler   rowcolumnSourceLocalsGlobalsx)re   fill
background)heightbothexpandr|   N)5rD   flistrR   r   rX   wm_titlewm_iconnamewm_protocolr[   bindr   bframepackbuttonsButtoncontbcontappendstepbstepnextbnextretbretrV   	configurecframevstack
BooleanVarr   setCheckbutton
show_stackbstackgridvsourceshow_sourcebsourcevlocalsshow_localsblocalsvglobalsshow_globalsbglobalsLabelstatuserrorcgeterrorbgfstackflocalsfglobalsget)r   rD   rR   rX   r   blbr   s           r   rF   Debugger.make_gui   s1   ,,]]
"<<'	D'--3/*W%*DJJ7j$**-$Sz)f$rT499EE
Q
		!VTYYGG
Q
		!VTYYGG
Q
		!vE488DD	A
		!vFDIIFF	A
		!AKKjK)FFF   %V},ff%{{$.sODNN!KKOOA!&$//DKKIQq)||%/_DNN""64#3#3dllLa*||%/_DNN"LLQ"64#3#3dllLa*}}&0oDNN##FD$5$5Oq+C,$3s+


s-zz|4C*/Sz0c!,!&1;;??OO<<== r   c                 ,   X l         U R                  R                  US9  U(       a   Uu  pEn UR                  nUb   U SU 3nSnOSnS nU R                  nU R                  R                  XxS9  U R                  n	U	(       a9  U R                  R                  U R                   U5      u  pU	R                  X5        U R                  S5        U R                  R                  5       (       a  U R                  5         U R                    H  nUR                  SS	9  M     U R"                  R%                  5         U =R&                  S-  sl        U R(                  R*                  R-                  S
S5        U =R&                  S-  sl        U R                    H  nUR                  SS	9  M     U R                  R                  SS9  U R                  R                  SU R                  S9  S U l         g ! [         a    S[        U5      -  n GNf = f!    GN= f)Nrg   z%sr7   yellow )rg   r}   rr   normalrn   vwait::idledebugwaitrm   )r   r   r   r$   AttributeErrorstrr   r   rZ   rE   	get_stack
load_stackshow_variablesr   r   sync_source_liner   rX   wakeuprH   rR   tkcall)r   r   r   infotypevaluetbm1bgsvstackir   s                r   r   Debugger.interaction   s   
7+"OD&]]  4r%)B BBBB

"4xx))$**b9HEMM%#A<<!!#AKKhK'  	 	a		'#45aAKKjK) 2&

">
Q " &CI%&s   G/ H /H
HHc                     U R                   nU(       d  g U R                  U5      u  p#US S USS  -   S:w  aA  [        R                  R	                  U5      (       a  U R
                  R                  X#5        g g g )Nrr   z<>)r   _Debugger__frame2fileliner9   r:   existsr   gotofileline)r   r   r>   r?   s       r   r   Debugger.sync_source_line.  sj    

007BQ<(23-'4/BGGNN84L4LJJ##H5 5M/r   c                 P    UR                   nUR                  nUR                  nX44$ r   )r.   r/   r8   )r   r   r=   r>   r?   s        r   __frame2filelineDebugger.__frame2fileline6  s(    ||##r   c                 X    U R                   R                  5         U R                  5         g r   )rE   set_continuerQ   r   s    r   r   Debugger.cont<  s    r   c                 X    U R                   R                  5         U R                  5         g r   )rE   r   rQ   r   s    r   r   Debugger.step@      r   c                 n    U R                   R                  U R                  5        U R                  5         g r   )rE   set_nextr   rQ   r   s    r   r   Debugger.nextD  s"    $**%r   c                 n    U R                   R                  U R                  5        U R                  5         g r   )rE   
set_returnr   rQ   r   s    r   r   Debugger.retH  s"    DJJ'r   c                 X    U R                   R                  5         U R                  5         g r   )rE   set_quitrQ   r   s    r   rV   Debugger.quitL  r   r   c                 R    U R                   R                  R                  SSS5        g )Nr   r   1)rR   r   r   r   s    r   rQ   Debugger.abort_loopP  s    		%!2C8r   c                    U R                   (       d  U R                  R                  5       (       at  [        U R                  U R
                  U 5      =U l         nU R                  (       a:  U R                  R                  U R                  S 5      u  p#UR                  X#5        g g U R                   nU(       a6  U R                  R                  5       (       d  S U l         UR                  5         SU R                  S'   g )Nrr   r~   )rZ   r   r   StackViewerr   r   r   rE   r   r   r[   )r   r   r   r   s       r   r   Debugger.show_stackS  s    DKKOO$5$5$/TZZ$NNDrzz88--djj$?e'  !!B$++//++#' 
$%DKK!r   c                 d    U R                   R                  5       (       a  U R                  5         g g r   )r   r   r   r   s    r   r   Debugger.show_source`  s&    <<!!# r   c                 8    US   U l         U R                  5         g Nr   )r   r   )r   	stackitems     r   
show_frameDebugger.show_framed  s    q\
r   c                    U R                   nU R                  R                  5       (       a#  U(       d  [        U R                  S5      U l         O-U(       a&  S U l         UR                  5         SU R                  S'   U R                  5         g )Nry   rr   r~   )localsviewerr   r   NamespaceViewerr   r[   r   )r   lvs     r   r   Debugger.show_localsh  se    <<$3DLL($K!$(!
)*X&r   c                    U R                   nU R                  R                  5       (       a#  U(       d  [        U R                  S5      U l         O-U(       a&  S U l         UR                  5         SU R                  S'   U R                  5         g )Nrz   rr   r~   )globalsviewerr   r   r   r   r[   r   )r   gvs     r   r   Debugger.show_globalst  se    ==%4T]]I%N"%)"
*+h'r   c                    U R                   nU R                  nU R                  nU(       d  S =pVO,UR                  nUR                  nU(       a  U(       a  XVL a  S nU(       a0  UR                  XQU R                  R                  R                  5        U(       a1  UR                  XaU R                  R                  R                  5        g g r   )	r   r   r   f_locals	f_globals	load_dictrD   interprpcclt)r   forcer   r   r   ldictgdicts          r   r   Debugger.show_variables  s    

  EENNEOOEbU^LLt||':':'A'ABLLt||':':'A'AB r   c                 :    U R                   R                  X5        g)zjSet a filename-lineno breakpoint in the debugger.

Called from self.load_breakpoints and EW.setbreakpoint
N)rE   	set_breakr   r>   r?   s      r   set_breakpointDebugger.set_breakpoint  s    
 	8,r   c                 :    U R                   R                  X5        g r   )rE   clear_breakr  s      r   clear_breakpointDebugger.clear_breakpoint  s    X.r   c                 :    U R                   R                  U5        g r   )rE   clear_all_file_breaks)r   r>   s     r   clear_file_breaksDebugger.clear_file_breaks  s    &&x0r   c                     U R                   R                  R                   H>  nUR                  R                  n UR
                   H  nU R                  X#5        M     M@     g! [         a     MP  f = f)z>Load PyShellEditorWindow breakpoints into subprocess debugger.N)rD   r   inversedictior>   breakpointsr  r   )r   editwinr>   r?   s       r   load_breakpointsDebugger.load_breakpoints  sb    ||))55Gzz**H%11F''9 2 6
 " s   $A$$
A21A2)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rE   rG   r   rH   rD   rR   rZ   r   rX   r   )r   )$r$   r%   r&   r'   r(   r   r   r   r   rZ   r   r   r   rM   r[   rF   r   r   r   r   r   r   r   rV   rQ   r   r   r   r   r   r   r  r  r  r  r)    r   r   rA   rA   U   s    
 FGGHKLM$'%R$J X0d6 9&$

C -/1r   rA   c                   R    \ rS rSrSrS rSS jrS rS rS r	S	 r
S
 rS rS rSrg)r   i  z#Code stack viewer for debugger GUI.c                     [         R                  " 5       (       a  [        R                  " X5        O[        R                  " XSS9  X l        X0l        / U l        g )NP   )width)r   isAquaTkr   r   r   r   r   )r   masterr   r   s       r   r   StackViewer.__init__  s@    ?? !!$/!!$b9

r   Nc                    Xl         U R                  5         [        [        U5      5       H  nX   u  pE UR                  S   nUR
                  nUR                  nUR                  n	SS Kn
U
R                  X5      nUR                  5       nU	S;   a  SXeU4-  nOSXiX[4-  nX2:X  a  SU-   nU R                  U5        M     Ub  U R                  U5        g g !   Sn N= f)Nr$   r6   r   )r6   r   Nz%s, line %d: %sz%s.%s(), line %d: %sz> )r   clearrangelenr   r.   r/   r<   	linecachegetlinestripr   select)r   r   indexr   r   r?   modnamer=   r>   funcnamer%  
sourcelineitems                r   r   StackViewer.load_stack  s    


s5z"A!HME//*5 <<D''H||H"**8<J#))+J?*(GZ+HH-171E Ezd{KK' #( KK !s   CCc                 R    U R                   (       a  [        R                  " X5      $ gOverride base method.N)r   r   popup_eventr^   s     r   r2  StackViewer.popup_event  s    ::++D88 r   c                     U R                   nUR                  SU R                  S9  UR                  SU R                  S9  g)r1  zGo to source line)labelrh   zShow stack frameN)menuadd_commandgoto_source_lineshow_stack_frame)r   r6  s     r   	fill_menuStackViewer.fill_menu  sI    yy2!%!6!6 	 	81!%!6!6 	 	8r   c                     SUs=::  a  [        U R                  5      :  a,  O  gU R                  R                  U R                  U   5        gg)r1  r   N)r$  r   r   r   r   r)  s     r   	on_selectStackViewer.on_select  s7    'DJJ'HH

5 12 (r   c                 &    U R                  U5        gr0  )r   r=  s     r   	on_doubleStackViewer.on_double  s    r   c                 \    U R                   R                  S5      nU R                  U5        g )Nactive)listboxr)  r   r=  s     r   r8  StackViewer.goto_source_line  s$    ""8,r   c                     U R                   R                  S5      nSUs=::  a  [        U R                  5      :  a,  O  g U R                  R                  U R                  U   5        g g )NrD  r   )rE  r)  r$  r   r   r   r=  s     r   r9  StackViewer.show_stack_frame  sK    ""8,'DJJ'HH

5 12 (r   c                 P   SUs=::  a  [        U R                  5      :  d   g   g U R                  U   u  p#UR                  nUR                  n[        R
                  R                  U5      (       a5  U R                  R                  U5      nU(       a  UR                  U5        g g g r   )
r$  r   r.   r/   r9   r:   isfiler   opengotoline)r   r)  r   r?   r=   r>   edits          r   r   StackViewer.show_source  s    U,S_, -

5)||##77>>(##::??8,Df%  $r   )r   r   r   r   )r$   r%   r&   r'   r(   r   r   r2  r:  r>  rA  r8  r9  r   r)   r  r   r   r   r     s2    )
49
83
  3
	&r   r   c                   6    \ rS rSrSrS	S jrSrS
S jrS rSr	g)r   i  z/Global/local namespace viewer for debugger GUI.Nc           
         SnSnU(       a  S[        U5      -  nXl        X l        SS KnUR	                  5       U l        SU R
                  l        SU R
                  l        [        U5      =U l	        nU R                  R                  SSS9  [        XrSS	S
9U l        U R                  R                  SS9  [        USS9=U l        nUR                  SSS9  [        U[!        S[#        SU5      5      SSXE4S9=U l        n	U	R                  SSSS9  U	R&                  US'   UR(                  U	S'   [        U	5      =U l        n
U	R-                  SSU
SS9U l        U R1                  U5        g )Nr   (      <   rr   r   r      groove)rg   borderwidthreliefr{   )r|   vbar)namerighty)rq   r|   ,  )r~   scrollregionrp   )rq   r|   r   rh   yscrollcommandnw)windowre   )r$  r  titlereprlibReprrepr	maxstringmaxotherr   r   r   r   r5  r   rX  Canvasminmaxcanvasyviewr   subframecreate_windowsfidr   )r   r  ra  odictr  r~   rb  r   rX  rj  rl  s              r   r   NamespaceViewer.__init__  sO   E
]F
LLN	 				"6]*
U

qv.5!HM


S!$U88	D		wS	)%e-0c"fo-F45q%3H J 	Jf 	fQ7 ,,Y#'88 #(=0((Aht(L	ur   r   c                 h   XR                   L a  U(       d  g U R                  nU R                  n[        UR                  R                  5       5       H  nUR                  5         M     S U l         U(       d  [        USS9nUR                  SSS9  OUR                  5       n[        U5      n	Sn
U	 H{  nX   nU R                  R                  U5      nU(       a  USS n[        XKS9nUR                  U
SSS9  [        USSS	9nUR                  SU5        UR                  U
SSS9  U
S-   n
M}     Xl         UR                  5         UR                  5       nUR!                  5       nU R"                  nSSX4U R"                  S
'   US:  a  SUS'   UR%                  SS9  g UUS'   UR%                  SS9  g )NNoner   r   ru   rr   r   r_  )rv   rw   sticky)r  rV  r]  r\  r~   )r   )
prev_odictrl  r   listchildrenvaluesr]   r   r   keyssortedrd  Entryinsertupdate_idletaskswinfo_reqwidthwinfo_reqheightrj  r   )r   ro  r  
rpc_clientrl  r   cl	keys_listnamesrv   rY  r   svaluer  r~   rj  s                    r   r   NamespaceViewer.load_dict  s   OO#E==

h''..01AIIK 2hV,AFFqF# 

I9%EC. #Ab\F(.3q6(!;F#3q6!e   !!#'')))+'(!U&;N#C<"F8JJaJ %F8JJaJ r   c                 8    U R                   R                  5         g r   )r   r]   r   s    r   r[   NamespaceViewer.closeR  s    

r   )
rj  r   r5  r  rt  rd  rn  rl  ra  rX  r   )r   N)
r$   r%   r&   r'   r(   r   rt  r   r[   r)   r  r   r   r   r     s    56 J4!lr   r   __main__)mainzidlelib.idle_test.test_debuggerrT  F)	verbosityexit)r(   bdbr9   tkintertkinter.ttkr   r   r,   r   idlelib.scrolledlistr   idlelib.windowr   Bdbr
   r   r   rA   r   r   r$   unittestr  r  r   r   <module>r     s   "  	  (  - )7#'' 7:(	P Pf
Q&, Q&hW Wt z	*aeD r   