
    JeXi                         d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ  G d de
      Z G d de	      Z G d de      Z G d de      Z  G d de e      Z!y)zS
Widget that handles communications between a console in debugging
mode and Spyder
    N)HistoryManager)TransformerManager)IPythonLexerIPython3Lexer)bygroups)KeywordOperatorText)ClassNotFound)RichJupyterWidget)QEvent)QTextCursor)SpyderConfigurationAccessorget_conf_pathc            	       b    e Zd Zej                  Zed   j                  dd eeee	      f       y)SpyderIPy3Lexerrootr   (!)(\w+)(.*\n)N
__name__
__module____qualname__r   tokensinsertr   r	   r   r
        Q/usr/lib/python3/dist-packages/spyder/plugins/ipythonconsole/widgets/debugging.pyr   r   !   s4    !!F
6N	x'4@ACr   r   c            	       b    e Zd Zej                  Zed   j                  dd eeee	      f       y)SpyderIPy2Lexerr   r   r   Nr   r   r   r   r    r    (   s4    !!F
6N	x'4@ACr   r    c                       e Zd ZddZy)
PdbHistoryNc                     t        d      S )z
        Get default pdb history file name.

        The profile parameter is ignored, but must exist for compatibility with
        the parent class.
        zpdb_history.sqliter   )selfprofiles     r   _get_hist_file_namezPdbHistory._get_hist_file_name0   s     122r   N)r   r   r   r&   r   r   r   r"   r"   .   s    3r   r"   c                        e Zd ZdZdZ fdZd Zd Zd Ze	d        Z
e
j                  d        Z
e	d	        Zej                  d
        Ze	d        Zej                  d        Z xZS )DebuggingHistoryWidgetzu
    Widget with the necessary attributes and methods to override the pdb
    history mechanism while debugging.
    i  c                 h   d| _         g | _        	 t               | _        | j                  j	                  | j
                  d      D cg c]  }|d   	 c}| _        i | _        t        | j                        | _
        t        t        | 6  |i | y c c}w # t        $ r d | _        g | _        Y Sw xY w)Nr   T)include_latest)_pdb_history_input_number_saved_pdb_history_input_numberr"   _pdb_history_fileget_tailPDB_HIST_MAX_pdb_history	Exception_pdb_history_editslen_pdb_history_indexsuperr)   __init__)r$   argskwargsline	__class__s       r   r8   zDebuggingHistoryWidget.__init__A   s    )*&/1,
	#%/\D"%)%;%;%D%D%%d &E &<!!R!D #%"%d&7&7"8 	$d4dEfE!  	#%)D" "D	#s#   8B 	BB B B10B1c                 `    d| _         | j                  | j                  j                          yy)zStart a new history session.r   N)r-   r/   new_sessionr$   s    r   new_history_sessionz*DebuggingHistoryWidget.new_history_sessionZ   -    )*&!!-""..0 .r   c                 `    d| _         | j                  | j                  j                          yy)zEnd an history session.r   N)r-   r/   end_sessionr?   s    r   end_history_sessionz*DebuggingHistoryWidget.end_history_session`   rA   r   c                 >   | xj                   dz  c_         | j                   }t        | j                        | _        i | _        |j                         }|sy| j                  }t        |      dkD  r	|d   |k(  ry|j                  d      d   }|j                  d      dd }|j                         xr( |d   dk7  xr d|z   t        t        j                        v }| j                         rA|xs= |j                         xr+ |d   dk(  xr! d|dd z   t        t        j                        v }|rp|rt        |      dkD  r_| j                  j                  |       t        | j                        | _        | j                  | j                  j                  ||       yyyy)zAdd command to history   Nr   r,    !do_)r-   r5   r2   r6   r4   rstripsplitstripdirpdbPdbis_pdb_using_exclamantion_markappendr/   store_inputs)r$   r;   line_numhistorycmdr9   
