
    <eh                     N   d Z dZdZddlZddlZddlmZ ddlmZm	Z	m
Z
  G d de          Zd	 Z G d
 de          Zed             Z G d de          Z G d de          Z G d dej                  Z G d de          Z G d dej        e          Z G d dej        e          Z G d de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ed#k    r ej                     dS dS )$z7Unit tests for the with statement specified in PEP 343.z
Mike Blandzmbland at acm dot org    N)deque)_GeneratorContextManagercontextmanagernullcontextc                   *     e Zd Z fdZd Zd Z xZS )MockContextManagerc                 ^     t                      j        |  d| _        d| _        d | _        d S NF)super__init__enter_calledexit_called	exit_args)selfargs	__class__s     %/usr/lib/python3.11/test/test_with.pyr   zMockContextManager.__init__   s2    $!     c                 8    d| _         t          j        |           S NT)r   r   	__enter__r   s    r   r   zMockContextManager.__enter__   s     '1$777r   c                 R    d| _         |||f| _        t          j        | |||          S r   )r   r   r   __exit__r   typevalue	tracebacks       r   r   zMockContextManager.__exit__   s9    y1'0t16	C C 	Cr   )__name__
__module____qualname__r   r   r   __classcell__)r   s   @r   r   r      s]            8 8 8C C C C C C Cr   r   c                       fd}|S )Nc                  &    t          | |          S N)r   )r   kwdsfuncs     r   helperz#mock_contextmanager.<locals>.helper    s    !$d333r    )r'   r(   s   ` r   mock_contextmanagerr*      s!    4 4 4 4 4Mr   c                       e Zd Zd ZdS )MockResourcec                 "    d| _         d| _        d S r
   )yieldedstoppedr   s    r   r   zMockResource.__init__&   s    r   Nr   r    r!   r   r)   r   r   r,   r,   %   s#            r   r,   c               #   d   K   t                      } 	 d| _        | V  d| _        d S # d| _        w xY wr   )r,   r.   r/   )mocks    r   mock_contextmanager_generatorr3   +   sB      >>D


