
    6bh2                         d 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 ddl	m
Z
  e
j                  d      Z G d d	ej                        Zed
k(  r ej                           yy)zCTest InteractiveConsole and InteractiveInterpreter from code module    N)dedent)	ExitStack)mock)import_helpercodec                   |    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)TestInteractiveConsoleNc                 V    t         j                         | _        | j                          y N)r   InteractiveConsoleconsolemock_sysselfs    ?/opt/python-3.12.12/usr/lib/python3.12/test/test_code_module.pysetUpzTestInteractiveConsole.setUp   s    ..0    c                    t               }| j                  |j                         |j                  t	        j
                  dd            | _        |j                  t	        j
                  d            | _        |j                  t	        j
                  d            | _        t	        j
                  dt        j                  t        j                        }|j                  |      | _        t        j                  t        j                  u r%| j                  j                  | j                  _        | j                  `| j                  `y)	z.Mock system environment for InteractiveConsolez
code.inputT)createzcode.sys.stdoutzcode.sys.stderrzcode.sys)wrapsspecN)r   
addCleanupcloseenter_contextr   patchinfuncstdoutstderrr   syssysmod
excepthook__excepthook__ps1ps2)r   stackprepatchs      r   r   zTestInteractiveConsole.mock_sys   s     $))$**\15+7 8))$**5F*GH))$**5F*GH::jtxxH))(3>>S///%)[[%?%?DKK"KKOKKOr   c                 Z   t        d      | j                  _        | j                  j	                          | j                  | j                  j                  d       d| j                  _        | j                  j	                          | j                  | j                  j                  d       y )NFinishedz>>> z	custom1> )EOFErrorr   side_effectr   interactassertEqualr    r#   r   s    r   test_ps1zTestInteractiveConsole.test_ps1%   n    "*:"6&1%+6r   c                 Z   t        d      | j                  _        | j                  j	                          | j                  | j                  j                  d       d| j                  _        | j                  j	                          | j                  | j                  j                  d       y )Nr(   z... z	custom2> )r)   r   r*   r   r+   r,   r    r$   r   s    r   test_ps2zTestInteractiveConsole.test_ps2-   r.   r   c                     ddt        d      g| j                  _        | j                  j	                          t        | j                  j                        D ]  }ddj                  |d         v s y  t        d      )Nz	'antioch' r(   antioch   zno console stdout)
r)   r   r*   r   r+   listr   method_callsjoinAssertionError)r   calls     r   test_console_stderrz*TestInteractiveConsole.test_console_stderr5   si    #.HZ4H"I112DBGGDG,, 3 !!455r   c                 <   dddt        d      g| j                  _        | j                  j	                          dj                  d | j                  j                  D              }||j                  d      d  }|d |j                  d       }| j                  |j                         dd  g d	       | j                  | j                  j                  t               | j                  t        | j                  j                         t               | j#                  | j                  j$                         | j#                  | j                  j                   j&                         | j                  | j                  j(                  | j                  j                          y )
Ndef f():	    x = ?r2   r(   c              3   D   K   | ]  }d j                  |d           ywr2   r4   Nr7   .0r9   s     r   	<genexpr>z;TestInteractiveConsole.test_syntax_error.<locals>.<genexpr>D         O6Nda)6N    (InteractiveConsole)
now exitingr4   )z  File "<console>", line 2r=   z	        ^zSyntaxError: invalid syntax)r)   r   r*   r   r+   r7   r   r6   indexr,   
splitlinesassertIsr    	last_typeSyntaxErrortype
last_valueassertIsNonelast_traceback__traceback__last_excr   outputs     r   test_syntax_errorz(TestInteractiveConsole.test_syntax_error>   s2   #-#.#%$,Z$8#: 	Odkk6N6NOO%;<=>6o67**,QR0 3+ 	,
 	dkk++[9d4;;112K@$++445$++00>>?dkk**DKK,B,BCr   c                 8   dt        d      g| j                  _        | j                  j	                          dj                  d | j                  j                  D              }||j                  d      d  }|d |j                  d       }| j                  |j                         dd  g d       | j                  | j                  j                  t               | j                  t        | j                  j                         t               | j#                  | j                  j$                         | j#                  | j                  j                   j&                         | j                  | j                  j(                  | j                  j                          y )	N  1r(   r2   c              3   D   K   | ]  }d j                  |d           ywr?   r@   rA   s     r   rC   z@TestInteractiveConsole.test_indentation_error.<locals>.<genexpr>U   rD   rE   rF   rG   r4   )z  File "<console>", line 1z    1z#IndentationError: unexpected indent)r)   r   r*   r   r+   r7   r   r6   rH   r,   rI   rJ   r    rK   IndentationErrorrM   rN   rO   rP   rQ   rR   rS   s     r   test_indentation_errorz-TestInteractiveConsole.test_indentation_errorR   s)   #((:*>"?Odkk6N6NOO%;<=>6o67**,QR0 33 	4 	dkk++-=>d4;;1124DE$++445$++00>>?dkk**DKK,B,BCr   c                 6   dt        d      g| j                  _        | j                  j	                          dj                  d | j                  j                  D              }||j                  d      d  }||j                  d      dz   d  }| j                  |j                  d      |       | j                  | j                  j                  t               | j                  t        | j                  j                         t               | j#                  | j                  j$                         | j#                  | j                  j                   j&                         | j                  | j                  j(                  | j                  j                          y )	Nu   ''r(   r2   c              3   D   K   | ]  }d j                  |d           ywr?   r@   rA   s     r   rC   z<TestInteractiveConsole.test_unicode_error.<locals>.<genexpr>e   rD   rE   rF   
