B
    18\                 @   s   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m	Z	m
Z
mZmZmZmZmZmZmZmZmZ G dd deZG dd deZG dd deZG d	d
 d
ejZG dd dejZedkre  dS )    N)TESTFNcaptured_stderrcheck_impl_detailcheck_warningscpython_only
gc_collectrun_unittest
no_tracingunlinkimport_modulescript_helperSuppressCrashReportc               @   s   e Zd Zdd ZdS )NaiveExceptionc             C   s
   || _ d S )N)x)selfr    r   X/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/test_exceptions.py__init__   s    zNaiveException.__init__N)__name__
__module____qualname__r   r   r   r   r   r      s   r   c               @   s   e Zd ZdZdd ZdS )SlottedNaiveException)r   c             C   s
   || _ d S )N)r   )r   r   r   r   r   r      s    zSlottedNaiveException.__init__N)r   r   r   	__slots__r   r   r   r   r   r      s   r   c               @   s   e Zd Zdd ZdS )BrokenStrExceptionc             C   s   t dd S )Nzstr() is broken)	Exception)r   r   r   r   __str__   s    zBrokenStrException.__str__N)r   r   r   r   r   r   r   r   r      s   r   c               @   s  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zedd Z	dd Z
eejdkddd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zed#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Z d3d4 Z!d5d6 Z"d7d8 Z#d9d: Z$d;d< Z%d=d> Z&d?d@ Z'dAdB Z(dCdD Z)dEdF Z*dGdH Z+dIdJ Z,edKdL Z-edMdN Z.edOdP Z/edQdR Z0edSdT Z1edUdV Z2edWdX Z3edYdZ Z4d[d\ Z5d]d^ Z6d_d` Z7edadb Z8dcdd Z9dedf Z:dgdh Z;diS )jExceptionTestsc          
   C   s   y|dW n* |k
r6 } zt |}W d d }~X Y nX y|dW n* |k
rn } zt |}W d d }~X Y nX | || | |j| d S )NZspam)strassertEqualr   )r   excexcnameerrZbuf1Zbuf2r   r   r   raise_catch    s    zExceptionTests.raise_catchc          
   C   s  |  td | tttd |  td ttd}|  ttd}tj	}z0ydd l
}|d W n tk
rv   Y nX W d |t_	|  tt X |  td | ttd	d |  td
 | ttd |  td g }| t|jd |  td i }| t|jd |  td |  td |  td yt}W n tk
rL   Y nX |  td d}xtdD ]}||7 }qhW |  td |  td |  td ytd W n tk
r   Y nX |  td |  td ytddd W n tk
r    Y nX |  d |  t!d  |  t"d! | t"tj#d |  t$d" yg d#  W n t$k
rb   Y nX |  t%d$ | t%t&d% |  t'd& ydd }W n t'k
r   Y nX |  t(d' ydd }W n$ t(k
r } zW d d }~X Y nX |  t)d( d S ))NAttributeErrorZundefined_attributeEOFErrorwrr       OSErrorzthis file does not existImportErrorZundefined_module
IndexError
   KeyErrorkeyKeyboardInterruptMemoryError	NameErrorOverflowError      RuntimeErrorRecursionErrorSyntaxErrorz/
IndentationErrorTabErrorz"try:
	1/0
    	1/0
finally:
 pass
z<string>execzTabError not raisedSystemError
SystemExit	TypeErrorr   
ValueErrori   ZeroDivisionErrorr   StopAsyncIteration)*r"   r#   assertRaisesgetattrsysr$   openr   closestdinmarshalloadsr
   r(   r)   
__import__r*   __getitem__r,   r.   r/   r0   Zundefined_variabler1   ranger4   r5   r6   r9   r7   r8   compilefailr:   r;   exitr<   r=   chrr>   r   r?   )r   fpZ	savestdinrF   r   dummyer   r   r   testRaising,   s    



      
      zExceptionTests.testRaisingc                sB    fdd}d}t jds&||d d}||d |dd d S )	Nc          
      s`   yt | dd W n@ tk
rP } z"|j|kr@ d||jf  W d d }~X Y nX  d d S )Nz
<fragment>r9   zexpected %s, got %sz"failed to get expected SyntaxError)rK   r6   msgrL   )srcrS   rQ   )r   r   r   ckmsg   s    
&z4ExceptionTests.testSyntaxErrorMessage.<locals>.ckmsgz\while 1:
            try:
                pass
            finally:
                continuejavaz0'continue' not supported inside 'finally' clausezHif 1:
        try:
            continue
        except:
            passz'continue' not properly in loopz	continue
)rB   platform
startswith)r   rU   sr   )r   r   testSyntaxErrorMessage   s    

z%ExceptionTests.testSyntaxErrorMessagec                s^   t f fdd	}d}||d d}||d d}||d d	}||d
t d}||dt d S )Nc          
      s`   yt | dd W n@ |k