ts   & 	/c                        e Zd Zd Zd Zd ZdS )Nestedc                 "    || _         d | _        d S r%   )managersenteredr   r7   s     r   r   zNested.__init__7   s     r   c                 <   | j         t          d          t                      | _         g }	 | j        D ]C}|                    |                                           | j                             |           Dn##   | j        t          j	                     s Y nxY w|S )NzContext is not reentrant)
r8   RuntimeErrorr   r7   appendr   
appendleftr   sysexc_info)r   varsmgrs      r   r   zNested.__enter__;   s    <#9:::ww	} - -CMMOO,,,'',,,,-	 4=#,..1  s   AA9 9Bc                     |}| j         D ])}	  |j        | rd}#  t          j                    }Y 'xY wd | _         ||ur0 |d         |d                                       |d                   d S )NNNNr         )r8   r   r>   r?   with_traceback)r   r?   exrA   s       r   r   zNested.__exit__I   s     < 	$ 	$C$3<$ ,+B$\^^X"Q%1,,--be444 s   1Nr   r    r!   r   r   r   r)   r   r   r5   r5   5   sA            5 5 5 5 5r   r5   c                        e Zd Zd Zd Zd ZdS )
MockNestedc                 T    t          j        | g|R   d| _        d| _        d | _        d S r
   )r5   r   r   r   r   r9   s     r   r   zMockNested.__init__Z   s5    (x((((! r   c                 D    d| _         t                              |           S r   )r   r5   r   r   s    r   r   zMockNested.__enter__`   s     %%%r   c                 B    d| _         || _        t          j        | g|R  S r   )r   r   r5   r   r   r?   s     r   r   zMockNested.__exit__d   s)    !t/h////r   NrH   r)   r   r   rJ   rJ   Y   sA          & & &0 0 0 0 0r   rJ   c                   J    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S )FailureTestCasec                 B    d }|                      t          |           d S )Nc                  F    t           5  d d d            d S # 1 swxY w Y   d S r%   foor)   r   r   fooNotDeclaredz5FailureTestCase.testNameError.<locals>.fooNotDeclaredl   s0    NNNNNNNNNNNNNNNNNNNNs   )assertRaises	NameError)r   rU   s     r   testNameErrorzFailureTestCase.testNameErrork   s-    	 	 	)^44444r   c                 p     G d dt                     fd}|                     t          d|           d S )Nc                       e Zd Zd ZdS )<FailureTestCase.testEnterAttributeError1.<locals>.LacksEnterc                     d S r%   r)   r   s       r   r   zEFailureTestCase.testEnterAttributeError1.<locals>.LacksEnter.__exit__r       r   N)r   r    r!   r   r)   r   r   
LacksEnterr[   q   #            r   r^   c                  R                 } | 5  d d d            d S # 1 swxY w Y   d S r%   r)   )rT   r^   s    r   fooLacksEnterz?FailureTestCase.testEnterAttributeError1.<locals>.fooLacksEnteru   s<    *,,CNNNNNNNNNNNNNNNNNNNN     the context managerobjectassertRaisesRegex	TypeError)r   ra   r^   s     @r   testEnterAttributeError1z(FailureTestCase.testEnterAttributeError1p   sg    	 	 	 	 	 	 	 		 	 	 	 	 	y*?OOOOOr   c                 p     G d dt                     fd}|                     t          d|           d S )Nc                       e Zd ZdS )CFailureTestCase.testEnterAttributeError2.<locals>.LacksEnterAndExitNr   r    r!   r)   r   r   LacksEnterAndExitrk   {   s        Dr   rm   c                  R                 } | 5  d d d            d S # 1 swxY w Y   d S r%   r)   )rT   rm   s    r   fooLacksEnterAndExitzFFailureTestCase.testEnterAttributeError2.<locals>.fooLacksEnterAndExit~   s?    ##%%CNNNNNNNNNNNNNNNNNNNNrb   rc   rd   )r   ro   rm   s     @r   testEnterAttributeError2z(FailureTestCase.testEnterAttributeError2z   sh    	 	 	 	 	 	 	 		 	 	 	 	 	y*?AUVVVVVr   c                 p     G d dt                     fd}|                     t          d|           d S )Nc                       e Zd Zd ZdS )9FailureTestCase.testExitAttributeError.<locals>.LacksExitc                     d S r%   r)   r   s    r   r   zCFailureTestCase.testExitAttributeError.<locals>.LacksExit.__enter__   r]   r   N)r   r    r!   r   r)   r   r   	LacksExitrs      r_   r   ru   c                  R                 } | 5  d d d            d S # 1 swxY w Y   d S r%   r)   )rT   ru   s    r   fooLacksExitz<FailureTestCase.testExitAttributeError.<locals>.fooLacksExit   s<    )++CNNNNNNNNNNNNNNNNNNNNrb   zthe context manager.*__exit__rd   )r   rw   ru   s     @r   testExitAttributeErrorz&FailureTestCase.testExitAttributeError   sg    	 	 	 	 	 	 	 		 	 	 	 	 	y*I<XXXXXr   c                 D    d }|                      t          ||           d S )Nc                 (    t          | dd           d S )N single)compile)ss    r   shouldRaiseSyntaxErrorzGFailureTestCase.assertRaisesSyntaxError.<locals>.shouldRaiseSyntaxError   s    Ar8$$$$$r   )rV   SyntaxError)r   codestrr   s      r   assertRaisesSyntaxErrorz'FailureTestCase.assertRaisesSyntaxError   s0    	% 	% 	%+'=wGGGGGr   c                 Z    |                      d           |                      d           d S )Nzwith mock as None:
  passzwith mock as (None):
  passr   r   s    r   testAssignmentToNoneErrorz)FailureTestCase.testAssignmentToNoneError   s>    $$%ABBB$$	 	 	 	 	r   c                 Z    |                      d           |                      d           d S )Nzwith mock as None,:
  passzwith mock as (None,):
  passr   r   s    r   ,testAssignmentToTupleOnlyContainingNoneErrorz<FailureTestCase.testAssignmentToTupleOnlyContainingNoneError   s>    $$%BCCC$$	 	 	 	 	r   c                 0    |                      d           d S )Nz%with mock as (foo, None, bar):
  passr   r   s    r   (testAssignmentToTupleContainingNoneErrorz8FailureTestCase.testAssignmentToTupleContainingNoneError   s)    $$	 	 	 	 	r   c                       G d dt                      fd}                     t          |                                 j        d            d S )Nc                       e Zd Zd Zd ZdS )4FailureTestCase.testEnterThrows.<locals>.EnterThrowsc                      t          d          )NzEnter threwr;   r   s    r   r   z>FailureTestCase.testEnterThrows.<locals>.EnterThrows.__enter__   s    "=111r   c                     d S r%   r)   r   r   s     r   r   z=FailureTestCase.testEnterThrows.<locals>.EnterThrows.__exit__   r]   r   Nr   r    r!   r   r   r)   r   r   EnterThrowsr      s2        2 2 2    r   r   c                  l                 } d _         | 5 _         	 d d d            d S # 1 swxY w Y   d S r%   rS   )ctr   r   s    r   shouldThrowz4FailureTestCase.testEnterThrows.<locals>.shouldThrow   s    BDH tx                 s   )--)re   rV   r;   assertEqualrT   )r   r   r   s   ` @r   testEnterThrowszFailureTestCase.testEnterThrows   s    	 	 	 	 	& 	 	 		 	 	 	 	 	
 	,4444(((((r   c                 n     G d dt                     fd}|                     t          |           d S )Nc                       e Zd Zd Zd ZdS )2FailureTestCase.testExitThrows.<locals>.ExitThrowsc                     d S r%   r)   r   s    r   r   z<FailureTestCase.testExitThrows.<locals>.ExitThrows.__enter__   s    r   c                      t          d          )N*   r   r   s     r   r   z;FailureTestCase.testExitThrows.<locals>.ExitThrows.__exit__   s    "2&&&r   Nr   r)   r   r   
ExitThrowsr      s2          ' ' ' ' 'r   r   c                  P                  5  	 d d d            d S # 1 swxY w Y   d S r%   r)   )r   s   r   r   z3FailureTestCase.testExitThrows.<locals>.shouldThrow   sv                       s   )re   rV   r;   )r   r   r   s     @r   testExitThrowszFailureTestCase.testExitThrows   sd    	' 	' 	' 	' 	' 	' 	' 	'
	 	 	 	 	 	,44444r   N)r   r    r!   rX   rh   rp   rx   r   r   r   r   r   r   r)   r   r   rP   rP   j   s        5 5 5
P P PW W WY Y YH H H
      
) ) )	5 	5 	5 	5 	5r   rP   c                   H    e Zd Zd Zd Zd Zd Zd Zd Zd Z		 dd	Z
d
 ZdS )ContextmanagerAssertionMixinc                 .    t          d          | _        d S )Nztest exception)r;   TEST_EXCEPTIONr   s    r   setUpz"ContextmanagerAssertionMixin.setUp   s    *+;<<r   c                     |                      |j                   |                     |j                   |                     |j        d            d S r%   )
