
    }e.                     \   d Z ddlZddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZ ddlmZmZ ddlmZ ddlmZ ddlmZ d	Zd
Zd Z G d dej4                        ZdZ G d dej4                        Z G d dej4                        ZdZ G d dej4                        Z dZ!dZ" G d dej4                        Z#ddlZ ejH                         dk7  r	  G d dej4                        Z% G d dej4                        Z& G d d ej4                        Z' G d! d"ej4                        Z(d# Z)y)$zTests for IPython.core.ultratb
    N)dedent)TemporaryDirectory)ColorTB	VerboseTB)tools)onlyif_unicode_paths)prepended_to_syspathz1
2
3
def f():
  1/0
zdef f():
  1/0
c                       fd}|S )z:
    decorator to set the recursion limit temporarily
    c                       fd}|S )Nc                      t        j                         }t        j                         	  | i |t        j                  |       S # t        j                  |       w xY wN)sysgetrecursionlimitsetrecursionlimit)argskwargsrlframestest_functions      A/usr/lib/python3/dist-packages/IPython/core/tests/test_ultratb.pywrapperz.recursionlimit.<locals>.inner.<locals>.wrapper&   sN    &&(B!!&)*$d5f5%%b)%%b)s   A	 	A  )r   r   r   s   ` r   innerzrecursionlimit.<locals>.inner%   s    	*     r   )r   r   s   ` r   recursionlimitr       s    
	 Lr   c                       e Zd Zd Zy)ChangedPyFileTestc                    t               5 }t        j                  j                  |d      }t	        |dd      5 }|j                  t               ddd       t        |      5  t        j                  d       ddd       t        j                  d      5  t        j                  d       ddd       t	        |dd      5 }|j                  t               ddd       t        j                  d	d
      5  t        j                  d      5  t        j                  d       ddd       t        j                  d      5  t        j                  d       ddd       ddd       ddd       y# 1 sw Y   !xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   XxY w# 1 sw Y   \xY w# 1 sw Y   yxY w)zTraceback produced if the line where the error occurred is missing?

        https://github.com/ipython/ipython/issues/1456
        foo.pywutf-8encodingN
import fooZeroDivisionErrorfoo.f()zInternal Python errorstderr)channel)r   ospathjoinopenwritefile_1r	   iprun_cellttAssertPrintsfile_2AssertNotPrintsselftdfnamefs       r   test_changing_py_filez'ChangedPyFileTest.test_changing_py_file4   sw   
  ! 	+RGGLLX.EeS73  q  &b) *L)* !45 'I&' eS73  q 
 ##$;XN +__%89 +KK	*+__%89 +KK	*++#	+ 	+   * *' '   + ++ ++ +#	+ 	+s   /GE/G#E<9GF	,GFG7F9F!#F9 F-F9G/E9	4G<F	G	F	GF	G!F*&F9-F62F99G	>GGN)__name__
__module____qualname__r:   r   r   r   r   r   3   s    +r   r   uL   # coding: iso-8859-5

def fail():
    """дбИЖ"""
    1/0     # дбИЖ
c                   (    e Zd Zed        Zd Zd Zy)NonAsciiTestc                    t        d      5 }t        j                  j                  |d      }t	        |dd      5 }|j                  t               d d d        t        |      5  t        j                  d       d d d        t        j                  d      5  t        j                  d	       d d d        d d d        y # 1 sw Y   nxY w# 1 sw Y   QxY w# 1 sw Y   *xY w# 1 sw Y   y xY w)
N   é)suffixu   fooé.pyr    r!   r"   r$   r%   r&   )r   r)   r*   r+   r,   r-   r.   r	   r/   r0   r1   r2   r5   s       r   test_nonascii_pathzNonAsciiTest.test_nonascii_pathX   s      u- 		'GGLL[1EeS73  q  &b) *L)* !45 'I&'		' 		'   * *' '		' 		'sR   /C#B?C#%C;C#C.C#?C	C#C	C#C 	C##C,c                 F   t               5 }t        j                  j                  |d      }t	        j
                  |dd      5 }|j                  t               d d d        t        |      5  t        j                  d       d d d        t        j                  d      5  t        j                  dd	      5  t        j                  d
       d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   qxY w# 1 sw Y   2xY w# 1 sw Y   6xY w# 1 sw Y   y xY w)Nz