rP } z"|j|kr@ d||jf  W d d }~X Y nX  d d S )Nz
<fragment>r9   zexpected %s, got %sz"failed to get expected SyntaxError)rK   rS   rL   )rT   rS   	exceptionrQ   )r   r   r   rU      s    
&z:ExceptionTests.testSyntaxErrorMissingParens.<locals>.ckmsgzprint "old style"zHMissing parentheses in call to 'print'. Did you mean print("old style")?zprint "old style",zQMissing parentheses in call to 'print'. Did you mean print("old style", end=" ")?zexec "old style"z%Missing parentheses in call to 'exec'zif True:
print "No indent"zexpected an indented blockz6if True:
        print()
	exec "mixed tabs and spaces"z2inconsistent use of tabs and spaces in indentation)r6   r7   r8   )r   rU   rY   r   )r   r   testSyntaxErrorMissingParens   s    	


z+ExceptionTests.testSyntaxErrorMissingParensc                sL    fdd}|ddd |ddd |d	d
d |ddd |ddd d S )Nc          	      sF     t}t| dd W d Q R X  |jj|  |jj| d S )Nz
<fragment>r9   )r@   r6   rK   r   r[   linenooffset)rT   r]   r^   cm)r   r   r   check   s    z3ExceptionTests.testSyntaxErrorOffset.<locals>.checkzdef fact(x):
	return x!
   r+   z1 +