assertTruer   assertFalser   r   r   r   mock_managers     r   assertInWithManagerInvariantsz:ContextmanagerAssertionMixin.assertInWithManagerInvariants   sM    12221222/66666r   c                     |                      |j                   |                      |j                   |                     |j        |           d S r%   )r   r   r   r   r   )r   r   r   s      r    assertAfterWithManagerInvariantsz=ContextmanagerAssertionMixin.assertAfterWithManagerInvariants   sK    12220111/;;;;;r   c                 2    |                      |d           d S )NrC   )r   r   s     r   'assertAfterWithManagerInvariantsNoErrorzDContextmanagerAssertionMixin.assertAfterWithManagerInvariantsNoError   s)    --l	  	  	  	  	 r   c                 n    |                      |j                   |                     |j                   d S r%   )r   r.   r   r/   r   mock_generators     r   assertInWithGeneratorInvariantsz<ContextmanagerAssertionMixin.assertInWithGeneratorInvariants   s4    .////00000r   c                 n    |                      |j                   |                      |j                   d S r%   r   r.   r/   r   s     r   )assertAfterWithGeneratorInvariantsNoErrorzFContextmanagerAssertionMixin.assertAfterWithGeneratorInvariantsNoError   2    .///./////r   c                     | j         r%   )r   r   s    r   raiseTestExceptionz/ContextmanagerAssertionMixin.raiseTestException   s    !!r   Nc                    |                      |j                   |                      |j                   |:|                     |j        d         | j                   t          | j                  }|                     |j        d         |           |                     |j        d         |           |                     |j        d         d            d S )NrD   r   rE   )	r   r   r   r   r   r   r   assertIsInstanceassertIsNot)r   r   exc_types      r   )assertAfterWithManagerInvariantsWithErrorzFContextmanagerAssertionMixin.assertAfterWithManagerInvariantsWithError   s    12220111\3A68KLLLD/00H/2H===l4Q7BBB/2D99999r   c                 n    |                      |j                   |                      |j                   d S r%   r   r   s     r   +assertAfterWithGeneratorInvariantsWithErrorzHContextmanagerAssertionMixin.assertAfterWithGeneratorInvariantsWithError   r   r   r%   )r   r    r!   r   r   r   r   r   r   r   r   r   r)   r   r   r   r      s        = = =7 7 7
< < <
     1 1 10 0 0" " " <@
: 
: 
: 
:0 0 0 0 0r   r   c                   8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )	NonexceptionalTestCasec                 V    t                      5  	 d d d            d S # 1 swxY w Y   d S r%   r3   r   s    r   testInlineGeneratorSyntaxz0NonexceptionalTestCase.testInlineGeneratorSyntax   su    *,, 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   ""c                     t                      }|5  	 d d d            n# 1 swxY w Y   |                     |           d S r%   )r3   r   )r   r2   s     r   testUnboundGeneratorz+NonexceptionalTestCase.testUnboundGenerator   s    ,.. 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	44T:::::s   ##c                     t                      5 }|                     |           d d d            n# 1 swxY w Y   |                     |           d S r%   r3   r   r   r   rT   s     r   testInlineGeneratorBoundSyntaxz5NonexceptionalTestCase.testInlineGeneratorBoundSyntax   s    *,, 	600555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	66s;;;;;s   155c                     d }t                      5 }|                     |           d d d            n# 1 swxY w Y   |                     |           d S r%   r   r   s     r   *testInlineGeneratorBoundToExistingVariablezANonexceptionalTestCase.testInlineGeneratorBoundToExistingVariable   s    *,, 	600555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	666s;;;;;s   377c                     t                      5 | _        |                     | j                   d d d            n# 1 swxY w Y   |                     | j                   d S r%   )r3   rT   r   r   r   s    r   (testInlineGeneratorBoundToDottedVariablez?NonexceptionalTestCase.testInlineGeneratorBoundToDottedVariable  s    *,, 	;00:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;66tx@@@@@s    ;??c                     t                      }|5 }|                     |           |                     |           d d d            n# 1 swxY w Y   |                     |           |                     |           d S r%   )r3   r   r   r   r   )r   r2   rT   s      r   testBoundGeneratorz)NonexceptionalTestCase.testBoundGenerator  s    ,.. 	5S00555..t444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	66s;;;44T:::::s   +AAAc                 J   t                      }|5 }t                      }|5 }|                     |           |                     |           |                     |           |                     |           d d d            n# 1 swxY w Y   |                     |           |                     |           |                     |           |                     |           d d d            n# 1 swxY w Y   |                     |           |                     |           d S r%   )r3   r   r   r   r   )r   mock_arT   mock_bbars        r   testNestedSingleStatementsz1NonexceptionalTestCase.testNestedSingleStatements  s   .00 
	6s244F :3226:::226:::44S99944S999	: : : : : : : : : : : : : : :
 88@@@::3???..v66600555
	6 
	6 
	6 
	6 
	6 
	6 
	6 
	6 
	6 
	6 
	6 
	6 
	6 
	6 
	6 	44V<<<66s;;;;;s6   C.AB7C.B	C.
