
    "e\                        d dl Z d dlZd dl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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  G d d      Z G d de      Z G d de      Z G d d      Z G d d      Z G d d      Zy)    N)	xmlrpclib_queueExec)get_description)pydevd_vars)
pydevd_xml)	IS_JYTHONNEXT_VALUE_SEPARATORget_global_debuggersilence_warnings_decorator)contextmanager)	pydev_log)interrupt_main_thread)StringIOc                   T    e Zd Zej                  fdZd Zd Zd Zd Z	d Z
d Zd Zy	)
	BaseStdInc                     	 t         j                  j                  | _        || _        	 t         j                  j                  | _        y #  Y ,xY w#  Y y xY wN)sysstdinencodingoriginal_stdinerrors)selfr   argskwargss       C/usr/lib/python3/dist-packages/_pydev_bundle/pydev_console_utils.py__init__zBaseStdIn.__init__   sK    	II..DM -	))**DK	
	s   A	 A 	AAc                      y)N
 r   r   r   s      r   readlinezBaseStdIn.readline$   s         c                      y r   r!   r"   s      r   writezBaseStdIn.write*       r$   c                      y r   r!   r"   s      r   flushzBaseStdIn.flush-   r'   r$   c                 "    | j                         S r   )r#   r"   s      r   readzBaseStdIn.read0   s    }}r$   c                      y r   r!   r"   s      r   closezBaseStdIn.close4   r'   r$   c                 6    | j                   j                         S r   )r   __iter__r   s    r   r/   zBaseStdIn.__iter__7   s    ""++--r$   c                     t        | j                  |      rt        | j                  |      S t        | j                  d|      )Nz has no attribute )hasattrr   getattrAttributeError)r   items     r   __getattr__zBaseStdIn.__getattr__;   s;    4&&-4..559L9LdSTTr$   N)__name__
__module____qualname__r   r   r   r#   r&   r)   r+   r-   r/   r6   r!   r$   r   r   r      s2    &)ii .Ur$   r   c                   :    e Zd ZdZej
                  fdZd Zd Zy)StdIne
        Object to be added to stdin (to emulate it as non-blocking while the next line arrives)
    c                 Z    t         j                  | |       || _        || _        || _        y r   )r   r   interpreterclient_porthost)r   r>   r@   r?   r   s        r   r   zStdIn.__init__J   s)    40&&	r$   c                     	 t        j                  d| j                  d| j                        }|j	                         }|sy|dz  }|S # t
        $ r   Y yxY w)Nhttp://:r    )r   Serverr@   r?   RequestInputKeyboardInterrupt)r   r   r   serverrequested_inputs        r   r#   zStdIn.readlineP   sa    	%%		4CSCS&TUF$113O"  4'""  		s   AA A Ac                      y r   r!   r"   s      r   r-   zStdIn.close`   r'   r$   N)	r7   r8   r9   __doc__r   r   r   r#   r-   r!   r$   r   r;   r;   E   s!     GJii  r$   r;   c                   8    e Zd ZdZd Zd Zed        Zd Zd Z	y)DebugConsoleStdInr<   c                 L    t         j                  | |       || _        d| _        y)zS
        :param py_db:
            If None, get_global_debugger() is used.
        r   N)r   r   _py_db_in_notification)r   py_dbr   s      r   r   zDebugConsoleStdIn.__init__l   s#    
 	40 !r$   c                     	 | j                   }|
