
    ړiN                       U d Z ddlmZ ddlZddlZddlZddlZddl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mZmZmZmZmZmZm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$ ddl%m&Z& ddl'm(Z( de)d<   	 ddl*m+Z+ g dZ-d#dZ. e.       Z/de)d<   d$dZ0 G d de(      Z1d%dZ2dZ3d&d'dZ4 G d d      Z5 G d de6      Z7e	 	 	 	 	 	 	 	 	 	 d(	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d)d        Z8e	 	 	 	 	 	 	 	 	 	 d(	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d*d!       Z8	 	 	 	 	 	 	 	 	 	 d+	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d,d"Z8y# e,$ r dZ+Y w xY w)-z}
Utility for creating a Python repl.

::

    from ptpython.repl import embed
    embed(globals(), locals(), vi_mode=False)

    )annotationsN)COMPILER_FLAG_NAMES)Path)	AnyCallableContextManager	CoroutineIterableLiteralNoReturnSequenceoverload)OneStyleAndTextTuple)patch_stdout)clear_title	set_title)DummyContext)PythonTracebackLexer   )OutputPrinter)PythonInputintPyCF_ALLOW_TOP_LEVEL_AWAIT)r   )
PythonReplenable_deprecation_warnings
run_configembedexitReplExit
int | Nonec                 N    t        j                         D ]  \  } }|dk(  s| c S  y )N	COROUTINE)r   items)kvs     4/tmp/pip-target-20744g9h/lib/python/ptpython/repl.py_get_coroutine_flagr'   @   s0    #))+ 1H
     COROUTINE_FLAGc                H    t         yt        | j                  t         z        S )NF)r)   boolco_flags)codes    r&   _has_coroutine_flagr.   L   s    .//r(   c                       e Zd Zd f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d
ZddZd fdZddZddZddZddZddZ	 d	 	 	 	 	 ddZ xZS )r   c                r    |j                  dd       | _        t        |   |i | | j	                          y )Nstartup_paths)pop_startup_pathssuper__init___load_start_paths)selfakw	__class__s      r&   r5   zPythonRepl.__init__U   s5    ;=66/SW;X!"r" r(   c                   | j                   r| j                   D ]  }t        j                  j                  |      rZt	        |d      5 }t        |j                         |d      }t        || j                         | j                                ddd       || j                  j                  }|j                  d| d        yy# 1 sw Y   xY w)zStart the Read-Eval-Print Loop.rbexecNzWARNING | File not found: z

)r3   ospathexistsopencompilereadr=   get_globals
get_localsappoutputwrite)r7   r?   fr-   rG   s        r&   r6   zPythonRepl._load_start_pathsZ   s    ++ J77>>$'dD) JQ&qvvxv>T4#3#3#5t7HIJ J "XX__FLL#=dV4!HIJ J Js   ACC	c                   	 	 | j                  |      }t        |t              rt        |B| j	                  |       | j
                  r%| j                  j                  j                  d       | xj                  dz  c_	        g | _
        y # t        $ r  t        $ r  t        $ r  t        $ r}| j                  |       Y d }~y d }~ww xY w# t        $ r}| j                  |       Y d }~y d }~ww xY w)N
r   )eval
isinstancer   r   _show_resultinsert_blank_line_after_outputrF   rG   rH   current_statement_index
signaturesKeyboardInterrupt
SystemExitBaseException_handle_exception_handle_keyboard_interrupt)r7   
expressionresultes       r&   run_and_show_expressionz"PythonRepl.run_and_show_expressionf   s    $	/%:. fd+ #N %%%f-::--d3 ,,1,"$/ %     *&&q))*$ ! 	/ ++A..	/s;   B A6C $C
/C C C