B	AC..C25C2N)
r   r    r!   r   r   r   r   r   r   r   r)   r   r   r   r      s          ; ; ;< < << < <A A A
; ; ;< < < < <r   r   c                   8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )	NestedNonexceptionalTestCasec                 p    t          t                                5  	 d d d            d S # 1 swxY w Y   d S r%   r5   r3   r   s    r   "testSingleArgInlineGeneratorSyntaxz?NestedNonexceptionalTestCase.testSingleArgInlineGeneratorSyntax#  s~    13344 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   +//c                     t                      }t          |          5 }|                     |           d d d            n# 1 swxY w Y   |                     |           d S r%   r3   r5   r   r   r   mrT   s      r   testSingleArgBoundToNonTuplez9NestedNonexceptionalTestCase.testSingleArgBoundToNonTuple'  s    )++ AYY 	2#..q111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	244Q77777   A  AAc                     t                      }t          |          5 }|                     |           d d d            n# 1 swxY w Y   |                     |           d S r%   r   r   s      r   2testSingleArgBoundToSingleElementParenthesizedListzONestedNonexceptionalTestCase.testSingleArgBoundToSingleElementParenthesizedList/  s    )++ AYY 	23..q111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	244Q77777r   c                 B    d }|                      t          |           d S )Nc                  v    t          t                                5 \  } }	 d d d            d S # 1 swxY w Y   d S r%   r   )rT   r   s     r   shouldThrowValueErrorziNestedNonexceptionalTestCase.testSingleArgBoundToMultipleElementTupleError.<locals>.shouldThrowValueError8  s    57788 JS#                 s   .22)rV   
ValueError)r   r   s     r   -testSingleArgBoundToMultipleElementTupleErrorzJNestedNonexceptionalTestCase.testSingleArgBoundToMultipleElementTupleError7  s0    	 	 	 	*&;<<<<<r   c                    t                      }t          |          }|5  |                     |           |                     |           d d d            n# 1 swxY w Y   |                     |           |                     |           d S r%   r3   rJ   r   r   )r   r*   mock_nesteds      r   testSingleArgUnboundz1NestedNonexceptionalTestCase.testSingleArgUnbound=  s    ;== !455 	< 	<../BCCC..{;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	445HIII44[AAAAAs   +AAAc                     t                      }t                      }t                      }t          |||          }|5  |                     |           |                     |           |                     |           |                     |           d d d            n# 1 swxY w Y   |                     |           |                     |           |                     |           |                     |           d S r%   r   )r   r   nor   s        r   testMultipleArgUnboundz3NestedNonexceptionalTestCase.testMultipleArgUnboundF  s7   )++)++)++ Aq)) 	< 	<..q111..q111..q111..{;;;		< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<
 	44Q77744Q77744Q77744[AAAAAs   ABB#&B#c                    t          t                      t                      t                                }|5 \  }}}|                     |           |                     |           |                     |           |                     |           d d d            n# 1 swxY w Y   |                     |           |                     |           |                     |           |                     |           d S r%   )rJ   r3   r   r   r   r   )r   r   r   r   r   s        r   testMultipleArgBoundz1NestedNonexceptionalTestCase.testMultipleArgBoundU  s4    !>!@!@)++-J-L-LN N 	<IQ1003330033300333..{;;;		< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<
 	66q99966q99966q99944[AAAAAs   ABB!$B!N)
r   r    r!   r   r   r   r   r   r   r   r)   r   r   r   r   !  s          8 8 88 8 8= = =B B BB B BB B B B Br   r   c                   V    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S )ExceptionalTestCasec                      t                       fd}                     t          |                                                                 j                   d S )Nc                       5 _                                                             j                                                     d d d            d S # 1 swxY w Y   d S r%   )resourcer   r   r   cmr   s   r   r   z;ExceptionalTestCase.testSingleResource.<locals>.shouldThrowf  s     *t}22266644T]CCC'')))* * * * * * * * * * * * * * * * * *s   A	AA!A)r3   rV   r;   r   r   r   r   r   r   s   ` @r   testSingleResourcez&ExceptionalTestCase.testSingleResourced  su    *,,	* 	* 	* 	* 	* 	*
 	,44466r:::88GGGGGr   c                      t                       fd}                     t          |                                t                     d S )Nc                  R     5 _         ddz   d d d            d S # 1 swxY w Y   d S NrD   r   )r   r   s   r   r   z@ExceptionalTestCase.testExceptionNormalized.<locals>.shouldThrowq  sw     t} Q                 s     )r3   rV   ZeroDivisionErrorr   r   s   ` @r   testExceptionNormalizedz+ExceptionalTestCase.testExceptionNormalizedo  sc    *,,	 	 	 	 	 	
 	+[99966r;LMMMMMr   c                 D    t                      t                       fd}                     t          |                                                                                                 j                                         j                   d S )Nc                  j    5 _         5 _                                                                                           j                                        j                                                    d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S r%   rT   r   r   r   r   r   r   r   s   r   r   zCExceptionalTestCase.testNestedSingleStatements.<locals>.shouldThrow|  s,    .48 .tx66v>>>66v>>>88BBB88BBB++---. . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . .s5   B(A8BB(B	B(B	B((B,/B,)r3   rV   r;   r   r   rT   r   r   r   r   r   s   ` @@r   r   z.ExceptionalTestCase.testNestedSingleStatementsy  s    .00.00	. 	. 	. 	. 	. 	. 	. 	,44466v>>>66v>>>88BBB88BBBBBr   c                     t                      t                      t                     fd}                     t          |                                                                                                                                 j                                         j                   d S )Nc                  f   5 \  _         _                                                                                                                          j                                        j                                                    d d d            d S # 1 swxY w Y   d S r%   )
resource_a
resource_br   r   r   )cm_acm_br   r   s   r   r   zOExceptionalTestCase.testMultipleResourcesInSingleStatement.<locals>.shouldThrow  s     * B$/22488822488822;???44T_EEE44T_EEE'')))* * * * * * * * * * * * * * * * * *s   BB&&B*-B*)r3   rJ   rV   r;   r   r   r  r	  )r   r   r
  r  r   s   ` @@@r   &testMultipleResourcesInSingleStatementz:ExceptionalTestCase.testMultipleResourcesInSingleStatement  s    ,..,.. t,,	* 	* 	* 	* 	* 	* 	* 	* 	,44466t<<<66t<<<66{CCC88III88IIIIIr   c                     t                      t                      d  _         fd}                     t          |                                                                 j                                         j        d                                 j	                                        j
                                        j        d            d S )Nc                      5 _                                                             j                                                     5 _        	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S r%   )rT   r   r   r   r   r  s   r   r   zPExceptionalTestCase.testNestedExceptionBeforeInnerStatement.<locals>.shouldThrow  s    48226:::44TX>>>''))) tx              	                 s6   AA:A"A:"A&	&A:)A&	*A::A>A>)r3   r   rV   r;   r   r   rT   r   r   r   r   r   r  s   ` @@r   'testNestedExceptionBeforeInnerStatementz;ExceptionalTestCase.testNestedExceptionBeforeInnerStatement  s    .00.00	 	 	 	 	 	 	 	,44466v>>>88BBB 	4(((,---+,,,)400000r   c                 D    t                      t                       fd}                     t          |                                                                                                 j                                         j                   d S )Nc                  j    5 _         5 _                                                                                           j                                        j                   d d d            n# 1 swxY w Y                                    d d d            d S # 1 swxY w Y   d S r%   r  r  s   r   r   zOExceptionalTestCase.testNestedExceptionAfterInnerStatement.<locals>.shouldThrow  s>    *48 Ctx66v>>>66v>>>88BBB88BBB	C C C C C C C C C C C C C C C
 '')))* * * * * * * * * * * * * * * * * *s5   B(A$A<0B(<B 	 B(B 	B((B,/B,)	r3   rV   r;   r   r   r   rT   r   r   r  s   ` @@r   &testNestedExceptionAfterInnerStatementz:ExceptionalTestCase.testNestedExceptionAfterInnerStatement  s    .00.00	* 	* 	* 	* 	* 	* 	* 	,44466v>>>44V<<<88BBB66tx@@@@@r   c                     t           d             fd}|                     t          d          5   |             d d d            d S # 1 swxY w Y   d S )Nc               3      K   d V  d S r%   r)   r)   r   r   r   z8ExceptionalTestCase.testRaisedStopIteration1.<locals>.cm        EEEEEr   c                  R                  5  t          d          # 1 swxY w Y   d S N	from withStopIterationr   s   r   r   zAExceptionalTestCase.testRaisedStopIteration1.<locals>.shouldThrow  Y     1 1#K0001 1 1 1 1 1 1 1 1 1     r  )r   rf   r  r   s     @r   testRaisedStopIteration1z,ExceptionalTestCase.testRaisedStopIteration1  s    		 	 
		1 	1 	1 	1 	1 ##M;?? 	 	KMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A