t               }|y |j                  j                  |      }|j                  j                  |       y # t        $ r t        j                          Y y w xY wr   )	rN   r   cmd_factorymake_input_requested_messagewriteradd_command	Exceptionr   	exception)r   
is_startedrP   cmds       r   __send_input_requested_messagez0DebugConsoleStdIn.__send_input_requested_messageu   sh    	"KKE}+-}##@@LCLL$$S) 	"!	"s   A 6A A43A4c              #   b  K   | xj                   dz  c_         | j                   dk(  r| j                  d       	 d  | xj                   dz  c_         | j                   dk(  r| j                  d       y y # | xj                   dz  c_         | j                   dk(  r| j                  d       w w xY ww)N   Tr   F)rO   0_DebugConsoleStdIn__send_input_requested_messager0   s    r   notify_input_requestedz(DebugConsoleStdIn.notify_input_requested   s     "  A%//5	;!!Q&!$$)33E: * !!Q&!$$)33E: *s   6B/A4 7B/48B,,B/c                     | j                         5   | j                  j                  |i |cd d d        S # 1 sw Y   y xY wr   )r^   r   r#   r"   s      r   r#   zDebugConsoleStdIn.readline   sB    ((* 	A/4&&//@@	A 	A 	A	   7A c                     | j                         5   | j                  j                  |i |cd d d        S # 1 sw Y   y xY wr   )r^   r   r+   r"   s      r   r+   zDebugConsoleStdIn.read   s>    ((* 	=+4&&++T<V<	= 	= 	=r`   N)
r7   r8   r9   rJ   r   r]   r   r^   r#   r+   r!   r$   r   rL   rL   g   s0    "" 	; 	;A=r$   rL   c                       e Zd ZddZd Zy)CodeFragmentc                      || _         || _        y r   textis_single_line)r   rf   rg   s      r   r   zCodeFragment.__init__   s    	,r$   c                 n    | j                   dz   |j                   z   | _         |j                  sd| _        y y )Nr    Fre   r   code_fragments     r   appendzCodeFragment.append   s3    II$}'9'99	++"'D ,r$   N)T)r7   r8   r9   r   rk   r!   r$   r   rc   rc      s    -(r$   rc   c                       e Zd Zd#dZd Zd Zd Zd Zd Zd$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 ee      Zd Zd Zd Zed        Zd Zd Zed        Zd Z d#dZ!d Z"d Z#d  Z$d! Z%d" Z&y)%BaseInterpreterInterfaceNc                     || _         d| _        t        j                  d      | _        d | _        d| _        || _        i | _        | j                          y )NFr   )

