
    4hhݽ                     n   S SK r S SKrS SKrS SKrS SK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JrJr  S SKJr  S SKJrJrJr  S SKJrJr  S SKJr  S SKJrJr  SS	KJrJrJrJrJ r J!r!J"r"  S S
K#J$r$  S SK%J&r&J'r'J(r(  S SK%J!r)   S SK*r* " S S\5      r, " S S\5      r- " S S\5      r. " S S\\5      r/ " S S\5      r0 " S S\5      r1\" \*S5       " S S\,5      5       r2\" \*S5      \" \Rf                  Ri                  S5      =(       d    SS:H  S5       " S S \,5      5       5       r5g! \+ a    Sr* Nf = f)!    N)TestCase
skipUnlessskipIf)patch)force_not_colorizedmake_clean_envPy_DEBUG)SHORT_TIMEOUT
STDLIB_DIR)import_module)EnvironmentVarGuardunlink   )FakeConsoleScreenEqualMixinhandle_all_eventshandle_events_narrow_console
more_linesmultiline_inputcode_to_events)Event)ReadlineAlikeReaderReadlineConfig_ReadlineWrapper)r   c                       \ rS rSr SSSSS.S\\\   -  S\S-  S\\   S-  S\S-  S	\S
\\\	4   4S jjjr
S\\\   -  S\S-  S\\   S-  S\S	\S
\\\	4   4S jrSrg)ReplTestCase&   NF)cmdline_argscwdskip
repl_inputenvr   r   r    returnc                    S nUc   [         R                  " SS9nUR                  n U R                  XX4US9Ub  UR	                  5         $ $ ! Ub  UR	                  5         f f = f)NT)ignore_cleanup_errors)r"   r   r   r    )tempfileTemporaryDirectoryname	_run_replcleanup)selfr!   r"   r   r   r    temp_dirs          E/opt/python-3.13.8/usr/lib/python3.13/test/test_pyrepl/test_pyrepl.pyrun_replReplTestCase.run_repl'   su     ;22NH--C	#>>,d "  #  " $x#  " $s   A A"c                   [         R                  " 5       u  pg[        R                  SS/nUc  UR	                  S5        O)SU;  a#  [
        R                  R                  US5      US'   Ub  UR                  U5         SS K	n	U	R                  U5      n
