o
    IXhg                     @   sN  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 deZd	d
 ZG dd deZedd ZG dd deZG dd deZG dd dejZG dd deZG dd dejeZG dd dejeZG dd deejZG dd dejZG dd  d ejZG d!d" d"ejZG d#d$ d$ejZed%kre  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                       s,   e Zd Z fddZdd Zdd Z  ZS )MockContextManagerc                    s"   t  j|  d| _d| _d | _d S NF)super__init__enter_calledexit_called	exit_argsselfargs	__class__ 8/opt/python-3.10.19/usr/lib/python3.10/test/test_with.pyr	      s   
zMockContextManager.__init__c                 C      d| _ t| S NT)r
   r   	__enter__r   r   r   r   r         
zMockContextManager.__enter__c                 C   s"   d| _ |||f| _t| |||S r   )r   r   r   __exit__r   typevalue	tracebackr   r   r   r      s
   zMockContextManager.__exit__)__name__
__module____qualname__r	   r   r   __classcell__r   r   r   r   r      s    r   c                    s    fdd}|S )Nc                     s   t  | |S N)r   )r   kwdsfuncr   r   helper    s   z#mock_contextmanager.<locals>.helperr   )r%   r&   r   r$   r   mock_contextmanager   s   r'   c                   @      e Zd Zdd ZdS )MockResourcec                 C   s   d| _ d| _d S r   )yieldedstoppedr   r   r   r   r	   &   r   zMockResource.__init__Nr   r   r    r	   r   r   r   r   r)   %       r)   c                  c   s*    t  } zd| _| V  W d| _d S d| _w r   )r)   r*   r+   )mockr   r   r   mock_contextmanager_generator+   s   r/   c                   @   $   e Zd Zdd Zdd Zdd ZdS )Nestedc                 G   s   || _ d | _d S r"   )managersenteredr   r2   r   r   r   r	   7   r   zNested.__init__c                 C   sh   | j d ur	tdt | _ g }z| jD ]}||  | j | qW |S    | jt	  s1 Y |S )NzContext is not reentrant)
r3   RuntimeErrorr   r2   appendr   
appendleftr   sysexc_info)r   varsmgrr   r   r   r   ;   s   

zNested.__enter__c                 G   s`   |}| j D ]}z	|j| rd}W q   t }Y qd | _ ||ur.|d |d |d d S )NNNNr         )r3   r   r8   r9   with_traceback)r   r9   exr;   r   r   r   r   I   s   

zNested.__exit__Nr   r   r    r	   r   r   r   r   r   r   r1   5   s    r1   c                   @   r0   )
MockNestedc                 G   s(   t j| g|R   d| _d| _d | _d S r   )r1   r	   r
   r   r   r4   r   r   r   r	   Z   s   
zMockNested.__init__c                 C   r   r   )r
   r1   r   r   r   r   r   r   `   r   zMockNested.__enter__c                 G   s   d| _ || _tj| g|R  S r   )r   r   r1   r   r   r9   r   r   r   r   d   s   zMockNested.__exit__NrA   r   r   r   r   rB   Y   s    rB   c                   @   s\   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdS )FailureTestCasec                 C      dd }|  t| d S )Nc                   S   s*   t 	 W d    d S 1 sw   Y  d S r"   foor   r   r   r   fooNotDeclaredl   s   *z5FailureTestCase.testNameError.<locals>.fooNotDeclared)assertRaises	NameError)r   rH   r   r   r   testNameErrork   s   zFailureTestCase.testNameErrorc                    .   G dd dt   fdd}| td| d S )Nc                   @   r(   )z<FailureTestCase.testEnterAttributeError1.<locals>.LacksEnterc                 S      d S r"   r   r   r   r   r   r   r      zEFailureTestCase.testEnterAttributeError1.<locals>.LacksEnter.__exit__N)r   r   r    r   r   r   r   r   