r4   zUnicodeEncodeError: )r)   r   r*   r   r+   r7   r   r6   rH   
assertTrue
startswithrJ   r    rK   UnicodeEncodeErrorrM   rN   rO   rP   rQ   rR   rS   s     r   test_unicode_errorz)TestInteractiveConsole.test_unicode_errorb   s!   #-x
/C"DOdkk6N6NOO%;<=>T*Q./0))*@A6Jdkk++-?@d4;;1124FG$++445$++00>>?dkk**DKK,B,BCr   c                    ddddt        d      g| j                  _        t        j                         }|| j
                  _        | j                  j                          |j                          |j                  | j
                  j                  | j
                  j                  | j
                  j                         | j                  | j
                  j                  t               | j                  t!        | j
                  j                        t               | j                  | j
                  j                  | j
                  j                  j"                         | j                  | j
                  j$                  | j
                  j                         | j'                  t)        j*                  | j
                  j$                        g d       y )Nr<   z    raise ValueError('BOOM!')r2   zf()r(   )z#Traceback (most recent call last):
z(  File "<console>", line 1, in <module>
z!  File "<console>", line 2, in f
zValueError: BOOM!
)r)   r   r*   r   Mockr    r!   r   r+   assert_calledassert_called_withrK   rN   rP   rJ   
ValueErrorrM   rQ   rR   r,   	tracebackformat_exceptionr   hooks     r   test_sysexcepthookz)TestInteractiveConsole.test_sysexcepthooko   s9   #-#B#%#($,Z$8	#:
 yy{!% 5 5 $ 6 6 $ : :	< 	dkk++Z8d4;;112J?dkk00$++2H2H2V2VWdkk**DKK,B,BC33DKK4H4HI L# 	$r   c                    dddt        d      g| j                  _        t        j                         }|| j
                  _        | j                  j                          |j                          |j                  | j
                  j                  | j
                  j                  | j
                  j                         | j                  | j
                  j                  t               | j                  t!        | j
                  j                        t               | j#                  | j
                  j                         | j#                  | j
                  j                  j$                         | j                  | j
                  j&                  | j
                  j                         | j)                  t+        j,                  | j
                  j&                        g d       y )Nr<   r=   r2   r(   )z  File "<console>", line 2
z
    x = ?
z
        ^