SU
S   U	R                  '   SU
S   U	R                  '   U	R                  XyR                  U
5        [         R"                  " UUUUUSSU(       a  UO[
        R$                  S	9n[
        R&                  " U5        [)        U[*        5      (       a  S
R                  U5      S
-   n[
        R,                  " XaR/                  S5      5        / n[0        R0                  " U// / [2        5      S   (       ai   [
        R4                  " US5      R7                  S5      nU(       d  O UR	                  U5        [0        R0                  " U// / [2        5      S   (       a  Mi  [
        R&                  " U5        UR;                  5         U R=                  SSR                  U5       35        [
        R&                  " U5         UR?                  [2        S9nSR                  U5      nU(       a  SU;   a  U RC                  S5        X4$ ! [         a     GNf = f! [8         a     Mz  f = f! [         R@                   a#    UR;                  5         UR?                  5       n Nf = f)Nz-iz-uz-IPYTHON_HISTORYz.regrtest_historyr      T)stdinstdoutstderrr   text	close_fdsr"   
utf-8i   z'Timeout while waiting for output, got:  )timeoutzcan't use pyreplzpyrepl not available)"ptyopenptysys
executableappendospathjoinextendtermios	tcgetattrVREPRINTVINTR	tcsetattrTCSANOWModuleNotFoundError
subprocessPopenenvironclose
isinstancelistwriteencodeselectr
   readdecodeOSErrorkillfailwaitTimeoutExpiredskipTest)r+   r!   r"   r   r   r    	master_fdslave_fdcmdrE   	term_attrprocessoutputdata	exit_codes                  r-   r)   ReplTestCase._run_repl<   s~    "kkm	~~tT*;JJtS($&GGLL6I$JC !#JJ|$	D  ))(3I-.IaL))**+IaL'hC""

	
 	j$'':.5J
--g67mmYKR?Bwwy$/66w?  MM$ mmYKR?BB HHYLLNII??PQR
	'];I &&0MM01  ] # 		:   (( 	'LLNI	's6   >J -J0 K 
J-,J-0
J>=J>4K87K8 )N)__name__
__module____qualname____firstlineno__strrQ   dictbooltupleintr.   r)   __static_attributes__rf       r-   r   r   &   s      #
 *.#$s)O# D[#
 3i$&# 4Z# # 
sCx#*C!$s)OC! D[	C!
 3i$&C! C! C! 
sCxC!rq   r   c                   t    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rSrg)TestCursorPosition   c                 @    [        U5      n[        S S9n[        X#S9nU$ Nreadline_completerconsoleconfigr   r   r   r+   eventsrz   r{   readers        r-   prepare_reader!TestCursorPosition.prepare_reader   %    f%48$WDrq   c           
          Sn[         R                  " [        U5      [        SS[	        S5      S9/5      n[        U5      u  p4U R                  UR                  S5        UR                  R                  SS5        g )	Ndef f():
  ...
keyup   OAevtrc   raw)r   r   r   r   
	itertoolschainr   r   	bytearrayr   assertEqualcxymove_cursorassert_called_once_withr+   coder~   r   rz   s        r-   test_up_arrow_simple'TestCursorPosition.test_up_arrow_simple   sr     	
 4 %d	)0DE
 ,F3V,33Aq9rq   c           
          Sn[         R                  " [        U5      [        SS[	        S5      S9/5      n[        U5      u  p4U R                  UR                  S5        UR                  R                  SS5        g )	Nr   r   down   OBr   )r      r   r   r   r   s        r-   test_down_arrow_end_of_input/TestCursorPosition.test_down_arrow_end_of_input   sr     	
 4 %f)I2FG
 ,F3V,33Aq9rq   c           
          [         R                  " [        S5      [        SS[	        S5      S9/5      n[        U5      u  p#U R                  UR                  S5        UR                  R                  SS5        g )	N11+11r   left   ODr   )   r   r   r   r   r+   r~   r   rz   s       r-   test_left_arrow_simple)TestCursorPosition.test_left_arrow_simple   sf    7#%f)I2FG
 ,F3V,33Aq9rq   c           
          [         R                  " [        S5      [        SS[	        S5      S9/5      n[        U5      u  p#U R                  UR                  S5        UR                  R                  SS5        g )	Nr   r   right   OCr   )   r   r   r   r   r   s       r-   test_right_arrow_end_of_line/TestCursorPosition.test_right_arrow_end_of_line   sf    7#%g9Y3GH
 ,F3V,33Aq9rq   c                     [         R                  " [        S5      5      n[        U5      u  p#U R	                  UR
                  S5        U R	                  UR                  S5        g )Nkr   )r   r   r   r   r   r   r   posr   r+   r~   r   _s       r-   %test_cursor_position_simple_character8TestCursorPosition.test_cursor_position_simple_character   sK    !45%f-	Q' 	V,rq   c                     [         R                  " [        S5      5      n[        U5      u  p#U R	                  UR
                  S5        U R	                  UR                  S5        g )N   樂r   r   r   r   r   s       r-   +test_cursor_position_double_width_character>TestCursorPosition.test_cursor_position_double_width_character   sK    !67%f-	Q' 	V,rq   c           
          [         R                  " [        S5      [        SS[	        S5      S9/5      n[        U5      u  p#U R                  UR                  S5        U R                  UR                  S5        g )Nr   r   r   r   r   r   )r   r   	r   r   r   r   r   r   r   r   r   r   s       r-   5test_cursor_position_double_width_character_move_leftHTestCursorPosition.test_cursor_position_double_width_character_move_left   sd    5!%f)I2FG
 &f-	Q'V,rq   c                    [         R                  " [        S5      [        SS[	        S5      S9[        SS[	        S5      S9/5      n[        U5      u  p#U R                  UR                  S5        U R                  UR                  S	5        g )
Nr   r   r   r   r   r   r   r   r   r   r   s       r-   ;test_cursor_position_double_width_character_move_left_rightNTestCursorPosition.test_cursor_position_double_width_character_move_left_right   sw    5!%f)I2FG%g9Y3GH
 &f-	Q' 	V,rq   c           
      *   SnU S3n[         R                  " [        U5      [        SS[	        S5      S9/5      n[        U5      u  pEU R                  UR                  [        U5      5        U R                  UR                  [        U5      S45        g )Nfor _ in _:    
  ' 可口可乐; 可口可樂'r   r   r   r   r   
r   r   r   r   r   r   r   r   lenr   r+   for_loopr   r~   r   r   s         r-   4test_cursor_position_double_width_characters_move_upGTestCursorPosition.test_cursor_position_double_width_characters_move_up   s      Z . . 	 4 %d	)0DE
 &f-	 	S]3c(mQ%78rq   c                 N   SnU S3n[         R                  " [        U5      [        SS[	        S5      S9[        SS[	        S5      S9[        SS	[	        S
5      S9/5      n[        U5      u  pEU R                  UR                  S5        U R                  UR                  S5        g )Nr   r   r   r   r   r   r   r   r   r      )
   r   r   r   s         r-   9test_cursor_position_double_width_characters_move_up_downLTestCursorPosition.test_cursor_position_double_width_characters_move_up_down	  s      Z . . 	 4 %d	)0DE%f)I2FG%f)I2FG
 &f-	
 	R(W-rq   c                 @   [         R                  " [        S5      [        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9/5      n[        U5      u  p#U R                  UR                  S5        U R                  UR                  S5        g )Nu   ' 可口可乐; 可口可樂'r   r   r   r   r   )   r   r   r   s       r-   ?test_cursor_position_multiple_double_width_characters_move_leftRTestCursorPosition.test_cursor_position_multiple_double_width_characters_move_left$  s    :;%f)I2FG%f)I2FG%f)I2FG
 &f-	R( 	W-rq   c                 x   SnSnU SU S3n[         R                  " [        U5      [        SS[	        S5      S9[        SS[	        S5      S9/5      n[        U5      u  pVU R                  UR                  [        U5      [        U5      -   S	-   5        U R                  UR                  [        U5      S	45        g )
Nr   z  hellor8   z

  h
  helr   r   r   r   r   r   )r+   
first_linesecond_liner   r~   r   r   s          r-   #test_cursor_position_move_up_to_eol6TestCursorPosition.test_cursor_position_move_up_to_eol5  s    "
 l"m  	 4 %d	)0DE%d	)0DE
 &f-	 	JJJ#k*::Q>	
 	c+&6%:;rq   c                    SnSU 3n[         R                  " [        U5      [        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9/5      n[        U5      u  pEU R                  UR                  [        U5      5        U R                  UR                  [        U5      S	45        g )
Nz  helzfor _ in _:
  hello
  h
r   r   r   r   r   r      r   )r+   	last_liner   r~   r   r   s         r-   %test_cursor_position_move_down_to_eol8TestCursorPosition.test_cursor_position_move_down_to_eolV  s    	 [ 	 4 %d	)0DE%d	)0DE%f)I2FG%f)I2FG	
 &f-	 	SY/c)na%89rq   c                 &   Sn[         R                  " [        U5      S[        SS[	        S5      S9/-  [        SS[	        S5      S9/5      n[        U5      u  p4U R                  UR                  S	5        U R                  UR                  S
5        g )NuI   def foo():
  x = '可口可乐; 可口可樂'
  y = 'abckdfjskldfjslkdjf'   r   r   r   r   r   r      )   r   r   r+   r   r~   r   r   s        r-   1test_cursor_position_multiple_mixed_lines_move_upDTestCursorPosition.test_cursor_position_multiple_mixed_lines_move_upv  s    * 	 4 %EIi4HIJJu4Yy-ABC
 &f-	 	R(W-rq   c           
          Sn[         R                  " [        U5      [        SS[	        S5      S9/5      n[        U5      u  p4U R                  UR                  S5        U R                  UR                  S5        g )Nzdef foo():
  hellor   r   r   r   r   )r   r   )	r   r   r   r   r   r   r   r   r   r   s        r-   +test_cursor_position_after_wrap_and_move_up>TestCursorPosition.test_cursor_position_after_wrap_and_move_up  sr     	 4 %d	)0DE
 18	 	R(V,rq   rf   N)rg   rh   ri   rj   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rp   rf   rq   r-   rs   rs      sT    :$:$