mainThreadinterruptabler   Queue
exec_queuebufferbanner_shownconnect_status_queuempl_modules_for_patchinginit_mpl_modules_for_patching)r   ro   ru   s      r   r   z!BaseInterpreterInterface.__init__   sK    $" ,,q/!$8!(*%**,r$   c                 R    dj                  t        | j                                     S )Nzprint({0})
)formatreprget_greeting_msgr0   s    r   build_bannerz%BaseInterpreterInterface.build_banner   s!    $$T$*?*?*A%BCCr$   c                      y)NzPyDev console: starting.
r!   r0   s    r   r{   z)BaseInterpreterInterface.get_greeting_msg   s    +r$   c                 :     ddl mm}m}  fd||d _        y )Nr   )activate_matplotlibactivate_pylabactivate_pyplotc                  (      j                         S r   )	enableGui)r   r   s   r   <lambda>zHBaseInterpreterInterface.init_mpl_modules_for_patching.<locals>.<lambda>   s    "5dnn"E r$   )
matplotlibzmatplotlib.pyplotpylab)pydev_ipython.matplotlibtoolsr   r   r   rv   )r   r   r   r   s   `  @r   rw   z6BaseInterpreterInterface.init_mpl_modules_for_patching   s    ffE!0#)
%r$   c                    |j                  d      ryt        | j                  d      r| j                  j                  |       S 	 d}| j                  j	                  |d|      }|yy# t
        t        t        f$ r Y yw xY w)N\Tis_completesinglez<input>F)endswithr2   r>   r   compileOverflowErrorSyntaxError
ValueError)r   sourcesymbolcodes       r   need_more_for_codez+BaseInterpreterInterface.need_more_for_code   s    
 ??4 4##]3''33F;;;	 F##++FIvFD <  {J7 		s   A) )B ?B c                     | j                   || _         n| j                   j                  |       | j                  | j                   j                        S r   )rs   rk   r   rf   ri   s     r   	need_morez"BaseInterpreterInterface.need_more   s@    ;;'DKKK}-&&t{{'7'788r$   c                 f    |#t        | | j                  | j                  |      S t        ||      S )N)r   )rP   r   )r;   r@   r?   rL   )r   debuggeroriginal_std_ins      r   create_std_inz&BaseInterpreterInterface.create_std_in   s0    tyy$*:*:?[[$8OTTr$   c                 4   t         j                  t         _        	 t         j                  }	 d }dt         j                  v r9t         j                  d   }t        |d      r|j                  }t        |d      sd }d}	 | j                  ||      t         _        	 	 	 t         j                  |_        	 | j!                          t        | d      r| j"                  j%                          | j'                  |      }t        | d      r| j"                  j)                          | j+                  |       |		 	 ||_        |t         _        	 t         j                  t         _        |S #  Y xY w# t        $ r t         j                  |_
        Y w xY w#  d }| j                  sYd| _        t         j                  j                  d       t         j                  j                  d       t        j                          Y HxY w# t        $ r
 ||_
        Y w xY w#  Y xY w# |'	 	 ||_        w # t        $ r
 ||_
        Y w w xY w#  Y w xY ww xY w# |t         _        w xY w# t,        $ r   t        j                          Y 2xY w# t         j                  t         _        w xY w)	NpydochelpinputFTz.
Error when trying to update pydoc.help.input
zO(help() may not work -- please report this as a bug in the pydev bugtracker).

r   )r   __excepthook__
excepthookr   modulesr2   r   r   r   r4   _input_input_error_printedstderrr&   	traceback	print_exc
start_execr   enable_tracingdo_add_execdisable_tracingfinish_exec
SystemExit)r   rj   r   original_inr   r   mores          r   add_execz!BaseInterpreterInterface.add_exec   sE    ++?	0))Kckk)KK0Euf-$zz&tW5#'D
 D,& ..xE	&,'68-0YY
%)"44 MM88:#//>"44 MM99;((.+%!>1<DJ !,CI "%Ck $2 8.1ii86#'D#'#<#<<@ 9 #

 0 01c d #

 0 0  2E  !F ) 3 3 5$ (6 !>2=DK!>% $  +%!>1<DJ'5 !>2=DK!>% $ , !,CI &##%!$Cs   I< AE" 9I< <I I E) 2A>H 0I 4G< ;I I< "E&$I< )F
F 	F

F A)G96I <HH HH HI IH&%I&H96H<7I8H99H<<I >II II I96I< <Jc                     t               )z
        Subclasses should override.

        @return: more (True if more input is needed to complete the statement and False if the statement is complete).
        NotImplementedError)r   codeFragments     r   r   z$BaseInterpreterInterface.do_add_exec5       "##r$   c                     t               )zT
        Subclasses should override.

        @return: dict with namespace.
        r   r0   s    r   get_namespacez&BaseInterpreterInterface.get_namespace=  r   r$   c                    d}d|vrR	 | j                         |   }|	 | j                         d   |   }|!	 t        | j                         d   |d      }|S |S 	 |j                  d      }|d| }t	        j
                  || j                         | j                               }t        |||dz   d       }|S # t        $ r Y w xY w#  Y xY w#  Y |S xY w#  Y |S xY w)z"

        :type text: str
        N.__builtins__r   r\   )r   KeyErrorr3   rindexr   eval_in_context)r   rf   objlast_dotparent_contextress         r   __resolve_reference__z.BaseInterpreterInterface.__resolve_reference__E  s   
 d?((*40 {,,.~>tDC {!$"4"4"6~"FdSC 
s
;;s+!%a!1!11.$BTBTBVX\XjXjXlmc41#67 
/  
 

s4   B7 C C AC 7	CCC
CCc                 P    	 | j                  |      }|yt        |      S #  Y yxY w)N )r   r   )r   rf   r   s      r   getDescriptionz'BaseInterpreterInterface.getDescriptiong  s4    	,,T2C{"3''	s   ! 
! %c                     	 t        ||      }| j                  |      }|s.| j                  }d | _        | j                  j	                  |       |S #  t        j                          Y yxY w)NF)rc   r   rs   rr   putr   r   )r   r   rg   rj   r   s        r   do_exec_codez%BaseInterpreterInterface.do_exec_codep  sa    	(~>M>>-0D $"##M2K	!s   AA A)c                 &    | j                  |d      S NT)r   )r   lines     r   execLinez!BaseInterpreterInterface.execLine~  s      t,,r$   c                     t         r,d}|j                  d      D ]  }| j                  |d      } |S | j                  |d      S )NFr    T)r	   splitr   )r   linesr   r   s       r   execMultipleLinesz*BaseInterpreterInterface.execMultipleLines  sL    DD) 5((t45K$$UE22r$   c                     d | _         	 | j                  rt        | j                         | j	                  d       y#  t        j                          Y yxY wNFT)rs   rp   r   ro   r   r   r   r0   s    r   	interruptz"BaseInterpreterInterface.interrupt  sJ    		!!%doo6U#	!s	   2< Ac                 .    t        j                  d       y )Nr   )r   exitr0   s    r   r-   zBaseInterpreterInterface.close  s    r$   c                     d| _         y r   )rp   r0   s    r   r   z#BaseInterpreterInterface.start_exec  s
    !r$   c                 |    t        | dd       /t        j                  d| j                  d| j                        S y )Nr@   rB   rC   )r3   r   rD   r@   r?   r0   s    r   
get_serverz#BaseInterpreterInterface.get_server  s4    4&2##diiAQAQ$RSSr$   c                 J    | j                         }||j                          y y r   )r   ShowConsole)r   rG   s     r   r   z$BaseInterpreterInterface.ShowConsole  s%    "  r$   c                 X    d| _         | j                         }||j                  |      S yr   )rp   r   NotifyFinished)r   r   rG   s      r   r   z$BaseInterpreterInterface.finish_exec  s0    ""((..r$   c                     t               }| j                         }|j                  d       |j                  t        j                  | j                         |             |j                  d       |j                         S N<xml></xml>)r   get_ipython_hidden_vars_dictr&   r   frame_vars_to_xmlr   getvalue)r   xml	hidden_nss      r   getFramez!BaseInterpreterInterface.getFrame  s]    j557			'		*..t/A/A/CYOP		(||~r$   c                 |   t               }|j                  d       t        j                  | j	                         |      }|i }|j                         D ]F  \  }}||   }t        j                  |      }|j                  t        j                  |||             H |j                  d       |j                         S )Nr   )evaluate_full_valuer   )
r   r&   r   "resolve_compound_var_object_fieldsr   itemsr   should_evaluate_full_value
var_to_xmlr   )r   
attributesr   val_dictkvalr   s          r   getVariablez$BaseInterpreterInterface.getVariable  s    j		'AA$BTBTBVXbcHnn& 	_FAs1+C","G"G"LIIk,,S!I\]^	_
 			(||~r$   c           	          |j                  d      d   }t        j                  || j                         | j                               }t        j                  |||||||      S )N	)r   r   r   r   table_like_struct_to_xml)	r   attrroffsetcoffsetrowscolsry   namearrays	            r   getArrayz!BaseInterpreterInterface.getArray  s[    zz$#++D$2D2D2FHZHZH\]33E4'SWY]_effr$   c                 *   t               }|j                  d       t        j                  || j	                         | j	                               }|j                  t        j
                  ||             |j                  d       |j                         S r   )r   r&   r   r   r   r   r   )r   
expressionr   results       r   evaluatez!BaseInterpreterInterface.evaluate  sl    j		',,Z9K9K9MtOaOaOcd		+((<=		(||~r$   c                     | j                         }g }|j                  t              }|D ]{  }d|v r|j                  dd      \  }}n|}d}||v r-t        j                  ||   |      }	|j                  |	|f       Rt        j                  |||      }	|j                  |	|f       } ddlm}
 t        | dd      }|
t               }|ddlm}  |       } |
|| j                         ||      }|j                          y)aU  
        Evaluate full value for async Console variables in a separate thread and send results to IDE side
        :param seq: id of command
        :param scope_attrs: a sequence of variables with their attributes separated by NEXT_VALUE_SEPARATOR
        (i.e.: obj	attr1	attr2NEXT_VALUE_SEPARATORobj2ttr1	attr2)
        :return:
        r   r\   Nr   )GetValueAsyncThreadConsoler   )PyDB)r   r   r
   r   resolve_var_objectrk   r   _pydevd_bundle.pydevd_commr  r3   r   pydevdr  r   start)r   seqscope_attrsframe_variablesvar_objectsvars	var_attrsr   attrs
var_objectr  rP   r  ts                 r   loadFullValuez&BaseInterpreterInterface.loadFullValue  s
    ,,.  !56 	7Iy 'oodA6e !&(;;OD<QSXY
""J#56(88P_`
""J#56	7 	Jj$/=')E=#FE&udoo.?kR		r$   c                 N      fd} j                   j                  |       y )Nc                  b    t         dj                         j                                y )N=)r   r   )r   r   values   r   do_change_variablezCBaseInterpreterInterface.changeVariable.<locals>.do_change_variable  s&    D%($*<*<*>@R@R@TUr$   rr   r   )r   r   r  r  s   ``` r   changeVariablez'BaseInterpreterInterface.changeVariable  s    	V
 	./r$   c                     i d}|v r|   j                         D ]  \  }}t        j                  j                  |d      }|r2d|t        j                  j
                  |fz  t        j                  |<   n|t        j                  |<   |dk(  sst        j                  j                  |        |=  fd} j                  j                  |       y)z
        Used to show console with variables connection.
        Mainly, monkey-patches things in the debugger structure so that the debugger protocol works.
        NPYDEVD_EXTRA_ENVSz%s%c%s