C 	C1C,,C1c                    t        | j                  j                  | j                  j                  | j                  | j
                  | j                        S )N)rG   inputstylestyle_transformationtitle)r   rF   rG   r\   _current_styler^   r_   r7   s    r&   _get_output_printerzPythonRepl._get_output_printer   s>    88??((..%%!%!:!:**
 	
r(   c                    | j                         j                  || j                         | j                  | j                  | j
                         y )N)rX   
out_promptreformat	highlightpaginate)rb   display_resultget_output_promptenable_output_formattingenable_syntax_highlightingenable_pager)r7   rX   s     r&   rN   zPythonRepl._show_result   sH      "11--/2255&& 	2 	
r(   c                   | j                   rt        | j                          | j                          	 	 	 | j                         }	 | j                  |       $# t        $ r) Y | j                   r
t                | j                          yt        $ r t        j                          Y tw xY w# t        $ r) Y | j                   r
t                | j                          yw xY w# | j                   r
t                | j                          w xY w)z$
        Run the REPL loop.
        N)terminal_titler   _add_to_namespacerC   EOFErrorr   _remove_from_namespacerT   	traceback	print_excrZ   r   )r7   texts     r&   runzPythonRepl.run   s     d))* 	*	99;D006     ""'') %  '')   ""'') ""'')sX   C  A B+ C  	B(!C  	B(%C  'B((C  +	C4C  CC   (Dc                   K   t        j                         }d 	 	 d fd}t        j                   |             |j                  t        j
                  fd        d {   	 |j                  d  fd       d {     xj                  dz  c_        g  _        |j                  t        j
                         S 7 f7 C# t        $ r  t        $ r  t        $ r} j                  |       Y d }~nd }~ww xY w	 |j                  t        j
                         y # |j                  t        j
                         w xY w# t        $ r} j                  |       Y d }~y d }~ww xY ww)Nc                 t   K   	 j                         d {   S 7 # t        $ r} | Y d } ~ y d } ~ ww xY wwN)
eval_asyncrS   )rY   r7   system_exitrt   s    r&   rL   z6PythonRepl.run_and_show_expression_async.<locals>.eval   s7     (%)__T%::::% ( '(	(s,   8    8  	50858c                 $    j                         S rx   )cancel)_tasks    r&   <lambda>z:PythonRepl.run_and_show_expression_async.<locals>.<lambda>   s    $++- r(   c                 &    j                         S rx   )rN   )rX   r7   s   r&   r   z:PythonRepl.run_and_show_expression_async.<locals>.<lambda>   s    T=N=Nv=V r(   r   )returnr   )asyncioget_running_loopcreate_taskadd_signal_handlersignalSIGINTrun_in_executorrP   rQ   remove_signal_handlerrR   rS   rT   rU   rV   )r7   rt   looprL   rY   rX   rz   r~   s   ``   @@@r&   run_and_show_expression_asyncz(PythonRepl.run_and_show_expression_async   sH    ''))-/	/%:( **462''7OP#*%% + %..t5VWWW ,,1,"$**6==9- $ X %    *&&q))*) **6==9**6==9  	/ ++A..	/s   E:A
C *C+C 4D/ C!D/ 1E E:C D/ D0DD/ DD/ E .E:/!EE 	E7E2-E:2E77E:c                  K   t        j                         }| j                  rt        | j                         | j	                          	 	 	 	 |j                  d| j                         d{   }| j                  |       d{    @7 # t        $ r) Y | j                  r
t                | j                          yt        $ r t        j                          Y w xY w7 Z# t        $ r}| j                  |       Y d}~wd}~wt         $ r) Y | j                  r
t                | j                          yw xY w# | j                  r
t                | j                          w xY ww)a6  
        Run the REPL loop, but run the blocking parts in an executor, so that
        we don't block the event loop. Both the input and output (which can
        display a pager) will run in a separate thread with their own event
        loop, this way ptpython's own event loop won't interfere with the
        asyncio event loop from where this is called.

        The "eval" however happens in the current thread, which is important.
        (Both for control-C to work, as well as for the code to see the right
        thread in which it was embedded).
        N)r   r   rn   r   ro   r   rC   rp   r   rq   rT   rr   rs   r   rR   rV   rS   )r7   r   rt   rY   s       r&   	run_asynczPythonRepl.run_async   s8     '')d))* 	*	!%)%9%9$		%JJ <<TBBB   K# & ""'')) ) ! "++- ! C( 7 33A66! ""'') ""'')s   AE#	D8 B +B
,B 0C! CC! 	D8 
B 	CC! 'E#=CC! D8 CC! !	D5*D ;D8  D5D8 'E#4D55D8 8(E  E#c                   dt         j                  vr t         j                  j                  dd       |j                         j	                  d      rt        j                  |dd        y	 | j                  |d      }t        || j                         | j                               }t        |      r#t        j                         j                  |      }| j                  |       |S # t         $ r Y nw xY w| j                  |d      }t        || j                         | j                               }t        |      r#t        j                         j                  |      }yz9
        Evaluate the line and print the result.
         r   !r   NrL   r=   )sysr?   insertlstrip
startswithr>   system_compile_with_flagsrL   rD   rE   r.   r   r   run_until_complete_store_eval_resultSyntaxErrorr7   liner-   rX   s       r&   rL   zPythonRepl.eval&  s"    SXXHHOOAr";;=##C(IId12h6 1//f=
 dD$4$4$68IJ&t,$557JJ6RF''/    ++D&9D$ 0 0 2DOO4EFF"4( 113FFvNs   ,C( (	C43C4c                l  K   dt         j                  vr t         j                  j                  dd       |j                         j	                  d      rt        j                  |dd        y	 | j                  |d      }t        || j                         | j                               }t        |      r