dfghjkl.pyr    z
iso-8859-5r"   zfrom dfghjkl import failr%   u   дбИЖFsuppresszfail())r   r)   r*   r+   ior,   r-   iso_8859_5_filer	   r/   r0   r1   r2   r5   s       r   test_iso8859_5zNonAsciiTest.test_iso8859_5f   s    ! 	*RGGLL\2El; )q() &b) 8678 !45 *__[5A *KK)**	* 	*) )8 8* ** *	* 	*sk   9DC'D-C3D D8C?DD'C0	,D3C<	8D?DDD	DD c                    d}d}t         j                  d       t        j                  |      5  t         j                  |       d d d        t         j                  d       t        j                  |      5  t         j                  |       d d d        t         j                  d       t        j                  |      5  t         j                  |       d d d        t         j                  d       t        j                  d      5  t         j                  |       d d d        t         j                  d       y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   CxY w)Nu   raise Exception('é')u   Exception('é')z%xmode plainz%xmode verbosez%xmode contextz%xmode minimalu   Exception: é)r/   r0   r1   r2   )r6   cellexpecteds      r   test_nonascii_msgzNonAsciiTest.test_nonascii_msgt   s	   '%
N#__X& 	KK	 	$%__X& 	KK	 	$%__X& 	KK	 	$%__-. 	KK	 	$%!	 		 		 		 	s/   D;7E?EE;EEEE(N)r;   r<   r=   r   rC   rI   rM   r   r   r   r?   r?   W   s    ' '*&r   r?   c                       e Zd ZdZd Zy)NestedGenExprTestCasez
    Regression test for the following issues:
    https://github.com/ipython/ipython/issues/8293
    https://github.com/ipython/ipython/issues/8205
    c                     t        d      }t        j                  dd      5  t        j	                  |       d d d        y # 1 sw Y   y xY w)Nz            class SpecificException(Exception):
                pass

            def foo(x):
                raise SpecificException("Success!")

            sum(sum(foo(x) for _ in [0]) for x in [0])
            zSpecificException: Success!FrE   )r   r1   r2   r/   r0   )r6   codes     r   test_nested_genexprz)NestedGenExprTestCase.test_nested_genexpr   sC    

 __:UK 	KK	 	 	s   AAN)r;   r<   r=   __doc__rR   r   r   r   rO   rO      s    
r   rO   zif True:
zoon()
c                       e Zd Zd Zy)IndentationErrorTestc                    t        j                  d      5  t        j                  dd      5  t        j                  t               d d d        d d d        t               5 }t        j                  j                  |d      }t        |dd      5 }|j                  t               d d d        t        j                  d      5  t        j                  dd      5  t        j                  d	|z         d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   >xY w# 1 sw Y   BxY w# 1 sw Y   y xY w)
NIndentationErrorzzoon()FrE   r   r    r!   r"   zrun %s)r1   r2   r/   r0   indentationerror_filer   r)   r*   r+   r,   r-   magicr5   s       r    test_indentationerror_shows_linez5IndentationErrorTest.test_indentationerror_shows_line   s   __/0 	3E: 3123	3  ! 	/RGGLLX.EeS73 /q-./ !34 /__X> /HHX-.//	/ 	/3 3	3 	3/ // // /	/ 	/sv   DDD"/E
D&'E
D>D25D>=E
D	DD#&D/	+E
2D;7D>>E	E