is_pdb_cmds          r   add_to_pdb_historyz)DebuggingHistoryWidget.add_to_pdb_historyf   s   &&!+&11"%d&7&7"8"${{} ##w<!t 3jjoa zz#qr"IIKICFcMIeckS\.I 	 ..0# 5		 4A# 4CGOs377|3  
c$i!m$$T*&)$*;*;&<D#%%1&&33HdC 2 '43r   c                 R    | j                         r| j                  S | j                  S )zGet history.is_debuggingr2    _DebuggingHistoryWidget__historyr?   s    r   _historyzDebuggingHistoryWidget._history   s&     $$$>>!r   c                 B    | j                         r|| _        y|| _        y)zSet history.NrY   )r$   rT   s     r   r\   zDebuggingHistoryWidget._history   s      'D$DNr   c                 R    | j                         r| j                  S | j                  S )zGet edited history.rZ   r4   &_DebuggingHistoryWidget__history_editsr?   s    r   _history_editsz%DebuggingHistoryWidget._history_edits   (     ***'''r   c                 B    | j                         r|| _        y|| _        y)zSet edited history.Nr_   )r$   history_editss     r   ra   z%DebuggingHistoryWidget._history_edits         &3D##0D r   c                 R    | j                         r| j                  S | j                  S )zGet history index.rZ   r6   &_DebuggingHistoryWidget__history_indexr?   s    r   _history_indexz%DebuggingHistoryWidget._history_index   rb   r   c                 B    | j                         r|| _        y|| _        y)zSet history index.Nrg   )r$   history_indexs     r   ri   z%DebuggingHistoryWidget._history_index   re   r   )r   r   r   __doc__r1   r8   r@   rD   rW   propertyr\   setterra   ri   __classcell__r<   s   @r   r)   r)   :   s     LF211D@ " " __% % ( ( 1 1 ( ( 1 1r   r)   c                   (    e Zd ZdZdZ fdZd Zd Zd Zd&dZ	 fdZ
	 	 d'd	Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd& fd	Zd Zd Z fdZd Zd(dZd Z d) fd	Z!d  Z"d(d!Z#	 	 d* fd"	Z$ fd#Z% fd$Z& fd%Z' xZ(S )+DebuggingWidgetz
    Widget with the necessary attributes and methods to handle
    communications between a console in debugging mode and
    Spyder
    ipython_consolec                 V   d| _         d| _        d| _        d| _        d| _        d| _        d| _        g | _        d| _        t        t        | .  |i | t        j                  dj                  t        j                  | j                               dz   dz         | j"                  _        y )Nr   FNN z^({})?(z[ \t]*\(*IPdb \[\d+\]\)*: |z)[ \t]*In \[\d+\]: |[ \t]*\ \ \ \.\.\.+: ))_pdb_in_loop_pdb_input_ready_waiting_pdb_input_pdb_prompt_pdb_last_cmd_pdb_frame_loc_pdb_focus_to_editor_pdb_input_queue_tmp_readingr7   rr   r8   recompileformatescapeother_output_prefix_highlighter_ipy_prompt_re)r$   r9   r:   r<   s      r   r8   zDebuggingWidget.__init__   s     %"''*$)! !#!ot-t>v> ,.::bii(@(@AB*+89,:(r   c                 T   | j                   ~	 | j                   j                  j                          t        j                  | j                   j                  j                         	 | j                   j                  j                          yy# t
        $ r Y 2w xY w# t
        $ r Y yw xY w)z:
        Close the save thread and database file.
        N)r/   save_threadstopatexit