| d{   }| j                  |       |S 7 # t        $ r Y nw xY w| j                  |d      }t        || j                         | j                               }t        |      r| d{  7  }ywr   )r   r?   r   r   r   r>   r   r   rL   rD   rE   r.   r   r   r   s       r&   ry   zPythonRepl.eval_asyncN  s	     SXXHHOOAr";;=##C(IId12h4 ///f=
 dD$4$4$68IJ&t,#)\F''/ *   ++D&9D$ 0 0 2DOO4EFF"4(%sC   A+D4.C  9D49C:D4	CD4CAD4-D0.D4c                R    | j                         }|x|d<   |d| j                   <   y )Nr}   )rE   rP   )r7   rX   localss      r&   r   zPythonRepl._store_eval_resultu  s/    !%!2CIIsfq!=!= >?@r(   c                .    t         |          t        z  S rx   )r4   get_compiler_flagsr   )r7   r:   s    r&   r   zPythonRepl.get_compiler_flagsy  s    w)+.HHHr(   c                >    t        |d|| j                         d      S )z+Compile code with the right compiler flags.z<stdin>T)flagsdont_inherit)rB   r   )r7   r-   modes      r&   r   zPythonRepl._compile_with_flags|  s'    ))+
 	
r(   c                    t        j                         \  }}}|||ct         _        t         _        t         _        | j                         j                  || j                  | j                         y )N)rf   rg   )	r   exc_info	last_type
last_valuelast_tracebackrb   display_exceptionrk   rl   )r7   rY   tr%   tbs        r&   rU   zPythonRepl._handle_exception  s[    <<>1b<=q"9s~s'9  "4455&& 	5 	
r(   c                r    | j                   j                  }|j                  d       |j                          y )NzKeyboardInterrupt

)rF   rG   rH   flush)r7   rY   rG   s      r&   rV   z%PythonRepl._handle_keyboard_interrupt  s%    ./r(   c                V      j                         }d fd}||d<   t               |d<   y)z=
        Add ptpython built-ins to global namespace.
        c                      S rx    ra   s   r&   get_ptpythonz2PythonRepl._add_to_namespace.<locals>.get_ptpython  s    Kr(   r   r   N)r   r   )rD   r   )r7   globalsr   s   `  r&   ro   zPythonRepl._add_to_namespace  s/     ""$	 #/&r(   c                *    | j                         }|d= y)z8
        Remove added symbols from the globals.
        r   N)rD   )r7   r   s     r&   rq   z!PythonRepl._remove_from_namespace  s     ""$N#r(   c                F    | j                         j                  |d       y )NT)rg   )rb   display_style_and_text_tuples)r7   formatted_textends      r&   print_paginated_formatted_textz)PythonRepl.print_paginated_formatted_text  s&     	  "@@T 	A 	
r(   r   None)rW   strr   r   )r   r   )rX   objectr   r   )rt   r   r   r   )r   r   r   r   )r   r   )r-   r   r   r   r   r   )rY   rT   r   r   )rY   rR   r   r   )rK   )r   zIterable[OneStyleAndTextTuple]r   r   r   r   )__name__
__module____qualname__r5   r6   rZ   rb   rN   ru   r   r   rL   ry   r   r   r   rU   rV   ro   rq   r   __classcell__)r:   s   @r&   r   r   T   s    !

J%/N

*B3/j/*b&P%NJI
	
!$ 	
6	
 	
 
		
r(   r   c                 <    t        j                  dt        d       y)a   
    Show deprecation warnings, when they are triggered directly by actions in
    the REPL. This is recommended to call, before calling `embed`.

    e.g. This will show an error message when the user imports the 'sha'
         library on Python 2.7.
    default__main__)categorymoduleN)warningsfilterwarningsDeprecationWarningr   r(   r&   r   r     s     I0B:Vr(   z~/.config/ptpython/config.pyc                   |du}t         j                  j                  ||nt              }dd}t         j                  j	                  |      s|rt        d|         |        y	 i }t        |d      5 }t        |j                         |d      }t        |||       ddd       d|v r |d   |        yy# 1 sw Y   xY w# t        $ r t        j                           |        Y yw xY w)z
    Execute REPL config file.

    :param repl: `PythonInput` instance.
    :param config_file: Path of the configuration file.
    Nc                     t        d       y )Nz
Press ENTER to continue...)r\   r   r(   r&   enter_to_continuez%run_config.<locals>.enter_to_continue  s    ,-r(   zImpossible to read r<   r=   	configurer   )r>   r?   
expanduserDEFAULT_CONFIG_FILEr@   printrA   rB   rC   r=   	Exceptionrr   rs   )replconfig_fileexplicit_config_filer   	namespacerI   r-   s          r&   r   r     s     'd2 ''$$".4GK. 77>>+&'}56$&	+t$ 	-1668[&9Dy),	- )#"Ik"4( $	- 	-  s*   (C 6)B8C 8C=C $C+*C+c                       e Zd ZdZddZddZy)r   z!
    Exit the ptpython REPL.
    c                    t         rx   )r   ra   s    r&   __call__zexit.__call__  s    r(   c                     y)Nz'Use exit() or Ctrl-D (i.e. EOF) to exitr   ra   s    r&   __repr__zexit.__repr__  s    8r(   N)r   r   )r   r   )r   r   r   __doc__r   r   r   r(   r&   r   r     s    9r(   r   c                      e Zd ZdZy)r   z7
    Exception raised by ptpython's exit function.
    N)r   r   r   r   r   r(   r&   r   r      s    r(   r   c
                     y rx   r   
r   r   r   vi_modehistory_filenamer_   r1   r   patch_stdout_rawreturn_asyncio_coroutines
             r&   r   r     s     r(   c
                     y rx   r   r   s
             r&   r   r     s     !$r(   c
                .     dddt         d xs  d	 fd}
d	fd}t        |
||||      |r|_        |r |       |rt        |      n	t	               |	rd
fd} |       S 5  j                          ddd       y# 1 sw Y   yxY w)a  
    Call this to embed  Python shell at the current point in your program.
    It's similar to `IPython.embed` and `bpython.embed`. ::

        from prompt_toolkit.contrib.repl import embed
        embed(globals(), locals())

    :param vi_mode: Boolean. Use Vi instead of Emacs key bindings.
    :param configure: Callable that will be called with the `PythonRepl` as a first
                      argument, to trigger configuration.
    :param title: Title to be displayed in the terminal titlebar. (None or string.)
    :param patch_stdout:  When true, patch `sys.stdout` so that background
        threads that are printing will print nicely above the prompt.
    :param patch_stdout_raw:  When true, patch_stdout will not escape/remove vt100 terminal escape sequences.
    Nr   )r   __package__r   __builtins__c                      S rx   r   )r   s   r&   rD   zembed.<locals>.get_globalsJ  s    r(   c                      S rx   r   )r   s   r&   rE   zembed.<locals>.get_localsM  s    r(   )rD   rE   r   r   r1   )rawc                 p   K    5  j                          d {    d d d        y 7 # 1 sw Y   y xY wwrx   )r   )patch_contextr   s   r&   	coroutinezembed.<locals>.coroutinef  s7      'nn&&&' '&' 's    6*(*	6*36)r   zdict[str, Any]r   )builtinsr   rn   patch_stdout_contextr   ru   )r   r   r   r   r   r_   r1   r   r   r   rD   rE   r   r   r   s   ``           @@r&   r   r   $  s    8 "$	
 wF )#D #$ 7C!12   	' { 	HHJ		s   1BB)r   r    )r-   ztypes.CodeTyper   r+   r   rx   )r   r   r   
str | Noner   r   )
..........)r   dict[str, Any] | Noner   r   r   #Callable[[PythonRepl], None] | Noner   r+   r   r   r_   r   r1   Sequence[str | Path] | Noner   r+   r   r+   r   zLiteral[False]r   r   )r   r   r   r   r   r   r   r+   r   r   r_   r   r1   r   r   r+   r   r+   r   zLiteral[True]r   zCoroutine[Any, Any, None])
NNNFNNNFFF)r   r   r   r   r   r   r   r+   r   r   r_   r   r1   r   r   r+   r   r+   r   r+   r   z None | Coroutine[Any, Any, None])9r   
__future__r   r   r   r>   r   r   rr   typesr   disr   pathlibr   typingr   r   r   r	   r
   r   r   r   r   prompt_toolkit.formatted_textr   prompt_toolkit.patch_stdoutr   r   prompt_toolkit.shortcutsr   r   prompt_toolkit.utilsr   pygments.lexersr   printerr   python_inputr   __annotations__astr   ImportError__all__r'   r)   r.   r   r   r   r   r   r   r   r   r   r(   r&   <module>r     s   #   	  
    # 
 
 
 ? L . 0 " % #.
 12
 20`
 `
FW 5 'T9 9"y  
%($'58#&14 /2"! 3 	
 !  /   - 
 
 
%($'58#&14 .1$"$!$ 3$ 	$
 !$ $ /$ $ $ ,$ $ 
$ &*$(59#'15"%*J"J!J 3J 	J
 !J J /J J J #J &Je  #!"#s   >D> >EE