
    0hh5                        S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSKJ	r	  SSK
JrJrJrJrJr  SSKJr  SSKJr   SSKr\(       d  \R,                  " S5      e\R.                  \R0                  S	.S
 jrS r\	R6                   " S S\R8                  5      5       r\	R6                   " S S\R8                  5      5       r " S S\R8                  5      r\ S:X  a  \RB                  " 5         gg! \ a    Sr Nf = f)z!Test the interactive interpreter.    N)dedent)support)cpython_onlyhas_subprocess_support	os_helperSuppressCrashReportSHORT_TIMEOUT)kill_python)import_moduleztest module requires subprocess)stdoutstderrc           	         [         R                  R                  [         R                  R                  [        R
                  5      S5      nUSS/nUR                  U5        UR                  S[        [         R                  5      5      nSUS'   [        R                  " U4[        R
                  S[        R                  XS.UD6$ )	z}Run the Python REPL with the given arguments.

kw is extra keyword args to pass to subprocess.Popen. Returns a Popen
object.
<stdin>-I-ienvvt100TERMT)
executabletextstdinr   r   )ospathjoindirnamesysr   extend
setdefaultdictenviron
subprocessPopenPIPE)r   r   argskwstdin_fnamecmd_liner   s          7/opt/python-3.13.8/usr/lib/python3.13/test/test_repl.py
spawn_replr)      s     '',,rwws~~>	JKT4(HOOD --tBJJ/
0CCKH "'*~~!%",//#)	"
 !" "    c                     [        5       nUR                  R                  U 5        [        U5      nUR                  S:w  a  [        S5      eU$ )zSpawn a new Python interpreter, pass the given
input source code from the stdin and return the
result back. If the interpreter exits non-zero, it
raises a ValueError.r   zProcess didn't exit properly.)r)   r   writer
   
returncode
ValueError)sourceprocessoutputs      r(   run_on_interactive_moder2   :   sF     lGMM!FQ899Mr*   c                       \ rS rSr\\R                  " \R                  S5      S 5       5       r	\S 5       r
S rS rS rS rS	 rS
 rS r\R$                  " \S5      S 5       rSrg)TestInteractiveInterpreterI   zcannot test Py_TRACE_REFS buildc                 8   [        S5        Sn[        U5      n[        5       n[        5          UR                  R                  U5        S S S 5        [        U5      nU R                  SU5        U R                  UR                  S5        g ! , (       d  f       NH= f)N	_testcapiz
            import sys, _testcapi
            1/0
            print('After the exception.')
            _testcapi.set_nomemory(0)
            sys.exit(0)
        zAfter the exception.)   x   )	r   r   r)   r   r   r,   r
   assertInr-   self
user_inputpr1   s       r(   test_no_memory)TestInteractiveInterpreter.test_no_memoryL   st    
 	k"
 J'