EN)r;   r<   r=   rZ   r   r   r   rU   rU      s    /r   rU   z1
2
7/
z7/
c                   $    e Zd Zd Zd Zd Zd Zy)SyntaxErrorTestc                    d}t        j                  d      5  t        j                  |       d d d        t        j                  d      5  t        j                  |       d d d        y # 1 sw Y   =xY w# 1 sw Y   y xY w)Nz
def foo():
    ..
SyntaxErrorfoo())r1   r2   r/   r0   r4   )r6   syntax_error_at_compile_times     r   .test_syntaxerror_no_stacktrace_at_compile_timez>SyntaxErrorTest.test_syntaxerror_no_stacktrace_at_compile_time   sq    ($ __]+ 	6KK45	6 ( 	6KK45	6 	6	6 	6	6 	6s   A*A6*A36A?c                 L   d}t        j                  d      5  t        j                  |       d d d        t        j                  ddg      5  t        j                  |       d d d        t        j                  d= t        j                  d= y # 1 sw Y   axY w# 1 sw Y   8xY w)Nz8
def foo():
    eval("..")

def bar():
    foo()

bar()
r^   r_   zbar()barfoo)r1   r2   r/   r0   user_ns)r6   syntax_error_at_runtimes     r   6test_syntaxerror_stacktrace_when_running_compiled_codezFSyntaxErrorTest.test_syntaxerror_stacktrace_when_running_compiled_code   s    # __]+ 	1KK/0	1 __gw/0 	1KK/0	1JJuJJu	1 	1	1 	1s   BBBB#c                 r   t               5 }t        j                  j                  |d      }t	        |dd      5 }|j                  t               d d d        t        j                  ddg      5  t        j                  d|z          d d d        t	        |dd      5 }|j                  t               d d d        t        j                  ddg      5  t        j                  d|z          d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   bxY w# 1 sw Y   6xY w# 1 sw Y   y xY w)Nr   r    r!   r"   z7/r^   zrun )r   r)   r*   r+   r,   r-   	se_file_1r1   r2   r/   rY   	se_file_2r5   s       r   r:   z%SyntaxErrorTest.test_changing_py_file   s   ! 	)RGGLLX.EeS73 #q	"# $!67 )%() eS73 #q	"# $!67 )%()	) 	)# #) )# #) )	) 	)sj   /D-C=D-/D	D-D4D-D!,D-=D	D-	D	D-D	D-!D*	&D--D6c                     	 t        d      # t         $ rB t        j                  d      5  t        j	                          d d d        Y y # 1 sw Y   Y y xY ww xY w)NQWERTY)
ValueErrorr1   r2   r/   showsyntaxerrorr6   s    r   test_non_syntaxerrorz$SyntaxErrorTest.test_non_syntaxerror   sN    	%X&& 	%* %""$% % %	%s&    AA
 A
A	AAN)r;   r<   r=   ra   rg   r:   rp   r   r   r   r\   r\      s    	6$)"%r   r\   PyPyc                       e Zd Zd Zy)MemoryErrorTestc                 2    d}t         j                  |       y )Na  (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))r/   r0   )r6   memoryerror_codes     r   test_memoryerrorz MemoryErrorTest.test_memoryerror   s    4KK()r   N)r;   r<   r=   rv   r   r   r   rs   rs      s    	*r   rs   c                   <    e Zd ZdZdZdZd Zd Zd Zd Z	d Z
d	 Zy
)Python3ChainedExceptionsTestz
try:
    x = 1 + 2
    print(not_defined_here)
except Exception as e:
    x += 55
    x - 1
    y = {}
    raise KeyError('uh') from e
    z
try:
    x = 1 + 2
    print(not_defined_here)
except Exception as e:
    x += 55
    x - 1
    y = {}
    raise KeyError('uh')
    zL
try:
    1/0
