B
    u9a                 @   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   %/usr/lib/python3.7/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   excZ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.   r/   Zundefined_variabler0   ranger3   r4   r5   r8   r6   r7   compilefailr9   r:   exitr;   r<   chrr=   r   r>   )r   fpZ	savestdinrE   r   Z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>r8   zexpected %s, got %sz"failed to get expected SyntaxError)rJ   r5   msgrK   )srcrQ   rO   )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
)rA   platform
startswith)r   rS   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>r8   zexpected %s, got %sz"failed to get expected SyntaxError)rJ   rQ   rK   )rR   rQ   	exceptionrO   )r   r   r   rS      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)r5   r6   r7   )r   rS   rW   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>r8   )r?   r5   rJ   r   rY   linenooffset)rR   r[   r\   cm)r   r   r   check   s    z3ExceptionTests.testSyntaxErrorOffset.<locals>.checkzdef fact(x):
	return x!
   r*   z1 +
r1      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)r3   )Zself_r   r   r   r      s    zBExceptionTests.testSettingException.<locals>.BadException.__init__N)r   r   r   r   r   r   r   r   BadException   s   rd   c               @   s   e Zd ZdS )z=ExceptionTests.testSettingException.<locals>.InvalidExceptionN)r   r   r   r   r   r   r   InvalidException   s   re   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   r1   
test_capi1ztest_exceptions.pyzExpected exception)	_testcapiraise_exceptionr;   rA   exc_infotb_framef_coder   co_name
assertTrueco_filenameendswithrK   )rg   r    r   tbco)rd   r   r   r   rf      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)rg   rh   r3   rA   ri   rj   rk   r   rl   rm   rn   ro   f_backrK   )rg   r    r   rp   rq   Zco2)rd   r   r   r   rr      s    
 z7ExceptionTests.testSettingException.<locals>.test_capi2c                 s   dd l } t| j d d S )Nr   r1   )rg   r?   r9   rh   )rg   )re   r   r   r   
test_capi3   s    
z7ExceptionTests.testSettingException.<locals>.test_capi3rT   )r   rA   rU   rV   )r   rf   rr   rt   r   )rd   re   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   ra   r_   z[WinError 3] foo: 'bar'   z[WinError 1001] foo)WindowsErrorr/   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)r1   )rx   )rx   r1   rx   )r   r   )r   r   r   r}   r   )rx   ry   ry   )rx   ry   bazr   )rx   ry   r   Nquuxr   )r   r   r   )errnoStrstrErrorStrfilenameStr)r   r   r   r   r   )r   r   r}   r   )r1   r   r   )r1   r   r1   )r   r}   r   r   r   )rQ   textr   r[   r\   print_file_and_line)msgStrr   )r   r   r   rQ   r   r[   r\   )r   )r   	linenoStr	offsetStrtextStrr   r   r   )r\   r   r   r   rQ   r   r[   )r   r   r   r   r   Zprint_file_and_lineStr)r   r   r   rQ   r   r[   r\   )asciiar   r1   zordinal not in ranger   r   r   zordinal not in range)r   encodingobjectstartreason   )r   r   r   r1   zordinal not in range)u   あr   r1   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'   r5   UnicodeErrorUnicodeEncodeErrorUnicodeDecodeError	bytearrayUnicodeTranslateErrorr   r   appendr{   r/   printrA   stderrtyper   ro   r   r   r   r@   reprpicklerI   HIGHEST_PROTOCOLdumpsrF   )r   ZexceptionListr   r   ZexpectedrO   rW   ZcheckArgNamevaluepZ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 )Nr`   r_      c               @   s   e Zd ZdS )z5ExceptionTests.testWithTraceback.<locals>.MyExceptionN)r   r   r   r   r   r   r   MyException  s   r   )	r)   rA   ri   r   with_tracebackassertIsInstancer   __traceback__r   )r   rp   rO   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   rK   )r   rO   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__r1   __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 )Nr`   r_   )r)   rA   ri   r   r   r   )r   rp   rO   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   rO   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   ZassertFalse__suppress_context__r/   r   rm   )r   r   rO   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 )Nr1   )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?   r4   )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)rm   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 )NrO   )r   rm   ZassertNotInlocals)r   rO   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)Z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_valueZ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   wrrO   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   )rO   r   r   print_error  s    zIExceptionTests.test_exception_target_in_nested_scope.<locals>.print_error)Z	somethingr   )r   r   r   )rO   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+   rA   ri   r   r   r   r   yield_raise  s    z:ExceptionTests.test_generator_leaking.<locals>.yield_raiser   rx   )r   nextr+   rA   ri   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)r3   r   StopIterationr   rA   ri   )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 )Nr1   )r=   rA   ri   r   r   r   r   r     s    
z1ExceptionTests.test_generator_leaking3.<locals>.gr1   r   )NNN)r   r=   r|   rA   ri   throwr   )r   r   r   rO   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 )Nr1   r   )r=   rA   ri   r   r   r   r   r     s
    z1ExceptionTests.test_generator_leaking4.<locals>.gr1   )NNN)r;   r   r|   r=   rA   ri   r   )r   r   r   tprO   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 )Nr1   )NNN)r   rA   ri   r3   r   r   )r   r   r   r     s    z>ExceptionTests.test_generator_doesnt_retain_old_exc.<locals>.g)r3   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 )Nr1   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)r3   r   )gen)r   r   r   run_gen  s
    zFExceptionTests.test_generator_finalizing_and_exc_info.<locals>.run_gen)NNN)r   r   rA   ri   )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   r3   )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)rC   )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   rK   )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   rK   )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)rA   ri   )r   )rO   r   r   __del__W  s    z2ExceptionTests.test_3114.<locals>.MyObject.__del__N)r   r   r   r   r   )rO   r   r   MyObjectV  s   r   r   )NNN)r   r   )r   r   r   )rO   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xxxxxr1   r   rx   z8'baz' codec can't encode characters in position 1-4: foor_   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)r4   rA   ri   r   )r   r   r   r     s    z,ExceptionTests.test_badisinstance.<locals>.gz maximum recursion depth exceeded)	r   r   r   r+   rK   r   r4   r   r   )r   r  r   r   rO   vrp   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-cr1   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-cr1   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   rK   	format_tb)rO   rp   )r  r   	tracebackr   r   raiseMemError  s    