zSyntaxError: invalid syntax
)r)   r   r*   r   rc   r    r!   r   r+   rd   re   rK   rN   rP   rJ   rL   rM   rO   rQ   rR   r,   rg   rh   ri   s     r   test_sysexcepthook_syntax_errorz6TestInteractiveConsole.test_sysexcepthook_syntax_error   sD   #-#.#%$,Z$8#: yy{!% 5 5 $ 6 6 $ : :	< 	dkk++[9d4;;112K@$++445$++00>>?dkk**DKK,B,BC33DKK4H4HI L- 	.r   c                    dt        d      g| j                  _        t        j                         }|| j
                  _        | j                  j                          |j                          |j                  | j
                  j                  | j
                  j                  | j
                  j                         | j                  | j
                  j                  t               | j                  t!        | j
                  j                        t               | j#                  | j
                  j                         | j#                  | j
                  j                  j$                         | j                  | j
                  j&                  | j
                  j                         | j)                  t+        j,                  | j
                  j&                        g d       y )NrW   r(   )z  File "<console>", line 1
z    1
z$IndentationError: unexpected indent
)r)   r   r*   r   rc   r    r!   r   r+   rd   re   rK   rN   rP   rJ   rY   rM   rO   rQ   rR   r,   rg   rh   ri   s     r   $test_sysexcepthook_indentation_errorz;TestInteractiveConsole.test_sysexcepthook_indentation_error   s;   #((:*>"?yy{!% 5 5 $ 6 6 $ : :	< 	dkk++-=>d4;;1124DE$++445$++00>>?dkk**DKK,B,BC33DKK4H4HI L5 	6r   c                    dddt        d      g| j                  _        d| j                  _        | j
                  j                          | j                  ddi g| j                  j                  d          d	j                  d
 | j                  j                  D              }| j                  d|       | j                  |j                  d      d       | j                  d|       | j                  d|       y )N1/0a = 123print(a)r(   r4   write123r   r2   c              3   L   K   | ]  }|d    dk(  s|j                   d      ywr   rt   NargsrA   s     r   rC   zWTestInteractiveConsole.test_sysexcepthook_crashing_doesnt_close_repl.<locals>.<genexpr>   (     `1ITRSWX_M_		!1I   $$Error in sys.excepthook:z'int' object is not callableOriginal exception was:division by zeror)   r   r*   r    r!   r   r+   r,   r   r6   r7   r   assertIncount)r   errors     r   -test_sysexcepthook_crashing_doesnt_close_replzDTestInteractiveConsole.test_sysexcepthook_crashing_doesnt_close_repl   s    #()Z*AU"V!"'9b14;;3K3KA3NO`1I1I``0%8%CDaH/7(%0r   c                    dddt        d      g| j                  _        dfd}|| j                  _        | j
                  j                          | j                  ddi g| j                  j                  d	          d
j                  d | j                  j                  D              }| j                  d|       | j                  |j                  d      d       | j                  d|       | j                  d|       y )Nrq   rr   rs   r(   znot so fastc                      t              r   )BaseException)rz   kwargsss     r   
raise_basezSTestInteractiveConsole.test_sysexcepthook_raising_BaseException.<locals>.raise_base   s    ""r   rt   ru   r   r2   c              3   L   K   | ]  }|d    dk(  s|j                   d      ywrx   ry   rA   s     r   rC   zRTestInteractiveConsole.test_sysexcepthook_raising_BaseException.<locals>.<genexpr>   r{   r|   r}   r4   r~   r   r   )r   r   r   r   s      @r   (test_sysexcepthook_raising_BaseExceptionz?TestInteractiveConsole.test_sysexcepthook_raising_BaseException   s    #()Z*AU"V	#!+'9b14;;3K3KA3NO`1I1I``0%8]3Q7/7(%0r   c                     dg| j                   _        d }|| j                  _        | j	                  t
              5  | j                  j                          d d d        y # 1 sw Y   y xY w)Nrq   c                      t         r   )
SystemExit)rz   r   s     r   r   z]TestInteractiveConsole.test_sysexcepthook_raising_SystemExit_gets_through.<locals>.raise_base   s    r   )r   r*   r    r!   assertRaisesr   r   r+   )r   r   s     r   2test_sysexcepthook_raising_SystemExit_gets_throughzITestInteractiveConsole.test_sysexcepthook_raising_SystemExit_gets_through   sK    #('	!+z*LL!!# +**s   A  A)c                 (   t        d      | j                  _        | j                  j	                  d       | j                  t        | j                  j                        d       | j                  j                  d   }| j                  |ddi g       | j                  j                          t        d      | j                  _        | j                  j	                  d       | j                  t        | j                  j                        d	       y )
Nr(   Foobanner   r   rt   )zFoo
r2      
r)   r   r*   r   r+   r,   lenr   r6   
reset_mock)r   banner_calls     r   test_bannerz"TestInteractiveConsole.test_banner   s    "*:"6U+T[[556:kk..q1w
B&?@ 	 "*:"6R(T[[556:r   c                    t        d      | j                  _        | j                  j	                  d       | j                  t        | j                  j                        d       | j                  j                  d   }d}| j                  |d|fi g       | j                  j                          t        d      | j                  _        | j                  j	                  dd       | j                  t        | j                  j                        d       | j                  j                          d	}t        d      | j                  _        | j                  j	                  d|       | j                  t        | j                  j                        d       | j                  j                  d   }|d
z   }| j                  |d|fi g       y )Nr(   r2   r   r   r4   z"now exiting InteractiveConsole...
rt   )r   exitmsgu	   bye! ζжr]   r   )r   err_msgexpectedmessages       r   test_exit_msgz$TestInteractiveConsole.test_exit_msg   sk   "*:"6R(T[[556:++**1-87XK"<= 	 "*:"6R4T[[556: 	 K 	 #+:"6R9T[[556:++**1-T>7XK"<=r   c                    dt        d      g| j                  _        | j                  j	                          dj                  d | j                  j                  D              }t        d      }| j                  ||       | j                  | j                  j                  t               | j                  t        | j                  j                        t               | j                  | j                  j                   | j                  j                  j"                         | j%                  | j                  j                          | j                  | j                  j&                  | j                  j                         y )Nz(raise ValueError('') from AttributeErrorr(   r2   c              3   D   K   | ]  }d j                  |d           ywr?   r@   rA   s     r   rC   z7TestInteractiveConsole.test_cause_tb.<locals>.<genexpr>   rD   rE   z
        AttributeError

        The above exception was the direct cause of the following exception:

        Traceback (most recent call last):
          File "<console>", line 1, in <module>
        ValueError
        )r)   r   r*   r   r+   r7   r   r6   r   r   rJ   r    rK   rf   rM   rN   rP   rQ   assertIsNotNonerR   r   rT   r   s      r   test_cause_tbz$TestInteractiveConsole.test_cause_tb   s    #M$,Z$8#:Odkk6N6NOO   	h'dkk++Z8d4;;112J?dkk00$++2H2H2V2VWT[[778dkk**DKK,B,BCr   c                    dt        d      g| j                  _        | j                  j	                          dj                  d | j                  j                  D              }t        d      }| j                  ||       | j                  | j                  j                  t               | j                  t        | j                  j                        t               | j                  | j                  j                   | j                  j                  j"                         | j%                  | j                  j                          | j                  | j                  j&                  | j                  j                         y )Nztry: ham
except: eggs
r(   r2   c              3   D   K   | ]  }d j                  |d           ywr?   r@   rA   s     r   rC   z9TestInteractiveConsole.test_context_tb.<locals>.<genexpr>  rD   rE   ah  
        Traceback (most recent call last):
          File "<console>", line 1, in <module>
        NameError: name 'ham' is not defined

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "<console>", line 2, in <module>
        NameError: name 'eggs' is not defined
        )r)   r   r*   r   r+   r7   r   r6   r   r   rJ   r    rK   	NameErrorrM   rN   rP   rQ   r   rR   r   s      r   test_context_tbz&TestInteractiveConsole.test_context_tb  s    #=$,Z$8#:Odkk6N6NOO 
 
 	h'dkk++Y7d4;;112I>dkk00$++2H2H2V2VWT[[778dkk**DKK,B,BCr   )__name__
__module____qualname__maxDiffr   r   r-   r0   r:   rU   rZ   ra   rk   rm   ro   r   r   r   r   r   r   r    r   r   r	   r	      sf    G 776D(D D$...6&	11$;>8D*Dr   r	   __main__)__doc__r   rg   unittesttextwrapr   
contextlibr   r   test.supportr   import_moduler   TestCaser	   r   mainr   r   r   <module>r      sb    E 
       & #}""6*WDX.. WDt zHMMO r   