unregisterAttributeErrordbcloser?   s    r   shutdownzDebuggingWidget.shutdown   s     !!-&&22779!!$"8"8"D"D"I"IJ&&))//1 .
 " 
 " s$   AB &$B 	BB	B'&B'c                    |rj| j                   dkD  r5| j                  j                  | j                         | j	                          | j                          | xj                   dz  c_         nr| j                   dkD  rc| xj                   dz  c_         | j	                          | j                   dkD  r/| j                          | j                  j                         | _        g | _        d| _        y)zUpdate the debug state.r   rF   ru   N)	rw   r.   rQ   r-   rD   r@   popr~   r|   )r$   rZ   s     r   set_debug_statezDebuggingWidget.set_debug_state   s      1$44;;224((*$$&"""$$&  1$((*88<<> . !#*r   c                 b    d}| j                   j                         r| j                         rd}|S )zReturn the command prefixrv   rH   )spyder_kernel_commis_openrP   )r$   prefixs     r   _pdb_cmd_prefixzDebuggingWidget._pdb_cmd_prefix  s/    ##++-335Fr   c                 ^    || _         | j                  | j                         |z   ddd       y)z'
        Execute a pdb command
        F)hiddenecho_stack_entryadd_historyN)r}   pdb_executer   )r$   commandfocus_to_editors      r   pdb_execute_commandz#DebuggingWidget.pdb_execute_command  s7     %4!  "W,U" 	 	7r   c                    | j                   j                         s|d   d   dk(  r| j                          | j                         }|j	                  | j
                  t        j                         |j                         j                         }t        j                  d|      }|r8|j                         \  }}| j                  j                  |t        |             t         t"        | K  |      S )zProcess an input request.contentpromptzipdb> z> (.*\.py)\((\d+)\))r   r   _flush_pending_stream_get_end_cursorsetPosition_prompt_posr   
KeepAnchor	selectiontoPlainTextr   searchgroupssig_pdb_stepemitintr7   rr   _handle_input_request)r$   msgcursortextmatchfnamelinenor<   s          r   r   z%DebuggingWidget._handle_input_request  s    ''//1Ix(H4&&())+Ft//1G1GH##%113DII4d;E %v!!&&uc&k:_dA#FFr   c                     | j                   j                         sB|s| j                  |dz          | j                          | j                  j                  |       y| j                         sy|j                         s| j                  }|rd}n| j                         s | j                  j                  ||||f       y|j                         r|| _        |j                         | j                  j                         k7  r|| _        | j                  d       |r| j                  |       | j                  | _        d| _        d| _        d| _        | j                          d}| j$                  j'                  |       | j(                  j'                  d| j+                                | j,                  r*d| _        | j/                  d      j1                  ||      S | j                  j                  ||||f       y)a  
        Send line to the pdb kernel if possible.

        Parameters
        ----------
        line: str
            the line to execute

        hidden: bool
            If the line should be hidden

        echo_stack_entry: bool
            If not hidden, if the stack entry should be printed

        add_history: bool
            If not hidden, wether the line should be added to history
        
NFT	interrupt)r   )r   r   _append_plain_text_finalize_input_requestkernel_clientinputrZ   rL   r{   is_waiting_pdb_inputr~   rQ   input_bufferrW   _input_buffer_executing
_executingry   r   	executingr   sig_pdb_state_changedget_pdb_last_steprx   call_kernelpdb_input_reply)r$   r;   r   r   r   s        r   r   zDebuggingWidget.pdb_execute'  s   ( &&..0''t4((*$$T*  "zz|%%D$,,.%%,,6#3[ACzz|%)" zz|t006688$(!##D)''- ,0+<+<D("DO&+D# !&D((*F NN%&&++t--/1   $)D!##d#3CC'7 D 9 9 	$$6+[9	;r   c                     | j                  di d      | j                  d      | j                  d      | j                         | j                  d      dS )zGet pdb settingsbreakpointsrundefaultsectionpdb_ignore_libpdb_execute_eventspdb_stop_first_line)r   r   r   pdb_use_exclamation_markr   )get_confrP   r?   s    r   get_pdb_settingsz DebuggingWidget.get_pdb_settingsw  sY      ==r5 ) :"mm,<="&--0D"E(,(K(K(M#'==1F#G
 	