LacksEnterq   r-   rO   c                     0     } | 	 W d    d S 1 sw   Y  d S r"   r   rF   rO   r   r   fooLacksEnteru      *z?FailureTestCase.testEnterAttributeError1.<locals>.fooLacksEnterr   objectassertRaisesRegexAttributeError)r   rR   r   rQ   r   testEnterAttributeError1p      z(FailureTestCase.testEnterAttributeError1c                    rL   )Nc                   @      e Zd ZdS )zCFailureTestCase.testEnterAttributeError2.<locals>.LacksEnterAndExitNr   r   r    r   r   r   r   LacksEnterAndExit{   s    r\   c                     rP   r"   r   rF   r\   r   r   fooLacksEnterAndExit~   rS   zFFailureTestCase.testEnterAttributeError2.<locals>.fooLacksEnterAndExitr   rT   )r   r^   r   r]   r   testEnterAttributeError2z   s   z(FailureTestCase.testEnterAttributeError2c                    rL   )Nc                   @   r(   )z9FailureTestCase.testExitAttributeError.<locals>.LacksExitc                 S   rM   r"   r   r   r   r   r   r      rN   zCFailureTestCase.testExitAttributeError.<locals>.LacksExit.__enter__N)r   r   r    r   r   r   r   r   	LacksExit   r-   r`   c                     rP   r"   r   rF   r`   r   r   fooLacksExit   rS   z<FailureTestCase.testExitAttributeError.<locals>.fooLacksExitr   rT   )r   rb   r   ra   r   testExitAttributeError   rY   z&FailureTestCase.testExitAttributeErrorc                 C   s   dd }|  t|| d S )Nc                 S   s   t | dd d S )N Zsingle)compile)sr   r   r   shouldRaiseSyntaxError   s   zGFailureTestCase.assertRaisesSyntaxError.<locals>.shouldRaiseSyntaxError)rI   SyntaxError)r   Zcodestrrg   r   r   r   assertRaisesSyntaxError   s   z'FailureTestCase.assertRaisesSyntaxErrorc                 C      |  d |  d d S )Nzwith mock as None:
  passzwith mock as (None):
  passri   r   r   r   r   testAssignmentToNoneError      
z)FailureTestCase.testAssignmentToNoneErrorc                 C   rj   )Nzwith mock as None,:
  passzwith mock as (None,):
  passrk   r   r   r   r   ,testAssignmentToTupleOnlyContainingNoneError   rm   z<FailureTestCase.testAssignmentToTupleOnlyContainingNoneErrorc                 C   s   |  d d S )Nz%with mock as (foo, None, bar):
  passrk   r   r   r   r   (testAssignmentToTupleContainingNoneError   s   z8FailureTestCase.testAssignmentToTupleContainingNoneErrorc                    s<   G dd dt   fdd}t| jd  d S )Nc                   @      e Zd Zdd Zdd ZdS )z4FailureTestCase.testEnterThrows.<locals>.EnterThrowsc                 S      t d)NzEnter threwr5   r   r   r   r   r         z>FailureTestCase.testEnterThrows.<locals>.EnterThrows.__enter__c                 W   rM   r"   r   r   r   r   r   r      rN   z=FailureTestCase.testEnterThrows.<locals>.EnterThrows.__exit__Nr   r   r    r   r   r   r   r   r   EnterThrows       ru   c                     s8     } d _ | 
_ W d    d S 1 sw   Y  d S r"   rF   )ctru   r   r   r   shouldThrow   s
   "z4FailureTestCase.testEnterThrows.<locals>.shouldThrow)rU   rI   r5   assertEqualrG   r   ry   r   rx   r   testEnterThrows   s   zFailureTestCase.testEnterThrowsc                    ,   G dd dt   fdd}| t| d S )Nc                   @   rp   )z2FailureTestCase.testExitThrows.<locals>.ExitThrowsc                 S   rM   r"   r   r   r   r   r   r      rN   z<FailureTestCase.testExitThrows.<locals>.ExitThrows.__enter__c                 W   rq   )N*   rr   r   r   r   r   r      rs   z;FailureTestCase.testExitThrows.<locals>.ExitThrows.__exit__Nrt   r   r   r   r   
ExitThrows   rv   r   c                      s,     	 W d    d S 1 sw   Y  d S r"   r   r   r   r   r   ry         "z3FailureTestCase.testExitThrows.<locals>.shouldThrow)rU   rI   r5   r{   r   r   r   testExitThrows   s   zFailureTestCase.testExitThrowsN)r   r   r    rK   rX   r_   rc   ri   rl   rn   ro   r|   r   r   r   r   r   rD   j   s    
	
rD   c                   @   sX   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z		dddZ
dd ZdS )ContextmanagerAssertionMixinc                 C   s   t d| _d S )Nztest exception)r5   TEST_EXCEPTIONr   r   r   r   setUp   s   z"ContextmanagerAssertionMixin.setUpc                 C   s*   |  |j | |j | |jd  d S r"   )
assertTruer
   assertFalser   rz   r   r   mock_managerr   r   r   assertInWithManagerInvariants      z:ContextmanagerAssertionMixin.assertInWithManagerInvariantsc                 C   s*   |  |j |  |j | |j| d S r"   )r   r
   r   rz   r   )r   r   r   r   r   r    assertAfterWithManagerInvariants   r   z=ContextmanagerAssertionMixin.assertAfterWithManagerInvariantsc                 C   s   |  |d d S )Nr<   )r   r   r   r   r   'assertAfterWithManagerInvariantsNoError   s   zDContextmanagerAssertionMixin.assertAfterWithManagerInvariantsNoErrorc                 C   s   |  |j | |j d S r"   )r   r*   r   r+   r   Zmock_generatorr   r   r   assertInWithGeneratorInvariants      z<ContextmanagerAssertionMixin.assertInWithGeneratorInvariantsc                 C      |  |j |  |j d S r"   r   r*   r+   r   r   r   r   )assertAfterWithGeneratorInvariantsNoError   r   zFContextmanagerAssertionMixin.assertAfterWithGeneratorInvariantsNoErrorc                 C   s   | j r"   )r   r   r   r   r   raiseTestException      z/ContextmanagerAssertionMixin.raiseTestExceptionNc                 C   sx   |  |j |  |j |d u r| |jd | j t| j}| |jd | | |jd | | |jd d  d S )Nr=   r   r>   )	r   r
   r   rz   r   r   r   ZassertIsInstanceZassertIsNot)r   r   exc_typer   r   r   )assertAfterWithManagerInvariantsWithError   s   
zFContextmanagerAssertionMixin.assertAfterWithManagerInvariantsWithErrorc                 C   r   r"   r   r   r   r   r   +assertAfterWithGeneratorInvariantsWithError   r   zHContextmanagerAssertionMixin.assertAfterWithGeneratorInvariantsWithErrorr"   )r   r   r    r   r   r   r   r   r   r   r   r   r   r   r   r   r      s    
r   c                   @   D   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )NonexceptionalTestCasec                 C   s,   t  	 W d    d S 1 sw   Y  d S r"   r/   r   r   r   r   testInlineGeneratorSyntax   r   z0NonexceptionalTestCase.testInlineGeneratorSyntaxc                 C   s8   t  }| W d    n1 sw   Y  | | d S r"   )r/   r   )r   r.   r   r   r   testUnboundGenerator   s
   z+NonexceptionalTestCase.testUnboundGeneratorc                 C   s>   t  }| | W d    n1 sw   Y  | | d S r"   r/   r   r   r   rG   r   r   r   testInlineGeneratorBoundSyntax   s   z5NonexceptionalTestCase.testInlineGeneratorBoundSyntaxc                 C   sB   d }t  }| | W d    n1 sw   Y  | | d S r"   r   r   r   r   r   *testInlineGeneratorBoundToExistingVariable   s
   zANonexceptionalTestCase.testInlineGeneratorBoundToExistingVariablec                 C   sD   t  | _| | j W d    n1 sw   Y  | | j d S r"   )r/   rG   r   r   r   r   r   r   (testInlineGeneratorBoundToDottedVariable  s   
z?NonexceptionalTestCase.testInlineGeneratorBoundToDottedVariablec                 C   sV   t  }|}| | | | W d    n1 sw   Y  | | | | d S r"   )r/   r   r   r   r   )r   r.   rG   r   r   r   testBoundGenerator  s   

z)NonexceptionalTestCase.testBoundGeneratorc              	   C   s   t  }|E}t  }|}| | | | | | | | W d    n1 s*w   Y  | | | | | | | | W d    n1 sMw   Y  | | | | d S r"   )r/   r   r   r   r   )r   mock_arG   mock_bbarr   r   r   testNestedSingleStatements  s    






z1NonexceptionalTestCase.testNestedSingleStatementsN)
r   r   r    r   r   r   r   r   r   r   r   r   r   r   r      s    r   c                   @   r   )NestedNonexceptionalTestCasec                 C   s0   t t 	 W d    d S 1 sw   Y  d S r"   r1   r/   r   r   r   r   "testSingleArgInlineGeneratorSyntax#  s   "z?NestedNonexceptionalTestCase.testSingleArgInlineGeneratorSyntaxc                 C   F   t  }t|}| | W d    n1 sw   Y  | | d S r"   r/   r1   r   r   r   mrG   r   r   r   testSingleArgBoundToNonTuple'  
   
z9NestedNonexceptionalTestCase.testSingleArgBoundToNonTuplec                 C   r   r"   r   r   r   r   r   2testSingleArgBoundToSingleElementParenthesizedList/  r   zONestedNonexceptionalTestCase.testSingleArgBoundToSingleElementParenthesizedListc                 C   rE   )Nc                  S   s4   t t \} }W d    d S 1 sw   Y  d S r"   r   )rG   r   r   r   r   shouldThrowValueError8  s   "ziNestedNonexceptionalTestCase.testSingleArgBoundToMultipleElementTupleError.<locals>.shouldThrowValueError)rI   
ValueError)r   r   r   r   r   -testSingleArgBoundToMultipleElementTupleError7  s   zJNestedNonexceptionalTestCase.testSingleArgBoundToMultipleElementTupleErrorc                 C   s^   t  }t|}| | | | | W d    n1 sw   Y  | | | | d S r"   r/   rB   r   r   )r   r'   mock_nestedr   r   r   testSingleArgUnbound=  s   

z1NestedNonexceptionalTestCase.testSingleArgUnboundc                 C   s   t  }t  }t  }t|||}| | | | | | | | | W d    n1 s0w   Y  | | | | | | | | d S r"   r   )r   r   nor   r   r   r   testMultipleArgUnboundF  s   





z3NestedNonexceptionalTestCase.testMultipleArgUnboundc                 C   s   t t t t }|\}}}| | | | | | | | W d    n1 s-w   Y  | | | | | | | | d S r"   )rB   r/   r   r   r   r   )r   r   r   r   r   r   r   r   testMultipleArgBoundU  s   





z1NestedNonexceptionalTestCase.testMultipleArgBoundN)
r   r   r    r   r   r   r   r   r   r   r   r   r   r   r   !  s    	r   c                   @   sl   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )ExceptionalTestCasec                    s:   t    fdd}t|   j d S )Nc                      sJ    _   j    W d    d S 1 sw   Y  d S r"   )resourcer   r   r   r   cmr   r   r   ry   f  s
   

"z;ExceptionalTestCase.testSingleResource.<locals>.shouldThrow)r/   rI   r5   r   r   r   r{   r   r   r   testSingleResourced  s
   
z&ExceptionalTestCase.testSingleResourcec                    s0   t    fdd}t|  t d S )Nc                      s4    _ dd  W d    d S 1 sw   Y  d S Nr=   r   )r   r   r   r   r   ry   q  s   
"z@ExceptionalTestCase.testExceptionNormalized.<locals>.shouldThrow)r/   rI   ZeroDivisionErrorr   r{   r   r   r   testExceptionNormalizedo  s   z+ExceptionalTestCase.testExceptionNormalizedc                    sX   t   t   fdd}t|    j j d S )Nc                	      s    ?_ #_   j  j   W d    n1 s,w   Y  W d    d S W d    d S 1 sDw   Y  d S r"   rG   r   r   r   r   r   r   r   r   r   r   ry   |  s   


"zCExceptionalTestCase.testNestedSingleStatements.<locals>.shouldThrow)r/   rI   r5   r   r   rG   r   r{   r   r   r   r   y     

z.ExceptionalTestCase.testNestedSingleStatementsc                    sn   t   t  t  fdd}t|     j j d S )Nc                      sp   ,\_ _    j  j   W d    d S 1 s1w   Y  d S r"   )
resource_a
resource_br   r   r   r   Zcm_aZcm_br   r   r   r   ry     s   



"zOExceptionalTestCase.testMultipleResourcesInSingleStatement.<locals>.shouldThrow)r/   rB   rI   r5   r   r   r   r   r{   r   r   r   &testMultipleResourcesInSingleStatement  s   



z:ExceptionalTestCase.testMultipleResourcesInSingleStatementc                    s|   t   t  d _ fdd}t|   j jd  j	 j
 jd  d S )Nc                	      s    4_   j    	_W d    n1 s!w   Y  W d    d S W d    d S 1 s9w   Y  d S r"   )rG   r   r   r   r   r   r   r   r   ry     s   
"zPExceptionalTestCase.testNestedExceptionBeforeInnerStatement.<locals>.shouldThrow)r/   r   rI   r5   r   r   rG   rz   r   r
   r   r   r{   r   r   r   'testNestedExceptionBeforeInnerStatement  s   
z;ExceptionalTestCase.testNestedExceptionBeforeInnerStatementc                    sX   t   t   fdd}t|    j j d S )Nc                	      s    7_ _   j  j W d    n1 s(w   Y    W d    d S 1 s<w   Y  d S r"   r   r   r   r   r   ry     s   


"zOExceptionalTestCase.testNestedExceptionAfterInnerStatement.<locals>.shouldThrow)	r/   rI   r5   r   r   r   rG   r   r   r{   r   r   r   &testNestedExceptionAfterInnerStatement  r   z:ExceptionalTestCase.testNestedExceptionAfterInnerStatementc                    sP   t dd   fdd}| td |  W d    d S 1 s!w   Y  d S )Nc                   s       d V  d S r"   r   r   r   r   r   r        
z8ExceptionalTestCase.testRaisedStopIteration1.<locals>.cmc                      $      t d1 sw   Y  d S N	from withStopIterationr   r   r   r   ry        zAExceptionalTestCase.testRaisedStopIteration1.<locals>.shouldThrowr   )r   rV   r   r{   r   r   r   testRaisedStopIteration1  s   
"z,ExceptionalTestCase.testRaisedStopIteration1c                    sT   G dd dt   fdd}| td |  W d    d S 1 s#w   Y  d S )Nc                   @   rp   )z8ExceptionalTestCase.testRaisedStopIteration2.<locals>.cmc                 S   rM   r"   r   r   r   r   r   r     rN   zBExceptionalTestCase.testRaisedStopIteration2.<locals>.cm.__enter__c                 S   rM   r"   r   r   r   r   r   r     rN   zAExceptionalTestCase.testRaisedStopIteration2.<locals>.cm.__exit__Nrt   r   r   r   r   r     rv   r   c                      r   r   r   r   r   r   r   ry     r   zAExceptionalTestCase.testRaisedStopIteration2.<locals>.shouldThrowr   )rU   rV   r   r{   r   r   r   testRaisedStopIteration2  s
   "z,ExceptionalTestCase.testRaisedStopIteration2c                    sN   t dd   fdd}| t |  W d    d S 1 s w   Y  d S )Nc                   s   r   r"   r   r   r   r   r   r     r   z8ExceptionalTestCase.testRaisedStopIteration3.<locals>.cmc                      s(      t tg 1 sw   Y  d S r"   )nextiterr   r   r   r   ry     s   zAExceptionalTestCase.testRaisedStopIteration3.<locals>.shouldThrow)r   rI   r   r{   r   r   r   testRaisedStopIteration3  s   
"z,ExceptionalTestCase.testRaisedStopIteration3c                    s(   t dd   fdd}| t| d S )Nc                   s   r   r"   r   r   r   r   r   r     r   z8ExceptionalTestCase.testRaisedGeneratorExit1.<locals>.cmc                      r   r   GeneratorExitr   r   r   r   ry     r   zAExceptionalTestCase.testRaisedGeneratorExit1.<locals>.shouldThrow)r   rI   r   r{   r   r   r   testRaisedGeneratorExit1  s   
z,ExceptionalTestCase.testRaisedGeneratorExit1c                    r}   )Nc                   @   rp   )z8ExceptionalTestCase.testRaisedGeneratorExit2.<locals>.cmc                 S   rM   r"   r   r   r   r   r   r     rN   zBExceptionalTestCase.testRaisedGeneratorExit2.<locals>.cm.__enter__c                 S   rM   r"   r   r   r   r   r   r     rN   zAExceptionalTestCase.testRaisedGeneratorExit2.<locals>.cm.__exit__Nrt   r   r   r   r   r     rv   r   c                      r   r   r   r   r   r   r   ry     r   zAExceptionalTestCase.testRaisedGeneratorExit2.<locals>.shouldThrow)rU   rI   r   r{   r   r   r   testRaisedGeneratorExit2  s   z,ExceptionalTestCase.testRaisedGeneratorExit2c                    s\   G dd dt   fdd}|   fdd}t|  fdd}t| d S )	Nc                   @   r0   )z0ExceptionalTestCase.testErrorsInBool.<locals>.cmc                    s   G  fddd}| | _ d S )Nc                       s   e Zd Z fddZdS )zGExceptionalTestCase.testErrorsInBool.<locals>.cm.__init__.<locals>.Boolc                    s     S r"   r   r   bool_conversionr   r   __bool__  r   zPExceptionalTestCase.testErrorsInBool.<locals>.cm.__init__.<locals>.Bool.__bool__N)r   r   r    r   r   r   r   r   Bool  s    r   Zexit_result)r   r   r   r   r   r   r	     s   z9ExceptionalTestCase.testErrorsInBool.<locals>.cm.__init__c                 S      dS )N   r   r   r   r   r   r     rN   z:ExceptionalTestCase.testErrorsInBool.<locals>.cm.__enter__c                 S   s   | j S r"   r   r   abcr   r   r   r     r   z9ExceptionalTestCase.testErrorsInBool.<locals>.cm.__exit__NrA   r   r   r   r   r     s    r   c                      <    dd   d W d    d S 1 sw   Y  d S )Nc                   S   r   r   r   r   r   r   r   <lambda>      zJExceptionalTestCase.testErrorsInBool.<locals>.trueAsBool.<locals>.<lambda>Should NOT see thisfailr   r   r   r   
trueAsBool     "z8ExceptionalTestCase.testErrorsInBool.<locals>.trueAsBoolc                      r   )Nc                   S   r   r   r   r   r   r   r   r     r   zKExceptionalTestCase.testErrorsInBool.<locals>.falseAsBool.<locals>.<lambda>zShould raiser   r   r   r   r   falseAsBool  r   z9ExceptionalTestCase.testErrorsInBool.<locals>.falseAsBoolc                      r   )Nc                   S   s   dd S r   r   r   r   r   r   r   !  s    zJExceptionalTestCase.testErrorsInBool.<locals>.failAsBool.<locals>.<lambda>r   r   r   r   r   r   
failAsBool   r   z8ExceptionalTestCase.testErrorsInBool.<locals>.failAsBool)rU   rI   AssertionErrorr   )r   r   r   r   r   r   r   testErrorsInBool  s   z$ExceptionalTestCase.testErrorsInBoolN)r   r   r    r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   c  s    
r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )NonLocalFlowControlTestCasec                 C   sX   d}	 |d7 }t   |d7 }	 W d    n1 sw   Y  |d7 }q| |d d S )Nr   Tr=   
   d      r/   rz   r   counterr   r   r   testWithBreak(  s   z)NonLocalFlowControlTestCase.testWithBreakc                 C   sb   d}	 |d7 }|dkrnt   |d7 }	 W d    q1 sw   Y  |d7 }q| |d d S )Nr   Tr=   r>   r   r      r   r   r   r   r   testWithContinue2  s   z,NonLocalFlowControlTestCase.testWithContinuec                 C   s   dd }|  | d d S )Nc                  S   sJ   d} 	 | d7 } t   | d7 } | W  d    S 1 sw   Y  | d7 } q)Nr   Tr=   r   r   r   )r   r   r   r   rG   ?  s    z7NonLocalFlowControlTestCase.testWithReturn.<locals>.foor   )rz   r   r   r   r   testWithReturn>  s   z*NonLocalFlowControlTestCase.testWithReturnc                 C   s&   dd }t | }| |ddg d S )Nc                   s   s:    t   dV  dV  W d    d S 1 sw   Y  d S )Nr      r   r   r   r   r   genJ  s
   "z6NonLocalFlowControlTestCase.testWithYield.<locals>.genr   r  )listrz   )r   r  xr   r   r   testWithYieldI  s   
z)NonLocalFlowControlTestCase.testWithYieldc                 C   sl   d}z|d7 }t   |d7 }t1 sw   Y  |d7 }W n ty.   | |d Y d S w | d d S )Nr   r=   r   r   r   zDidn't raise RuntimeError)r/   r5   rz   r   r   r   r   r   testWithRaiseQ  s   z)NonLocalFlowControlTestCase.testWithRaiseN)r   r   r    r   r   r   r  r  r   r   r   r   r   &  s    
r   c                   @   r0   )AssignmentTargetTestCasec                 C   sj  dg di}t  "|d d< | t| dg | |d d jt W d    n1 s.w   Y  t  &t| d d< | t| dg | |d d jt W d    n1 s_w   Y  t  |d< t| }|  | |ddg W d    n1 sw   Y  G dd d}| }t  |_| t	|dd W d    d S 1 sw   Y  d S )	Nr=   r   r=   r>   r   r>   c                   @   rZ   )z;AssignmentTargetTestCase.testSingleComplexTarget.<locals>.CNr[   r   r   r   r   Cm      r	  rG   T)
r/   rz   r  keysr   r)   valuessortrG   hasattr)r   targetsr  r	  blahr   r   r   testSingleComplexTargeta  s&   
"z0AssignmentTargetTestCase.testSingleComplexTargetc                 C   s  G dd d}dg di}| !\|d d< |d d< |d d< |  |dg di W d    n1 s4w   Y  | -\t| d d< t| d d< t| d d< |  |dg di W d    n1 slw   Y  | \|d< |d< |d	< |  |ddd	d W d    n1 sw   Y  G d
d d}| }| $\|_|_|_|  |jd |  |jd |  |jd	 W d    d S 1 sw   Y  d S )Nc                   @   rp   )z>AssignmentTargetTestCase.testMultipleComplexTargets.<locals>.Cc                 S   r   )Nr=   r>   r   r   r   r   r   r   r   t  r   zHAssignmentTargetTestCase.testMultipleComplexTargets.<locals>.C.__enter__c                 S   rM   r"   r   r   tvtbr   r   r   r   u  r   zGAssignmentTargetTestCase.testMultipleComplexTargets.<locals>.C.__exit__Nrt   r   r   r   r   r	  s      r	  r=   r  r   r>   r  )r   r>   r=   r   c                   @   rZ   )z>AssignmentTargetTestCase.testMultipleComplexTargets.<locals>.BNr[   r   r   r   r   B}  r
  r  )rz   r  r  ZoneZtwoZthree)r   r	  r  r  r  r   r   r   testMultipleComplexTargetsr  s$   &>"z3AssignmentTargetTestCase.testMultipleComplexTargetsc                 C   sd   t tdd!^}}}| |d | |ddg | |d W d    d S 1 s+w   Y  d S )Nr=      r>   r      )r   rangerz   r   r   r   r   testWithExtendedTargets  s
   "z0AssignmentTargetTestCase.testWithExtendedTargetsN)r   r   r    r  r  r  r   r   r   r   r  _  s    r  c                   @   rp   )ExitSwallowsExceptionTestCasec                 C   sf   G dd d}z|  dd  W d    W d S 1 sw   Y  W d S  t y2   | d Y d S w )Nc                   @   rp   )zSExitSwallowsExceptionTestCase.testExitTrueSwallowsException.<locals>.AfricanSwallowc                 S   rM   r"   r   r   r   r   r   r     r   z]ExitSwallowsExceptionTestCase.testExitTrueSwallowsException.<locals>.AfricanSwallow.__enter__c                 S   r   r   r   r  r   r   r   r     r   z\ExitSwallowsExceptionTestCase.testExitTrueSwallowsException.<locals>.AfricanSwallow.__exit__Nrt   r   r   r   r   AfricanSwallow  r  r  r=   r   z,ZeroDivisionError should have been swallowedr   r   )r   r  r   r   r   testExitTrueSwallowsException  s   
&z;ExitSwallowsExceptionTestCase.testExitTrueSwallowsExceptionc                 C   sd   G dd d}z|  dd  W d    n1 sw   Y  W n
 t y*   Y d S w | d d S )Nc                   @   rp   )zZExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowException.<locals>.EuropeanSwallowc                 S   rM   r"   r   r   r   r   r   r     r   zdExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowException.<locals>.EuropeanSwallow.__enter__c                 S   r   r   r   r  r   r   r   r     r   zcExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowException.<locals>.EuropeanSwallow.__exit__Nrt   r   r   r   r   EuropeanSwallow  r  r"  r=   r   z)ZeroDivisionError should have been raisedr   )r   r"  r   r   r   #testExitFalseDoesntSwallowException  s   
zAExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowExceptionN)r   r   r    r!  r#  r   r   r   r   r    s    
r  c                   @   st   e Zd ZG dd deZG dd deZG dd deZG dd deZd	d
 Zdd Z	dd Z
dd Zdd ZdS )
NestedWithc                   @   s&   e Zd Zd	ddZdd Zdd ZdS )
zNestedWith.DummyNFc                 C   s(   |d u r| }|| _ || _d| _d| _d S r   )r   gobbler
   r   )r   r   r%  r   r   r   r	     s   
zNestedWith.Dummy.__init__c                 C   s   d| _ | jS r   )r
   r   r   r   r   r   r     s   zNestedWith.Dummy.__enter__c                 G   s   d| _ || _| jrdS d S r   )r   r9   r%  rC   r   r   r   r     s
   zNestedWith.Dummy.__exit__r   rA   r   r   r   r   Dummy  s    
r&  c                   @   r(   )zNestedWith.InitRaisesc                 C      t  r"   rr   r   r   r   r   r	         zNestedWith.InitRaises.__init__Nr,   r   r   r   r   
InitRaises  r-   r)  c                   @   rp   )zNestedWith.EnterRaisesc                 C   r'  r"   rr   r   r   r   r   r     r(  z NestedWith.EnterRaises.__enter__c                 G   rM   r"   r   rC   r   r   r   r     r   zNestedWith.EnterRaises.__exit__Nrt   r   r   r   r   EnterRaises  r  r*  c                   @   rp   )zNestedWith.ExitRaisesc                 C   rM   r"   r   r   r   r   r   r     r   zNestedWith.ExitRaises.__enter__c                 G   r'  r"   rr   rC   r   r   r   r     r(  zNestedWith.ExitRaises.__exit__Nrt   r   r   r   r   
ExitRaises  r  r+  c              	   C   s   |   (}|   }| |j | |j W d    n1 s w   Y  W d    n1 s/w   Y  | |j | |j d S r"   )r&  r   r
   r   )r   r   r   r   r   r   testNoExceptions  s    zNestedWith.testNoExceptionsc              	   C   sz   z*|   }|   W d    n1 sw   Y  W d    n1 s$w   Y  W n   Y | |j | |j d S r"   )r&  r)  r   r
   r   r   r   r   r   r   testExceptionInExprList  s    z"NestedWith.testExceptionInExprListc              	   C   s   z/|   !}|   | d W d    n1 sw   Y  W d    n1 s)w   Y  W n	 ty8   Y nw | d | |j | |j d S )Nzbody of bad with executedzRuntimeError not reraised)r&  r*  r   r5   r   r
   r   r-  r   r   r   testExceptionInEnter  s    
zNestedWith.testExceptionInEnterc              	   C   s   d}| j dd}|  
 d}W d    n1 sw   Y  W d    n1 s)w   Y  | |j | |j | | | |jd d  d S )NFT)r%  r   )r&  r+  r   r
   r   ZassertNotEqualr9   )r   Zbody_executedr   r   r   r   testExceptionInExit  s    
zNestedWith.testExceptionInExitc              	   C   s   | j ddC\}}| j dd"\}}| d| | d| | d| | d| W d    n1 s4w   Y  W d    d S W d    d S 1 sLw   Y  d S )N)r=   r>   )r   )r      r=   r>   r   r1  )r&  rz   )r   Za1Za2b1b2r   r   r   testEnterReturnsTuple  s   
Pz NestedWith.testEnterReturnsTupleN)r   r   r    rU   r&  r)  r*  r+  r,  r.  r/  r0  r4  r   r   r   r   r$    s    		r$  __main__)__doc__
__author__Z	__email__r8   Zunittestcollectionsr   
contextlibr   r   r   r   r'   rU   r)   r/   r1   rB   ZTestCaserD   r   r   r   r   r   r  r  r$  r   mainr   r   r   r   <module>   s:    
	$S/5B D9,L