r2      z def spam():
  print(1)
 print(2)   zPython = "Python" +   u   Python = "Ṕýţĥòñ" +r   )r   r`   r   )r   r   testSyntaxErrorOffset   s    z$ExceptionTests.testSyntaxErrorOffsetc                sj   G dd dt  G dd d fdd} fdd}fd	d
}tjdsf|  |  |  d S )Nc               @   s   e Zd Zdd ZdS )z9ExceptionTests.testSettingException.<locals>.BadExceptionc             S   s   t dd S )Nzcan't instantiate BadException)r4   )Zself_r   r   r   r      s    zBExceptionTests.testSettingException.<locals>.BadException.__init__N)r   r   r   r   r   r   r   r   BadException   s   rf   c               @   s   e Zd ZdS )z=ExceptionTests.testSettingException.<locals>.InvalidExceptionN)r   r   r   r   r   r   r   InvalidException   s   rg   c           
      s   dd l } y|  d W nX tk
rp } z:t \}}}|jj}|jd 	|j
d W d d }~X Y nX d d S )Nr   r2   
test_capi1ztest_exceptions.pyzExpected exception)	_testcapiraise_exceptionr<   rB   exc_infotb_framef_coder   co_name
assertTrueco_filenameendswithrL   )ri   r!   r   tbco)rf   r   r   r   rh      s    $z7ExceptionTests.testSettingException.<locals>.test_capi1c           
      s   dd l } y|  d W np tk
r } zRt \}}}|jj}|jd 	|j
d |jjj}|jd W d d }~X Y nX d d S )Nr   r   ztest_exceptions.py
test_capi2zExpected exception)ri   rj   r4   rB   rk   rl   rm   r   rn   ro   rp   rq   f_backrL   )ri   r!   r   rr   rs   Zco2)rf   r   r   r   rt      s    
 z7ExceptionTests.testSettingException.<locals>.test_capi2c                 s   dd l } t| j d d S )Nr   r2   )ri   r@   r:   rj   )ri   )rg   r   r   r   
test_capi3   s    
z7ExceptionTests.testSettingException.<locals>.test_capi3rV   )r   rB   rW   rX   )r   rh   rt   rv   r   )rf   rg   r   r   testSettingException   s    z#ExceptionTests.testSettingExceptionc             C   s  yt  W n tk
r   Y nX | t t | ttdd | ttddd tddd}| |jd | |jd  | t|d td	ddd
}| |jd | |jd
 | |jd | |j	d | |j
d  | t|d td	dd d}| |jd | |jd | |jd | |j	d  | |j
d  | t|d tdd}| |jd | |jd  | |jd | |j	d  | |j
d  d S )Ni  Z1001messagez[Errno 1001] message	   foobarz[Errno 9] foo: 'bar'r   rc   ra   z[WinError 3] foo: 'bar'   z[WinError 1001] foo)WindowsErrorr0   assertIsr(   r   r   errnowinerrorstrerrorfilename	filename2)r   r%   r   r   r   test_WindowsError   s@    
z ExceptionTests.test_WindowsErrorwin32ztest specific to Windowsc          	   C   s8   t d}d}| td|  |j| W dQ R X dS )z:Should fill in unknown error code in Windows error messagectypesl   cs@ zWindows Error 0x%xN)r   assertRaisesRegexr(   Z	pythonapiZPyErr_SetFromWindowsErr)r   r   coder   r   r   test_windows_message  s    z#ExceptionTests.test_windows_messagec             C   s"  t dddift dddift dddift dddiftddddftddd d d d dftd	d	d d dd
dftdd	dd dd
dftdd	dddftddddddftdddddd dftdd d d d d d dftddd d dd d d dftddd dd ddd!d"ftd#d d#d dd d d d$ftdddiftd%d%d&d'd(d)d*ftd&td+d(dd)fd&td+d(dd)fd&d+d(d)d*ftd,d,d&d+d(d)d*ftd-d-d.d/d(dd0ft	dddd1ft
dddd1fg}y"|tdddd ddd d2f W n tk
r   Y nX x:|D ]0\}}}y|| }W n$   td3||f tjd4  Y nX t|jd5sJ| t|jd6 t|}xB|D ]:}t||}| t|t|| d7||||| f  qXW xtgD ]v}	xnt|	jd D ]\}
|	||
}|	|}x>|D ]6}tt||}t|| }| ||d8||f  qW qW qW qW d S )9Nr   args)r2   )rz   )rz   r2   rz   )r   r   )r   r   r   r   r   )rz   r{   r{   )rz   r{   bazr   )rz   r{   r   Nquuxr   )r   r   r   )errnoStrstrErrorStrfilenameStr)r   r   r   r   r   )r   r   r   r   )r2   r   r   )r2   r   r2   )r   r   r   r   r   )rS   textr   r]   r^   print_file_and_line)msgStrr   )r   r   r   rS   r   r]   r^   )r   )r   	linenoStr	offsetStrtextStrr   r   r   )r^   r   r   r   rS   r   r]   )r   r   r   r   r   Zprint_file_and_lineStr)r   r   r   rS   r   r]   r^   )asciiar   r2   zordinal not in ranger   r   r   zordinal not in range)r   encodingobjectstartreason   )r   r   r   r2   zordinal not in range)u   あr   r2   ouchu   あr   )r   r   r   r   end)r   r   )r   r   r   r   r   r   z
exc=%r, args=%r)filer   builtinsz%r.%s == %r, expected %rzpickled "%r", attribute "%s)BaseExceptionr;   r(   r6   UnicodeErrorUnicodeEncodeErrorUnicodeDecodeError	bytearrayUnicodeTranslateErrorr   r   appendr}   r0   printrB   stderrtyper   rq   r   r   r   rA   reprpicklerJ   HIGHEST_PROTOCOLdumpsrG   )r   ZexceptionListr   r   expectedrQ   rY   ZcheckArgNamevaluepprotocolnewZgotZwantr   r   r   testAttributes)  s    
















zExceptionTests.testAttributesc             C   s   yt dW n   t d }Y nX t |}| |t | |j| t d|}| |t  | |j| G dd dt}| |}| || | |j| d S )Nrb   ra      c               @   s   e Zd ZdS )z5ExceptionTests.testWithTraceback.<locals>.MyExceptionN)r   r   r   r   r   r   r   MyException  s   r   )	r*   rB   rk   r   with_tracebackassertIsInstancer   __traceback__r   )r   rr   rQ   r   r   r   r   testWithTraceback  s    z ExceptionTests.testWithTracebackc          
   C   sN   ydt  _W n2 tk
r> } z| dt| W d d }~X Y nX | d d S )Nr   z!__traceback__ must be a tracebackzNo exception raised)r   r   r<   assertInr   rL   )r   rQ   r   r   r   testInvalidTraceback  s
    "z#ExceptionTests.testInvalidTracebackc             C   sP   |  ttt dd |  ttt d |  ttt dd |  ttt d d S )N	__cause__r2   __context__)r@   r<   setattrr   delattr)r   r   r   r   testInvalidAttrs  s    zExceptionTests.testInvalidAttrsc             C   sJ   yt dW n   t d }Y nX t }||_d |_| |jd  d S )Nrb   ra   )r*   rB   rk   r   r   r   )r   rr   rQ   r   r   r   testNoneClearsTracebackAttr  s    z*ExceptionTests.testNoneClearsTracebackAttrc             C   sn   t  }| |j | |j t }| |j | |j G dd dt}| }| |j | |j d S )Nc               @   s   e Zd ZdS )z5ExceptionTests.testChainingAttrs.<locals>.MyExceptionN)r   r   r   r   r   r   r   r     s   r   )r   assertIsNoner   r   r<   r(   )r   rQ   r   r   r   r   testChainingAttrs  s    z ExceptionTests.testChainingAttrsc          
   C   s   y
t  W n& t k
r0 } z|}W d d }~X Y nX | |j | |j | |j t |_d |_| |jt | |j | |j d|_| |j d S )NF)	r   r   r   r   assertFalse__suppress_context__r0   r   ro   )r   r   rQ   r   r   r   testChainingDescriptors  s    
z&ExceptionTests.testChainingDescriptorsc             C   s<   | j ttdd G dd dt}|dd}| |jd d S )Nr2   )r   c               @   s   e Zd Zdd ZdS )z8ExceptionTests.testKeywordArgs.<locals>.DerivedExceptionc             S   s   t |  || _d S )N)r   r   	fancy_arg)r   r   r   r   r   r     s    
zAExceptionTests.testKeywordArgs.<locals>.DerivedException.__init__N)r   r   r   r   r   r   r   r   DerivedException  s   r   *   )r   )r@   r<   r   r   r   )r   r   r   r   r   r   testKeywordArgs  s    
zExceptionTests.testKeywordArgsc                s4    fdd |  t  fdd|  t d S )Nc                  s     S )Nr   r   )fr   r   r     s    z/ExceptionTests.testInfiniteRecursion.<locals>.fc                  s    y  S  t k
r   dS X d S )N)r=   r   )gr   r   r   
  s    z/ExceptionTests.testInfiniteRecursion.<locals>.g)r@   r5   )r   r   )r   r   r   testInfiniteRecursion  s    z$ExceptionTests.testInfiniteRecursionc             C   s8   |  tt |  ttd |  ttdd d S )Nr   b)ro   r   r   )r   r   r   r   test_str  s    zExceptionTests.test_strc          
   C   sL   y
t  W n. t k
r8 } z| | ~W d d }~X Y nX | dt  d S )NrQ   )r   ro   assertNotInlocals)r   rQ   r   r   r   testExceptionCleanupNames  s    

z(ExceptionTests.testExceptionCleanupNamesc                s  G dd dt  G dd d} fdd}| t}y
|  W n"  k
rh } zW d d }~X Y nX d | |  | t}y
|  W n  k
r   Y nX d | |  | t}y
|  W n   Y nX d | |  | t}x(dD ] }y
|  W n   P Y nX qW d | |  | t}y y
|  W n   tY nX W nP tk
r } z0d |_d | tdd	rt  |  W d d }~X Y nX | t}y
|  W n>  k
r&   yz W d  X W n  k
r    Y nX Y nX d tdd	r>t  | |  G d
d d}| t}|  |  W d Q R X d tdd	rt  | |  d S )Nc               @   s   e Zd Zdd ZdS )z=ExceptionTests.testExceptionCleanupState.<locals>.MyExceptionc             S   s
   || _ d S )N)obj)r   r   r   r   r   r   &  s    zFExceptionTests.testExceptionCleanupState.<locals>.MyException.__init__N)r   r   r   r   r   r   r   r   r   %  s   r   c               @   s   e Zd ZdS )z7ExceptionTests.testExceptionCleanupState.<locals>.MyObjN)r   r   r   r   r   r   r   MyObj(  s   r   c                 s   }  d S )Nr   )Z	local_ref)r   r   r   r   inner_raising_func+  s    zDExceptionTests.testExceptionCleanupState.<locals>.inner_raising_func)r   F)cpythonc               @   s   e Zd Zdd Zdd ZdS )z9ExceptionTests.testExceptionCleanupState.<locals>.Contextc             S   s   | S )Nr   )r   r   r   r   	__enter__  s    zCExceptionTests.testExceptionCleanupState.<locals>.Context.__enter__c             S   s   dS )NTr   )r   exc_type	exc_valueexc_tbr   r   r   __exit__  s    zBExceptionTests.testExceptionCleanupState.<locals>.Context.__exit__N)r   r   r   r   r   r   r   r   r   Context  s   r   )r   weakrefrefr   r,   r   r   r   )r   r   r   wrrQ   ir   r   )r   r   r   testExceptionCleanupState!  s    


















z(ExceptionTests.testExceptionCleanupStatec          
      sB    fdd}yt  W n( tk
r<   z
|  W d d   X Y nX d S )Nc                  s     d S )Nr   r   )rQ   r   r   print_error  s    zIExceptionTests.test_exception_target_in_nested_scope.<locals>.print_error)Z	somethingr   )r   r   r   )rQ   r   %test_exception_target_in_nested_scope  s
    z4ExceptionTests.test_exception_target_in_nested_scopec             C   s   dd }| }|  t|t |  t d d  |  t|t |  t d d  |  t|d  ytdW n tk
r   | }|  t|t |  t d t |  t|t |  t d t |  t|t ~|  t d t Y nX d S )Nc               s   sP   yt dW n0 t k
r<   t d V  t d V  Y nX t d V  d S )NZcaughtr   )r,   rB   rk   r   r   r   r   yield_raise  s    z:ExceptionTests.test_generator_leaking.<locals>.yield_raiser   rz   )r   nextr,   rB   rk   r<   )r   r   r   r   r   r   test_generator_leaking  s$    z%ExceptionTests.test_generator_leakingc             C   sj   dd }yt W n" t k
r2   | }t| Y nX yt| W n tk
rT   Y nX | t d d S )Nc               s   s
   d V  d S )Nr   r   r   r   r   r     s    z1ExceptionTests.test_generator_leaking2.<locals>.g)NNN)r4   r   StopIterationr   rB   rk   )r   r   itr   r   r   test_generator_leaking2  s    z&ExceptionTests.test_generator_leaking2c          
   C   s   dd }| }t | ydd  W n` tk