r   c                     | j                   j                         r | j                         s| j                          | j	                  d       y)zStop debugging.exitN)r   r   r   interrupt_kernelr   r?   s    r   stop_debuggingzDebuggingWidget.stop_debugging  s:    ##++---/!!#  (r   c                 j    | j                  d      j                  | j                  di d             y)z/Set Spyder breakpoints into a debugging sessionTr   r   r   r   N)r   set_breakpointsr   r?   s    r   set_spyder_breakpointsz&DebuggingWidget.set_spyder_breakpoints  s1    4(88MM-UMC	Er   c                 F    | j                  d      j                  |       y)z+Set pdb_ignore_lib into a debugging sessionTr   N)r   set_pdb_ignore_lib)r$   r   s     r   r   z"DebuggingWidget.set_pdb_ignore_lib  s!    4(;;	r   c                 F    | j                  d      j                  |       y)z/Set pdb_execute_events into a debugging sessionTr   N)r   set_pdb_execute_events)r$   r   s     r   r   z&DebuggingWidget.set_pdb_execute_events  s!    4(??	 r   c                 F    | j                  d      j                  |       y)z5Set pdb_use_exclamation_mark into a debugging sessionTr   N)r   set_pdb_use_exclamation_mark)r$   r   s     r   r   z,DebuggingWidget.set_pdb_use_exclamation_mark  s!    4(EE$	&r   c                 $    | j                  d      S )Nr   )r   r?   s    r   rP   z.DebuggingWidget.is_pdb_using_exclamantion_mark  s    }}788r   c                 `    | j                   \  }}|r| j                  j                  ||       yy)z-Where was called, go to the current location.N)r|   r   r   r$   r   r   s      r   do_wherezDebuggingWidget.do_where  s/    ++v""5&1 r   c                    d|v rOd|d   v rH|d   d   }|d   d   }| j                   }||f| _         ||f|k7  r| j                  j                  ||       d|v r| j                  |d          d|v r| j	                  |d          yy)z
        Refresh Variable Explorer and Editor from a Pdb session,
        after running any pdb command.

        See publish_pdb_state and notify_spyder in spyder_kernels
        stepr   r   namespace_viewvar_propertiesN)r|   r   r   set_namespace_viewset_var_properties)r$   	pdb_stater   r   last_pdb_locs        r   refresh_from_pdbz DebuggingWidget.refresh_from_pdb  s     Y7i.?#?f%g.Ev&x0F..L#(&/D v,.!!&&uf5y(##I.>$?@y(##I.>$?@ )r   c                 N    |#t        |t              r| j                  |       yyy)zSet current pdb state.N)
isinstancedictr   )r$   r   s     r   set_pdb_statezDebuggingWidget.set_pdb_state  s(     Z	4%@!!), &A r   c                 T   | j                  | j                  d       | j                         }| j                  d|z  d       d|v r.| j                  j	                  d      s| j                  dd       | j                  || j
                  z   d       | j                  dd       y)zShow Pdb output.T)before_promptz"<span class="out-prompt">%s</span>r   N)r   