:
:--
--9..6."<B:@.4-rq   rs   c                   D    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
rg)TestPyReplAutoindenti  c                 @    [        U5      n[        S S9n[        X#S9nU$ rv   r|   r}   s        r-   r   #TestPyReplAutoindent.prepare_reader  r   rq   c                 |    SnSn[        U5      nU R                  U5      n[        U5      nU R                  XR5        g )Nzdef f():
pass

zdef f():
    pass
    r   r   r   r   r+   
input_codeoutput_coder~   r   rb   s         r-   test_auto_indent_default-TestPyReplAutoindent.test_auto_indent_default  sJ     	 	  
+$$V, (-rq   c           
          [         R                  " [        S5      [        SS[	        S5      S9/[        S5      5      nSnU R                  U5      n[        U5      nU R                  XB5        g )N	def f():
r   	backspace   r     pass
pass

zdef f():
  pass
  pass
  r   r   r   r   r   r   r   r   r+   r~   r   r   rb   s        r-   test_auto_indent_continuation2TestPyReplAutoindent.test_auto_indent_continuation  su     <( %ky7IJ 

 	 $$V, (-rq   c           
         [         R                  " [        S5      [        SS[	        S5      S9/[        S5      [        S5      5      nSnU R                  U5      n[        U5      n[        U5      nU R                  XR5        g )	Nr   r   r   r   r   r   zdef g():
pass

zdef g():
  pass
  r   )r+   r~   r   r   output1output2s         r-   test_auto_indent_prev_block0TestPyReplAutoindent.test_auto_indent_prev_block  s     <( %ky7IJ  
" 	 $$V,!&)!&).rq   c                    [         R                  " [        S5      [        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S	5      S9/[        S
5      [        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S	5      S9[        SS[	        S	5      S9/5      nSnU R                  U5      n[        U5      nU R                  XB5        g )Nzdef f():
passr   r   r   r   s   O5r8      
passr   r   zdef f():
    pass
    pass
    r   r   s        r-   test_auto_indent_multiline/TestPyReplAutoindent.test_auto_indent_multiline  s     %d	)0DE%f)I2FG%d	%0@A 
 %f)I2FG%f)I2FG%d	%0@A%d	%0@A
4 	 $$V, (-rq   c                 x    [        S5      nSnU R                  U5      n[        U5      nU R                  XB5        g )Nzdef f():  # foo
pass

zdef f():  # foo
    pass
    r   r   s        r-   test_auto_indent_with_comment2TestPyReplAutoindent.test_auto_indent_with_comment$  E    
 	 $$V, (-rq   c                 x    [        S5      nSnU R                  U5      n[        U5      nU R                  XB5        g )Nzdef f():  ## foo
pass

zdef f():  ## foo
    pass
    r   r   s        r-   "test_auto_indent_with_multicomment7TestPyReplAutoindent.test_auto_indent_with_multicomment6  r  rq   c                 x    [        S5      nSnU R                  U5      n[        U5      nU R                  XB5        g )Nz	pass  #:
zpass  #:r   r   s        r-    test_auto_indent_ignore_comments5TestPyReplAutoindent.test_auto_indent_ignore_commentsH  sC    

  	
 $$V, (-rq   rf   N)rg   rh   ri   rj   r   r   r   r   r  r  r	  r  rp   rf   rq   r-   r   r     s+    .&.6/>%.N.$.$.rq   r   c                   b    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rSrg)TestPyReplOutputiX  c                 N    [        U5      n[        S S9n[        X#S9nSUl        U$ )Nrw   ry   F)r   r   r   can_colorizer}   s        r-   r   TestPyReplOutput.prepare_readerY  s-    f%48$WD#rq   c                      [         R                  " [        R                  R	                  5       5      (       a  g  U R                  S5        g ! [         a  nU R                  SU 35         S nAg S nAff = f)Nzstdin is not a ttyzstdin tty check failed: )rA   isattyr>   r3   filenor\   rW   r+   oses     r-   test_stdin_is_tty"TestPyReplOutput.test_stdin_is_tty`  sc    	0yy))+,, -
 MM./  	<MM4SE:;;	<   7A 
A5A00A5c                      [         R                  " [        R                  R	                  5       5      (       a  g  U R                  S5        g ! [         a  nU R                  SU 35         S nAg S nAff = f)Nzstdout is not a ttyzstdout tty check failed: )rA   r  r>   r4   r  r\   rW   r  s     r-   test_stdout_is_tty#TestPyReplOutput.test_stdout_is_ttyj  sc    	1yy**,-- .
 MM/0  	=MM5cU;<<	=r  c                     U R                  [        S5      5      n[        U5      nU R                  US5        U R	                  USSS9  g )Nz1+1
1+1Tclean)r   r   r   r   assert_screen_equal)r+   r   rb   s      r-   
test_basicTestPyReplOutput.test_basict  sF    $$^G%<= ('  d ;rq   c                     U R                  [        S5      5      n[        S S US9nU R                  [	        UR                  5       5      [        5        g )Nr8   )f_inf_outr   )r   r   r   assertIstypeget_line_bufferrk   )r+   r   wrappers      r-    test_get_line_buffer_returns_str1TestPyReplOutput.test_get_line_buffer_returns_str{  sA    $$^D%9:"DHd72245s;rq   c                    [         R                  " [        S5      [        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S	5      S9[        SS
[	        S5      S9[        SS[	        S5      S9[        SS[	        S	5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S	5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9/5      nU R                  U5      n[        U5      nSnU R                  X45        U R                  X$SS9  [        U5      nSnU R                  X45        U R                  X$SS9  g )Nzdef f():
...

r   r   r   r   r   r   r      g   gr   r   deleter   r   p   pa   as   sr8   r   zdef f():
    ...
    Tr   zdef g():
    pass
    	r   r   r   r   r   r   r   r   r"  )r+   r~   r   rb   expecteds        r-   test_multiline_edit$TestPyReplOutput.test_multiline_edit  s   ./%d	)0DE%d	)0DE%f)I2FG%f)I2FG%f)I2FG%ky7IJ%cy?%f)I2FG%ky7IJ%hIg4FG%g9T?C%ky7IJ%cy?%cy?%cy?%cy?%d	%0@A%d	%0@A%
. $$V, (,*   > (-*   >rq   c                    [         R                  " [        S5      [        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9/5      nU R                  U5      n[        U5      nU R                  US5        U R                  USS	S
9  [        U5      nU R                  US5        U R                  USS	S
9  [        U5      nU R                  US5        U R                  USS	S
9  [        U5      nU R                  US5        U R                  USS	S
9  g )N1+1
2+2
r   r   r   r   r8   r   r  Tr   2+2r9  r+   r~   r   rb   s       r-   %test_history_navigation_with_up_arrow6TestPyReplOutput.test_history_navigation_with_up_arrow  sQ   <(%d	)0DE%d	%0@A%d	)0DE%d	)0DE%d	)0DE%d	%0@A

 $$V, ('  d ; ('  d ; ('  d ; ('  d ;rq   c                    Sn[        [        R                  " [        U5      [	        SS[        S5      S9[	        SS[        S5      S9[	        SS[        S5      S9[	        SS[        S5      S9[	        SS[        S5      S9/5      5      nU R                  U5      n[        U5      n[        U5      n[        U5      nSnU R                  X5S	S
9  U R                  XE5        g )Nz4def foo():
x = 1
y = 2
z = 3

def bar():
return 42

r   r   r   r   r8   r   z-def foo():
    x = 1
    y = 2
    z = 3
    Tr   )
rQ   r   r   r   r   r   r   r   r"  r   )r+   r   r~   r   rb   r:  s         r-   #test_history_with_multiline_entries4TestPyReplOutput.test_history_with_multiline_entries  s    Mioo4 %d	)0DE%d	)0DE%d	)0DE%d	%0@A%d	%0@A	
 	 $$V, ( ( (F   >*rq   c                    [         R                  " [        S5      [        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S	5      S9[        SS[	        S	5      S9/5      nU R                  U5      n[        U5      nU R                  US
5        U R                  US
SS9  g )Nr>  r   r   r   r   r8   r   r   r   r  Tr   r9  r@  s       r-   'test_history_navigation_with_down_arrow8TestPyReplOutput.test_history_navigation_with_down_arrow  s    <(%d	)0DE%d	)0DE%d	%0@A%f)I2FG%f)I2FG	
 $$V, ('  d ;rq   c                 n   [         R                  " [        S5      [        SS[	        S5      S9[        SS[	        S5      S9[        SS[	        S	5      S9[        SS[	        S	5      S9/5      nU R                  U5      n[        U5      nU R                  US
5        U R                  US
SS9  [        U5      nU R                  US5        U R                  USSS9  [        U5      nU R                  US5        U R                  USSS9  [        U5      nU R                  US
5        U R                  US
SS9  g )Nz1+1
2+2
3+3
r      r   1   1r8   r   r  Tr   r?  z3+3r9  r@  s       r-   test_history_search$TestPyReplOutput.test_history_search  s/   ,-%f)G2DE%cy?%d	%0@A%d	%0@A	
 $$V, ('  d ; ('  d ; ('  d ; ('  d ;rq   c                     [        S5      nU R                  U5      n[        U5      nU R                  US5        U R	                  USSS9  g )Nzc
zcTr   )r   r   r   r   r"  r@  s       r-   test_control_character'TestPyReplOutput.test_control_character  sK    	*$$V, ()   =rq   c                 8   [         R                  " [        S5      [        S5      [        SS[	        S5      S9[        SS[	        S5      S9/5      nU R                  U5      n[        U5        [        U5      nU R                  US	5        U R                  US	S
S9  g )N
import os
impr   page up   [5~r   r8   r   	import osTr   r9  r@  s       r-   test_history_search_backward-TestPyReplOutput.test_history_search_backward  s    =)5!%iYz5JK%d	%0@A
 $$V, !(-  D Arq   c                 $   [         R                  " [        S5      [        SS[	        S5      S9[        SS[	        S5      S9/5      nU R                  U5      n[        U5        [        U5      nU R                  US5        U R                  USS	S
9  g )NrT  r   rV  rW  r   r8   r   rX  Tr   r9  r@  s       r-   "test_history_search_backward_empty3TestPyReplOutput.test_history_search_backward_empty  s    =)%iYz5JK%d	%0@A
 $$V, !(-  D Arq   rf   N)rg   rh   ri   rj   r   r  r  r#  r,  r;  rA  rD  rG  rN  rQ  rY  r\  rp   rf   rq   r-   r  r  X  sF    01<<
!?F<8+,<$<4>B(Brq   r  c                       \ rS rSrS r\" SS5      S 5       rS rS rS r	S	 r
\" S
5      \" S\R                  S9S 5       5       rSrg)TestPyReplCompleteri0  c                     [        U5      n[        5       n[        R                  " U5      R                  Ul        [        X4S9nU$ )Nry   )r   r   rlcompleter	Completercompleterx   r   )r+   r~   	namespacerz   r{   r   s         r-   r   "TestPyReplCompleter.prepare_reader1  s<    f%!$/$9$9)$D$M$M!$WDrq   zrlcompleter._readline_availableFc                     [        S5      nS[        0nU R                  X5      n[        X25      nU R	                  US5        g )Nzos.getpid	
rA   zos.getpid()r   rA   r   r   r   r+   r~   rd  r   rb   s        r-   test_simple_completion*TestPyReplCompleter.test_simple_completion8  s?    02J	$$V7 3/rq   c                     [        S5      nS[        0nU R                  X5      n[        X25      nU R	                  US5        g )Nzos.		O_AP	
rA   zos.O_APPENDrg  rh  s        r-   !test_completion_with_many_options5TestPyReplCompleter.test_completion_with_many_optionsB  sB       122J	$$V7 3/rq   c                 z    [        S5      n0 nU R                  X5      n[        X25      nU R                  US5        g )Nz
os.geten	
zos.getenr   rh  s        r-   test_empty_namespace_completion3TestPyReplCompleter.test_empty_namespace_completionP  s;    /	$$V7 3,rq   c                 ~    [        S5      nSS 0nU R                  X5      n[        X25      nU R                  US5        g )Nzpy	
pythonr   rh  s        r-    test_global_namespace_completion4TestPyReplCompleter.test_global_namespace_completionX  s@    )t$	$$V7 3*rq   c                 p   SnS[         0n[        R                  " [        U5      [	        SS[        S5      S9[	        SS[        S5      S9[	        SS[        S5      S9/[        S	5      5      nU R                  X2S
9n[        XB5      nU R                  US5        [        XB5      nU R                  US5        g)zUp arrow in the middle of unfinished tab completion when the menu is displayed
should work and trigger going back in history. Down arrow should subsequently
get us back to the incomplete command.zimport os
os.		rA   r   r   r   r   r   r   r8   )rd  rX  zos.N)	rA   r   r   r   r   r   r   r   r   )r+   r   rd  r~   r   rb   s         r-   'test_up_down_arrow_with_completion_menu;TestPyReplCompleter.test_up_down_arrow_with_completion_menu_  s     $2J	4 %f)I2FG%d	)0DE%f)I2FG
 4 
 $$V$A 3- !3'rq   z,_pyrepl.readline._ReadlineWrapper.get_readerz
sys.stderr)new_callablec                      " S S5      nU" 5       n[        S5      nSU0nU R                  XV5      nXrl        [        [        SS5      nU R                  US5        U R                  UR                  5       S5        g )	Nc                   $    \ rS rSr\S 5       rSrg)@TestPyReplCompleter.test_completion_with_warnings.<locals>.Dummyi{  c                 .    SS K nUR                  S5        g )Nr   z	warnings
)warningswarn)r+   r}  s     r-   	test_funcJTestPyReplCompleter.test_completion_with_warnings.<locals>.Dummy.test_func|  s    l+rq   rf   N)rg   rh   ri   rj   propertyr  rp   rf   rq   r-   Dummyr{  {  s     rq   r  zdummy.test_func.	

dummyz>>>z...zdummy.test_func.__r:   )r   r   return_valuereadline_multiline_inputr   r   getvalue)	r+   mock_stderrmock_get_readerr  r  r~   rd  r   rb   s	            r-   test_completion_with_warnings1TestPyReplCompleter.test_completion_with_warningsx  sx    	 	  89e$	$$V7'-$)*eUC!56--/4rq   rf   N)rg   rh   ri   rj   r   r   ri  rl  ro  rs  rv  ioStringIOr  rp   rf   rq   r-   r_  r_  0  sd     ,e40 500-+(2 9:
<bkk25 3 ;5rq   r_  c                   >    \ rS rSrS rS rS rS rS rS r	S r
S	rg
)TestPasteEventi  c                 @    [        U5      n[        S S9n[        X#S9nU$ rv   r|   r}   s        r-   r   TestPasteEvent.prepare_reader  r   rq   c                    Sn[         R                  " [        SS[        S5      S9/[	        U5      [        SS[        S5      S9/[	        S5      5      nU R                  U5      n[        U5      nU R                  XA5        g )NzOdef a():
  for x in range(10):
    if x%2:
      print(x)
    else:
      pass
r   f3   ORr   r8   r   r   r   r   r   r   r   r   r+   r   r~   r   rb   s        r-   
test_pasteTestPasteEvent.test_paste  s     	 %d	)0DE 4 %d	)0DE 4 	
 $$V, (&rq   c                    Sn[         R                  " [        SS[        S5      S9/[	        U5      [        SS[        S5      S9/[	        S5      5      nU R                  U5      n[        U5      nU R                  XA5        g )Nzdef f():
  x = y
  
  y = z
r   r  r  r   r8   r  r  s        r-   test_paste_mid_newlines&TestPasteEvent.test_paste_mid_newlines  s     	 %d	)0DE 4 %d	)0DE 4 	
 $$V, (&rq   c                 |    SnSn[        U5      nU R                  U5      n[        U5      nU R                  XR5        g )Nzdef f():
x = y

y = z

zdef f():
    x = y
    r   )r+   r   r:  r~   r   rb   s         r-   )test_paste_mid_newlines_not_in_paste_mode8TestPasteEvent.test_paste_mid_newlines_not_in_paste_mode  sJ     	 	  %$$V, (*rq   c                 |    SnSn[        U5      nU R                  U5      n[        U5      nU R                  XR5        g )Nz:def a():
for x in range(10):
if x%2:
print(x)
else:
pass

zWdef a():
    for x in range(10):
        if x%2:
            print(x)
            else:r   r   s         r-   test_paste_not_in_paste_mode+TestPasteEvent.test_paste_not_in_paste_mode  sJ    # 	  	  
+$$V, (-rq   c                     SnSnSnSn[         R                  " [        U5      [        U5      [        U5      [        S5      5      nU R                  U5      n[	        U5      nU R                  Xr5        g)z8Test that bracketed paste using [200~ and [201~ works.zQdef a():
  for x in range(10):

    if x%2:
      print(x)

    else:
      pass
[200~[201~r8   Nr   r   r   r   r   r   )r+   r   r   paste_start	paste_endr~   r   rb   s           r-   test_bracketed_paste#TestPasteEvent.test_bracketed_paste  sz     	 	 "	;':&9%4 	
 $$V, (-rq   c                     SnSnSn[         R                  " [        U5      [        U5      [        U5      [        S5      5      nU R                  U5      n[	        U5      nU R                  Xa5        g )Noneliner  r  r8   r  )r+   r   r  r  r~   r   rb   s          r-    test_bracketed_paste_single_line/TestPasteEvent.test_bracketed_paste_single_line  si    
!	;':&9%4 	
 $$V, (,rq   rf   N)rg   rh   ri   rj   r   r  r  r  r  r  r  rp   rf   rq   r-   r  r    s&    '4'0+*.2%.N-rq   r  zrequires ptyc                       \ rS rSrS rSrg)TestDumbTerminali,  c                    [         R                  R                  5       nUR                  SS05        U R	                  SUS9u  p#U R                  US5        U R                  SU5        U R                  SU5        U R                  SU5        g )	NTERMdumbzexit()
r"   r   zwarning: can't use pyrepl	Exception	Traceback)rA   rN   copyupdater.   r   assertInassertNotIn)r+   r"   rb   rd   s       r-    test_dumb_terminal_exits_cleanly1TestDumbTerminal.test_dumb_terminal_exits_cleanly.  sw    jjoo

FF#$ MM*#M>A&16:f-f-rq   rf   N)rg   rh   ri   rj   r  rp   rf   rq   r-   r  r  ,  s    .rq   r  r  r  z!can't use pyrepl in dumb terminalc                   &   \ rS rSrS r\S 5       rS\S\\R                  -  S\SS4S	 jr
\S
S
S.S j5       rS rS r\S 5       r\S 5       rS r\S 5       rS r\S 5       r\S 5       rS r\S 5       rS rS rS r\" \S5      S 5       rSrg)TestMaini8  c                 ~    [        S[        5       S9nU R                  UR                  5        UR	                  5         g )Nz
os.environ)new)r   r   
addCleanupstopstart)r+   patchers     r-   setUpTestMain.setUp;  s,     .*:;%rq   c                    SnSnU R                  SS/SS9u  p4U R                  US5        U SU 3U;   nU S	U 3U;   nU S
U 3U;   nU SU 3U;   nU R                  U=(       d    U=(       d    U=(       d    UU5        g )Nz"['__annotations__', '__builtins__'z4'__loader__', '__name__', '__package__', '__spec__']zsorted(dir())exit()T)r    r   z, '__doc__', z, '__doc__', '__file__', z', '__cached__', '__doc__', '__file__', z, '__cached__', '__doc__', )r.   r   
assertTrue)	r+   prepostrb   rd   case1case2case3case4s	            r-   test_exposed_globals_in_repl%TestMain.test_exposed_globals_in_replC  s    2E MM?H*EDMQA& %}TF+v5 %076A %>tfEO %24&9VC88%85&Arq   varr:  actualr#   Nc                     [        U[        R                  5      (       a4  U R                  UR	                  U5      U SU SUR
                   35        g U R                  UUSU SU SU SU 35        g )N=z does not match z	expected z, got )rP   rePatternr  matchpatternr   )r+   r  r:  r  s       r-   _assertMatchOKTestMain._assertMatchOKX  s{     h

++OOv&%q 01A1A0BC
 C5(6#ax@rq   F)as_file	as_modulec          	         [        5       nSUS'   [        R                  " 5        n[        R                  " U5      S-  nUR                  5         US-  nUR                  SSS9  U Vs/ s H  nSU-   S	-   PM     snS
/-   n	U(       a  U(       a  U R                  S5        OVU(       a  U R                  U	[        U5      /USS9u  pO0U(       a  U R                  U	SS/UUSS9u  pOU R                  S5        S S S 5        U R                  WS5        UR                  5        Hy  u  pU R                  XS9   [        R                  " SU S3W
5      =n(       a   U R                  XUR!                  S5      S9  OU R                  U SU
< SU
 35        S S S 5        M{     U R#                  SW
5        U R#                  SU
5        g s  snf ! , (       d  f       N= f! , (       d  f       M  = f)NrL  NO_COLORbluezcalx.pyzFOO = 42r9   )encodingz
print(f'^{z=}')r  z,as_file and as_module are mutually exclusiveT)r   r"   r    z-mz	blue.calx)r   r"   r   r    z"Choose one of as_file or as_moduler   )r  r:  z\^z=(.+?)[\r\n]r   )r  z= not found in output: z

r  r  )r   r&   r'   pathlibPathmkdir
write_textrY   r.   rk   r   itemssubTestr  searchr  groupr  )r+   expectationsr  r  	clean_envtdr  modr  commandsrb   rd   r:  ms                 r-   _run_repl_globals_testTestMain._run_repl_globals_testg  s   "$	 #	*((*b<<#f,DJJL"CNN:N87C7Cs"V+|
H 9		HI$(MM"%c(!	 %2 %!	 $(MM"&!4! %2 %!	 		>?5 +8 	A&)//1MC#9		RuL"96BB1B''aggaj'III%<VJd6(ST	 :9 2 	f-f-A +*< :9s,   AG(G:A=G5AGG
G
G(	c                 0    SSSS.nU R                  USS9  g )N42
'__main__'None)FOOrg   __package__T)r  )r  r+   r  s     r-   .test_inspect_keeps_globals_from_inspected_file7TestMain.test_inspect_keeps_globals_from_inspected_file  s'    $!
 	##L$#?rq   c                 Z    SSS[         R                  " S5      S.nU R                  USS9  g )Nr  r  z'blue'z^'.*calx.py'$)r  rg   r  __file__T)r  )r  compiler  r  s     r-   0test_inspect_keeps_globals_from_inspected_module9TestMain.test_inspect_keeps_globals_from_inspected_module  s4    $#

#34	
 	##LD#Arq   c                    [         R                  R                  5       nSnUR                  SS 5        U R	                  X!SS9u  p4U R                  US5        U R                  SU5        U R                  SU5        U R                  SU5        U R                  S	U5        S
US'   U R	                  X!S9u  p4U R                  US5        U R                  SU5        U R                  SU5        U R                  SU5        U R                  S	U5        SnS
US'   U R	                  X!S9u  p4U R                  US5        U R                  SU5        U R                  SU5        U R                  SU5        U R                  S	U5        g )NzRfrom test.support import initialized_with_pyrepl
initialized_with_pyrepl()
exit()
PYTHON_BASIC_REPLTr"   r    r   TrueFalser  r  rL  r  z2import sys
print('_pyrepl' in sys.modules)
exit()
)rA   rN   r  popr.   r   r  r  r+   r"   r  rb   rd   s        r-   test_python_basic_replTestMain.test_python_basic_repl  sn   jjoo 	#T* MM($MGA&ff%&)f-f-#&  MM(M<A&gv&(f-f- $'  MM(M<A&gv&(f-f-rq   c           	         [         R                  R                  [        SS5      nU R	                  [         R                  R                  U5      U5        [        U5       nUR                  5       nS S S 5        [        [        S [        [        R                  W5      5      5      nS H  nU R                  XC5        M     g ! , (       d  f       NV= f)N_pyreplz__main__.py)?zunknown-filenamez<foo>z<...>)rA   rB   rC   r   r  existsopen	readlinesrQ   filtermaprk   strip_test_no_pyrepl_source_in_exc)r+   pyrepl_main_filefpexcluded_linesfilenames        r-   test_no_pyrepl_source_in_exc%TestMain.test_no_pyrepl_source_in_exc  s     77<<
I}M'78:JK"#r\\^N $fT3syy.+IJKCH..xH D	 $#s    C
Cc           	         [        5        o0R                  US9   UR                  S5        SU< S3nU R                  XCS9u  pVU R	                  SU5        U R	                  SU5        U H,  nU R                  US9   U R                  Xu5        S S S 5        M.     S S S 5        S S S 5        g ! , (       d  f       MP  = f! , (       d  f       N)= f! , (       d  f       g = f)	N)r  r  zeval(compile('spam', z, 'eval'))
exit()
r  z!Traceback (most recent call last)z%NameError: name 'spam' is not defined)line)r   r  unsetr.   r  r  )r+   r  r  r"   r  rb   r   r  s           r-   r  &TestMain._test_no_pyrepl_source_in_exc  s     "c<<<+JII)*/| <# $Hh8IFMM=vFMMA6J&\\t\,$$T2 -, ' ,K"" -, ,K+J""s;   CA"B=<B+	B=C+
B:5B==
C	C
Cc                    ^  [         R                  R                  5       nSnU 4S jnUR                  SS 5        T R	                  X!SS9u  pEU" XE5        SUS'   T R	                  X!S9u  pEU" XE5        g )Nz)import sys
sys.excepthook = 1
1/0
exit()
c                    > TR                  SU 5        TR                  U R                  S5      S5        TR                  SU 5        TR                  SU 5        TR                  US5        g )NzError in sys.excepthook:z'int' object is not callabler   zOriginal exception was:zdivision by zeror   )r  r   count)rb   exitcoder+   s     r-   checkCTestMain.test_bad_sys_excepthook_doesnt_crash_pyrepl.<locals>.check  sZ    MM4f=V\\*HI1MMM3V<MM,f5Xq)rq   r  Tr   rL  r  )rA   rN   r  r  r.   )r+   r"   r  r  rb   rd   s   `     r-   +test_bad_sys_excepthook_doesnt_crash_pyrepl4TestMain.test_bad_sys_excepthook_doesnt_crash_pyrepl  ss    jjoo
	* 	#T* MM($MGf #&  MM(M<f rq   c                    [        S5        [        R                  " SS9nU R                  [        UR
                  5        [        R                  R                  5       nUR
                  US'   SnUR                  SS 5        U R                  X2S9u  pEU R                  US5        U R                  S	U5        U R                  S
U5        U R                  [        R                  " UR
                  5      R!                  5       R"                  S5        UR$                  R'                  5         UR)                  5         SUS'   U R                  X2S9u  pEU R                  US5        U R                  S	U5        U R                  S
U5        U R                  [        R                  " UR
                  5      R!                  5       R"                  S5        g )NreadlineF)r2  r1   z123
spam
exit()
r  r  r   123spamrL  )r   r&   NamedTemporaryFiler  r   r(   rA   rN   r  r  r.   r   r  assertNotEqualr  r  statst_sizefiletruncaterO   )r+   hfiler"   r  rb   rd   s         r-   test_not_wiping_history_file%TestMain.test_not_wiping_history_file  sY   j!++59

+jjoo %

(#T* MM(M<A&eV$ff%GLL499;CCQG

#&  MM(M<A&eV$ff%GLL499;CCQGrq   c                    [         R                  R                  5       nSnU R                  X!SS9u  p4U R	                  SU5        U R	                  SU5        SnU R                  X!S9u  p4U R	                  SU5        U R	                  SU5        g )	Nza b c
exit()
Tr   zSyntaxError: invalid syntaxz<python-input-0>z
 b
exit()
r  z#IndentationError: unexpected indent)rA   rN   r  r.   r  r  s        r-   %test_correct_filename_in_syntaxerrors.TestMain.test_correct_filename_in_syntaxerrors  s    jjoo$ MM($MG3V<(&1! MM(M<;VD(&1rq   c           	      J   [         R                  R                  5       nS H  nSU(       a  SOS-   S-   nS H  nU(       a  SUS'   OUR                  SS 5        U R	                  UUS9   U R                  X1S	S
9u  pVU R                  SU5        U(       a7  U R                  SU5        U R                  SU5        U R                  SU5        O6U R                  SU5        U R                  SU5        U R                  SU5        S S S 5        M     M     g ! , (       d  f       M  = f)N)TFzimport sys
zsys.tracebacklimit = 1
r:   z;def x1(): 1/0

def x2(): x1()

def x3(): x2()

x3()
exit()
rL  r  )set_tracebacklimit
basic_replTr   zin x1zin x2zin x3zin <module>)rA   rN   r  r  r  r.   r  r  )r+   r"   r4  r  r5  rb   rd   s          r-   test_proper_tracebacklimit#TestMain.test_proper_tracebacklimit  s   jjoo"/&7I3rS##H ,
/2C+,GG/6\\5G-7 " 9(,hd(S%FMM'62)((&9((&9((?gv6gv6mV<9 9 , #09 9s   )BD
D"c                 r    U R                  S5      u  pU R                  US5        U R                  SU5        g )Nz	 
exit()
r   	TypeError)r.   r   r  r+   rb   rd   s      r-   test_null_byteTestMain.test_null_byte<  s4     MM*:;A&f-rq   c                     SnU R                  U5      u  p#U R                  US5        U R                  SU5        U R                  SU5        g )NzNimport runpy
runpy._run_module_code('blech', {0: '', 'bluch': ''}, '')
exit()
r   z,all elements in 'candidates' must be stringsbluch)r.   r   r  r  )r+   r  rb   rd   s       r-   %test_non_string_suggestion_candidates.TestMain.test_non_string_suggestion_candidatesA  sK     !MM(3A&GPgv&rq   c                 ~   [        S5      nUR                  S:w  a  U R                  S5        [        R                  " 5        n[
        R                  R                  5       nUR                  US'   SUS'   U R                  SUS9u  pEU R                  US	5        U R                  S
U5        U R                  [        R                  " UR                  5      R                  5       R                   S	5        U R                  S[        R                  " UR                  5      R#                  5       5        UR%                  SS 5        U R                  SUS9u  pEU R                  US	5        U R'                  S[        R                  " UR                  5      R#                  5       5        S S S 5        g ! , (       d  f       g = f)Nr$  editlinez*GNU readline is not affected by this issuer1   rL  r  zspam 
exit()
r  r   zspam zspam\040zexit
z\040)r   backendr\   r&   r'  rA   rN   r  r(   r.   r   r  r(  r  r  r)  r*  	read_textr  r  )r+   r$  r-  r"   rb   rd   s         r-   test_readline_history_file#TestMain.test_readline_history_fileL  sI    ,z)MMFG((*e**//#C$)JJC !'*C#$ $.?S IFY*MM'6*UZZ 8 = = ? G GKMM+w||EJJ'?'I'I'KLGG'. $hC @FY*Wgll5::&>&H&H&JK +**s   E#F..
F<c                 R    U R                  / SQ5      u  pU R                  US5        g )N)rJ  exitr   )r.   r   r:  s      r-   %test_keyboard_interrupt_after_isearch.TestMain.test_keyboard_interrupt_after_isearchb  s$     MM*BCA&rq   c                     U R                  / SQ5      u  p[        R                  " S5      nUR                  SU5      nU R	                  US5        U R                  SU5        g )N)helpqrI  z"(\x1B(=|>|(\[)[0-?]*[ -\/]*[@-~]))r:   r   z>>> 
>>> >>>)r.   r  r  subr   r  )r+   rb   rd   ansi_escapecleaned_outputs        r-   test_prompt_after_helpTestMain.test_prompt_after_helpf  sW     MM*?@ jj!FG$V4A&
 	.9rq   z--X showrefcount requires a Python debug buildc                    [         R                  R                  5       nUR                  SS5        U R	                  SS/US9u  p#[
        R                  " SU5      nU R                  [        U5      S5        SUS'   U R	                  SS/US9u  p#[
        R                  " SU5      nU R                  [        U5      S5        g )	Nr  r:   z1
1+2
exit()
z-Xshowrefcount)r   r"   z\[-?\d+ refs, \d+ blocks\]r   rL  )	rA   rN   r  r  r.   r  findallr   r   )r+   r"   rb   r   matchess        r-   test_showrefcountTestMain.test_showrefcounts  s    jjoo#R(MM"4DTCU[^M_	**:FCWq)#& MM"4DTCU[^M_	**:FCWq)rq   rf   )rg   rh   ri   rj   r  r   r  rk   r  r  r  r  r  r  r  r  r  r!  r.  r1  r6  r;  r?  rE  rJ  rR  r   r	   rW  rp   rf   rq   r-   r  r  8  s)    B B(&)BJJ&6@C	 >Cu ). ).V@B  .  .D 
I 
I
3 ! !*H6 	2 	2 = =:.
 ' 'L,': IJ
* K
*rq   r  )6r  r   rA   r  r  ra  rT   rL   r>   r&   unittestr   r   r   unittest.mockr   test.supportr   r   r	   r
   r   test.support.import_helperr   test.support.os_helperr   r   supportr   r   r   r   r   r   r   _pyrepl.consoler   _pyrepl.readliner   r   r   r  r<   ImportErrorr   rs   r   r  r_  r  r  rN   getr  rf   rq   r-   <module>rc     sI   	  	  	    
  1 1  F F 2 4 >   "0 0 H
Y!8 Y!xd- d-N	l.8 l.^UB' UBpZ5( Z5z\-X \-~ C .| . !. C 		)6f46YZD*| D* [ !D*q   
Cs   D) )D43D4