r } zB| t d | ||}| t d | | || W d d }~X Y nX | t d d S )Nc               s   s2   y
d V  W n" t k
r,   t d V  Y nX d S )Nr2   )r>   rB   rk   r   r   r   r   r     s    
z1ExceptionTests.test_generator_leaking3.<locals>.gr2   r   )NNN)r   r>   r~   rB   rk   throwr   )r   r   r   rQ   Zgen_excr   r   r   test_generator_leaking3  s    
z&ExceptionTests.test_generator_leaking3c          
   C   s   dd }| }yt W n t k
r2   t|}Y nX | |t yt| W n6 tk
r } z| t d | W d d }~X Y nX | t d d S )Nc               s   s6   ydd  W n$ t k
r0   t d V   Y nX d S )Nr2   r   )r>   rB   rk   r   r   r   r   r     s
    z1ExceptionTests.test_generator_leaking4.<locals>.gr2   )NNN)r<   r   r~   r>   rB   rk   r   )r   r   r   tprQ   r   r   r   test_generator_leaking4  s    &z&ExceptionTests.test_generator_leaking4c                sJ    fdd}| }yt W n t k
r6   t| Y nX  tt| d S )Nc               3   s.     t d t d V   t d d S )Nr2   )NNN)r   rB   rk   r4   r   r   )r   r   r   r     s    z>ExceptionTests.test_generator_doesnt_retain_old_exc.<locals>.g)r4   r   r@   r   )r   r   r   r   )r   r   $test_generator_doesnt_retain_old_exc  s    z3ExceptionTests.test_generator_doesnt_retain_old_excc                s4   dd   fdd}|  t   | t d d S )Nc               s   s
   dV  d S )Nr2   r   r   r   r   r   