L "GGMM*% #Q,f5allH- #"s   B
Bc                     Sn[        U5      n[        5       nUR                  R                  U5        [	        U5      nU R                  UR                  S5        g )Na          x = """<?xml version="1.0" encoding="iso-8859-1"?>
        <test>
            <Users>
                <fun25>
                    <limits>
                        <total>0KiB</total>
                        <kbps>0</kbps>
                        <rps>1.3</rps>
                        <connections>0</connections>
                    </limits>
                    <usages>
                        <total>16738211KiB</total>
                        <kbps>237.15</kbps>
                        <rps>1.3</rps>
                        <connections>0</connections>
                    </usages>
                    <time_to_refresh>never</time_to_refresh>
                    <limit_exceeded_URL>none</limit_exceeded_URL>
                </fun25>
            </Users>
        </test>"""
        r   )r   r)   r   r,   r
   assertEqualr-   r;   s       r(   test_multiline_string_parsing8TestInteractiveInterpreter.test_multiline_string_parsinge   sI    
. J'
L	j!Qq)r*   c                     [        S5      n[        S5      n[        SU5      nUR                  U5      S   nU R                  UR                  S5        U R                  SU5        g )NzY
            import os
            print("before close")
            os.close(0)
        zl
            from test.support import suppress_msvcrt_asserts
            suppress_msvcrt_asserts()
        z-cr   zbefore close)r   r)   communicaterB   r-   r:   )r<   r=   prepare_replr0   r1   s        r(   test_close_stdin+TestInteractiveInterpreter.test_close_stdin   si      

    T<0$$Z03++Q/nf-r*   c                     Sn[        5       nUR                  R                  U5        [        U5      nU R	                  UR
                  S5        UR                  5       SS n/ SQnU R	                  XE5        g )Nz1 / 0 / 3 / 4r   i)z"Traceback (most recent call last):%  File "<stdin>", line 1, in <module>z    1 / 0 / 3 / 4	    ~~^~~#ZeroDivisionError: division by zero)r)   r   r,   r
   rB   r-   
splitlines)r<   r=   r>   r1   traceback_linesexpected_liness         r(   $test_interactive_traceback_reporting?TestInteractiveInterpreter.test_interactive_traceback_reporting   sg    $
L	j!Qq) ++-b4
 	9r*   c                 <   [        S5      n[        5       nUR                  R                  U5        SnUR                  R                  U5        [	        U5      nU R                  UR                  S5        UR                  5       SS n/ SQnU R                  XV5        g )Nz0
        def foo(x):
            1 / x

        zfoo(0)r   irK   )rL   z
    foo(0)z
    ~~~^^^z   File "<stdin>", line 2, in fooz	    1 / xrM   rN   )r   r)   r   r,   r
   rB   r-   rO   )r<   user_input1r>   user_input2r1   rP   rQ   s          r(   3test_interactive_traceback_reporting_multiple_inputNTestInteractiveInterpreter.test_interactive_traceback_reporting_multiple_input   s      
 L	k"	k"Qq) ++-b4
 	9r*   c                    S n[         R                  " 5        n[        R                  R	                  US5      n[        US5       nUR                  S[        R                  -   5        S S S 5        [        R                  R                  5       nX5S'   [        R                  R	                  US5      US'   U" U5      nUR                  R                  S5        [        U5      nS S S 5        [        S	5      nU R                  S
W5        U R                  X5        [         R                  " 5        n[        R                  R	                  US5      n[        US5       nUR                  S5        S S S 5        [        R                  R                  5       nX5S'   [        R                  R	                  US5      US'   U" U5      nUR                  R                  S5        [        U5      nS S S 5        [        S5      W-  nU R                  X5        g ! , (       d  f       GN= f! , (       d  f       GNF= f! , (       d  f       N= f! , (       d  f       Nc= f)Nc           
      <   [         R                  " [        R                  R	                  [        R                  R                  [        R                  5      S5      S/[        R                  S[         R                  [         R                  [         R                  U S9$ )Nr   r   T)r   r   r   r   r   r   )
r!   r"   r   r   r   r   r   r   r#   STDOUT)r   s    r(   	make_replPTestInteractiveInterpreter.test_pythonstartup_error_reporting.<locals>.make_repl   sa    ##bggoocnn=yI4P>> oo!!(( r*   pythonstartup.pywzprint('from pythonstartup')PYTHONSTARTUPz.pythonhistPYTHON_HISTORYz1/0z
            Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
                1/0
                ~^~
            ZeroDivisionError: division by zero
        zfrom pythonstartupzdef foo():
    1/0
zfoo()a  
            Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
                foo()
                ~~~^^
              File "%s", line 2, in foo
                1/0
                ~^~
            ZeroDivisionError: division by zero
        )r   temp_dirr   r   r   openr,   linesepr    copyr   r
   r   r:   )	r<   r\   tmpdirscriptfr   r>   r1   expecteds	            r(   "test_pythonstartup_error_reporting=TestInteractiveInterpreter.test_pythonstartup_error_reporting   s   		 !VWW\\&*<=Ffc"a5

BC # **//#C#) $&GGLL$GC !#AGGMM%  ^F "    	*F3h' !VWW\\&*<=Ffc"a/0 # **//#C#) $&GGLL$GC !#AGGMM'" ^F "  	 	 	 	h'S #" "!0 #" "!sI   -H##H)A;H#/-IH5.A;I
H 	H##
H25
I	?I
Ic                     [        S5      n[        5       nUR                  R                  U5        [	        U5      n/ SQnU R                  UR                  5       SS U5        g )Nz-def f(x, x): ...
                            )z    def f(x, x): ...z             ^z:SyntaxError: duplicate argument 'x' in function definition   rK   )r   r)   r   r,   r
   rB   rO   )r<   r=   r>   r1   rQ   s        r(   )test_runsource_show_syntax_error_locationDTestInteractiveInterpreter.test_runsource_show_syntax_error_location   sZ       !
L	j!Q

 	**,Qr2NCr*   c                 &   [        S5      n[        5       nUR                  R                  U5        [        S5      nUR                  R                  U5        [	        U5      nU R                  UR                  S5        SnU R                  XTU5        g )Nzi
        def foo(x):
            return x + 1

        def bar(x):
            return foo(x) + 2
        zq
        import linecache
        print(linecache._interactive_cache[linecache._make_key(foo.__code__)])
        r   zD(30, None, ['def foo(x):\n', '    return x + 1\n', '\n'], '<stdin>')r   r)   r   r,   r
   rB   r-   r:   r<   r=   r>   rV   r1   ri   s         r(   'test_interactive_source_is_in_linecacheBTestInteractiveInterpreter.test_interactive_source_is_in_linecache
  s{      
 L	j!   	
k"Qq)dh1r*   c           	         [         R                  " 5        n[        R                  R	                  US5      n[        US5       nUR                  S[        R                  -   5        UR                  S[        R                  -   5        S S S 5        [        R                  R                  5       n[        R                  R	                  US5      US'   X$S'   [        R                  " [        R                  SS	/[        R                  [        R                  U[        S
9  S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)Nr^   r_   zprint('pythonstartup done!')zexit(0)z.asyncio_historyra   r`   -masyncio)r   r   r   timeout)r   rb   r   r   r   rc   r,   rd   r    re   r!   
check_callr   r   r#   r	   )r<   rf   rg   rh   r   s        r(   /test_asyncio_repl_reaches_python_startup_scriptJTestInteractiveInterpreter.test_asyncio_repl_reaches_python_startup_script  s    !VWW\\&*<=Ffc"a6CD	BJJ./ # **//#C$&GGLL9K$LC !#) !!y1!!% "!"" "!s%   -D8AD'BD8'
D5	1D88
Ezrequires ptyc           
         [         R                  " 5       u  p[        R                  SSS/n[        R
                  R                  5       n[        R                  " UUUUSSUS9n[        R                  " U5        [        R                  " US5        [        R                  " US5        / n[        R                  " U// / [        5      S   (       ai   [        R                  " US	5      R                  S
5      nU(       d  O: UR!                  U5        [        R                  " U// / [        5      S   (       a  Mi  [        R                  " U5         UR#                  [        S9nU R)                  USSR+                  U5      5        g ! [         a     MZ  f = f! [        R$                   a#    UR'                  5         UR#                  5       n Njf = f)Nr   rv   rw   T)r   r   r   r   	close_fdsr   s   await asyncio.sleep(0)
s   exit()
r   i   zutf-8)rx    )ptyopenptyr   r   r   r    re   r!   r"   closer,   selectr	   readdecodeOSErrorappendwaitTimeoutExpiredkillrB   r   )	r<   mscmdr   procr1   data	exit_codes	            r(   test_asyncio_repl_is_ok2TestInteractiveInterpreter.test_asyncio_repl_is_ok0  sq   {{}~~tT95jjoo
 	
/0
K mmQCR7:wwq$'..w7  MM$ mmQCR7:: 		$		-	8I
 	Arwwv7   (( 	$IIK		I	$s$   -F F 
FF4GG N)__name__
__module____qualname____firstlineno__r   unittestskipIfr   Py_TRACE_REFSr?   rC   rH   rR   rW   rj   rn   rs   rz   
skipUnlessr   r   __static_attributes__r   r*   r(   r4   r4   I   s      __W**,MN. O .* * *@.:":2:(|D2($ n- 8 . 8r*   r4   c                       \ rS rSrS rSrg)TestInteractiveModeSyntaxErrorsiT  c                 |    [        [        S5      5      nUR                  5       SS n/ SQnU R                  X#5        g )NzJ        def f():
            print(0)
            return yield 42
        rK   )z    return yield 42z           ^^^^^zSyntaxError: invalid syntax)r2   r   rO   rB   )r<   r1   rP   rQ   s       r(   *test_interactive_syntax_error_correct_lineJTestInteractiveModeSyntaxErrors.test_interactive_syntax_error_correct_lineW  sF    ( 1 *  !++-b4

 	9r*   r   N)r   r   r   r   r   r   r   r*   r(   r   r   T  s    :r*   r   c                   &    \ rS rSrS rS rS rSrg)TestAsyncioREPLig  c                     Sn[        SS5      nUR                  R                  U5        [        U5      nU R	                  SU5        U R                  SU5        g )Nz"1 / 0; print(f'afterwards: {1+1}')rv   rw   ZeroDivisionErrorzafterwards: 2)r)   r   r,   r
   r:   assertNotInr;   s       r(   #test_multiple_statements_fail_early3TestAsyncioREPL.test_multiple_statements_fail_earlyh  sK    9
tY'	j!Q)62&1r*   c                 *   [        S5      n[        SS5      nUR                  R                  U5        [        S5      nUR                  R                  U5        [	        U5      nU R                  UR                  S5        SnU R                  XTU5        g )Nz{        from contextvars import ContextVar
        var = ContextVar("var", default="failed")
        var.set("ok")
        rv   rw   zA
        print(f"toplevel contextvar test: {var.get()}")
        r   toplevel contextvar test: okrq   rr   s         r(   test_toplevel_contextvars_sync.TestAsyncioREPL.test_toplevel_contextvars_syncp  s      

 tY'	j!   	
k"Qq)1h1r*   c                    [        S5      n[        SS5      nUR                  R                  U5        SnUR                  R                  U5        SnUR                  R                  U5        SnUR                  R                  U5        [	        U5      nU R                  UR                  S5        SnU R                  XvU5        g )	Nze        from contextvars import ContextVar
        var = ContextVar('var', default='failed')
        rv   rw   z#async def set_var(): var.set('ok')
zawait set_var()
z0print(f'toplevel contextvar test: {var.get()}')
r   r   rq   )r<   r=   r>   rV   user_input3user_input4r1   ri   s           r(   test_toplevel_contextvars_async/TestAsyncioREPL.test_toplevel_contextvars_async  s      
 tY'	j!<	k")	k"I	k"Qq)1h1r*   r   N)r   r   r   r   r   r   r   r   r   r*   r(   r   r   g  s    22"2r*   r   __main__)"__doc__r   r   r!   r   r   textwrapr   testr   test.supportr   r   r   r   r	   test.support.script_helperr
   test.support.import_helperr   r   ImportErrorSkipTestr#   r[   r)   r2   force_not_colorized_test_classTestCaser4   r   r   r   mainr   r*   r(   <module>r      s    ' 	   
     3 4
 


=
>> (__Z5F5F "8 	''G8!2!2 G8 (G8T 	'':h&7&7 : (:$*2h'' *2Z zMMO }  
Cs   C0 0C;:C;