except Exception:
    raise ValueError("Yikes") from None
    c                     t        j                  g d      5  t        j                  | j                         d d d        y # 1 sw Y   y xY w)NKeyError	NameErrorzdirect causer1   r2   r/   r0   DIRECT_CAUSE_ERROR_CODEro   s    r   test_direct_cause_errorz4Python3ChainedExceptionsTest.test_direct_cause_error  s6    __FG 	6KK445	6 	6 	6    AA
c                     t        j                  g d      5  t        j                  | j                         d d d        y # 1 sw Y   y xY w)Nr{   r|   zDuring handlingr1   r2   r/   r0   EXCEPTION_DURING_HANDLING_CODEro   s    r   $test_exception_during_handling_errorzAPython3ChainedExceptionsTest.test_exception_during_handling_error#  s6    __IJ 	=KK;;<	= 	= 	=r   c                     t        j                  d      5  t        j                  dd      5  t        j	                  | j
                         d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr%   rm   FrE   r1   r4   r2   r/   r0   SUPPRESS_CHAINING_CODEro   s    r    test_suppress_exception_chainingz=Python3ChainedExceptionsTest.test_suppress_exception_chaining'  s[     34 	5__\E:	5KK334	5 	5 	5 	5 	5 	5s"   A+ AA+A(	$A++A4c                     t        j                  g d      5  t        j                  d       t        j                  | j                         t        j                  d       d d d        y # 1 sw Y   y xY w)Nrz   %xmode Plain%xmode Verboser}   ro   s    r   test_plain_direct_cause_errorz:Python3ChainedExceptionsTest.test_plain_direct_cause_error,  sO    __FG 	*KK'KK445KK()	* 	* 	*   A
A++A4c                     t        j                  g d      5  t        j                  d       t        j                  | j                         t        j                  d       d d d        y # 1 sw Y   y xY w)Nr   r   r   r   ro   s    r   *test_plain_exception_during_handling_errorzGPython3ChainedExceptionsTest.test_plain_exception_during_handling_error2  sO    __IJ 	*KK'KK;;<KK()	* 	* 	*r   c                 B   t        j                  d      5  t        j                  dd      5  t        j	                  d       t        j	                  | j
                         t        j	                  d       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr%   rm   FrE   r   r   r   ro   s    r   &test_plain_suppress_exception_chainingzCPython3ChainedExceptionsTest.test_plain_suppress_exception_chaining8  st     34 	*__\E:	*KK'KK334KK()		* 	* 	* 	* 	* 	*s#   BA
B	8B	B	BBN)r;   r<   r=   r~   r   r   r   r   r   r   r   r   r   r   r   rx   rx     s8    		&"6=5
***r   rx   c                   T    e Zd ZdZd Zd Z ed      d        Z ed      d        Zy)	RecursionTestz
def non_recurs():
    1/0

def r1():
    r1()

def r3a():
    r3b()

def r3b():
    r3c()

def r3c():
    r3a()

def r3o1():
    r3a()

def r3o2():
    r3o1()
c                 B    t         j                  | j                         y r   )r/   r0   DEFINITIONSro   s    r   setUpzRecursionTest.setUpW  s    
D$$%r   c                     t        j                  d      5  t        j                  d       d d d        y # 1 sw Y   y xY w)Nzskipping similar framesznon_recurs())r1   r4   r/   r0   ro   s    r   test_no_recursionzRecursionTest.test_no_recursionZ  s2     9: 	(KK'	( 	( 	(s   5>   c                     t        j                  t        j                  d            5  t        j                  d       d d d        y # 1 sw Y   y xY w)NzB\[\.\.\. skipping similar frames: r1 at line 5 \(\d{2,3} times\)\]zr1()r1   r2   recompiler/   r0   ro   s    r   test_recursion_one_framez&RecursionTest.test_recursion_one_frame^  s@    __RZZQS 
 	  KK	  	  	 s   AA   c           	         t        j                  d      5  t        j                  t        j                  d      d      5  t        j                  t        j                  d      d      5  t        j                  t        j                  d      d      5  t        j                  d       d d d        d d d        d d d        d d d        y # 1 sw Y   "xY w# 1 sw Y   &xY w# 1 sw Y   *xY w# 1 sw Y   y xY w)Nz[... skipping similar frames: zr3a at line 8 \(\d{2} times\)FrE   zr3b at line 11 \(\d{2} times\)zr3c at line 14 \(\d{2} times\)zr3o2()r   ro   s    r   test_recursion_three_framesz)RecursionTest.test_recursion_three_framese  s    __=> 	"

