
    "e'                        d Z ddlZddlZddlmZmZ ddlmZ ddl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 ddlZddlmZ dZdZ G d d      Z G d de
      Z G d dee	      Z G d d      Zd Z d Z!ddZ"d Z#d Z$y)z5An helper file for the pydev debugger (REPL) console
    N)InteractiveConsole_EvalAwaitInNewEventLoop)_pydev_completer)BaseInterpreterInterface	BaseStdIn)Exec)	overrides)pydevd_save_locals)IOBuf)get_exception_traceback_str)make_valid_xml_value)update_globals_and_localsoutputerrorc                   (    e Zd ZdZd Zd Zd Zd Zy)ConsoleMessagezConsole Messages
    c                      d| _         g | _        y )NF)moreconsole_messages)selfs    ?/usr/lib/python3/dist-packages/_pydevd_bundle/pydevd_console.py__init__zConsoleMessage.__init__   s    	 "    c                     |j                  d      D ]0  }|j                         s| j                  j                  ||f       2 y)z2add messages in the console_messages list
        
N)splitstripr   append)r   message_typemessagems       r   add_console_messagez"ConsoleMessage.add_console_message!   s@     t$ 	@Awwy%%,,lA->?	@r   c                     || _         y)zimore is set to true if further input is required from the user
        else more is set to false
        N)r   )r   r   s     r   update_morezConsoleMessage.update_more(   s     	r   c           	          t         }d| j                  z  }| j                  D ]  \  }}|d|d ||      d|dz  } |dz  }|S )zCreate an XML for console message_list, error and more (true/false)
        <xml>
            <message_list>console message_list</message_list>
            <error>console error</error>
            <more>true/false</more>
        </xml>
        z<xml><more>%s</more><z
 message="z"></>z</xml>)r   r   r   )r   	makeValidxmlr   r    s        r   to_xmlzConsoleMessage.to_xml.   sZ     )	$		2%)%:%: 	_!L'|Yw=OQ]^^C	_ 	x
r   N)__name__
__module____qualname____doc__r   r"   r$   r*    r   r   r   r      s    #
@r   r   c                   <    e Zd Z eej
                        d        Zy)_DebugConsoleStdInc                 B    t         j                  j                  d       y)NzDWarning: Reading from stdin is still not supported in this console.
r   )sysstderrwriter   argskwargss      r   readlinez_DebugConsoleStdIn.readlineG   s    