AAc                      G d dt                     fd}|                     t          d          5   |             d d d            d S # 1 swxY w Y   d S )Nc                       e Zd Zd Zd ZdS )8ExceptionalTestCase.testRaisedStopIteration2.<locals>.cmc                     d S r%   r)   r   s    r   r   zBExceptionalTestCase.testRaisedStopIteration2.<locals>.cm.__enter__  r]   r   c                     d S r%   r)   r   s       r   r   zAExceptionalTestCase.testRaisedStopIteration2.<locals>.cm.__exit__  r]   r   Nr   r)   r   r   r   r!    2              r   r   c                  R                  5  t          d          # 1 swxY w Y   d S r  r  r  s   r   r   zAExceptionalTestCase.testRaisedStopIteration2.<locals>.shouldThrow  r  r  r  )re   rf   r  r   s     @r   testRaisedStopIteration2z,ExceptionalTestCase.testRaisedStopIteration2  s    	 	 	 	 	 	 	 		1 	1 	1 	1 	1 ##M;?? 	 	KMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AAAc                     t           d             fd}|                     t                    5   |             d d d            d S # 1 swxY w Y   d S )Nc               3      K   d V  d S r%   r)   r)   r   r   r   z8ExceptionalTestCase.testRaisedStopIteration3.<locals>.cm  r  r   c                  l                  5  t          t          g                     # 1 swxY w Y   d S r%   )nextiterr  s   r   r   zAExceptionalTestCase.testRaisedStopIteration3.<locals>.shouldThrow  s]     % %488nn$% % % % % % % % % %s   )--)r   rV   r  r   s     @r   testRaisedStopIteration3z,ExceptionalTestCase.testRaisedStopIteration3  s     
	 	 
		% 	% 	% 	% 	% }-- 	 	KMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A		AAc                 h    t           d             fd}|                     t          |           d S )Nc               3      K   d V  d S r%   r)   r)   r   r   r   z8ExceptionalTestCase.testRaisedGeneratorExit1.<locals>.cm  r  r   c                  R                  5  t          d          # 1 swxY w Y   d S r  GeneratorExitr  s   r   r   zAExceptionalTestCase.testRaisedGeneratorExit1.<locals>.shouldThrow  r  r  )r   rV   r1  r   s     @r   testRaisedGeneratorExit1z,ExceptionalTestCase.testRaisedGeneratorExit1  sS    		 	 
		1 	1 	1 	1 	1 	-55555r   c                 n     G d dt                     fd}|                     t          |           d S )Nc                       e Zd Zd Zd ZdS )8ExceptionalTestCase.testRaisedGeneratorExit2.<locals>.cmc                     d S r%   r)   r   s    r   r   zBExceptionalTestCase.testRaisedGeneratorExit2.<locals>.cm.__enter__  r]   r   c                     d S r%   r)   r   s       r   r   zAExceptionalTestCase.testRaisedGeneratorExit2.<locals>.cm.__exit__  r]   r   Nr   r)   r   r   r   r5    r$  r   r   c                  R                  5  t          d          # 1 swxY w Y   d S r  r0  r  s   r   r   zAExceptionalTestCase.testRaisedGeneratorExit2.<locals>.shouldThrow  r  r  )re   rV   r1  r   s     @r   testRaisedGeneratorExit2z,ExceptionalTestCase.testRaisedGeneratorExit2  sd    	 	 	 	 	& 	 	 		1 	1 	1 	1 	1 	-55555r   c                       G d dt                      fd} |              fd}                     t          |            fd}                     t          |           d S )Nc                        e Zd Zd Zd Zd ZdS )0ExceptionalTestCase.testErrorsInBool.<locals>.cmc                 D     G fdd          } |            | _         d S )Nc                       e Zd Z fdZdS )GExceptionalTestCase.testErrorsInBool.<locals>.cm.__init__.<locals>.Boolc                                  S r%   r)   )r   bool_conversions    r   __bool__zPExceptionalTestCase.testErrorsInBool.<locals>.cm.__init__.<locals>.Bool.__bool__  s    .000r   N)r   r    r!   rB  )rA  s   r   Boolr?    s.        1 1 1 1 1 1 1r   rC  exit_result)r   rA  rC  s    ` r   r   z9ExceptionalTestCase.testErrorsInBool.<locals>.cm.__init__  sK    1 1 1 1 1 1 1 1 1 1 $(466   r   c                     dS )N   r)   r   s    r   r   z:ExceptionalTestCase.testErrorsInBool.<locals>.cm.__enter__  s    qr   c                     | j         S r%   rD  r   abcs       r   r   z9ExceptionalTestCase.testErrorsInBool.<locals>.cm.__exit__  s    ''r   NrH   r)   r   r   r   r<    sA        * * *
  ( ( ( ( (r   r   c                  |      d           5                       d           d d d            d S # 1 swxY w Y   d S )Nc                      dS r   r)   r)   r   r   <lambda>zJExceptionalTestCase.testErrorsInBool.<locals>.trueAsBool.<locals>.<lambda>  s    D r   Should NOT see thisfailr   s   r   
trueAsBoolz8ExceptionalTestCase.testErrorsInBool.<locals>.trueAsBool      LL!! 1 1		/0001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1   155c                  |      d           5                       d           d d d            d S # 1 swxY w Y   d S )Nc                      dS r
   r)   r)   r   r   rO  zKExceptionalTestCase.testErrorsInBool.<locals>.falseAsBool.<locals>.<lambda>  s    E r   zShould raiserQ  r   s   r   falseAsBoolz9ExceptionalTestCase.testErrorsInBool.<locals>.falseAsBool  s    MM"" * *		.)))* * * * * * * * * * * * * * * * * *rU  c                  |      d           5                       d           d d d            d S # 1 swxY w Y   d S )Nc                      ddz  S r   r)   r)   r   r   rO  zJExceptionalTestCase.testErrorsInBool.<locals>.failAsBool.<locals>.<lambda>!  s
    AqD r   rP  rQ  r   s   r   