simple_gen	  s    zIExceptionTests.test_generator_finalizing_and_exc_info.<locals>.simple_genc                 s,     } yt W n t k
r&   t| S X d S )N)r4   r   )gen)r   r   r   run_gen  s
    zFExceptionTests.test_generator_finalizing_and_exc_info.<locals>.run_gen)NNN)r   r   rB   rk   )r   r   r   )r   r   &test_generator_finalizing_and_exc_info  s
    z5ExceptionTests.test_generator_finalizing_and_exc_infoc                sn   G dd dt  G dd d} fdd}| t}| }t| || d  }| |  d S )Nc               @   s   e Zd Zdd ZdS )zFExceptionTests._check_generator_cleanup_exc_state.<locals>.MyExceptionc             S   s
   || _ d S )N)r   )r   r   r   r   r   r     s    zOExceptionTests._check_generator_cleanup_exc_state.<locals>.MyException.__init__N)r   r   r   r   r   r   r   r   r     s   r   c               @   s   e Zd ZdS )z@ExceptionTests._check_generator_cleanup_exc_state.<locals>.MyObjN)r   r   r   r   r   r   r   r     s   r   c               3   s,   y W n  k
r&   d V  Y nX d S )Nr   r   )r   r   r   r   raising_gen  s    zFExceptionTests._check_generator_cleanup_exc_state.<locals>.raising_gen)r   r   r   r   r   )r   Ztestfuncr   r   r   r   r   )r   r   r   "_check_generator_cleanup_exc_state  s    
z1ExceptionTests._check_generator_cleanup_exc_statec             C   s   dd }|  | d S )Nc             S   s*   y|  t  W n tk
r$   Y nX d S )N)r   r4   )r   r   r   r   do_throw.  s    zGExceptionTests.test_generator_throw_cleanup_exc_state.<locals>.do_throw)r   )r   r   r   r   r   &test_generator_throw_cleanup_exc_state-  s    z5ExceptionTests.test_generator_throw_cleanup_exc_statec             C   s   dd }|  | d S )Nc             S   s   |    d S )N)rD   )r   r   r   r   do_close6  s    zGExceptionTests.test_generator_close_cleanup_exc_state.<locals>.do_close)r   )r   r   r   r   r   &test_generator_close_cleanup_exc_state5  s    z5ExceptionTests.test_generator_close_cleanup_exc_statec             C   s   dd }|  | d S )Nc             S   s   d } d S )Nr   )r   r   r   r   do_del;  s    zCExceptionTests.test_generator_del_cleanup_exc_state.<locals>.do_del)r   )r   r   r   r   r   $test_generator_del_cleanup_exc_state:  s    z3ExceptionTests.test_generator_del_cleanup_exc_statec                s    fdd}  | d S )Nc                s0   yt |  W n tk
r    Y nX  d d S )Nz should have raised StopIteration)r   r   rL   )r   )r   r   r   do_next@  s
    zEExceptionTests.test_generator_next_cleanup_exc_state.<locals>.do_next)r   )r   r   r   )r   r   %test_generator_next_cleanup_exc_state?  s    z4ExceptionTests.test_generator_next_cleanup_exc_statec                s    fdd}  | d S )Nc                s2   y|  d  W n tk
r"   Y nX  d d S )Nz should have raised StopIteration)sendr   rL   )r   )r   r   r   do_sendJ  s
    zEExceptionTests.test_generator_send_cleanup_exc_state.<locals>.do_send)r   )r   r   r   )r   r   %test_generator_send_cleanup_exc_stateI  s    z4ExceptionTests.test_generator_send_cleanup_exc_statec                sB   G  fddd}d yt | W n   Y nX |  d d S )Nc                   s   e Zd Z fddZdS )z*ExceptionTests.test_3114.<locals>.MyObjectc                s   t   d S )N)rB   rk   )r   )rQ   r   r   __del__W  s    z2ExceptionTests.test_3114.<locals>.MyObject.__del__N)r   r   r   r  r   )rQ   r   r   MyObjectV  s   r  r   )NNN)r   r   )r   r  r   )rQ   r   	test_3114S  s    zExceptionTests.test_3114c             C   sf  t ddddd}| t|d d|_| t|d d|_d	|_| t|d
 d|_| t|d d|_| t|d tddddd}| t|d d|_| t|d d|_d	|_| t|d d|_| t|d d|_| t|d tdddd}| t|d d|_| t|d d|_d	|_| t|d d|_| t|d d S )Nr   Zxxxxxr2   r   rz   z8'baz' codec can't encode characters in position 1-4: foora   z<'baz' codec can't encode character '\x78' in position 1: fool   ESh
