
    dafy                        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 ddl	m
Z
 ddlmZ ddlmZ ddlZ G d dej                         Z G d	 d
ej                         Z G d dej                         Z G d dej                         Z G d dej                         Z G d dej                         Z G d de      Z G d dej                         Z G d d      Z G d deej                         Z G d d      Z G d deej                         Z G d d eej                         Z G d! d"eej                         Z G d# d$ej                         Z e!d%k(  r ejD                          yy)&z9Unit tests for contextlib.py, and other context managers.    N)*)support)	os_helper)ExceptionIsLikeMixinc                       e Zd Zd Zd Zd Zy)TestAbstractContextManagerc                 p     G d dt               } |       }| j                  |j                         |       y )Nc                        e Zd Z fdZ xZS );TestAbstractContextManager.test_enter.<locals>.DefaultEnterc                     t        |   |  y Nsuper__exit__selfargs	__class__s     //root/Python-3.12.4/Lib/test/test_contextlib.pyr   zDTestAbstractContextManager.test_enter.<locals>.DefaultEnter.__exit__        $'    __name__
__module____qualname__r   __classcell__r   s   @r   DefaultEnterr          ( (r   r   )AbstractContextManagerassertIs	__enter__)r   r   managers      r   
test_enterz%TestAbstractContextManager.test_enter   s/    	(1 	( .g'')73r   c                      G d dt               }| j                  t              5   |        d d d        y # 1 sw Y   y xY w)Nc                       e Zd Zy)ETestAbstractContextManager.test_exit_is_abstract.<locals>.MissingExitNr   r   r    r   r   MissingExitr'          r   r*   )r    assertRaises	TypeError)r   r*   s     r   test_exit_is_abstractz0TestAbstractContextManager.test_exit_is_abstract   s9    	0 	 y) 	M	 	 	s   6?c                 Z    G d d      }| j                  t        |t                      G d dt              }| j                  t        |t                      G d d|      }| j                  t        |t                      G d d|      }| j                  t        |t                     y )	Nc                       e Zd Zd Zd Zy)RTestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratchc                     | S r   r)   r   s    r   r"   z\TestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratch.__enter__$       r   c                      y r   r)   )r   exc_type	exc_value	tracebacks       r   r   z[TestAbstractContextManager.test_structural_subclassing.<locals>.ManagerFromScratch.__exit__&   s    r   Nr   r   r   r"   r   r)   r   r   ManagerFromScratchr1   #   s    r   r:   c                        e Zd Z fdZ xZS )LTestAbstractContextManager.test_structural_subclassing.<locals>.DefaultEnterc                     t        |   |  y r   r   r   s     r   r   zUTestAbstractContextManager.test_structural_subclassing.<locals>.DefaultEnter.__exit__,   r   r   r   r   s   @r   r   r<   +   r   r   r   c                       e Zd ZdZy)GTestAbstractContextManager.test_structural_subclassing.<locals>.NoEnterNr   r   r   r"   r)   r   r   NoEnterr?   1   s    Ir   rA   c                       e Zd ZdZy)FTestAbstractContextManager.test_structural_subclassing.<locals>.NoExitNr   r   r   r   r)   r   r   NoExitrC   6   s    Hr   rE   )
assertTrue
issubclassr    assertFalse)r   r:   r   rA   rE   s        r   test_structural_subclassingz6TestAbstractContextManager.test_structural_subclassing"   s    	 	 	
#57MNO	(1 	( 	
<1GHI	( 	 	G-CDE	' 	 	F,BCDr   N)r   r   r   r$   r.   rI   r)   r   r   r   r      s    4Er   r   c                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zej&                  d        Zej&                  d        Zd Zd Zd Zd Zy)ContextManagerTestCasec                     g t         fd       } |       5 }| j                  dg       | j                  |d       j                  |       d d d        | j                  g d       y # 1 sw Y   xY w)Nc               3   Z   K    j                  d       d  j                  d       y wN   *     appendstates   r   woohooz@ContextManagerTestCase.test_contextmanager_plain.<locals>.woohoo@   s#     LLOHLL   (+rO   rP   rO   rP   rQ   )contextmanagerassertEqualrS   r   rV   xrU   s      @r   test_contextmanager_plainz0ContextManagerTestCase.test_contextmanager_plain>   sw    		 
	 X 	UQC(Q#LLO	 	-		 	s   7A--A6c                 N   g t         fd       }| j                  t              5   |       5 }| j                  dg       | j                  |d       j	                  |       t               # 1 sw Y   nxY w	 d d d        n# 1 sw Y   nxY w| j                  g d       y )Nc               3      K    j                  d       	 d  j                  d       y #  j                  d       w xY wwrN   rR   rT   s   r   rV   zBContextManagerTestCase.test_contextmanager_finally.<locals>.woohooM   s4     LLO"S!S!s   A, A?ArO   rP   rX   )rY   r,   ZeroDivisionErrorrZ   rS   r[   s      @r   test_contextmanager_finallyz2ContextManagerTestCase.test_contextmanager_finallyK   s    		" 
	" 01 	* *Q  ,  B'Q'))	* * *	* 	* 	* 	-s   BAA00A9	5BBc                 @   t         d        }	  |       5  ddz   d d d        | j                  t              d       | j                  |d   j                  d       | j                  |d   j                  d        G d dt              }	  |       5   |d      # 1 sw Y   xY w# t        $ r)}t        j                  |j                        }Y d }~d }~ww xY w# 1 sw Y   nxY wn1# |$ r)}t        j                  |j                        }Y d }~nd }~ww xY w| j                  t        |      d       | j                  |d   j                  d       | j                  |d   j                  d	        G d
 dt              }t        d       |d      fD ]  }| j                  t        |            5  	  |       5  |# 1 sw Y   nxY w	 | j                  | d       nL# t        |      $ r;}| j                  ||       t        j                  |j                        }Y d }~nd }~ww xY w| j                  t        |      d       | j                  |d   j                  d       | j                  |d   j                  d       d d d        # 1 sw Y   xY w y )Nc               3      K   d  y wr   r)   r)   r   r   fz?ContextManagerTestCase.test_contextmanager_traceback.<locals>.f]   	        rO   r   test_contextmanager_traceback1/0c                       e Zd Zy)RContextManagerTestCase.test_contextmanager_traceback.<locals>.RuntimeErrorSubclassNr(   r)   r   r   RuntimeErrorSubclassrj   l   r+   r   rk   rP   zraise RuntimeErrorSubclass(42)c                       e Zd Zy)SContextManagerTestCase.test_contextmanager_traceback.<locals>.StopIterationSubclassNr(   r)   r   r   StopIterationSubclassrm   y   r+   r   rn   spamtype was suppressedzraise stop_exc)rY   r`   r8   