PYTHONPATHc                     	 dd l } ddlm} ddlm	} ddl
m}  |j                         d       d}d}t               }d |_        i |_        
j!                         |_         j%                         
_        
j&                  j)                  |||       	 | j+                  	       
j&                  j-                  |j/                                
j&                  j1                          
j&                  j3                          d

_        	 dd l}|j9                  
j&                  j:                         y #  t        j                          t
        j                  j                  d       Y axY w#  t        j                          t
        j                  j                  d	       Y xY w#  t        j                          t
        j                  j                  d       Y y xY w)Nr   )	threadingz(pydevd is not available, cannot connect
)set_thread_id)pydev_localhostconsole_main1)	thread_idframe_idframez&Failed to connect to target debugger.
FzCVersion of Python does not support debuggable Interactive Console.
)r  "_pydev_bundle._pydev_saved_modulesr  r   r   r   r   r&   _pydevd_bundle.pydevd_constantsr  _pydev_bundler  current_thread	FakeFramef_back	f_globalsr   f_localsr  r   add_fake_frameapply_debugger_optionsconnectget_localhostprepare_to_runr   debugrunningpydevconsoleset_debug_hookprocess_internal_commands)r  r  r  r  VIRTUAL_FRAME_IDVIRTUAL_CONSOLE_IDfr3  debuggerPortdebugger_optionsr   s           r   do_connect_to_debuggerzJBaseInterpreterInterface.connectToDebugger.<locals>.do_connect_to_debugger   su   NH F5)224nE"!/AAHAK++-AJ"KKMDMMM((3EP`hi(jL--.>?%%o&C&C&E|T,,.--/ !&Di#++DMM,S,ST?N##%

  !LM(L##%

  !JKi##%

  !ghs$   
D6 A/E1 )F+ 65E.15F(+5G")zconnect complete)
r   osenvirongetpathpathsepr   rk   rr   r   )r   r9  r:  env_keyenv_namer  existing_valuer;  s   ```     r   connectToDebuggerz*BaseInterpreterInterface.connectToDebugger  s     #!%&&%5g%>%D%D%F +!5!#$!?!+3~rwwX]6^+^BJJx(+0BJJx(|+HHOOE*+ !)'	iV 	23$r$   c                 R    | j                   | j                   j                  d       y)NTPyCharm)ru   r   r0   s    r   	handshakez"BaseInterpreterInterface.handshakeO  s%    $$0%%))$/r$   c                     | j                   S r   )ru   r0   s    r   get_connect_status_queuez1BaseInterpreterInterface.get_connect_status_queueT  s    (((r$   c                      y)N)zHello eclipser!   )r   	input_strs     r   hellozBaseInterpreterInterface.helloW  s    !r$   c                 F    fd}| j                   j                  |       y)z Enable the GUI specified in guiname (see inputhook for list).
            As with IPython, enabling multiple GUIs isn't an error, but
            only the last one's main loop runs and it may not work
        c                     ddl m}   |        r	 ddlm}  |       S dvr"t        j
                  j                  dz         S #  t        j
                  j                  dz         t        j                          Y S xY w)Nr   )versionok_for_gui)
enable_guiz5Failed to enable GUI event loop integration for '%s'
)noner   NzSPyDev console: Python version does not support GUI event loop integration for '%s'
)	 _pydev_bundle.pydev_versioncheckrO  pydev_ipython.inputhookrP  r   r   r&   r   r   )rO  rP  guinames     r   do_enable_guiz9BaseInterpreterInterface.enableGui.<locals>.do_enable_guia  s    J "*Bw' N	  22

  !w  {B  "B  CN*JJ$$%]`g%gh'')
 Ns   A 8BNr  )r   rT  rU  s    ` r   r   z"BaseInterpreterInterface.enableGui[  s    	" 	M*r$   c                      y r   r!   r0   s    r   r   z5BaseInterpreterInterface.get_ipython_hidden_vars_dictt  s    r$   r   )NN)'r7   r8   r9   r   r|   r{   rw   r   r   r   r   r   r   r   r   r   r   r   r   r-   r   r   propertyrG   r   r   r   r   r   r   r   r  r  rD  rG  rI  rL  r   r   r!   r$   r   rm   rm      s    -D,
>9UEN$$ D-3" j!F!
     g
  $  $L0B%H
)"+2r$   rm   c                       e Zd ZdZy)r)  zf
    Used to show console with variables connection.
    A class to be used as a mock of a frame.
    N)r7   r8   r9   rJ   r!   r$   r   r)  r)  {  s    r$   r)  )r<  r   r   _pydev_bundle.pydev_importsr   r   r   !_pydev_bundle._pydev_calltip_utilr   _pydevd_bundler   r   r&  r	   r
   r   r   
contextlibr   r'  r   _pydevd_bundle.pydevd_utilsr   ior   r   r;   rL   rc   rm   r)  r!   r$   r   <module>r_     s{    	 
  ? ? > & %    % # = +U +UbI D.=	 .=b	( 	(N Nh r$   