MQ)EzJ'baz' codec can't encode characters in position 1-4: 965230951443685724997i  zK'4000' codec can't encode characters in position 1-4: 965230951443685724997i  zN'4000' codec can't encode characters in position 1000-4: 965230951443685724997s   xxxxxz3'baz' codec can't decode bytes in position 1-4: fooz5'baz' codec can't decode byte 0x78 in position 1: foozE'baz' codec can't decode bytes in position 1-4: 965230951443685724997zF'4000' codec can't decode bytes in position 1-4: 965230951443685724997zI'4000' codec can't decode bytes in position 1000-4: 965230951443685724997Zxxxxz/can't translate characters in position 1-4: fooz3can't translate character '\x78' in position 1: foozAcan't translate characters in position 1-4: 965230951443685724997zDcan't translate characters in position 1000-4: 965230951443685724997)	r   r   r   r   r   r   r   r   r   )r   ur   r   r   test_unicode_change_attributesa  s>    z-ExceptionTests.test_unicode_change_attributesc             C   s2   t ttf}x"|D ]}| t||d qW d S )N )r   r   r   r   r   __new__)r   Zklassesklassr   r   r   test_unicode_errors_no_object  s    

z,ExceptionTests.test_unicode_errors_no_objectc                s   G dd dt }G dd dt|d}t n}y
t W nR |k
rb } z| d W d d }~X Y n2 tk
rt   Y n    | d Y nX | d W d Q R X  fdd	   \}}}| |tt | | d
t| d S )Nc               @   s   e Zd Zdd ZdS )z/ExceptionTests.test_badisinstance.<locals>.Metac             S   s
   t  d S )N)r=   )clssubclassr   r   r   __subclasscheck__  s    zAExceptionTests.test_badisinstance.<locals>.Meta.__subclasscheck__N)r   r   r   r  r   r   r   r   Meta  s   r  c               @   s   e Zd ZdS )z6ExceptionTests.test_badisinstance.<locals>.MyExceptionN)r   r   r   r   r   r   r   r     s   r   )	metaclassz%exception should not be a MyExceptionzShould have raised KeyErrorc                  s$   y  S  t k
r   t S X d S )N)r5   rB   rk   r   )r   r   r   r     s    z,ExceptionTests.test_badisinstance.<locals>.gz maximum recursion depth exceeded)	r   r   r   r,   rL   r   r5   r   r   )r   r  r   r   rQ   vrr   r   )r   r   test_badisinstance  s     
z!ExceptionTests.test_badisinstancec             C   sP   dt  }tdd|\}}}| |d | d| | d| | d| d S )Na  if 1:
            import sys
            from _testcapi import get_recursion_depth

            class MyException(Exception): pass

            def setrecursionlimit(depth):
                while 1:
                    try:
                        sys.setrecursionlimit(depth)
                        return depth
                    except RecursionError:
                        # sys.setrecursionlimit() raises a RecursionError if
                        # the new recursion limit is too low (issue #25274).
                        depth += 1

            def recurse(cnt):
                cnt -= 1
                if cnt:
                    recurse(cnt)
                else:
                    generator.throw(MyException)

            def gen():
                f = open(%a, mode='rb', buffering=0)
                yield

            generator = gen()
            next(generator)
            recursionlimit = sys.getrecursionlimit()
            depth = get_recursion_depth()
            try:
                # Upon the last recursive invocation of recurse(),
                # tstate->recursion_depth is equal to (recursion_limit - 1)
                # and is equal to recursion_limit when _gen_throw() calls
                # PyErr_NormalizeException().
                recurse(setrecursionlimit(depth + 2) - depth - 1)
            finally:
                sys.setrecursionlimit(recursionlimit)
                print('Done.')
        z-Wdz-cr2   s   RecursionErrors   ResourceWarnings   Done.)__file__r   assert_python_failurer   r   )r   r   rcoutr!   r   r   r   $test_recursion_normalizing_exception  s    3z3ExceptionTests.test_recursion_normalizing_exceptionc             C   s>   d}t d|\}}}| |d | d| | d| d S )Nzif 1:
            import _testcapi
            try:
                raise _testcapi.RecursingInfinitelyError
            finally:
                print('Done.')
        z-cr2   sO   RecursionError: maximum recursion depth exceeded while normalizing an exceptions   Done.)r   r  r   r   )r   r   r  r  r!   r   r   r   -test_recursion_normalizing_infinite_exception  s    z<ExceptionTests.test_recursion_normalizing_infinite_exceptionc          	   C   s8   d}t  $ td|\}}}| d| W d Q R X d S )Na+  if 1:
            import _testcapi
            class C(): pass
            def recurse(cnt):
                cnt -= 1
                if cnt:
                    recurse(cnt)
                else:
                    _testcapi.set_nomemory(0)
                    C()
            recurse(16)
        z-csR   Fatal Python error: Cannot recover from MemoryErrors while normalizing exceptions.)r   r   r  r   )r   r   r  r  r!   r   r   r   )test_recursion_normalizing_with_no_memory  s
    z8ExceptionTests.test_recursion_normalizing_with_no_memoryc                s@   dd l ddlm   fdd}| }| }|| d S )Nr   )raise_memoryerrorc           
      sH   y
   W n( t k
r2 }  z
| j}W d d } ~ X Y nX d |S )Nz Should have raises a MemoryError)r/   r   rL   	format_tb)rQ   rr   )r  r   	tracebackr   r   raiseMemError  s    