extract_tb__traceback__rZ   lennamelineRuntimeErrorStopIterationsubTestrq   failr!   )r   rd   eframesrk   rn   stop_excs          r   rg   z4ContextManagerTestCase.test_contextmanager_traceback\   s_   		 
		; !
 	Va()HI/	< 		; /*2..   	;))!//:F	;/ / /# 	;))!//:F	; 	Va()HI)IJ	M 	 &!!&)
 	CH 4>2 C< '&' ' ' II
/:;	 H~ CMM!X.&11!//BFC   Va0  1PQ  1ABC C C		Cs   B( BB( C* 	CB%!B( (	C1CCC&"C* *D/DD5J7G#>GG
G#J#H,11H'"J'H,,AJJ	c                     t         d        } |       }|j                          | j                  |j                  t        t	        d      d              y )Nc               3      K   d  y wr   r)   r)   r   r   wheezCContextManagerTestCase.test_contextmanager_no_reraise.<locals>.whee   re   rf   foo)rY   r"   rH   r   r-   )r   r   ctxs      r   test_contextmanager_no_reraisez5ContextManagerTestCase.test_contextmanager_no_reraise   sD    		 
	fi51A4HIr   c                 V   t         d        } |       }|j                          | j                  t              5  |j	                  t
        t        d      d        d d d        t        j                  d      r&| j                  |j                  j                         y y # 1 sw Y   FxY w)Nc               3   ,   K   	 d  y #  d  Y y xY wwr   r)   r)   r   r   whoozOContextManagerTestCase.test_contextmanager_trap_yield_after_throw.<locals>.whoo   s     s   	 r   Tcpython)rY   r"   r,   rx   r   r-   r   check_impl_detailrH   gengi_suspendedr   r   r   s      r   *test_contextmanager_trap_yield_after_throwzAContextManagerTestCase.test_contextmanager_trap_yield_after_throw   s    		 
	
 f|, 	<LLIe$4d;	<$$T2SWW112 3	< 	<s   !BB(c                     t         d        } |       }| j                  t              5  |j                          d d d        y # 1 sw Y   y xY w)Nc               3      K   y wr   r)   r)   r   r   r   zFContextManagerTestCase.test_contextmanager_trap_no_yield.<locals>.whoo   s	     s   )rY   r,   rx   r"   r   s      r   !test_contextmanager_trap_no_yieldz8ContextManagerTestCase.test_contextmanager_trap_no_yield   sI    		 
	 f|, 	MMO	 	 	s   AAc                 <   t         d        } |       }|j                          | j                  t              5  |j	                  d d d        d d d        t        j                  d      r&| j                  |j                  j                         y y # 1 sw Y   FxY w)Nc               3      K   d  d  y wr   r)   r)   r   r   r   zJContextManagerTestCase.test_contextmanager_trap_second_yield.<locals>.whoo   s        
Tr   )
rY   r"   r,   rx   r   r   r   rH   r   r   r   s      r   %test_contextmanager_trap_second_yieldz<ContextManagerTestCase.test_contextmanager_trap_second_yield   s    		 
	 f|, 	+LLtT*	+$$T2SWW112 3	+ 	+s   BBc                     t         d        } |       }|j                          | j                  t              5  |j	                  t
        d d        d d d        y # 1 sw Y   y xY w)Nc               3   <   K   	 d  y # t         $ r t        w xY wwr   )rx   SyntaxErrorr)   r   r   r   zGContextManagerTestCase.test_contextmanager_non_normalised.<locals>.whoo   s"     " "!!"s   	 )rY   r"   r,   r   r   rx   r   s      r   "test_contextmanager_non_normalisedz9ContextManagerTestCase.test_contextmanager_non_normalised   sY    		" 
	" f{+ 	3LLtT2	3 	3 	3s   AA#c                      g t          fd       } |       5 } j                  dg        j                  |d       j                  |       t        d      # 1 sw Y   nxY w j                  g d       y )Nc               3      K   j                  d       	 d y # t        $ r<} j                  | j                  d          j                  g d       Y d } ~ y d } ~ ww xY ww)NrO   rP   r   rX   )rS   r`   r   rZ   )r|   r   rU   s    r   rV   zAContextManagerTestCase.test_contextmanager_except.<locals>.woohoo   sR     LLO6$ 6QVVAY'  556s*   A# A#	A 2AA#A  A#rO   rP   rQ   rX   )rY   rZ   rS   r`   r[   s   `  @r   test_contextmanager_exceptz1ContextManagerTestCase.test_contextmanager_except   s}    		6 
	6 X 	)UQC(Q#LLO#C((		) 	)
 	-s   AAA&c                 t   t         d        } G d dt              }t        d       |d      fD ](  }| j                  t        |            5  	  |       5  | y # 1 sw Y   nxY w	 | j	                  | d       n(# t
        $ r}| j                  ||       Y d }~nd }~ww xY wd d d        # 1 sw Y   xY w)Nc               3      K   d  y wr   r)   r)   r   r   rV   zJContextManagerTestCase.test_contextmanager_except_stopiter.<locals>.woohoo   re   rf   c                       e Zd Zy)YContextManagerTestCase.test_contextmanager_except_stopiter.<locals>.StopIterationSubclassNr(   r)   r   r   rn   r      r+   r   rn   ro   rp   rr   )rY   ry   rz   rq   r{   	Exceptionr!   )r   rV   rn   r~   exs        r   #test_contextmanager_except_stopiterz:ContextManagerTestCase.test_contextmanager_except_stopiter   s    		 
		M 	 'v.0Ef0MN 	<H4>2 << '&		<' ' '
 II
/:; ! 0MM"h//0	< < <sH   B.A=AA$ A=(B.=	B"BB.B""B..B7	c                     d}i }t        |||       |d   }t        d      }	  |       5  |# 1 sw Y   nxY w	 | j                  d       y # t        $ r}| j	                  ||       Y d }~y d }~ww xY w)Nztfrom __future__ import generator_stop
from contextlib import contextmanager
@contextmanager
def woohoo():
    yield
rV   ro   zStopIteration was suppressed)execry   r{   r   r!   )r   codelocalsrV   r~   r   s         r   !test_contextmanager_except_pep479z8ContextManagerTestCase.test_contextmanager_except_pep479   s     T66"! (	6   
 II45  	(MM"h''	(s$   A -6A 	A1A,,A1c                 X   t         d        }	  |       5  t        # 1 sw Y   nxY wnt# t        $ rh}| j                  t	        |      t
               | j                  |j                  d   d       | j                  |j                  t               Y d }~nd }~ww xY w	  |       5  t        d      # 1 sw Y   y xY w# t        $ rc}| j                  t	        |      t               | j                  |j                  d   d       | j                  |j                         Y d }~y d }~ww xY w)Nc               3   P   K   	 d  y # t         $ r} t        d      | d } ~ ww xY ww)Nissue29692:Chainedr   rx   excs    r   test_issue29692zoContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptions.<locals>.test_issue29692  s.     B B"#78cABs   &	 &	##&r   r   zissue29692:Unchained)rY   r`   r   r!   rq   rx   rZ   r   assertIsInstance	__cause__ry   assertIsNone)r   r   r   s      r   ?test_contextmanager_do_not_unchain_non_stopiteration_exceptionszVContextManagerTestCase.test_contextmanager_do_not_unchain_non_stopiteration_exceptions  s   		B 
	B
	C " (''( ( ( 	CMM$r(L1RWWQZ)=>!!",,0ABB	C
	, " <#$:;;< < 	,MM$r(M2RWWQZ)?@bll++	,sR   ) %) 	BABBB= %B11B:6B= :B= =	D)AD$$D)c                 <   t         d        }| j                  t              5   |       5  ddz   d d d        d d d        | j                  t              5   |       5  t        # 1 sw Y   5xY w# 1 sw Y   9xY w# 1 sw Y   nxY w	 d d d        y # 1 sw Y   y xY w)Nc               3   V   K   	 d  y # t         $ r} t        d|        | d } ~ ww xY ww)Nzcaught r   r   s    r   rV   zLContextManagerTestCase.test_contextmanager_wrap_runtimeerror.<locals>.woohoo  s0     = ="WSE?3<=s   )	 )	&!&)rO   r   )rY   r,   rx   ry   r   rV   s     r   %test_contextmanager_wrap_runtimeerrorz<ContextManagerTestCase.test_contextmanager_wrap_runtimeerror  s    		= 
	= |, 	 A	 }- 	$ $## 	 	$ $ $	$ 	$ 	$s?   A0A$A0BA<$A-	)A00A9<B	BBc                 <    d }t          |d      d               }|S )Nc                        fd}|S )Nc                 R    j                         D ]  \  }}t        | ||        | S r   )itemssetattr)funckvkws      r   decoratezXContextManagerTestCase._create_contextmanager_attribs.<locals>.attribs.<locals>.decorate-  s-    88: &CAaD1%&r   r)   )r   r   s   ` r   attribszFContextManagerTestCase._create_contextmanager_attribs.<locals>.attribs,  s     Or   bar)r   c              3      K   d yw)Whee!Nr)   )ro   s    r   bazzBContextManagerTestCase._create_contextmanager_attribs.<locals>.baz2  s      rf   rY   )r   r   r   s      r   _create_contextmanager_attribsz5ContextManagerTestCase._create_contextmanager_attribs+  s/    	 
	U		 
 
	 
r   c                     | j                         }| j                  |j                  d       | j                  |j                  d       y )Nr   r   )r   rZ   r   r   r   r   s     r   test_contextmanager_attribsz2ContextManagerTestCase.test_contextmanager_attribs9  s8    113e,%(r   c                 \    | j                         }| j                  |j                  d       y Nr   r   rZ   __doc__r   s     r   test_contextmanager_doc_attribz5ContextManagerTestCase.test_contextmanager_doc_attrib>  s$    113g.r   c                 h     | j                         d       }| j                  |j                  d       y r   r   r   s     r   *test_instance_docstring_given_cm_docstringzAContextManagerTestCase.test_instance_docstring_given_cm_docstringC  s+    3d113D9g.r   c                     t         d        } |dddd      5 }| j                  |d       d d d        y # 1 sw Y   y xY w)Nc              3      K   | |||f y wr   r)   r   r   r   kwdss       r   rV   z4ContextManagerTestCase.test_keywords.<locals>.woohooJ  s     tT**r         !   ,   r   )r   r   r   r   rY   rZ   )r   rV   targets      r   test_keywordsz$ContextManagerTestCase.test_keywordsH  sJ    		+ 
	+"2B7 	76V%56	7 	7 	7s   5>c                       G d d      }t          fd       } | |        |             5  	 d d d        y # 1 sw Y   y xY w)Nc                       e Zd Zy)0ContextManagerTestCase.test_nokeepref.<locals>.ANr(   r)   r   r   Ar   Q  r+   r   r   c              3      K   t        j                  |       } t        j                  |      }t        j                          j	                   |               j	                   |              d  y wr   )weakrefrefr   
gc_collectr   )abr   s     r   rV   z5ContextManagerTestCase.test_nokeepref.<locals>.woohooT  sP     AAAA ac"ac"s   A0A3r   r   )r   r   rV   s   `  r   test_nokeeprefz%ContextManagerTestCase.test_nokeeprefP  sI    	 	 
	 
	 AC13 		 	 	s	   9Ac                 J   t         d        }| j                  t              5   |        d d d        | j                  t              5   |dd       d d d        | j                  t              5   |d       d d d        y # 1 sw Y   XxY w# 1 sw Y   =xY w# 1 sw Y   y xY w)Nc             3      K   d  y wr   r)   r   r   s     r   rV   z8ContextManagerTestCase.test_param_errors.<locals>.woohoob  re   rf         r   )rY   r,   r-   r   s     r   test_param_errorsz(ContextManagerTestCase.test_param_errorsa  s    		 
	 y) 	H	y) 	1aL	y) 	QK	 			 		 		 	s#   B
B.
BB
BB"c                      ddt          fd       } |       fd                 j                  d        j                  d       y )Nr   c               3   \   K   dz  } dz  d  dz  j                  |        y wNrO   )rZ   )beforedepthncolsr   s    r   rV   z5ContextManagerTestCase.test_recursive.<locals>.woohoop  s<      QJEFQJEQJEUF+s   ),c                        dk  r         y y )N
   r)   )r   	recursives   r   r   z8ContextManagerTestCase.test_recursive.<locals>.recursive{  s    rz r   r   r   )r   rV   r   r   r   s   ` @@@r   test_recursivez%ContextManagerTestCase.test_recursivem  s\    		, 
	, 
	 
	 	#"r   N)r   r   r   r]   ra   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   requires_docstringsr   r   r   r   r   r   r)   r   r   rK   rK   <   s    .."0CdJ333."<$6*,.$&)
   / !/   / !/7"
#r   rK   c                   <    e Zd Zej                  d        Zd Zd Zy)ClosingTestCasec                 r    t         j                  }t        d       }| j                  |j                  |       y r   )closingr   rZ   r   cm_docstringobjs      r   test_instance_docsz"ClosingTestCase.test_instance_docs  s*     dml3r   c                     g  G fdd      } |       }| j                  g        t        |      5 }| j                  ||       d d d        | j                  dg       y # 1 sw Y   xY w)Nc                       e Zd Z fdZy)'ClosingTestCase.test_closing.<locals>.Cc                 (    j                  d       y r   rR   r   rU   s    r   closez-ClosingTestCase.test_closing.<locals>.C.close      Qr   Nr   r   r   r  rT   s   r   Cr         r   r  rO   )rZ   r   r   r  r\   yrU   s       @r   test_closingzClosingTestCase.test_closing  sk    	  	  C#QZ 	#1Q"	#$	# 	#s   A##A,c                 <   g  G fdd      } |       }| j                  g        | j                  t              5  t        |      5 }| j                  ||       ddz   d d d        d d d        | j                  dg       y # 1 sw Y   %xY w# 1 sw Y   )xY w)Nc                       e Zd Z fdZy)-ClosingTestCase.test_closing_error.<locals>.Cc                 (    j                  d       y r   rR   r  s    r   r  z3ClosingTestCase.test_closing_error.<locals>.C.close  r	  r   Nr
  rT   s   r   r  r    r  r   r  rO   r   )rZ   r,   r`   r   r  s       @r   test_closing_errorz"ClosingTestCase.test_closing_error  s    	  	  C#01 	 q  A&A	 	$ 	 	s#   B
B"BB	BBN)r   r   r   r   r   r  r  r  r)   r   r   r   r     s$      4 !4	%%r   r   c                       e Zd Zd Zy)NullcontextTestCasec                      G d d      } |       }t        |      5 }| j                  ||       d d d        y # 1 sw Y   y xY w)Nc                       e Zd Zy)/NullcontextTestCase.test_nullcontext.<locals>.CNr(   r)   r   r   r  r    r+   r   r  )nullcontextr!   )r   r  cc_ins       r   test_nullcontextz$NullcontextTestCase.test_nullcontext  s?    	 	C^ 	#tMM$"	# 	# 	#s	   9AN)r   r   r   r  r)   r   r   r  r    s    #r   r  c                       e Zd Zd Zy)FileContextTestCasec                    t        j                         }	 d }t        |dd      5 }| j                  |j                         |j                  d       d d d        | j                  |j                         d }| j                  t              5  t        |dd      5 }| j                  |j                         | j                  |j                         d       ddz   d d d        d d d        | j                  |j                         t        j                  |       y # 1 sw Y   xY w# 1 sw Y   NxY w# 1 sw Y   RxY w# t        j                  |       w xY w)Nwzutf-8)encodingzBooh
rrO   r   )tempfilemktempopenrH   closedwriterF   r,   r`   rZ   readr   unlink)r   tfnrd   s      r   testWithOpenz FileContextTestCase.testWithOpen  s   oo	"Ac31 "Q  *!" OOAHH%A""#45 #sW5 $$QXX.$$QVVXx8E
 OOAHH%S!" "   S!sS   E -D:E D6AD*D6%#E D'#E *D3	/D66D?;E EN)r   r   r   r,  r)   r   r   r  r    s    "r   r  c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)LockContextTestCasec                    | j                   |              |5  | j                   |              d d d        | j                   |              | j                  t              5  |5  | j                   |              ddz   d d d        d d d        | j                   |              y # 1 sw Y   zxY w# 1 sw Y   4xY w# 1 sw Y   8xY wNrO   r   )rH   rF   r,   r`   )r   locklockeds      r   boilerPlatezLockContextTestCase.boilerPlate  s    " 	&OOFH%	&"01 	 )A	 	"	& 	& 	 	s/   B)#C&B5C)B25B>	:CC
c                 d    t        j                         }| j                  ||j                         y r   )	threadingLockr3  r2  r   r1  s     r   testWithLockz LockContextTestCase.testWithLock  s"    ~~t{{+r   c                 d    t        j                         }| j                  ||j                         y r   )r5  RLockr3  	_is_ownedr7  s     r   testWithRLockz!LockContextTestCase.testWithRLock  s"     t~~.r   c                 \    t        j                         fd}| j                  |       y )Nc                  $     j                         S r   )r;  r1  s   r   r2  z5LockContextTestCase.testWithCondition.<locals>.locked  s    >>##r   )r5  	Conditionr3  r   r2  r1  s     @r   testWithConditionz%LockContextTestCase.testWithCondition  s&    ""$	$v&r   c                 \    t        j                         fd}| j                  |       y )Nc                  J     j                  d      r j                          yyNFTacquirereleaser?  s   r   r2  z5LockContextTestCase.testWithSemaphore.<locals>.locked      ||E"r   )r5  	Semaphorer3  rA  s     @r   testWithSemaphorez%LockContextTestCase.testWithSemaphore  s(    ""$	 	v&r   c                 \    t        j                         fd}| j                  |       y )Nc                  J     j                  d      r j                          yyrE  rF  r?  s   r   r2  z<LockContextTestCase.testWithBoundedSemaphore.<locals>.locked  rI  r   )r5  BoundedSemaphorer3  rA  s     @r   testWithBoundedSemaphorez,LockContextTestCase.testWithBoundedSemaphore  s(    ))+	 	v&r   N)	r   r   r   r3  r8  r<  rB  rK  rO  r)   r   r   r.  r.    s     	#,/'''r   r.  c                   (    e Zd ZdZdZdZdZd Zd Zy)	mycontextz9Example decoration-compatible context manager for testingFNc                     d| _         | S NTstartedr3   s    r   r"   zmycontext.__enter__  s    r   c                 (    || _         | j                  S r   )r   catchr   r   s     r   r   zmycontext.__exit__  s    zzr   )	r   r   r   r   rU  r   rW  r"   r   r)   r   r   rQ  rQ    s    CG
CEr   rQ  c                   f    e Zd Zej                  d        Zd Zd Zd Zd Z	d Z
d Zd Zd	 Zd
 Zy)TestContextDecoratorc                 p    t         j                  }t               }| j                  |j                  |       y r   )rQ  r   rZ   r   s      r   r  z'TestContextDecorator.test_instance_docs  s*     !((kl3r   c                     t               }|5 }| j                  ||       | j                  |j                         d d d        | j	                  |j
                  d       y # 1 sw Y   &xY w)NNNN)rQ  r!   rF   rU  rZ   r   )r   contextresults      r   test_contextdecoratorz*TestContextDecorator.test_contextdecorator  sX    + 	-MM&'*OOGOO,	- 	&89		- 	-s   .A  A)c                    t               }| j                  t        d      5  |5  t        d      # 1 sw Y   nxY w	 d d d        n# 1 sw Y   nxY w| j                  |j                         | j                  |j                  d   t               t               }d|_        |5  t        d      # 1 sw Y   nxY w| j                  |j                         | j                  |j                  d   t               y )Nr   r   T)rQ  assertRaisesRegex	NameErrorassertIsNotNoner   r!   rW  )r   r^  s     r   $test_contextdecorator_with_exceptionz9TestContextDecorator.test_contextdecorator_with_exception  s    +##Iu5 	' '&&' ' '	' 	' 	' 	W[[)gkk!ni0+ 	#E""	# 	#W[[)gkk!ni0s$   A09	AA#B//B8c                 x     t                fd       } |         j                  j                  d       y )Nc                  r    j                   j                         j                   j                         y r   r   r   rF   rU  r^  r   s   r   testz1TestContextDecorator.test_decorator.<locals>.test)  %    gkk*OOGOO,r   r]  )rQ  rZ   r   r   rj  r^  s   ` @r   test_decoratorz#TestContextDecorator.test_decorator&  s8    +		- 
	- 	&89r   c                     t                fd       } j                  t        d      5   |        d d d         j                  j                          j                  j                  d   t               y # 1 sw Y   HxY w)Nc                      j                   j                         j                   j                         t	        d      )Nr   )r   r   rF   rU  rc  ri  s   r   rj  z@TestContextDecorator.test_decorator_with_exception.<locals>.test4  s0    gkk*OOGOO,E""r   r   r   )rQ  rb  rc  rd  r   r!   rl  s   ` @r   test_decorator_with_exceptionz2TestContextDecorator.test_decorator_with_exception1  sp    +		# 
	#
 ##Iu5 	F	W[[)gkk!ni0	 	s   A==Bc                    t                G fddt              } |       }|j                  dd       | j                  |j                  d       | j                  |j
                  d       | j                  |j                  d         |       }|j                  ddd       | j                  |j                  d       | j                  |j
                  d       | j                  |j                  d        |       }|j                  dd       | j                  |j                  d       | j                  |j
                  d       y )	Nc                   "    e Zd ZW  dd       Zy)9TestContextDecorator.test_decorating_method.<locals>.TestNc                 .    || _         || _        || _        y r   )r   r   r  )r   r   r   r  s       r   methodz@TestContextDecorator.test_decorating_method.<locals>.Test.methodE  s    r   r   )r   r   r   ru  )r^  s   r   Testrs  C  s     r   rv  rO      r   r   r  r   )rQ  objectru  rZ   r   r   r  )r   rv  rj  r^  s      @r   test_decorating_methodz+TestContextDecorator.test_decorating_method@  s    +	6 	 vAq##&vCc"%%%va1##r   c                      G d dt               }| j                  t        d      5   |       5  	 d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nc                       e Zd Zd Zd Zy)7TestContextDecorator.test_typo_enter.<locals>.mycontextc                      y r   r)   r3   s    r   	__unter__zATestContextDecorator.test_typo_enter.<locals>.mycontext.__unter__`      r   c                      y r   r)   rX  s     r   r   z@TestContextDecorator.test_typo_enter.<locals>.mycontext.__exit__b  r  r   N)r   r   r   r~  r   r)   r   r   rQ  r|  _      r   rQ  the context managerContextDecoratorrb  r-   r   rQ  s     r   test_typo_enterz$TestContextDecorator.test_typo_enter^  sY    	( 	 ##I/DE 	 	 	 	 	!   AAAA
	AAc                      G d dt               }| j                  t        d      5   |       5  	 d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nc                       e Zd Zd Zd Zy)6TestContextDecorator.test_typo_exit.<locals>.mycontextc                      y r   r)   r3   s    r   r"   z@TestContextDecorator.test_typo_exit.<locals>.mycontext.__enter__l  r  r   c                      y r   r)   rX  s     r   __uxit__z?TestContextDecorator.test_typo_exit.<locals>.mycontext.__uxit__n  r  r   N)r   r   r   r"   r  r)   r   r   rQ  r  k  r  r   rQ  zthe context manager.*__exit__r  r  s     r   test_typo_exitz#TestContextDecorator.test_typo_exitj  sY    	( 	 ##I/NO 	 	 	 	 	r  c                       G d dt               } G d d|t              } |        fd       } |         j                  j                  d       y )Nc                        e Zd ZdZdZd Zd Zy)HTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontextFNc                     d| _         | S rS  rT  r3   s    r   r"   zRTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__enter__{  s    #r   c                     || _         y r   r   rX  s     r   r   zQTestContextDecorator.test_contextdecorator_as_mixin.<locals>.somecontext.__exit__  	    r   )r   r   r   rU  r   r"   r   r)   r   r   somecontextr  w  s    GCr   r  c                       e Zd Zy)FTestContextDecorator.test_contextdecorator_as_mixin.<locals>.mycontextNr(   r)   r   r   rQ  r    r+   r   rQ  c                  r    j                   j                         j                   j                         y r   rh  ri  s   r   rj  zATestContextDecorator.test_contextdecorator_as_mixin.<locals>.test  rk  r   r]  )rx  r  rZ   r   )r   r  rQ  rj  r^  s   `   @r   test_contextdecorator_as_mixinz3TestContextDecorator.test_contextdecorator_as_mixinv  sU    		& 			%5 	 +		- 
	- 	&89r   c                      t         fd       }g  |d       fd       } |d        j                  g d       g  |d        j                  g d       y )Nc              3   Z   K   j                  |        d  j                  d       y w)NrQ   rR   )r  rU   s    r   rV   zETestContextDecorator.test_contextmanager_as_decorator.<locals>.woohoo  s!     LLOLLrW   rO   c                 N    j                  dg       j                  |        y r   )rZ   rS   )r\   r   rU   s    r   rj  zCTestContextDecorator.test_contextmanager_as_decorator.<locals>.test  s     UQC(LLOr   	something)rO   r  rQ   something else)rO   r  rQ   r   )r   rV   rj  rU   s   `  @r    test_contextmanager_as_decoratorz5TestContextDecorator.test_contextmanager_as_decorator  sn    		 
	
 		 
	 	[ 56  :;r   N)r   r   r   r   r   r  r`  re  rm  rp  ry  r  r  r  r  r)   r   r   rZ  rZ    sG      4 !4:1":1$<		:0<r   rZ  c                       e Zd ZdZej
                  d        Zd Zd Zd Z	d Z
d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)TestBaseExitStackNc                     | j                   j                  }| j                         }| j                  |j                  |       y r   )
exit_stackr   rZ   r   s      r   r  z$TestBaseExitStack.test_instance_docs  s3     ..ool3r   c                 P    | j                         5  	 d d d        y # 1 sw Y   y xY wr   )r  r3   s    r   test_no_resourcesz#TestBaseExitStack.test_no_resources  s$    __ 		 	 	s   %c                    di fdi fdi fdt        d      fdt        d      fdt        d      fdt        dd      fg}g fd	}| j                         5 }t        |      D ]o  \  }}|r|r |j                  |g|i |}n=|r |j                  |g| }n'|r |j                  |fi |}n|j                  |      }| j	                  ||       q |j
                  D ]s  }| j	                  |d   j                  |       | j                  |d   j                  |j                         | j                  |d   j                  |j                         u 	 d d d        | j                  |       g | j                         5 }| j                  t              5  |j                  d
       d d d        | j                  t              5  | j                  j                  d
       d d d        | j                  t              5  |j                  |d       d d d        d d d        | j                  g        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   mxY w# 1 sw Y   HxY w# 1 sw Y   LxY w)Nr)   )rO   )rO   rw  rO   )exampler      )r   callbackc                  ,    j                  | |f       y)zTest metadata propagationNrR   )r   r   r_  s     r   _exitz.TestBaseExitStack.test_callback.<locals>._exit  s    MM4,'r   )argrw  )r  r  )dictr  reversedr  r!   _exit_callbacks__wrapped__assertNotEqualr   r   r   rZ   r,   r-   )	r   expectedr  stackr   r   rd   wrapperr_  s	           @r   test_callbackzTestBaseExitStack.test_callback  s[   H2JBKa!4?#DO$Da!,-
 	( __ 	E%&x0 	(
dD&u<t<t<A&u4t4A&u55Au-Aa'	( !00 Egaj44e<##GAJ$7$7H!!'!*"4"4emmDE	E 	*__ 	6%""9- &1%&""9- 0((Q(/0""9- 6156	6 	$3	E 	E&& &0 06 6	6 	6sa   D H=I-I	/I-I)I-I!I-=I	I	I-I	I-!I*	&I--I6c                 N    t          fd}d } fd} G d dt              } j                         5 }|j                  |        j	                  |j
                  d   d   |        ||      }|j                  |        j	                  |j
                  d   d   j                  |       |j                  |        j	                  |j
                  d   d   |        ||      }|j                  |        j	                  |j
                  d   d   j                  |       |j                  |        j	                  |j
                  d   d   |       |j                  |        j	                  |j
                  d   d   |       ddz   d d d        y # 1 sw Y   y xY w)	Nc                 *    j                  |        y r   )r!   )r6   r   exc_tb
exc_raisedr   s      r   _expect_excz0TestBaseExitStack.test_push.<locals>._expect_exc  s    MM(J/r   c                       yrS  r)   exc_detailss    r   _suppress_excz2TestBaseExitStack.test_push.<locals>._suppress_exc      r   c                 l    j                  |        j                  |       j                  |       y r   )r   )r6   r   r  r   s      r   
_expect_okz/TestBaseExitStack.test_push.<locals>._expect_ok  s-    h'c"f%r   c                       e Zd Zd Zd Zd Zy)+TestBaseExitStack.test_push.<locals>.ExitCMc                     || _         y r   	check_exc)r   r  s     r   __init__z4TestBaseExitStack.test_push.<locals>.ExitCM.__init__  s	    !*r   c                 &    | j                  d       y )NzShould not be called!)r{   r3   s    r   r"   z5TestBaseExitStack.test_push.<locals>.ExitCM.__enter__  s    		12r   c                 "     | j                   |  y r   r  r   r  s     r   r   z4TestBaseExitStack.test_push.<locals>.ExitCM.__exit__  s    ,r   Nr   r   r   r  r"   r   r)   r   r   ExitCMr    s    +3-r   r  rO   r   )r`   rx  r  pushr!   r  __self__)r   r  r  r  r  r  cmr  s   `      @r   	test_pushzTestBaseExitStack.test_push  sg   &
	0		&	-V 	- __ 	%JJz"MM%//3A6
C
#BJJrNMM%//3A6??DJJ}%MM%//3A6F$BJJrNMM%//3A6??DJJ{#MM%//3A6DJJ{#MM%//3A6DaC	 	 	s   EFF$c                     G fddt               }g  |       }| j                         5 }|j                  fd       }| j                  |       |j	                  |       | j                  |j                  d   d   j                  |       j                  d       d d d        | j                  g d       y # 1 sw Y   xY w)Nc                   "    e Zd Z fdZ fdZy)4TestBaseExitStack.test_enter_context.<locals>.TestCMc                 (    j                  d       y r   rR   )r   r_  s    r   r"   z>TestBaseExitStack.test_enter_context.<locals>.TestCM.__enter__      a r   c                 (    j                  d       y Nr   rR   )r   r  r_  s     r   r   z=TestBaseExitStack.test_enter_context.<locals>.TestCM.__exit__   r  r   Nr9   r_  s   r   TestCMr    s    !!r   r  c                  (     j                  d       y )Nr  rR   r  s   r   r  z3TestBaseExitStack.test_enter_context.<locals>._exit      a r   r  rO   rw  )rO   rw  r   r  )
rx  r  r  rd  enter_contextr!   r  r  rS   rZ   )r   r  r  r  r  r_  s        @r   test_enter_contextz$TestBaseExitStack.test_enter_context  s    	!V 	! X__ 	%^^! !  '#MM%//3A6??DMM!	 	.	 	s   A4B==Cc                 F    G d d      } G d d      } G d d      }| j                         5 }| j                  t        d      5  |j                   |              d d d        | j                  t        d      5  |j                   |              d d d        | j                  t        d      5  |j                   |              d d d        | j	                  |j
                         d d d        y # 1 sw Y   xY w# 1 sw Y   nxY w# 1 sw Y   ExY w# 1 sw Y   y xY w)Nc                       e Zd Zy)FTestBaseExitStack.test_enter_context_errors.<locals>.LacksEnterAndExitNr(   r)   r   r   LacksEnterAndExitr    r+   r   r  c                       e Zd Zd Zy)?TestBaseExitStack.test_enter_context_errors.<locals>.LacksEnterc                      y r   r)   )r   exc_infos     r   r   zHTestBaseExitStack.test_enter_context_errors.<locals>.LacksEnter.__exit__  r  r   NrD   r)   r   r   
LacksEnterr        r   r  c                       e Zd Zd Zy)>TestBaseExitStack.test_enter_context_errors.<locals>.LacksExitc                      y r   r)   r3   s    r   r"   zHTestBaseExitStack.test_enter_context_errors.<locals>.LacksExit.__enter__  r  r   Nr@   r)   r   r   	LacksExitr    r  r   r  r  )r  rb  r-   r  rH   r  )r   r  r  r  r  s        r   test_enter_context_errorsz+TestBaseExitStack.test_enter_context_errors  s    	 		 		 	 __ 	4%''	3HI 9##$5$789''	3HI 2##JL12''	3HI 1##IK01U223	4 	49 92 21 1	4 	4sS   DC3D;C?D0D#D3C<	8D?D	DD	DD c                    g | j                         5 }|j                  fd       }| j                  |       |j                          j	                  d       d d d        | j                  ddg       y # 1 sw Y   xY w)Nc                  (     j                  d       y r   rR   r  s   r   r  z+TestBaseExitStack.test_close.<locals>._exit%  r  r   rw  rO   )r  r  rd  r  rS   rZ   )r   r  r  r_  s      @r   
test_closezTestBaseExitStack.test_close"  sx    __ 	%^^! !  'KKMMM!	 	!Q(	 	s   AA88Bc                 J   g | j                         5 }|j                  fd       }| j                  |       |j                         }j	                  d       d d d        j	                  d       j                          | j                  g d       y # 1 sw Y   ?xY w)Nc                  (     j                  d       y r  rR   r  s   r   r  z-TestBaseExitStack.test_pop_all.<locals>._exit0  r  r   rO   rw  )rO   rw  r   )r  r  rd  pop_allrS   r  rZ   )r   r  r  	new_stackr_  s       @r   test_pop_allzTestBaseExitStack.test_pop_all-  s    __ 	%^^! !  'IMM!	 	a+	 	s   ABB"c                     | j                  t              5  | j                         5 }|j                  d        ddz   d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nc                       y)NFr)   r   s    r   <lambda>z3TestBaseExitStack.test_exit_raise.<locals>.<lambda>=      r   rO   r   )r,   r`   r  r  r   r  s     r   test_exit_raisez!TestBaseExitStack.test_exit_raise:  s`    01 	" e

-.!	 	 	 	s!   AAAA	AA%c                 |    | j                         5 }|j                  d        ddz   d d d        y # 1 sw Y   y xY w)Nc                       yrS  r)   r   s    r   r  z6TestBaseExitStack.test_exit_suppress.<locals>.<lambda>B  r  r   rO   r   r  r  r  s     r   test_exit_suppressz$TestBaseExitStack.test_exit_suppress@  s7    __ 	%JJ()aC	 	 	s   2;c                    d }	 | j                         5 }|j                  |t               ddz   d d d        | j                  t               t	        j
                  |j                        }dg| j                  z   ddgz   }| j                  |D cg c]  }|j                  |j                  f c}|       | j                  |j                  t               t	        j
                  |j                  j                        }| j                  |D cg c]  }|j                  |j                  f c}dg       y # 1 sw Y   xY w# t        $ r}|}Y d }~d }~ww xY wc c}w c c}w )Nc                     | r   r)   r   s    r   	raise_exczBTestBaseExitStack.test_exit_exception_traceback.<locals>.raise_excK      Ir   rO   r   )test_exit_exception_tracebackz with self.exit_stack() as stack:)_exit_wrapperzcallback(*args, **kwds))r  z	raise exc)r  rh   )r  r  
ValueErrorr   r8   rs   rt   callback_error_internal_framesrZ   rv   rw   __context__r`   )	r   r  r  r|   r   	ve_framesr  rd   
zde_framess	            r   r  z/TestBaseExitStack.test_exit_exception_tracebackE  s@   		" ey*5! 	c:.(():):;	RS//09')) 	 	'01!affaff18	= 	coo/@A))#//*G*GH
J?q166166*?BC	E'   	C	 2 @s9   E D:E E E%:E?E 	EEEc           	          G d d      } G d d      } G d d      }	  |t               5   |t        t              5   |       5   |t              5  ddz   d d d        d d d        d d d        d d d        | j	                  d	       |j                  d   }| j                  |t               | j                  |j                  t               y # 1 sw Y   xxY w# 1 sw Y   |xY w# 1 sw Y   xY w# 1 sw Y   xY w# t         $ r}| j                  |j                  t               | j                  |j                  j                  t               | j                  |j                  j                  j                         Y d }~d }~ww xY w)
Nc                       e Zd Zd Zd Zd Zy)JTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcc                     || _         y r   r   rX  s     r   r  zSTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__init__i  r  r   c                     | S r   r)   r3   s    r   r"   zTTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__enter__k  r4   r   c                     | j                   r   r   r  s     r   r   zSTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExc.__exit__m  s    hhr   Nr  r)   r   r   RaiseExcr  h  s    r   r  c                       e Zd Zd Zd Zd Zy)UTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContextc                      || _         || _        y r   )outerinner)r   r  r  s      r   r  z^TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__init__q  s    "
"
r   c                     | S r   r)   r3   s    r   r"   z_TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__enter__t  r4   r   c                 >    	 | j                   #  | j                  xY wr   )r  r  r  s     r   r   z^TestBaseExitStack.test_exit_exception_chaining_reference.<locals>.RaiseExcWithContext.__exit__v  s"    %**$%**$s    Nr  r)   r   r   RaiseExcWithContextr  p  s    #%r   r  c                       e Zd Zd Zd Zy)MTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExcc                     | S r   r)   r3   s    r   r"   zWTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__enter__}  r4   r   c                 $    |t        |       _        yrS  )rq   saved_detailsr  s     r   r   zVTestBaseExitStack.test_exit_exception_chaining_reference.<locals>.SuppressExc.__exit__  s    +6T
(r   Nr9   r)   r   r   SuppressExcr  |  s    r   r  rO   r   0Expected IndexError, but no exception was raised)

IndexErrorKeyErrorAttributeErrorr  r{   r   r  r   r  r`   )r   r  r  r  r   	inner_excs         r   &test_exit_exception_chaining_referencez8TestBaseExitStack.test_exit_exception_chaining_referencee  sH   	 	
	% 
	%	 		J*% "(>B "$ "%j1 "E"""" IIHI--a0	i4i335FG" "" "" "" "
  	G!!#//8<!!#//"="=~Ncoo99EEFF		Gsv   D  C4C(CC	C!C()C41D  CCC%!C((C1	-C44C=9D   	F	A9FFc                    d }d fd}	 | j                         5 }|j                  |t               |j                  |t               |j                  |t               |j                  |       |j                  |t               ddz   d d d        | j                  d       d   }| j                  |t               | j                  |j                  t               y # 1 sw Y   VxY w# t        $ r}| j                  |j                  t               | j                  |j                  j                  t               | j                  |j                  j                  j                         Y d }~d }~ww xY w)Nc                     | r   r)   r   s    r   r  zATestBaseExitStack.test_exit_exception_chaining.<locals>.raise_exc  r  r   c                  
    | yrS  r)   )r  r  s    r   suppress_exczDTestBaseExitStack.test_exit_exception_chaining.<locals>.suppress_exc  s    'Mr   rO   r   r   )r  r  r!  r"  r#  r  r  r{   r   r  r   r`   )r   r  r)  r  r   r$  r  s         @r   test_exit_exception_chainingz.TestBaseExitStack.test_exit_exception_chaining  s(   	 	
	J" ey*5y(3y.9

<(y*5A IIHI!!$	i4i335FG#   	G!!#//8<!!#//"="=~Ncoo99EEFF		Gs0   C- A/C!C- !C*&C- -	E96A9E44E9c                 |     G d dt               t        fd       t         fd       }|fD ]%  } j                         5  	  |       5  t                y # 1 sw Y   nxY w	  j	                  d       n-# $ r%} j                  |j                         Y d }~nd }~ww xY wd d d        ~# 1 sw Y   xY w)Nc                       e Zd Zy)PTestBaseExitStack.test_exit_exception_explicit_none_context.<locals>.MyExceptionNr(   r)   r   r   MyExceptionr-    r+   r   r.  c               3   ^   K   	 d  y # t         $ r         } 	 | # d | _        w xY ww xY wwr   )BaseExceptionr  )r   r.  s    r   my_cmzJTestBaseExitStack.test_exit_exception_explicit_none_context.<locals>.my_cm  s7     +  +!m+I&*CO+s    -
 -*	&*-c               3      K   j                         5 } | j                                 |  d d d        y # 1 sw Y   y xY wwr   )r  r  )r  r1  r   s    r   my_cm_with_exit_stackzZTestBaseExitStack.test_exit_exception_explicit_none_context.<locals>.my_cm_with_exit_stack  s<     " e##EG,  s   A8	AAAr   )r   rY   rz   r!  r{   r   r  )r   r3  r  r   r.  r1  s   `   @@r   )test_exit_exception_explicit_none_contextz;TestBaseExitStack.test_exit_exception_explicit_none_context  s    	) 	 
	+ 
	+ 
	 
	
 /0 	RB RR +(l*		R+ + +
 IIPQ # 7%%coo667	R R RsH   B2	A<AA&"A<*B2<B&B!B2!B&&B22B;	c                 f   d }d }	 | j                         5 }|j                  d        |j                  |t               d d d        | j                  d       	 | j                         5 }|j                  |t               |j                  |       |j                  |t               d d d        | j                  d       y # 1 sw Y   xY w# t        $ r }| j                  |t               Y d }~d }~ww xY w# 1 sw Y   SxY w# t        $ r }| j                  |t               Y d }~y d }~ww xY w)Nc                     | r   r)   r   s    r   r  zHTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.raise_exc  r  r   c                       yrS  r)   r  s    r   r)  zKTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.suppress_exc  r  r   c                       y r   r)   r)   r   r   r  zGTestBaseExitStack.test_exit_exception_non_suppressing.<locals>.<lambda>  r  r   r   z.Expected KeyError, but no exception was raised)r  r  r!  r{   r   r   r"  r  )r   r  r)  r  r   s        r   #test_exit_exception_non_suppressingz5TestBaseExitStack.test_exit_exception_non_suppressing  s   			J" 6e|,y*56 IIHI	H" 6ey(3

<(y*56 IIFG!6 6  	3!!#z22	36 6  	1!!#x00	1s^   C )CC D +>C;)D CC 	C8C33C8;D D 	D0D++D0c                    t         d        }t        d      }t        d      }t        d      }t        d      }	 | j                         5 }|j                   ||             |j                   ||             |j                   ||             |# 1 sw Y   y xY w# t        $ r}| j	                  ||       | j	                  |j
                  |       | j	                  |j
                  j
                  |       | j	                  |j
                  j
                  j
                  |       | j                  |j
                  j
                  j
                  j
                         Y d }~y d }~ww xY w)Nc              3   $   K   	 d  | # | xY wwr   r)   r   s    r   gets_the_context_rightzZTestBaseExitStack.test_exit_exception_with_correct_context.<locals>.gets_the_context_right  s     	c	s   
 rO   rw  r   r  )rY   r   r  r  r!   r  r   )r   r<  exc1exc2exc3exc4r  r   s           r   (test_exit_exception_with_correct_contextz:TestBaseExitStack.test_exit_exception_with_correct_context  s4   		 
	 ||||
	L" e##$:4$@A##$:4$@A##$:4$@A
	 
  	LMM#t$MM#//40MM#//55t<MM#//55AA4H22>>JJL L	Ls1   B 
ABBB B 	E.'B=E))E.c                 2   d }t        d      }t        d      }t        d      }t        d      }t        d      }	 | j                         5 }|j                  |||       |j                  |||       |# 1 sw Y   y xY w# t         $ r}| j                  ||       | j                  |j                  |       | j                  |j                  j                  |       | j                  |j                  j                  j                  |       | j                  |j                  j                  j                  j                  |       | j                  |j                  j                  j                  j                  j                         Y d }~y d }~ww xY w)Nc                     	 | # |xY wr   r)   )r$  	outer_excs     r   raise_nestedzQTestBaseExitStack.test_exit_exception_with_existing_context.<locals>.raise_nested  s     s    rO   rw  r   r  r   )r   r  r  r!   r  r   )	r   rE  r=  r>  r?  r@  exc5r  r   s	            r   )test_exit_exception_with_existing_contextz;TestBaseExitStack.test_exit_exception_with_existing_context  sB   	 
 |||||	Q" e|T48|T48
   	QMM#t$MM#//40MM#//55t<MM#//55AA4HMM,,88DDdL++77CCOOQ Q	Qs0   B )A55A>:B >B 
FDFFc                     d }	 | j                         5 }|j                  |       ddz   d d d        y # 1 sw Y   y xY w# t        $ r}| j                  d       Y d }~y d }~ww xY w)Nc                       yrS  r)   r  s    r   r)  zDTestBaseExitStack.test_body_exception_suppress.<locals>.suppress_exc-  r  r   rO   r   z%Expected no exception, got IndexError)r  r  r!  r{   )r   r)  r  r   s       r   test_body_exception_suppressz.TestBaseExitStack.test_body_exception_suppress,  sa    		?" e

<(!    	?II=>>	?s.   A 5A >A A 	A%
A  A%c                     | j                         5 }|j                  d        |j                  d        |j                  d        d d d        y # 1 sw Y   y xY w)Nc                       yrS  r)   r   s    r   r  zITestBaseExitStack.test_exit_exception_chaining_suppress.<locals>.<lambda>8  r  r   c                      ddz  S r0  r)   r   s    r   r  zITestBaseExitStack.test_exit_exception_chaining_suppress.<locals>.<lambda>9  s
    AaC r   c                      i d   S r   r)   r   s    r   r  zITestBaseExitStack.test_exit_exception_chaining_suppress.<locals>.<lambda>:  s
    BqE r   r  r  s     r   %test_exit_exception_chaining_suppressz7TestBaseExitStack.test_exit_exception_chaining_suppress6  sJ    __ 	+%JJ()JJ'(JJ)*	+ 	+ 	+s   7AAc                     | j                         5 }t        d      D ]  }|j                  t                	 d d d        y # 1 sw Y   y xY w)Ni'  )r  ranger  int)r   r  is      r   test_excessive_nestingz(TestBaseExitStack.test_excessive_nesting<  sB    __ 	$%5\ $s#$	$ 	$ 	$s   &AA
c                 j    G d dt               } |       }t               |_        t               |_        | j                         }| j	                  t
        d      5  |j                  |       d d d        |j                  |       | j                  |j                  d   d   |       y # 1 sw Y   =xY w)Nc                       e Zd Zy)7TestBaseExitStack.test_instance_bypass.<locals>.ExampleNr(   r)   r   r   ExamplerW  C      r   rX  r  r  rO   )
rx  r"   r   r  rb  r-   r  r  r!   r  )r   rX  r  r  s       r   test_instance_bypassz&TestBaseExitStack.test_instance_bypassB  s    #f#Yxh!##I/DE 	$#	$

2e++B/2B7	$ 	$s   B))B2c                     G d dt                G d dt              }t        fd       }t        d        }| j                        5 }| j	                         5 }|j                   |              |j                   |               |d      # 1 sw Y   nxY w	 d d d        n# 1 sw Y   nxY wj                  }| j                  |       | j                  |j                  |       | j                  |j                  j                         | j                  |j                  j                         | j                  |j                  |j                         y )Nc                       e Zd Zy)ITestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueExceptionNr(   r)   r   r   UniqueExceptionr]  O  rY  r   r^  c                       e Zd Zy)LTestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.UniqueRuntimeErrorNr(   r)   r   r   UniqueRuntimeErrorr`  P  rY  r   ra  c               3   L   K   	 d y # t         $ r}  d      | d } ~ ww xY ww)NrO   znew exceptionr   )r   r^  s    r   secondz@TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.secondR  s0     @ @%o6C?@s   $
 $	!	!$c               3   <   K   	 d y # t         $ r} | d } ~ ww xY wwr   rc  r   s    r   firstz?TestBaseExitStack.test_dont_reraise_RuntimeError.<locals>.firstY  s$      	s   	 	zplease no infinite loop.)r   rx   rY   r,   r  r  	exceptionr   r  r   r   r!   )r   ra  rd  rf  err_ctxes_ctxr   r^  s          @r   test_dont_reraise_RuntimeErrorz0TestBaseExitStack.test_dont_reraise_RuntimeErrorM  s/   .i.44		@ 
	@ 
	 
	 / 	E7" Ef$$VX.$$UW-()CDDE E E	E 	E 	E c?3coo/AB#//556#//334cmmS__5s   B'5BB	B''B0)r   r   r   r  r   r   r  r  r  r  r  r  r  r  r  r  r  r%  r*  r4  r9  rA  rG  rJ  rO  rT  rZ  rj  r)   r   r   r  r    s    J  4 !4'%R D/&4&	),
E@.H`H> RDH6L>Q8?+$	8 6r   r  c                       e Zd ZeZddgZy)TestExitStack)r   zraise exc_details[1])r   zif cb(*exc_details):N)r   r   r   	ExitStackr  r  r)   r   r   rl  rl  p  s    J,,&"r   rl  c                   V    e Zd ZdZdZej                  d        Zd Zd Z	d Z
d Zd Zy)TestRedirectStreamNc                     | j                   j                  }| j                  d       }| j                  |j                  |       y r   )redirect_streamr   rZ   r   s      r   r  z%TestRedirectStream.test_instance_docs}  s9     ++33""4(l3r   c                     t        t        | j                        }| j                  d        | j	                  t        t        | j                        |       y r   )getattrsysorig_streamrq  r!   )r   orig_stdouts     r   test_no_redirect_in_initz+TestRedirectStream.test_no_redirect_in_init  s=    c4#3#34T"gc4#3#34kBr   c                    t        j                         }d}t        t        | j                        }| j                  |      5  t        |t        t        | j                               d d d        | j                  t        t        | j                        |       |j                         j                         }| j                  ||       y # 1 sw Y   dxY w)Nz<Consider an API like help(), which prints directly to stdoutfile)ioStringIOrs  rt  ru  rq  printr!   getvaluestriprZ   )r   rd   msgrv  ss        r   test_redirect_to_string_ioz-TestRedirectStream.test_redirect_to_string_io  s    KKMLc4#3#34!!!$ 	<#GC)9)9:;	<gc4#3#34kBJJL C 		< 	<s   &CCc                     t        j                         }| j                  |      5 }| j                  ||       d d d        y # 1 sw Y   y xY wr   )r{  r|  rq  r!   )r   rd   enter_results      r   test_enter_result_is_targetz.TestRedirectStream.test_enter_result_is_target  s@    KKM!!!$ 	+MM,*	+ 	+ 	+s   AAc           	         t        j                         }| j                  |      }t        t        | j
                        }|5  t        ddt        t        | j
                               d d d        |5  t        dt        t        | j
                               d d d        | j                  t        t        | j
                        |       |j                         }| j                  |d       y # 1 sw Y   xY w# 1 sw Y   bxY wNHello )endrz  zWorld!ry  zHello World!

r{  r|  rq  rs  rt  ru  r}  r!   r~  rZ   r   rd   
write_to_frv  r  s        r   test_cm_is_reusablez&TestRedirectStream.test_cm_is_reusable  s    KKM))!,
c4#3#34 	I'sd6F6F)GH	I 	A(d.>.>!?@	Agc4#3#34kBJJL,-	I 	I	A 	As   'C.3&C:.C7:Dc           	         t        j                         }| j                  |      }t        t        | j
                        }|5  t        ddt        t        | j
                               |5  t        dt        t        | j
                               d d d        d d d        | j                  t        t        | j
                        |       |j                         }| j                  |d       y # 1 sw Y   ^xY w# 1 sw Y   bxY wr  r  r  s        r   test_cm_is_reentrantz'TestRedirectStream.test_cm_is_reentrant  s    KKM))!,
c4#3#34 	E'sd6F6F)GH EhWS$2B2B%CDE	E 	gc4#3#34kBJJL,-	E E	E 	Es$   )C:+&C.C:.C7	3C::D)r   r   r   rq  ru  r   r   r  rw  r  r  r  r  r)   r   r   ro  ro  x  s>    OK  4 !4C
!+

.
.r   ro  c                       e Zd ZeZdZy)TestRedirectStdoutstdoutN)r   r   r   redirect_stdoutrq  ru  r)   r   r   r  r        %OKr   r  c                       e Zd ZeZdZy)TestRedirectStderrstderrN)r   r   r   redirect_stderrrq  ru  r)   r   r   r  r    r  r   r  c                   f    e Zd Zej                  d        Zd Zd Zd Zd Z	d Z
d Zd Zd	 Zd
 Zy)TestSuppressc                 p    t         j                  }t               }| j                  |j                  |       y r   )suppressr   rZ   r   s      r   r  zTestSuppress.test_instance_docs  s*      ''jl3r   c                 n    t        t              5 }| j                  |       d d d        y # 1 sw Y   y xY wr   )r  r  r   )r   r  s     r   test_no_result_from_enterz&TestSuppress.test_no_result_from_enter  s/    j! 	,\l+	, 	, 	,s   +4c                     t        t              5  | j                  t        dd      d       d d d        y # 1 sw Y   y xY w)Nrw  r       )r  r  rZ   powr3   s    r   test_no_exceptionzTestSuppress.test_no_exception  s5    j! 	,SAY+	, 	, 	,s   6?c                 b    t        t              5  t        d       d d d        y # 1 sw Y   y xY w)Nr   )r  r-   ru   r3   s    r   test_exact_exceptionz!TestSuppress.test_exact_exception  s&    i  	F	 	 	s   %.c                 V    t        t              5  dd    d d d        y # 1 sw Y   y xY w)Nr  2   )r  LookupErrorr3   s    r   test_exception_hierarchyz%TestSuppress.test_exception_hierarchy  s&    k" 	BK	 	 	s   (c                     | j                  t              5  t        t              5  ddz   d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wr0  )r,   r`   r  r-   r3   s    r   test_other_exceptionz!TestSuppress.test_other_exception  sO    01 	)$ !	 	 	 	s   A	=A	A	A		Ac                     | j                  t              5  t               5  ddz   d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wr0  )r,   r`   r  r3   s    r   test_no_argszTestSuppress.test_no_args  sL    01 	 !	 	 	 	s   A8AA	AAc                     t        t        t              5  ddz   d d d        t        t        t              5  t        d       d d d        y # 1 sw Y   2xY w# 1 sw Y   y xY w)NrO   r   r   )r  r`   r-   ru   r3   s    r   test_multiple_exception_argsz)TestSuppress.test_multiple_exception_args  sU    '3 	aC	'3 	F	 		 		 	s   AAAA!c                 8   t        t              }|5  	 d d d        |5  t        d       d d d        |5  |5  t        d       d d d        d}ddz   d d d        | j                         y # 1 sw Y   YxY w# 1 sw Y   OxY w# 1 sw Y   BxY w# 1 sw Y   ?xY w)Nr   TrO   r   )r  r   ru   rF   )r   ignore_exceptionsouter_continueds      r   r  z!TestSuppress.test_cm_is_reentrant  s    $Y/ 		 	F	 	" A"OaC		
 	(	 		 	 	 	s8   A,A8BBB,A58BB		BBc           
      z   d }d }t        t              5   |       # 1 sw Y   nxY wt        t        t              5   |       # 1 sw Y   nxY w| j                  t              5 }t        t              5   |       # 1 sw Y   nxY w	 d d d        n# 1 sw Y   nxY w| j                  j                  t	        dt        d      t        d      g             t        t              5  t        dt               g      # 1 sw Y   nxY w| j                  t              5 }t        t              5  t        dt        d      t        d      g      # 1 sw Y   nxY w	 d d d        n# 1 sw Y   nxY w| j                  |j                  t        dt        d      g             | j                  t              5 }t        t              5  t        dt        d      t        d      g      # 1 sw Y   nxY w	 d d d        n# 1 sw Y   nxY w| j                  |j                  t	        dt        d      g             y )	Nc                  V    t        dt        d      t        d      t        d      g      S )NzEG with ValueErrors onlyve1ve2ve3)ExceptionGroupr  r)   r   r   r  z4TestSuppress.test_exception_groups.<locals>.<lambda>  s'    &
5 1:e3DE
 r   c            	      j    t        dt        d      t        d      t        d      t        d      g      S )N EG with many types of exceptionsr  ke1r  ke2)r  r  r"  r)   r   r   r  z4TestSuppress.test_exception_groups.<locals>.<lambda>  s,    .E1BHUOT
 r   r  r  r  messagegr   )	r  r  r"  r,   r  assertExceptionIsLikerg  GeneratorExitBaseExceptionGroup)r   eg_veeg_alleg1s       r   test_exception_groupsz"TestSuppress.test_exception_groups  s   

 j! 	'M	 	j(+ 	(N	 	~. 	#*% h  	 	 	 	""MM2%(5/2	
 m$ 	C$Y0ABB	C 	C 12 	Yc(# Y(]35GRU4WXXY Y Y	Y 	Y 	Y 	""MM-i-:L9MN	
 ~. 	Y#-( Y(]35GRU4WXXY Y Y	Y 	Y 	Y 	""MM>)hsm_E	
sy   'AA'B7A??B	BB*D  D	!E(1!EE	E((E19H 	!G**G3	/H  H	N)r   r   r   r   r   r  r  r  r  r  r  r  r  r  r  r)   r   r   r  r    sG      4 !4,,

)(
r   r  c                   $    e Zd Zd Zd Zd Zd Zy)	TestChdirc                     t        j                  j                  t         j                  j                  t         j                  j	                  t
                    g| S r   )ospathjoindirnamerealpath__file__)r   partss     r   make_relative_pathzTestChdir.make_relative_path  s>    ww||GGOOBGG,,X67

 	
r   c                 B   t        j                         }| j                  d      }| j                  ||       t	        |      5  | j                  t        j                         |       d d d        | j                  t        j                         |       y # 1 sw Y   .xY w)Ndata)r  getcwdr  r  chdirrZ   )r   old_cwdr   s      r   test_simplezTestChdir.test_simple%  su    ))+((0GV,6] 	2RYY[&1	2g.	2 	2s   %BBc                    t        j                         }| j                  d      }| j                  d      }| j                  |||f       t	        |      t	        |      }}|5  | j                  t        j                         |       |5  | j                  t        j                         |       |5  | j                  t        j                         |       d d d        | j                  t        j                         |       d d d        | j                  t        j                         |       d d d        | j                  t        j                         |       y # 1 sw Y   xY w# 1 sw Y   fxY w# 1 sw Y   FxY w)Nr  ziptestdata)r  r  r  assertNotInr  rZ   )r   r  target1target2chdir1chdir2s         r   test_reentrantzTestChdir.test_reentrant.  s#   ))+))&1))-87G"45ww 	3RYY['2 7  g6 ;$$RYY[':;  g6	7
 RYY['2	3 	g.	; ;7 7	3 	3s<   #'E3
'E'1%E,E',E3E$ E''E0	,E33E<c                    t        j                         }| j                  d      }| j                  ||       	 t	        |      5  | j                  t        j                         |       t        d      # 1 sw Y   nxY wn1# t        $ r%}| j                  t        |      d       Y d }~nd }~ww xY w| j                  t        j                         |       y )Nr  boom)r  r  r  r  r  rZ   rx   str)r   r  r   res       r   test_exceptionzTestChdir.test_exception?  s    ))+((0GV,	.v +  f5"6**+ + +  	.SWf--	.g.s)   B 0A44A=9B 	B/
B**B/N)r   r   r   r  r  r  r  r)   r   r   r  r    s    
//"/r   r  __main__)#r   r{  r  rt  r$  r5  r8   unittest
contextlibrj  r   test.supportr   test.support.testcaser   r   TestCaser   rK   r   r  r  r.  r  rQ  rZ  r  rl  ro  r  r  r  r  r   mainr)   r   r   <module>r     s^   ? 	 	 
       " 6 (E!2!2 (EVF#X.. F#R
%h'' %D#(++ #"(++ "(-'(++ -'`  ]<8,, ]<@J6 J6Z%x'8'8 6. 6.r+X->-> +X->-> ^
'):): ^
B,/!! ,/^ zHMMO r   