`ar   N)r+   r,   r-   r	   r   r9   r/   r   r   r1   r1   E   s    y!!" #r   r1   c                       e Zd ZdZ eej                        d        Z eej                        dd       Z eej                        d        Z	 eej                        d        Z
d Zy)	DebugConsoleziWrapper around code.InteractiveConsole, in order to send
    errors and outputs to the debug console
    c                 r    	 | j                   st        j                  S 	 t               S #  Y t               S xY wN)_DebugConsole__buffer_outputr3   stdinr1   r6   s      r   create_std_inzDebugConsole.create_std_inU   s=    	''yy  (
 "##	!##s   ) 6c                 p   || _         d}|r t        j                  }t        j                  }	 	 || _        |r*t               x}t        _        t               x}t        _        | j                  |      }d| _        d}|rt        _        t        _        	 |r|j                  j                  fS |g g fS # t        $ rQ t               }	|rj                  j                  d|	       n#t        j                  j                  d|	d       Y w xY w# d| _        d}|rt        _        t        _        w w xY w)a|  Change built-in stdout and stderr methods by the
        new custom StdMessage.
        execute the InteractiveConsole.push.
        Change the stdout and stderr back be the original built-ins

        :param buffer_output: if False won't redirect the output.

        Return boolean (True if more input is required else False),
        output_messages and input_messages
        FzInternal Error: r   N)r>   r3   stdoutr4   framer   add_exec	Exceptionr   buflistr   r5   )
r   linerC   buffer_outputr   original_stdoutoriginal_stderrouterrexcs
             r   pushzDebugConsole.push_   s     -!jjO!jjO	-F"
 ',w.C#*',w.C#*}}T* DJE,
,
ckk11R<#  F13 KK&&s'DEJJ$$s%DEF DJE,
,
 s%   AB4 4ADD DD $D5c                 .    t        j                  | |      S r=   )r   rN   )r   rG   s     r   do_add_execzDebugConsole.do_add_exec   s    !&&tT22r   c                 *   	 | j                         }|j                         }d}d}t        t        d      r.t        j                  |j
                  z  t        j                  k(  }|r{t        |||      }|j                          |j                          t        ||| j                         |j                  rQ|j                  d   j                  |j                  d         	 t        |||       t        ||| j                         yy# t        ||| j                         w xY w# t        $ r   t        j                   t        _        	 | j%                          t        j"                  t        _        Y y# t        j"                  t        _        w xY wxY w)a  Execute a code object.

        When an exception occurs, self.showtraceback() is called to
        display a traceback.  All exceptions are caught except
        SystemExit, which is reraised.

        A note about KeyboardInterrupt: this exception may occur
        elsewhere in this code, and may not always be caught.  The
        caller should be prepared to deal with it.

        NFCO_COROUTINE      )get_namespacecopyhasattrinspectrR   co_flagsr   startjoinr   rC   rM   with_tracebackexec
SystemExitr3   __excepthook__
excepthookshowtraceback)r   codeupdated_globalsinitial_globalsupdated_localsis_asyncts          r   runcodezDebugConsole.runcode   s;   !	4"002O-224O!NHw/"//$--?7CWCWW,T?NS	)/?DJJW55%%(11!%%(;;\?-oPTPZPZ[  .oPTPZPZ[ 		4 !//CN4""$%(^^"S^^"s<   CD$ "D /D$ D!!D$ $$F	E4F4FFc                     i }|j                  | j                  j                         |j                  | j                  j                         |S r=   )updaterC   	f_globalsf_locals)r   dbg_namespaces     r   rU   zDebugConsole.get_namespace   s=    TZZ112TZZ001r   NT)r+   r,   r-   r.   r	   r   r@   r   rN   rP   rh   rU   r/   r   r   r;   r;   P   s     '556$ 7$ !&&'(  (( T '3343 53 !))*-4 +-4^r   r;   c                       e Zd ZdZdZdZy)InteractiveConsoleCacheN)r+   r,   r-   	thread_idframe_idinteractive_console_instancer/   r   r   rp   rp      s    IH#' r   rp   c                 v   t         j                  | k(  r#t         j                  |k(  rt         j                  S t	               t         _        | t         _        |t         _        t        j                  |d      }|r2|d   }d|d   d|d   d|d   }|j                  t        d|       t         j                  S )	zreturns the global interactive console.
    interactive console should have been initialized by this time
    :rtype: DebugConsole
    rS   )limitr   zFile "z", line z, in rT   z[Current context]: )	rp   rq   rr   rs   r;   	tracebackextract_stackr"   CONSOLE_OUTPUT)rq   rr   rC   console_messageconsole_stacktracecurrent_contextcontext_messages          r   get_interactive_consoler}      s    
 ((I5:Q:Z:Z^f:f&CCC;G>8(1%'/$"00a@,Q/9H9K_]^M_apqrast++NWf<hi"???r   c                  F    d t         _        d t         _        d t         _        y r=   )rp   rq   rr   rs   r/   r   r   clear_interactive_consoler      s    (,%'+$;?8r   c                     t               }t        ||| |      }|j                  || |      \  }}}	|j                  |       |D ]  }
|j	                  t
        |
        |	D ]  }
|j	                  t        |
        |S )zfetch an interactive console instance from the cache and
    push the received command to the console.

    create and return an instance of console_message
    )r   r}   rN   r$   r"   rx   CONSOLE_ERROR)rC   rq   rr   rG   rH   ry   interpreterr   output_messageserror_messagesr    s              r   execute_console_commandr      s     %&O))XuoVK,7,<,<T5-,X)D/>%" E++NGDE " D++M7CD r   c                     t               }t        ||| |      }	 | |_        |j                  |      d |_        S # d |_        w xY wr=   )r   r}   rC   getDescription)rC   rq   rr   
expressionry   r   s         r   get_descriptionr      sF    $&O))XuoVK!!))*5 Ds	   9 	Ac                 .    t        j                  | |      S )zS fetch all completions, create xml for the same
    return the completions xml
    )r   generate_completions_as_xml)rC   act_toks     r   get_completionsr   	  s     77wGGr   rn   )%r.   r3   rv    _pydevd_bundle.pydevconsole_coder   r   _pydev_bundler   !_pydev_bundle.pydev_console_utilsr   r   _pydev_bundle.pydev_importsr   _pydev_bundle.pydev_overrider	   _pydevd_bundler
   _pydevd_bundle.pydevd_ior   pydevd_tracingr   _pydevd_bundle.pydevd_xmlr   rX   !_pydevd_bundle.pydevd_save_localsr   rx   r   r   r1   r;   rp   r}   r   r   r   r   r/   r   r   <module>r      s    
  Y * Q , 2 - * 6 :  G' 'Z r%'? rp( (@(@*!Hr   