z6ExceptionTests.test_MemoryError.<locals>.raiseMemError)r  ri   r  r   )r   r  Ztb1Ztb2r   )r  r   r  r   test_MemoryError  s    	zExceptionTests.test_MemoryErrorc       
      C   s"  dd l }d}d}| t|jd |d}| t|t | t|t | 	|j
 |d|}| |j
| |jd|d}| t|| G d	d
 d
t}|d|||f}| t|| | t|| | |j
| |dd|ddi}	| t|	| | |	jd | |	j
d d S )Nr   zThis is a test docstring.zThis is another test docstring.error1z_testcapi.error1z_testcapi.error2z_testcapi.error3)basec               @   s   e Zd ZdS )z1ExceptionTests.test_exception_with_doc.<locals>.CN)r   r   r   r   r   r   r   C;  s   r   z_testcapi.error4z_testcapi.error5r  r   r2   )ri   r@   r:   Zmake_exception_with_docr~   r   ro   
issubclassr   r   __doc__r   r   r   )
r   ri   Zdoc2Zdoc4r  Zerror2Zerror3r   Zerror4Zerror5r   r   r   test_exception_with_doc"  s2    


z&ExceptionTests.test_exception_with_docc          
      s   ddl m G dd d d  fdd}y
|  W n0 tk
rh } z|  d  W d d }~X Y nX | d |  d  d S )Nr   )r  c               @   s   e Zd ZdS )z3ExceptionTests.test_memory_error_cleanup.<locals>.CN)r   r   r   r   r   r   r   r   O  s   r   c                 s     } t |   d S )N)r   r   )c)r   r  r   r   r   innerR  s    
z7ExceptionTests.test_memory_error_cleanup.<locals>.innerzMemoryError not raised)ri   r  r/   assertNotEqualrL   r   )r   r%  rQ   r   )r   r  r   r   test_memory_error_cleanupJ  s    
 