failAsBoolz8ExceptionalTestCase.testErrorsInBool.<locals>.failAsBool   rT  rU  )re   rV   AssertionErrorr   )r   rS  rX  r[  r   s   `   @r   testErrorsInBoolz$ExceptionalTestCase.testErrorsInBool  s    		( 		( 		( 		( 		( 		( 		( 		(	1 	1 	1 	1 	1 	1 	
	* 	* 	* 	* 	* 	* 	.+666	1 	1 	1 	1 	1 	1 	+Z88888r   N)r   r    r!   r   r   r   r  r  r  r  r&  r,  r2  r9  r]  r)   r   r   r   r   c  s        	H 	H 	HN N NC C C"J J J&1 1 1*A A A"      
6 
6 
66 6 69 9 9 9 9r   r   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )NonLocalFlowControlTestCasec                     d}	 |dz  }t                      5  |dz  }	 d d d            n# 1 swxY w Y   |dz  }7|                     |d           d S )Nr   TrD   
   d      r3   r   r   counters     r   testWithBreakz)NonLocalFlowControlTestCase.testWithBreak(  s    	qLG.00  2               sNG	 	"%%%%%s   *..c                     d}	 |dz  }|dk    rn2t                      5  |dz  }	 d d d            .# 1 swxY w Y   |dz  }>|                     |d           d S )Nr   TrD   rE   ra  rb     rd  re  s     r   testWithContinuez,NonLocalFlowControlTestCase.testWithContinue2  s    	qLG{{.00  2               sNG	 	"%%%%%s   155c                 H    d }|                       |            d           d S )Nc                  x    d} 	 | dz  } t                      5  | dz  } | cd d d            S # 1 swxY w Y   | dz  } 8)Nr   TrD   ra  rb  r   )rf  s    r   rT   z7NonLocalFlowControlTestCase.testWithReturn.<locals>.foo?  s    G1244 # #rMG"# # # # # # # # # # # # # # # # 3s   +//rc  )r   r   s     r   testWithReturnz*NonLocalFlowControlTestCase.testWithReturn>  s5    	 	 	 	#####r   c                 j    d }t           |                      }|                     |ddg           d S )Nc               3   h   K   t                      5  dV  dV  d d d            d S # 1 swxY w Y   d S )Nri     r   r)   r   r   genz6NonLocalFlowControlTestCase.testWithYield.<locals>.genJ  s      .00                   s   	'++ri  rp  )listr   )r   rq  xs      r   testWithYieldz)NonLocalFlowControlTestCase.testWithYieldI  sD    	 	 	 KKRH%%%%%r   c                     d}	 |dz  }t                      5  |dz  }t          # 1 swxY w Y   |dz  }|                     d           d S # t          $ r |                     |d           Y d S w xY w)Nr   rD   ra  rb  zDidn't raise RuntimeErrorrc  )r3   r;   rR  r   re  s     r   testWithRaisez)NonLocalFlowControlTestCase.testWithRaiseQ  s    		3qLG.00 # #2""# # # # # # # # sNG II122222  	* 	* 	*Wb))))))	*s'   A $(A (A  A/.A/N)r   r    r!   rg  rj  rm  rt  rv  r)   r   r   r_  r_  &  s_        & & &
& 
& 
&	$ 	$ 	$& & &3 3 3 3 3r   r_  c                        e Zd Zd Zd Zd ZdS )AssignmentTargetTestCasec                 N   dg di}t                      5 |d         d<   |                     t          |                                          dg           |                     |d         d         j        t
                     d d d            n# 1 swxY w Y   t                      5 t          |                                          d         d<   |                     t          |                                          dg           |                     |d         d         j        t
                     d d d            n# 1 swxY w Y   t                      5 |d<   t          |                                          }|                                 |                     |ddg           d d d            n# 1 swxY w Y    G d d          } |            }t                      5 |_        |                     t          |d          d           d d d            d S # 1 swxY w Y   d S )	NrD   r   rD   rE   r   rE   c                       e Zd ZdS );AssignmentTargetTestCase.testSingleComplexTarget.<locals>.CNrl   r)   r   r   Cr|  m            r   r}  rT   T)
r3   r   rr  keysr   r,   valuessortrT   hasattr)r   targetsr  r}  blahs        r   testSingleComplexTargetz0AssignmentTargetTestCase.testSingleComplexTargeta  s   iii.*,, 	D
1T',,..11A3777WQZ]4lCCC	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D +,, 	DW^^5E5E0F0Fq0I!0LT',,..11A3777WQZ]4lCCC	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D +,, 	+
''DIIKKKTAq6***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	qss*,, 	9WT5114888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9sJ   A,BBB&BD==EEAF33F7:F7#*HH!Hc                      G d d          }dg di} |            5 \  |d         d<   |d         d<   |d         d<   |                      |dg di           d d d            n# 1 swxY w Y    |            5 \  t          |                                          d         d<   t          |                                          d         d<   t          |                                          d         d<   |                      |dg di           d d d            n# 1 swxY w Y    |            5 \  |d<   |d<   |d	<   |                      |ddd	d           d d d            n# 1 swxY w Y    G d
 d          } |            } |            5 \  |_        |_        |_        |                      |j        d           |                      |j        d           |                      |j        d	           d d d            d S # 1 swxY w Y   d S )Nc                       e Zd Zd Zd ZdS )>AssignmentTargetTestCase.testMultipleComplexTargets.<locals>.Cc                     dS )NrD   rE   rG  r)   r   s    r   r   zHAssignmentTargetTestCase.testMultipleComplexTargets.<locals>.C.__enter__t  s    r   c                     d S r%   r)   r   tvtbs       r   r   zGAssignmentTargetTestCase.testMultipleComplexTargets.<locals>.C.__exit__u        r   Nr   r)   r   r   r}  r  s  s"        ///.....r   r}  rD   rz  r   rE   r  )rG  rE   rD   rG  c                       e Zd ZdS )>AssignmentTargetTestCase.testMultipleComplexTargets.<locals>.BNrl   r)   r   r   Br  }  r~  r   r  )r   rr  r  onetwothree)r   r}  r  r  r  s        r   testMultipleComplexTargetsz3AssignmentTargetTestCase.testMultipleComplexTargetsr  s    	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ iii.QSS 	6AWQZ]GAJqM71:a=Wq)))n555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6QSS 	6nT'..**++A.q148H8H3I3I!3LQ3OQUV]VdVdVfVfQgQghiQjklQmWq)))n555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6QSS 	:8WQZWQZW!a&8&8999	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:qssQSS 	,4TXtxTXq)))TXq)))TZ+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,sI   :A$$A(+A(9BDD #D 1(E%%E),E)A%HH
Hc                    t          t          dd                    5 ^}}}|                     |d           |                     |ddg           |                     |d           d d d            d S # 1 swxY w Y   d S )NrD      rE   rG     )r   ranger   rI  s       r   testWithExtendedTargetsz0AssignmentTargetTestCase.testWithExtendedTargets  s    q!%% 	#!aQ"""QA'''Q"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#s   A	A44A8;A8N)r   r    r!   r  r  r  r)   r   r   rx  rx  _  sA        9 9 9", , ,$# # # # #r   rx  c                       e Zd Zd Zd ZdS )ExitSwallowsExceptionTestCasec                      G d d          }	  |            5  ddz   d d d            d S # 1 swxY w Y   d S # t           $ r |                     d           Y d S w xY w)Nc                       e Zd Zd Zd ZdS )SExitSwallowsExceptionTestCase.testExitTrueSwallowsException.<locals>.AfricanSwallowc                     d S r%   r)   r   s    r   r   z]ExitSwallowsExceptionTestCase.testExitTrueSwallowsException.<locals>.AfricanSwallow.__enter__  r  r   c                     dS r   r)   r  s       r   r   z\ExitSwallowsExceptionTestCase.testExitTrueSwallowsException.<locals>.AfricanSwallow.__exit__  s    r   Nr   r)   r   r   AfricanSwallowr    s"        %%%55555r   r  rD   r   z,ZeroDivisionError should have been swallowed)r   rR  )r   r  s     r   testExitTrueSwallowsExceptionz;ExitSwallowsExceptionTestCase.testExitTrueSwallowsException  s    	6 	6 	6 	6 	6 	6 	6 	6	F!!  !                   	F 	F 	FIIDEEEEEE	Fs'   
9 ,9 09 09 AAc                      G d d          }	  |            5  ddz   d d d            n# 1 swxY w Y   |                      d           d S # t          $ r Y d S w xY w)Nc                       e Zd Zd Zd ZdS )ZExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowException.<locals>.EuropeanSwallowc                     d S r%   r)   r   s    r   r   zdExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowException.<locals>.EuropeanSwallow.__enter__  r  r   c                     dS r
   r)   r  s       r   r   zcExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowException.<locals>.EuropeanSwallow.__exit__  s    r   Nr   r)   r   r   EuropeanSwallowr    s"        %%%66666r   r  rD   r   z)ZeroDivisionError should have been raised)rR  r   )r   r  s     r   #testExitFalseDoesntSwallowExceptionzAExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowException  s    	7 	7 	7 	7 	7 	7 	7 	7	C ""  !              
 IIABBBBB ! 	 	 	DD	s,   
A +A /A /A 
AAN)r   r    r!   r  r  r)   r   r   r  r    s:        F F F
C 
C 
C 
C 
Cr   r  c                       e Zd Z G d de          Z G d de          Z G d de          Z G d de          Zd	 Zd
 Z	d Z
d Zd ZdS )
NestedWithc                   "    e Zd ZddZd Zd ZdS )NestedWith.DummyNFc                 F    || }|| _         || _        d| _        d| _        d S r
   )r   gobbler   r   )r   r   r  s      r   r   zNestedWith.Dummy.__init__  s0    }DJ DK %D$Dr   c                     d| _         | j        S r   )r   r   r   s    r   r   zNestedWith.Dummy.__enter__  s     $D:r   c                 4    d| _         || _        | j        rdS d S r   )r   r?   r  rN   s     r   r   zNestedWith.Dummy.__exit__  s*    #D$DM{ t r   r
   rH   r)   r   r   Dummyr    sF        	% 	% 	% 	%	 	 		 	 	 	 	r   r  c                       e Zd Zd ZdS )NestedWith.InitRaisesc                     t                      r%   r   r   s    r   r   zNestedWith.InitRaises.__init__  s    ,..0r   Nr0   r)   r   r   
InitRaisesr    s        00000r   r  c                       e Zd Zd Zd ZdS )NestedWith.EnterRaisesc                     t                      r%   r   r   s    r   r   z NestedWith.EnterRaises.__enter__  s    <>>1r   c                     d S r%   r)   rN   s     r   r   zNestedWith.EnterRaises.__exit__  r  r   Nr   r)   r   r   EnterRaisesr    s"        111+++++r   r  c                       e Zd Zd Zd ZdS )NestedWith.ExitRaisesc                     d S r%   r)   r   s    r   r   zNestedWith.ExitRaises.__enter__  r  r   c                     t                      r%   r   rN   s     r   r   zNestedWith.ExitRaises.__exit__  s    \^^';r   Nr   r)   r   r   
ExitRaisesr    s"        !!!;;;;;r   r  c                    |                                  5 }|                                  5 }|                     |j                   |                     |j                   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |                     |j                   |                     |j                   d S r%   )r  r   r   r   )r   rJ  rK  s      r   testNoExceptionszNestedWith.testNoExceptions  s   ZZ\\ 	,Q

 	,OOAN+++OOAN+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	&&&&&&&&s4   B5A+B+A/	/B2A/	3BB	Bc                 2   	 |                                  5 }|                                 5  	 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   n#  Y nxY w|                     |j                   |                     |j                   d S r%   )r  r  r   r   r   r   rJ  s     r   testExceptionInExprListz"NestedWith.testExceptionInExprList  s   	 DOO$5$5                               	D'''&&&&&sK   A A9A=	A =	AA AA AA A c                    	 |                                  5 }|                                 5  |                     d           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |                     d           n# t          $ r Y nw xY w|                     |j                   |                     |j                   d S )Nzbody of bad with executedzRuntimeError not reraised)r  r  rR  r;   r   r   r   r  s     r   testExceptionInEnterzNestedWith.testExceptionInEnter  sJ   	3 7D$4$4$6$6 7 7		56667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
 II12222  	 	 	D	 	'''&&&&&sW   B A$AA$A	A$A	A$B $A((B +A(,B 
BBc                    d}|                      d          5 }|                                 5  d}d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |                     |j                   |                     |j                   |                     |           |                     |j        d         d            d S )NFT)r  r   )r  r  r   r   r   assertNotEqualr?   )r   body_executedrJ  s      r   testExceptionInExitzNestedWith.testExceptionInExit  s2   ZZtZ$$ 	!4??+<+< 	! 	! M	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!'''&&&&&&AJqM400000s1   A=AA	AA	AAAc                 |   |                      d          5 \  }}|                      d          5 \  }}|                     d|           |                     d|           |                     d|           |                     d|           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )N)rD   rE   )r   )ra     rD   rE   ra  r  )r  r   )r   a1a2b1b2s        r   testEnterReturnsTuplez NestedWith.testEnterReturnsTuple  s>   ZZeZ$$ 	%RZZhZ''	%+3BQ###Q###R$$$R$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s5   B1ABB1B	B1 B	!B11B58B5N)r   r    r!   re   r  r  r  r  r  r  r  r  r  r)   r   r   r  r    s              &1 1 1 1 1V 1 1 1, , , , ,f , , ,< < < < <V < < <' ' '' ' '	' 	' 	'1 1 1% % % % %r   r  __main__)__doc__
__author__	__email__r>   unittestcollectionsr   
contextlibr   r   r   r   r*   re   r,   r3   r5   rJ   TestCaserP   r   r   r   r   r_  rx  r  r  r   mainr)   r   r   <module>r     s'   = = 
#	 



        L L L L L L L L L LC C C C C1 C C C$      6      !5 !5 !5 !5 !5V !5 !5 !5H0 0 0 0 0 0 0 0"Q5 Q5 Q5 Q5 Q5h' Q5 Q5 Q5f,0 ,0 ,0 ,0 ,06 ,0 ,0 ,0^2< 2< 2< 2< 2<X.0L 2< 2< 2<j?B ?B ?B ?B ?B8#4 ?B ?B ?BD@9 @9 @9 @9 @968I @9 @9 @9F63 63 63 63 63("3 63 63 63r)# )# )# )# )#x0 )# )# )#XC C C C CH$5 C C C2J% J% J% J% J%" J% J% J%X zHMOOOOO r   