z6ExceptionTests.test_MemoryError.<locals>.raiseMemError)r  rg   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   r1   )rg   r?   r9   Zmake_exception_with_docr|   r   rm   
issubclassr   r   __doc__r   r   r   )
r   rg   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)rg   r  r.   assertNotEqualrK   r   )r   r  rO   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)r4   r  rK   r   )r   rO   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   rY   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   rm   ro   )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   rA   __excepthook__ri   r(  r   r   rm   ro   )r   r   r   r   r)  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()
        r1   rb   z-c)r1   x   s   MemoryError)rI   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   r/  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   r0  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   )r/  r0  r   r   main  s    

z=ExceptionTests.test_yield_in_nested_try_excepts.<locals>.main)r   r   r?   r   )r   r1  coror   )r/  r0  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 )Nr1   )NNNr_   )r<   r   rA   ri   r   )r   r   r   r     s    z?ExceptionTests.test_generator_doesnt_retain_old_exc2.<locals>.gr1   r_   )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 )Nr1   r_   r   r   r   r   r   r     s    z1ExceptionTests.test_raise_in_generator.<locals>.gr1   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!   rP   rX   rZ   rc   r   ru   r   unittestZ
skipUnlessrA   rU   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#  r*  r,  r.  r3  r4  r5  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)r;  r9  z8'invalid' is an invalid keyword argument for ImportErrorkeyword)invalidr9  )r9  r>  r;  )r;  r>  T)r>  Zanother)r(   r   r9  r;  r   r   r;   )r   r   rQ   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 )Nr8  r9  r;  )r9  r;  )r8  r   )r(   r   r   rQ   r9  r;  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 )
Nr<  )r9  r:  )r;  )r9  r;  r8  r1   )r8  )r8  )dictr(   rI   r   r   rF   r   r   r   rQ   r9  r;  copyZdeepcopy)r   kwargsZorigprotor   r  r   r   r   test_copy_pickle2  s"    
z!ImportErrorTests.test_copy_pickleN)r   r   r   r?  r@  rD  rI  r   r   r   r   r7    s   "r7  __main__)rF  r!  rA   r6  r   r   r}   Ztest.supportr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   ZTestCaser   r7  r   r1  r   r   r   r   <module>   s.   8         dM