z(ExceptionTests.test_memory_error_cleanupc          
      sz   G dd d d  fddy
  W n0 t k
r\ } z|  d  W d d }~X Y nX | d |  d  d S )Nc               @   s   e Zd ZdS )z6ExceptionTests.test_recursion_error_cleanup.<locals>.CN)r   r   r   r   r   r   r   r   c  s   r   c                 s     } t |   d S )N)r   r   )r$  )r   r%  r   r   r   r%  f  s    
z:ExceptionTests.test_recursion_error_cleanup.<locals>.innerzRecursionError not raised)r5   r&  rL   r   )r   rQ   r   )r   r%  r   r   test_recursion_error_cleanup`  s    
 
z+ExceptionTests.test_recursion_error_cleanupc          	   C   s:   |  t}tt W d Q R X | |jjtj|j d S )N)	r@   r(   oslistdirr  r   r[   r   ZENOTDIR)r   r_   r   r   r   test_errno_ENOTDIRt  s    z!ExceptionTests.test_errno_ENOTDIRc             C   s   G dd d}G dd d}x||fD ]}|  | | }t }~W d Q R X | }| d| | |jj| | d| | d| ||kr| d| | d	| n| d
| | d| | |d W d Q R X q&W d S )Nc               @   s   e Zd Zdd ZdS )z1ExceptionTests.test_unraisable.<locals>.BrokenDelc             S   s   t d}|d S )Nzdel is broken)r=   )r   r   r   r   r   r  }  s    z9ExceptionTests.test_unraisable.<locals>.BrokenDel.__del__N)r   r   r   r  r   r   r   r   	BrokenDel|  s   r,  c               @   s   e Zd Zdd ZdS )z:ExceptionTests.test_unraisable.<locals>.BrokenExceptionDelc             S   s   t  }|d S )N)r   )r   r   r   r   r   r    s    zBExceptionTests.test_unraisable.<locals>.BrokenExceptionDel.__del__N)r   r   r   r  r   r   r   r   BrokenExceptionDel  s   r-  zException ignoredztest_exceptions.pyz	raise excr   z<exception str() failed>r=   zdel is broken
)subTestr   getvaluer   r  r   ro   rq   )r   r,  r-  Z
test_classr   r   reportr   r   r   test_unraisablez  s$    zExceptionTests.test_unraisablec             C   s   xt tfD ]}| | y|d}|W n4 |k
r^   t }tjt   W d Q R X Y nX | }| d| | d| | |j	| |tkr| d| n| d| | 
|d W d Q R X q
W d S )Nztest messageztest_exceptions.pyz	raise excz<exception str() failed>r.  )r=   r   r/  r   rB   __excepthook__rk   r0  r   r   ro   rq   )r   r   r   r   r1  r   r   r   test_unhandled  s     zExceptionTests.test_unhandledc             C   sJ   d}x@t ddD ]2}td|| \}}}| |d | d| qW d S )Nzif 1:
            import _testcapi
            class C(): pass
            _testcapi.set_nomemory(0, %d)
            C()
        r2   rd   z-c)r2   x   s   MemoryError)rJ   r   r  r   )r   r   r   r  r  r!   r   r   r   "test_memory_error_in_PyErr_PrintEx  s
    z1ExceptionTests.test_memory_error_in_PyErr_PrintExc          	      sd   G dd dt  G dd dt  fdd}| }|d  |   |  W d Q R X d S )Nc               @   s   e Zd ZdS )zBExceptionTests.test_yield_in_nested_try_excepts.<locals>.MainErrorN)r   r   r   r   r   r   r   	MainError  s   r7  c               @   s   e Zd ZdS )zAExceptionTests.test_yield_in_nested_try_excepts.<locals>.SubErrorN)r   r   r   r   r   r   r   SubError  s   r8  c               3   sF   y
  W n6  k
r@   y
d V  W n k
r8   Y nX  Y nX d S )Nr   r   )r7  r8  r   r   main  s    

z=ExceptionTests.test_yield_in_nested_try_excepts.<locals>.main)r   r   r@   r   )r   r9  coror   )r7  r8  r    test_yield_in_nested_try_excepts  s    

z/ExceptionTests.test_yield_in_nested_try_exceptsc                sT    fdd}| }yt W n$ t k
r>    t|d Y nX  t|d d S )Nc               3   s>   yt W n t k
r"   dV  Y nX  t d dV  d S )Nr2   )NNNra   )r=   r   rB   rk   r   )r   r   r   r     s    z?ExceptionTests.test_generator_doesnt_retain_old_exc2.<locals>.gr2   ra   )r*   r   r   )r   r   r   r   )r   r   %test_generator_doesnt_retain_old_exc2  s    z4ExceptionTests.test_generator_doesnt_retain_old_exc2c          	   C   sR   dd }|  t6 | }ydd  W n   t| t| Y nX W d Q R X d S )Nc               s   s   dV   dV  d S )Nr2   ra   r   r   r   r   r   r     s    z1ExceptionTests.test_raise_in_generator.<locals>.gr2   r   )r@   r>   r   )r   r   r   r   r   r   test_raise_in_generator  s    z&ExceptionTests.test_raise_in_generatorN)<r   r   r   r"   rR   rZ   r\   re   r   rw   r   unittest
skipUnlessrB   rW   r   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r
  r  r  r  r  r  r#  r'  r(  r+  r2  r4  r6  r;  r<  r=  r   r   r   r   r      sh   T0'	~
t

%;( r   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ImportErrorTestsc          	   C   sV  t d}| |j | |j t ddd}| |jd | |j t ddd}| |jd | |j t dddd}| |jd | |jd d}| t| t dd	d
 W d Q R X | t| t ddd	d W d Q R X | t| t ddd	d W d Q R X | t| t d	d
 W d Q R X | t| t dd	dd W d Q R X d S )NtestZ
somemodule)namesomepath)pathsomename)rD  rB  z8'invalid' is an invalid keyword argument for ImportErrorkeyword)invalidrB  )rB  rG  rD  )rD  rG  T)rG  Zanother)r)   r   rB  rD  r   r   r<   )r   r   rS   r   r   r   test_attributes  s.    z ImportErrorTests.test_attributesc             C   s   t dddd}| |jd | |jd | |jd | |jd |  | |jd | |jd  | |jd  | |jd  d S )NrA  rB  rD  )rB  rD  )rA  r   )r)   r   r   rS   rB  rD  r   )r   r   r   r   r   test_reset_attributes  s    z&ImportErrorTests.test_reset_attributesc          	   C   s@   t dtfdd& d}t|}| t|t| W d Q R X d S )Nr  T)quiets   abc)r   BytesWarningr)   r   r   )r   argr   r   r   r   test_non_str_argument+  s    z&ImportErrorTests.test_non_str_argumentc             C   s   xt  t ddt ddt dddfD ]}td	|}xdttjd D ]R}tt||}| |jd | |j	d | |j
|j
 | |j|j qBW xXtjtjfD ]H}||}| |jd | |j	d | |j
|j
 | |j|j qW q$W d S )
NrE  )rB  rC  )rD  )rB  rD  rA  r2   )rA  )rA  )dictr)   rJ   r   r   rG   r   r   r   rS   rB  rD  copydeepcopy)r   kwargsorigprotor   r$  r   r   r   test_copy_pickle2  s"    
z!ImportErrorTests.test_copy_pickleN)r   r   r   rH  rI  rM  rT  r   r   r   r   r@    s   "r@  __main__)rO  r)  rB   r>  r   r   r   Ztest.supportr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   TestCaser   r@  r   r9  r   r   r   r   <module>   s.   8         dM