output_sep_current_out_prompt_append_htmlendswithoutput_sep2)r$   r   r   s      r   show_pdb_outputzDebuggingWidget.show_pdb_output  s    tD))+069 	 	
 4< 8 8 >##D#=t'7'7 7tLD9r   c                 2    | j                   \  }}|i S ||dS )z/Get last pdb step retrieved from a Pdb session.)r   r   )r|   r   s      r   r   z!DebuggingWidget.get_pdb_last_step  s+    ++v=I " 	"r   c                      | j                   dkD  S )zCheck if we are debugging.r   )rw   r?   s    r   rZ   zDebuggingWidget.is_debugging  s      1$$r   c                 >    | j                         xr | j                  S )z$Check if we are waiting a pdb input.)rZ   ry   r?   s    r   r   z$DebuggingWidget.is_waiting_pdb_input  s       ">t'>'>>r   c                    t         t        |   |       |r| j                         r~| j                  \  }}	 | j
                  j                  j                         s$| j
                  j                  j                          d| _
        | j                  || j                  |       yyy# t        $ r' | j
                  j                  j                          Y Ww xY w)z_
        Resets the widget to its initial state if ``clear`` parameter
        is True
        Fr   callbackpasswordN)r7   rr   resetr   rz   r   iopub_channelclosedflushr   _reading	_readline_pdb_readline_callback)r$   clearr   r   r<   s       r   r   zDebuggingWidget.reset  s    
 	ot*51T..0#//FH9 ))77>>@&&44::< "DMNN&43N3N$,  . 15 " 9""006689s   AB' '-CCc                     dj                  | j                  dz         }t        | j                  dz
        D ]  }dj                  |      } |dz   S )Nz	IPdb [{}]rF   ({}): r   r-   rangerw   r$   r   is      r   _current_promptzDebuggingWidget._current_prompt  sV    ##D$B$BQ$FGt((1,- 	+A]]6*F	+ }r   c                     dj                  | j                        }t        | j                  dz
        D ]  }dj                  |      } |dz   S )zGet current out prompt.u   Out  [{}]rF   r	  r
  r  r  s      r   r   z#DebuggingWidget._current_out_prompt  sQ    &--d.L.LMt((1,- 	+A]]6*F	+ }r   c                     t         t        |   |       |d   d   j                  dd      }	 |dk(  rt	               }n|dk(  rt               }ny|| j                  _        y# t        $ r Y yw xY w)zHandle kernel info replies.r   language_infopygments_lexerrv   ipython3ipython2N)	r7   rr   _handle_kernel_info_replygetr   r    r   _lexerr   )r$   repr  lexerr<   s       r   r  z)DebuggingWidget._handle_kernel_info_reply  sx    ot>sCY8<<b"
	+'):-')',D$ 		s    A! A! !	A-,A-c                 @     j                   d fd	}|_         y)zARedefine kernel client's complete method to work while debugging.Nc                     j                         r:j                  r.j                  }j                  _        	  | |      |_        S  | |      S # |_        w xY wr'   )r   comm_channelshell_channel_shell_channel)code
cursor_posr  clientoriginal_completer$   s      r   completez<DebuggingWidget._redefine_complete_for_dbg.<locals>.complete"  s`    ((*v/B/B & 4 4(.(;(;%:,T:>,9F)(z:: -:F)s   A 	Ar'   )r$  )r$   r"  r$  r#  s   `` @r   _redefine_complete_for_dbgz*DebuggingWidget._redefine_complete_for_dbg  s     #OO		; #r   c                     || j                   d   k(  ry|| j                   d   }||f| _         | j                  | j                  |      d       y)z5Update the prompt that is recognised as a pdb prompt.r   NrF   T)html)rz   _set_continuation_prompt_make_continuation_prompt)r$   r   r   s      r   _update_pdb_promptz"DebuggingWidget._update_pdb_prompt/  s`    T%%a((''*H"H- 	%%**62 	& 	?r   c                 x    |r|d   dk(  r|dd }t               }|j                  |      \  }}||dz  }|dk7  |fS )zA
        Check if the pdb input is ready to be executed.
        r   rH   rF   NrG   
incomplete)r   check_complete)r$   sourcetmr$  indents        r   _is_pdb_completez DebuggingWidget._is_pdb_complete=  sY     fQi3&ABZF!,,V4&c\F<'//r   c                 j   | j                         r||ry| j                  }n| j                         sd|z   }|s|| _        |r*| j                  |      \  }}|s| j	                  |||       y|r| j                  ||       y| j                  r| j                          yt        t        | '  |||      S )z
        Executes source or the input buffer, possibly prompting for more
        input.

        Do not use to run pdb commands (such as `continue`).
        Use pdb_execute instead. This will add a '!' in front of the code.
        NrH   )