+K LW\]	"

+L MX]^	" 

+L MX]^	" KK!		" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"sS   +C2+C&,+CC	-C5C&=C2CCC#C&&C/	+C22C;N)	r;   r<   r=   r   r   r   r   r   r   r   r   r   r   r   @  sE    K,&( C    C" "r   r   c                       e Zd ZdZd Zd Zy)PEP678NotesReportingTestz
try:
    raise AssertionError("Message")
except Exception as e:
    try:
        e.add_note("This is a PEP-678 note.")
    except AttributeError:  # Python <= 3.10
        e.__notes__ = ("This is a PEP-678 note.",)
    raise
    c                     t        j                  g d      5  t        j                  | j                         d d d        y # 1 sw Y   y xY w)NAssertionErrorMessagezThis is a PEP-678 note.r1   r2   r/   r0   ERROR_WITH_NOTEro   s    r   test_verbose_reports_notesz3PEP678NotesReportingTest.test_verbose_reports_notesz  s6    __UV 	.KK,,-	. 	. 	.r   c                     t        j                  g d      5  t        j                  d       t        j                  | j                         t        j                  d       d d d        y # 1 sw Y   y xY w)Nr   r   r   r   ro   s    r   test_plain_reports_notesz1PEP678NotesReportingTest.test_plain_reports_notes~  sO    __UV 	*KK'KK,,-KK()	* 	* 	*r   N)r;   r<   r=   r   r   r   r   r   r   r   r   n  s    	O.*r   r   c                     fd} dfd	t               fdt        j                         }|j                  d       |j                  d       	 |j                   | dd             t        |	      }|j                  d
       	 |j                   | dd             |j                  d       t        |	      }|j                  d       	 |j                   | dd             |j                  d       y #  t	        j
                  |       Y xY w#   |t        j                           Y xY w#   |t        j                           Y hxY w)Nc                 6    |\  }}| |z   }| |z  } ||       y r   r   )cd_edexyrd   s         r   spamztest_handlers.<locals>.spam  s'    AEEAq	r      c                      | ||z          y r   r   )abrc   eggss      r   rd   ztest_handlers.<locals>.foo  s    QCr   c                      | |z   }| |z
  }||z  S r   r   )r9   gzhis        r   r   ztest_handlers.<locals>.eggs  s    EE1ur    z*** Before ***)      )file)ostreamz*** ColorTB ***z*** VerboseTB ***)r   )
globalsrG   StringIOr-   	traceback	print_excr   r   exc_infor   )r   buffhandlerr   rd   s      @@r   test_handlersr     s
    Y 
 ;;=DJJrNJJ '

46?# d#GJJ !!

46?# 	JJrN%GJJ"#!

46?# 	JJrN#'&! ! s$   C9 D D4 9DD14E)*rS   rG   os.pathr)   platformr   r   r   unittesttextwrapr   tempfiler   IPython.core.ultratbr   r   IPython.testingr   r1   IPython.testing.decoratorsr   IPython.utils.syspathcontextr	   r.   r3   r   TestCaser   rH   r?   rO   rX   rU   ri   rj   r\   python_implementationrs   rx   r   r   r   r   r   r   <module>r      s)   	   	 
    ' 3 ' ; =


&+)) +:1&8$$ 1&fH-- , /8,, / 	
	7%h'' 7%r !8!!#v-*(++ *<*8#4#4 <*~+"H%% +"\*x00 *4&r   