r   r   rP   r1  
do_executer   _reading_callbackr7   rr   execute)r$   r.  r   interactiver$  r0  r<   s         r   r5  zDebuggingWidget.executeI  s     $$&~!..F::< 6\F(.D%#'#8#8#@ &OOFHf=  0  ))**,_d3FK) 	)r   c                 &    | j                  |       y)z/Callback used when the user inputs text in pdb.N)r   )r$   r;   s     r   r  z&DebuggingWidget._pdb_readline_callbackn  s    r   c                    | j                         }| j                  ||       | j                   xr- t        | j                        dk(  xs | j                  d   d    }|r	 | j
                  j                  j                         s$| j
                  j                  j                          d| _
        | j                  || j                  |       d| _        d| j                  _        | j                   j#                  | j$                         | j&                  j#                          | j(                  j#                  d| j+                                d| _        | j/                  dd      }|r&|r$| j1                         r| j3                  |d	       y
t        | j                        dkD  r+| j                  j5                  d      } | j2                  |  y
y
# t        $ r( | j
                  j                  j                          Y \w xY w)zGet input for a command.r   rF   Tr   Fzstartup/pdb_run_linesrv   )r   )r   N)r  r*  r  r5   r~   r   r  r  r  r   ry   r  r  r   r   highlighting_onexecutedr   rz   sig_pdb_prompt_readyr   r   rx   r   r   r   r   )r$   r   r   print_prompt
start_liner9   s         r   	pdb_inputzDebuggingWidget.pdb_inputr  s    %%'1
 !MM) A !6!671< @$($9$9!$<Q$? ? 	 9 ))77>>@&&44::< '+D#NN&43N3N$,  .#DO04D-MMt//0%%**,&&++D$2H2H2JK $]]#:B]G
,4+D+D+FZ5 t$$%)((,,Q/DDd# *5 " 9""006689s   "AG -HHc                 d    || j                   d   k(  rd}d|z  }t        t        |   ||||       y)z?
        Writes a new prompt at the end of the buffer.
        r   Tz!<span class="in-prompt">%s</span>N)rz   r7   rr   _show_prompt)r$   r   r'  newline	separatorr<   s        r   r@  zDebuggingWidget._show_prompt  s?    
 T%%a((D86AFot1&$2;	=r   c                 *   | j                         rZ| j                  | j                  _        | j                  | _        d| _        	 t        t        | #  |      }|| j
                  | _        S t        t        | #  |      S # | j
                  | _        w xY w)z'Handle Key_Up/Key_Down while debugging.F)	r   r   _controlcurrent_prompt_posr  r   r7   rr   _event_filter_console_keypress)r$   eventretr<   s      r   rF  z.DebuggingWidget._event_filter_console_keypress  s    $$&/3/?/?DMM, $D!DM2O AAF"G  $ 1 1>>CDE !% 1 1s   A? ?Bc                     | j                         r| j                  |      \  }} |||       yt        t        |   ||      S )z1Call the callback with the result of is_complete.N)r   r1  r7   rr   _register_is_complete_callback)r$   r.  r   r$  r0  r<   s        r   rJ  z.DebuggingWidget._register_is_complete_callback  sI     $$&#44V<HfXv&$N" "r   c                 F    t        |t              syt        |   ||      S )NT)r   r   r7   eventFilter)r$   objrG  r<   s      r   rL  zDebuggingWidget.eventFilter  s$     %(w"3..r   )F)FTTr'   )NFF)NFTT))r   r   r   rl   CONF_SECTIONr8   r   r   r   r   r   r   r   r   r   r   r   r   rP   r   r   r   r   r   rZ   r   r   r  r   r  r%  r*  r1  r5  r  r>  r@  rF  rJ  rL  ro   rp   s   @r   rr   rr      s     %L:6&+27G @D $N;`	
)E

 
&
92A0-
:"%?.0"#$?
0#)J4n =A#	=E"	"
/ 
/r   rr   )"rl   r   rN   r   IPython.core.historyr   IPython.core.inputtransformer2r   IPython.lib.lexersr   r   pygments.lexerr   pygments.tokenr   r	   r
   pygments.utilr   qtconsole.rich_jupyter_widgetr   qtpy.QtCorer   
qtpy.QtGuir   spyder.api.config.mixinsr   spyder.config.baser   r   r    r"   r)   rr   r   r   r   <module>rZ     s     
 	 0 = : # 2 2 ' ;  " A ,Cm CCl C	3 	3z1. z1zd/,.I d/r   