B
    u9aÛw  ã               @   sN   d Z ddlZddlZddlmZmZmZ G dd„ dejƒZe	dkrJe 
¡  dS )z½
Test suite for PEP 380 implementation

adapted from original tests written by Greg Ewing
see <http://www.cosc.canterbury.ac.nz/greg.ewing/python/yield-from/YieldFrom-Python3.1.2-rev5.zip>
é    N)Úcaptured_stderrÚ
disable_gcÚ
gc_collectc               @   s  e Zd 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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d0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Z d<d=„ Z!d>d?„ Z"d@dA„ Z#dBdC„ Z$dDS )EÚTestPEP380Operationz
    Test semantics.
    c                sX   g ‰‡ ‡fdd„}‡fdd„‰ x|ƒ D ]}ˆ  d|f ¡ q&W |  ˆdddd	d
g¡ dS )zH
        Test delegation of initial next() call to subgenerator
        c               3   s$   ˆ  d¡ ˆ ƒ E d H  ˆ  d¡ d S )NzStarting g1zFinishing g1)Úappend© )Úg2Útracer   ú%/usr/lib/python3.7/test_yield_from.pyÚg1   s    
zOTestPEP380Operation.test_delegation_of_initial_next_to_subgenerator.<locals>.g1c               3   s   ˆ   d¡ dV  ˆ   d¡ d S )NzStarting g2é*   zFinishing g2)r   r   )r	   r   r
   r      s    
zOTestPEP380Operation.test_delegation_of_initial_next_to_subgenerator.<locals>.g2z
Yielded %szStarting g1zStarting g2z
Yielded 42zFinishing g2zFinishing g1N)r   ÚassertEqual)Úselfr   Úxr   )r   r	   r
   Ú/test_delegation_of_initial_next_to_subgenerator   s    zCTestPEP380Operation.test_delegation_of_initial_next_to_subgeneratorc          
      sš   g ‰‡ ‡fdd„}‡fdd„‰ y$x|ƒ D ]}ˆ  d|f ¡ q(W W n4 tk
rv } z|  |jd d¡ W dd}~X Y nX |  d	¡ |  ˆd
dddg¡ dS )z?
        Test raising exception in initial next() call
        c            	   3   s,   zˆ  d¡ ˆ ƒ E d H  W d ˆ  d¡ X d S )NzStarting g1zFinishing g1)r   r   )r   r	   r   r
   r   0   s    
zKTestPEP380Operation.test_raising_exception_in_initial_next_call.<locals>.g1c            	      s(   zˆ   d¡ tdƒ‚W d ˆ   d¡ X d S )NzStarting g2zspanish inquisition occurredzFinishing g2)r   Ú
ValueErrorr   )r	   r   r
   r   6   s    
zKTestPEP380Operation.test_raising_exception_in_initial_next_call.<locals>.g2z
Yielded %sr   zspanish inquisition occurredNz'subgenerator failed to raise ValueErrorzStarting g1zStarting g2zFinishing g2zFinishing g1)r   r   r   ÚargsÚfail)r   r   r   Úer   )r   r	   r
   Ú+test_raising_exception_in_initial_next_call+   s    $
z?TestPEP380Operation.test_raising_exception_in_initial_next_callc                s^   g ‰‡ ‡fdd„}‡fdd„‰ x|ƒ D ]}ˆ  d|f ¡ q&W |  ˆdddd	d
dddg¡ dS )z@
        Test delegation of next() call to subgenerator
        c               3   s0   ˆ  d¡ dV  ˆ ƒ E d H  dV  ˆ  d¡ d S )NzStarting g1zg1 hamzg1 eggszFinishing g1)r   r   )r   r	   r   r
   r   O   s
    
zLTestPEP380Operation.test_delegation_of_next_call_to_subgenerator.<locals>.g1c               3   s$   ˆ   d¡ dV  dV  ˆ   d¡ d S )NzStarting g2zg2 spamzg2 more spamzFinishing g2)r   r   )r	   r   r
   r   U   s    
zLTestPEP380Operation.test_delegation_of_next_call_to_subgenerator.<locals>.g2z
Yielded %szStarting g1zYielded g1 hamzStarting g2zYielded g2 spamzYielded g2 more spamzFinishing g2zYielded g1 eggszFinishing g1N)r   r   )r   r   r   r   )r   r	   r
   Ú,test_delegation_of_next_call_to_subgeneratorJ   s    z@TestPEP380Operation.test_delegation_of_next_call_to_subgeneratorc          
      sž   g ‰‡ ‡fdd„}‡fdd„‰ y$x|ƒ D ]}ˆ  d|f ¡ q(W W n4 tk
rv } z|  |jd d¡ W dd}~X Y nX |  d	¡ |  ˆd
dddddg¡ dS )zA
        Test raising exception in delegated next() call
        c            	   3   s8   z&ˆ  d¡ dV  ˆ ƒ E d H  dV  W d ˆ  d¡ X d S )NzStarting g1zg1 hamzg1 eggszFinishing g1)r   r   )r   r	   r   r
   r   l   s    

zMTestPEP380Operation.test_raising_exception_in_delegated_next_call.<locals>.g1c            	   3   s4   z"ˆ   d¡ dV  tdƒ‚dV  W d ˆ   d¡ X d S )NzStarting g2zg2 spamzhovercraft is full of eelszg2 more spamzFinishing g2)r   r   r   )r	   r   r
   r   t   s    

zMTestPEP380Operation.test_raising_exception_in_delegated_next_call.<locals>.g2z
Yielded %sr   zhovercraft is full of eelsNz'subgenerator failed to raise ValueErrorzStarting g1zYielded g1 hamzStarting g2zYielded g2 spamzFinishing g2zFinishing g1)r   r   r   r   r   )r   r   r   r   r   )r   r	   r
   Ú-test_raising_exception_in_delegated_next_callg   s     $
zATestPEP380Operation.test_raising_exception_in_delegated_next_callc                s˜   g ‰‡ ‡fdd„}‡fdd„‰ |ƒ }t |ƒ}d}y,x&| |¡}ˆ d|f ¡ |d7 }q4W W n tk
rp   Y nX |  ˆddd	d
dddddddg¡ dS )z+
        Test delegation of send()
        c              3   sP   ˆ  d¡ dV } ˆ  d| f ¡ ˆ ƒ E d H  dV } ˆ  d| f ¡ ˆ  d¡ d S )NzStarting g1zg1 hamzg1 received %szg1 eggszFinishing g1)r   )r   )r   r	   r   r
   r   ‘   s    
z7TestPEP380Operation.test_delegation_of_send.<locals>.g1c              3   sD   ˆ   d¡ dV } ˆ   d| f ¡ dV } ˆ   d| f ¡ ˆ   d¡ d S )NzStarting g2zg2 spamzg2 received %szg2 more spamzFinishing g2)r   )r   )r	   r   r
   r   ™   s    
z7TestPEP380Operation.test_delegation_of_send.<locals>.g2é   z
Yielded %szStarting g1zg1 received 1zStarting g2zYielded g2 spamzg2 received 2zYielded g2 more spamzg2 received 3zFinishing g2zYielded g1 eggszg1 received 4zFinishing g1N)ÚnextÚsendr   ÚStopIterationr   )r   r   ÚgÚyr   r   )r   r	   r
   Útest_delegation_of_sendŒ   s2    
z+TestPEP380Operation.test_delegation_of_sendc                sR   g ‰‡‡fdd„‰ ‡fdd„‰‡ ‡fdd„}|   t|¡ |  ˆddd	d
dg¡ dS )zA
        Test handling exception while delegating 'send'
        c              3   sP   ˆ  d¡ dV } ˆ  d| f ¡ ˆ ƒ E d H  dV } ˆ  d| f ¡ ˆ  d¡ d S )NzStarting g1zg1 hamzg1 received %szg1 eggszFinishing g1)r   )r   )r   r	   r   r
   r   ½   s    
zMTestPEP380Operation.test_handling_exception_while_delegating_send.<locals>.g1c              3   sL   ˆ   d¡ dV } ˆ   d| f ¡ tdƒ‚dV } ˆ   d| f ¡ ˆ   d¡ d S )NzStarting g2zg2 spamzg2 received %szhovercraft is full of eelszg2 more spamzFinishing g2)r   r   )r   )r	   r   r
   r   Å   s    
zMTestPEP380Operation.test_handling_exception_while_delegating_send.<locals>.g2c                 sb   ˆ ƒ } t | ƒ}d}y,x&|  |¡}ˆ d|f ¡ |d7 }qW W n tk
r\   ˆ d¡ Y nX d S )Nr   z
Yielded %sr   )r   r   r   r   )r   r   r   )r   r	   r   r
   ÚrunÍ   s    
zNTestPEP380Operation.test_handling_exception_while_delegating_send.<locals>.runzStarting g1zg1 received 1zStarting g2zYielded g2 spamzg2 received 2N)ÚassertRaisesr   r   )r   r   r   )r   r   r	   r
   Ú-test_handling_exception_while_delegating_send¸   s    zATestPEP380Operation.test_handling_exception_while_delegating_sendc          	      sr   g ‰‡ ‡fdd„}‡fdd„‰ |ƒ }x(t dƒD ]}t|ƒ}ˆ d|f ¡ q.W | ¡  |  ˆddd	d
ddg¡ dS )z)
        Test delegating 'close'
        c            	   3   s8   z&ˆ  d¡ dV  ˆ ƒ E d H  dV  W d ˆ  d¡ X d S )NzStarting g1zg1 hamzg1 eggszFinishing g1)r   r   )r   r	   r   r
   r   æ   s    

z5TestPEP380Operation.test_delegating_close.<locals>.g1c            	   3   s,   zˆ   d¡ dV  dV  W d ˆ   d¡ X d S )NzStarting g2zg2 spamzg2 more spamzFinishing g2)r   r   )r	   r   r
   r   î   s
    

z5TestPEP380Operation.test_delegating_close.<locals>.g2é   z
Yielded %szStarting g1zYielded g1 hamzStarting g2zYielded g2 spamzFinishing g2zFinishing g1N)Úranger   r   Úcloser   )r   r   r   Úir   r   )r   r	   r
   Útest_delegating_closeá   s    z)TestPEP380Operation.test_delegating_closec          
      sÄ   g ‰‡ ‡fdd„}‡fdd„‰ y<|ƒ }x(t dƒD ]}t|ƒ}ˆ d|f ¡ q0W | ¡  W nB tk
rœ } z$|  |jd d¡ |  |jt	¡ W d	d	}~X Y nX |  
d
¡ |  ˆddddddg¡ d	S )zB
        Test handling exception while delegating 'close'
        c            	   3   s8   z&ˆ  d¡ dV  ˆ ƒ E d H  dV  W d ˆ  d¡ X d S )NzStarting g1zg1 hamzg1 eggszFinishing g1)r   r   )r   r	   r   r
   r     s    

zMTestPEP380Operation.test_handing_exception_while_delegating_close.<locals>.g1c            	   3   s4   zˆ   d¡ dV  dV  W d ˆ   d¡ tdƒ‚X d S )NzStarting g2zg2 spamzg2 more spamzFinishing g2z"nybbles have exploded with delight)r   r   r   )r	   r   r
   r     s    


zMTestPEP380Operation.test_handing_exception_while_delegating_close.<locals>.g2r"   z
Yielded %sr   z"nybbles have exploded with delightNz'subgenerator failed to raise ValueErrorzStarting g1zYielded g1 hamzStarting g2zYielded g2 spamzFinishing g2zFinishing g1)r#   r   r   r$   r   r   r   ÚassertIsInstanceÚ__context__ÚGeneratorExitr   )r   r   r   r%   r   r   r   )r   r	   r
   Ú-test_handing_exception_while_delegating_close  s(     
zATestPEP380Operation.test_handing_exception_while_delegating_closec          
      sÀ   g ‰‡ ‡fdd„}‡fdd„‰ yF|ƒ }x(t dƒD ]}t|ƒ}ˆ d|f ¡ q0W tdƒ}| |¡ W n4 tk
r˜ } z|  |jd d¡ W d	d	}~X Y nX |  d
¡ |  ˆddddddg¡ d	S )z)
        Test delegating 'throw'
        c            	   3   s8   z&ˆ  d¡ dV  ˆ ƒ E d H  dV  W d ˆ  d¡ X d S )NzStarting g1zg1 hamzg1 eggszFinishing g1)r   r   )r   r	   r   r
   r   1  s    

z5TestPEP380Operation.test_delegating_throw.<locals>.g1c            	   3   s,   zˆ   d¡ dV  dV  W d ˆ   d¡ X d S )NzStarting g2zg2 spamzg2 more spamzFinishing g2)r   r   )r	   r   r
   r   9  s
    

z5TestPEP380Operation.test_delegating_throw.<locals>.g2r"   z
Yielded %sztomato ejectedr   Nz'subgenerator failed to raise ValueErrorzStarting g1zYielded g1 hamzStarting g2zYielded g2 spamzFinishing g2zFinishing g1)r#   r   r   r   Úthrowr   r   r   )r   r   r   r%   r   r   r   )r   r	   r
   Útest_delegating_throw,  s(    $
z)TestPEP380Operation.test_delegating_throwc          	      sX   g ‰ ‡ fdd„}t ƒ }||ƒ t dƒ}||ƒ d|_||ƒ |  ˆ dddddd	g¡ d
S )zC
        Test 'value' attribute of StopIteration exception
        c                s,   ˆ   d| jj| f ¡ ˆ   d| jf ¡ d S )Nz%s: %sz
value = %s)r   Ú	__class__Ú__name__Úvalue)r   )r	   r   r
   ÚpexY  s    zPTestPEP380Operation.test_value_attribute_of_StopIteration_exception.<locals>.pexÚspamÚeggszStopIteration: zvalue = NonezStopIteration: spamzvalue = spamzvalue = eggsN)r   r/   r   )r   r0   r   r   )r	   r
   Ú/test_value_attribute_of_StopIteration_exceptionT  s    zCTestPEP380Operation.test_value_attribute_of_StopIteration_exceptionc                s,   ‡ fdd„}dd„ ‰ |   t|ƒ ƒdg¡ d S )Nc               3   s   ˆ ƒ E d H  d S )Nr   r   )r   r   r
   r   o  s    z:TestPEP380Operation.test_exception_value_crash.<locals>.g1c               s   s   dV  dgS )Nr   r   r   r   r   r   r
   r   q  s    z:TestPEP380Operation.test_exception_value_crash.<locals>.g2r   )r   Úlist)r   r   r   )r   r
   Útest_exception_value_crashl  s    z.TestPEP380Operation.test_exception_value_crashc                s€   g ‰‡ ‡fdd„}d‡fdd„	‰ x|ƒ D ]}ˆ  d|f ¡ q(W |  ˆddd	d
dddd	d
dddd	d
dddd	d
dddddg¡ dS )z-
        Test generator return value
        c              3   sv   ˆ  d¡ dV  ˆ ƒ E d H } ˆ  d| f ¡ x4ddtdƒfD ]"}ˆ |ƒE d H } ˆ  d| f ¡ q<W dV  ˆ  d¡ d S )	NzStarting g1zg1 hamzg2 returned %rr   )r"   é   zg1 eggszFinishing g1)r   r   )ÚretÚv)r   r	   r   r
   r   |  s    
z;TestPEP380Operation.test_generator_return_value.<locals>.g1Nc             3   s,   ˆ   d¡ dV  dV  ˆ   d¡ | r(| S d S )NzStarting g2zg2 spamzg2 more spamzFinishing g2)r   )r8   )r	   r   r
   r   †  s    

z;TestPEP380Operation.test_generator_return_value.<locals>.g2z
Yielded %szStarting g1zYielded g1 hamzStarting g2zYielded g2 spamzYielded g2 more spamzFinishing g2zg2 returned Nonezg2 returned 1zg2 returned (2,)zg2 returned StopIteration(3)zYielded g1 eggszFinishing g1)N)r   r   )r   r   r   r   )r   r	   r
   Útest_generator_return_valuew  s<    
z/TestPEP380Operation.test_generator_return_valuec             C   sB   g }dd„ }x|ƒ D ]}|  d|f ¡ qW |  |dddg¡ dS )z<
        Test delegation of next() to non-generator
        c               s   s   t dƒE d H  d S )Nr6   )r#   r   r   r   r
   r   ¯  s    zGTestPEP380Operation.test_delegation_of_next_to_non_generator.<locals>.gz
Yielded %sz	Yielded 0z	Yielded 1z	Yielded 2N)r   r   )r   r	   r   r   r   r   r
   Ú(test_delegation_of_next_to_non_generatorª  s    z<TestPEP380Operation.test_delegation_of_next_to_non_generatorc             C   sT   g }dd„ }|ƒ }x*t dƒD ]}| d¡}| d|f ¡ qW |  |dddg¡ dS )	z9
        Test conversion of send(None) to next()
        c               s   s   t dƒE d H  d S )Nr6   )r#   r   r   r   r
   r   ¿  s    zBTestPEP380Operation.test_conversion_of_sendNone_to_next.<locals>.gr6   NzYielded: %sz
Yielded: 0z
Yielded: 1z
Yielded: 2)r#   r   r   r   )r   r	   r   Úgir   r   r   r   r
   Ú#test_conversion_of_sendNone_to_nextº  s    
z7TestPEP380Operation.test_conversion_of_sendNone_to_nextc          	      s\   g ‰ ‡ fdd„}|ƒ }t |ƒ tƒ }| ¡  W dQ R X |  | ¡ d¡ |  ˆ ddg¡ dS )z=
        Test delegation of close() to non-generator
        c            	   3   s8   z&ˆ   d¡ tdƒE d H  ˆ   d¡ W d ˆ   d¡ X d S )Nz
starting gr6   zg should not be herezfinishing g)r   r#   r   )r	   r   r
   r   Ð  s
    
zHTestPEP380Operation.test_delegation_of_close_to_non_generator.<locals>.gNÚ z
starting gzfinishing g)r   r   r$   r   Úgetvalue)r   r   r;   Úoutputr   )r	   r
   Ú)test_delegation_of_close_to_non_generatorË  s    z=TestPEP380Operation.test_delegation_of_close_to_non_generatorc          
      s´   g ‰ ‡ fdd„}yF|ƒ }x(t dƒD ]}t|ƒ}ˆ  d|f ¡ q"W tdƒ}| |¡ W n4 tk
rŠ } z|  |jd d¡ W dd}~X Y nX |  d¡ |  ˆ d	d
dddddg¡ dS )z:
        Test delegating 'throw' to non-generator
        c            	   3   s.   zˆ   d¡ tdƒE d H  W d ˆ   d¡ X d S )Nz
Starting gé
   zFinishing g)r   r#   r   )r	   r   r
   r   æ  s    
zETestPEP380Operation.test_delegating_throw_to_non_generator.<locals>.gé   z
Yielded %sztomato ejectedr   Nz'subgenerator failed to raise ValueErrorz
Starting gz	Yielded 0z	Yielded 1z	Yielded 2z	Yielded 3z	Yielded 4zFinishing g)r#   r   r   r   r+   r   r   r   )r   r   r;   r%   r   r   r   )r	   r
   Ú&test_delegating_throw_to_non_generatorá  s(    $
z:TestPEP380Operation.test_delegating_throw_to_non_generatorc          
      s¢   g ‰ ‡ fdd„}y>|ƒ }t |ƒ x*tdƒD ]}| d¡}ˆ  d|f ¡ q*W W n4 tk
r‚ } z|  d|jd ¡ W dd}~X Y nX |  d	¡ |  ˆ d
dg¡ dS )z:
        Test attempting to send to non-generator
        c            	   3   s8   z&ˆ   d¡ tdƒE d H  ˆ   d¡ W d ˆ   d¡ X d S )Nz
starting gr6   zg should not be herezfinishing g)r   r#   r   )r	   r   r
   r     s
    
zGTestPEP380Operation.test_attempting_to_send_to_non_generator.<locals>.gr6   r   zShould not have yielded: %sr   r   Nz#was able to send into non-generatorz
starting gzfinishing g)	r   r#   r   r   ÚAttributeErrorÚassertInr   r   r   )r   r   r;   r   r   r   r   )r	   r
   Ú(test_attempting_to_send_to_non_generator  s    
$
z<TestPEP380Operation.test_attempting_to_send_to_non_generatorc          	      sÊ   G dd„ dƒ‰ ‡ fdd„}|   t¡& |ƒ }|  t|ƒd¡ | d¡ W dQ R X |   t¡& |ƒ }|  t|ƒd¡ | t¡ W dQ R X tƒ $}|ƒ }|  t|ƒd¡ | ¡  W dQ R X |  	d| 
¡ ¡ dS )zG
        Test subiterator with a broken getattr implementation
        c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )z@TestPEP380Operation.test_broken_getattr_handling.<locals>.Brokenc             S   s   | S )Nr   )r   r   r   r
   Ú__iter__!  s    zITestPEP380Operation.test_broken_getattr_handling.<locals>.Broken.__iter__c             S   s   dS )Nr   r   )r   r   r   r
   Ú__next__#  s    zITestPEP380Operation.test_broken_getattr_handling.<locals>.Broken.__next__c             S   s   dd  d S )Nr   r   r   )r   Úattrr   r   r
   Ú__getattr__%  s    zLTestPEP380Operation.test_broken_getattr_handling.<locals>.Broken.__getattr__N)r.   Ú
__module__Ú__qualname__rG   rH   rJ   r   r   r   r
   ÚBroken   s   rM   c               3   s   ˆ ƒ E d H  d S )Nr   r   )rM   r   r
   r   (  s    z;TestPEP380Operation.test_broken_getattr_handling.<locals>.gr   NÚZeroDivisionError)r    rN   r   r   r   r+   rD   r   r$   rE   r>   )r   r   r;   r?   r   )rM   r
   Útest_broken_getattr_handling  s    z0TestPEP380Operation.test_broken_getattr_handlingc                sD   g ‰‡‡fdd„‰ dd„ ‰‡ fdd„}|   t|¡ |  ˆdg¡ dS )	z7
        Test exception in initial next() call
        c               3   s$   ˆ  d¡ ˆ ƒ E d H  ˆ  d¡ d S )Nzg1 about to yield from g2zg1 should not be here)r   r   )r   r	   r   r
   r   @  s    
zCTestPEP380Operation.test_exception_in_initial_next_call.<locals>.g1c               s   s   dd V  d S )Nr   r   r   r   r   r   r
   r   D  s    zCTestPEP380Operation.test_exception_in_initial_next_call.<locals>.g2c                 s   ˆ ƒ } t | ƒ d S )N)r   )r;   )r   r   r
   r   F  s    zDTestPEP380Operation.test_exception_in_initial_next_call.<locals>.runzg1 about to yield from g2N)r    rN   r   )r   r   r   )r   r   r	   r
   Ú#test_exception_in_initial_next_call;  s    z7TestPEP380Operation.test_exception_in_initial_next_callc          
      s¤   g ‰‡ ‡fdd„}‡‡fdd„‰ y(|ƒ ‰xˆD ]}ˆ  d|f ¡ q.W W n4 tk
r| } z|  |jd d¡ W dd}~X Y nX |  d	¡ |  ˆd
dddddg¡ dS )z0
        Test attempted yield-from loop
        c               3   s4   ˆ  d¡ dV  ˆ  d¡ ˆ ƒ E d H  ˆ  d¡ d S )Nzg1: startingZy1zg1: about to yield from g2zg1 should not be here)r   r   )r   r	   r   r
   r   S  s
    

z>TestPEP380Operation.test_attempted_yield_from_loop.<locals>.g1c               3   s2   ˆ  d¡ dV  ˆ  d¡ ˆ E d H  ˆ  d¡ d S )Nzg2: startingZy2zg2: about to yield from g1zg2 should not be here)r   r   )r;   r	   r   r
   r   Z  s
    


z>TestPEP380Operation.test_attempted_yield_from_loop.<locals>.g2zYielded: %sr   zgenerator already executingNz$subgenerator didn't raise ValueErrorzg1: startingzYielded: y1zg1: about to yield from g2zg2: startingzYielded: y2zg2: about to yield from g1)r   r   r   r   r   )r   r   r   r   r   )r   r;   r	   r
   Útest_attempted_yield_from_loopN  s"    
$
z2TestPEP380Operation.test_attempted_yield_from_loopc                s°   g ‰‡‡fdd„}‡ ‡fdd„‰G dd„ dt ƒ‰ |ƒ }x(tdƒD ]}t|ƒ}ˆ d|f ¡ q@W ˆ d	ƒ}| |¡ x|D ]}ˆ d|f ¡ qxW |  ˆd
dddddddg¡ dS )z=
        Test returning value from delegated 'throw'
        c            	   3   s8   z&ˆ  d¡ dV  ˆ ƒ E d H  dV  W d ˆ  d¡ X d S )NzStarting g1zg1 hamzg1 eggszFinishing g1)r   r   )r   r	   r   r
   r   v  s    

zITestPEP380Operation.test_returning_value_from_delegated_throw.<locals>.g1c               3   sJ   yˆ  d¡ dV  dV  W n* ˆ k
rD   ˆ  d¡ dV  dV  Y nX d S )NzStarting g2zg2 spamzg2 more spamzCaught LunchError in g2zg2 lunch savedzg2 yet more spam)r   r   )Ú
LunchErrorr	   r   r
   r   ~  s    


zITestPEP380Operation.test_returning_value_from_delegated_throw.<locals>.g2c               @   s   e Zd ZdS )zQTestPEP380Operation.test_returning_value_from_delegated_throw.<locals>.LunchErrorN)r.   rK   rL   r   r   r   r
   rR   ‡  s   rR   r"   z
Yielded %sztomato ejectedzStarting g1zYielded g1 hamzStarting g2zYielded g2 spamzCaught LunchError in g2zYielded g2 yet more spamzYielded g1 eggszFinishing g1N)Ú	Exceptionr#   r   r   r+   r   )r   r   r   r%   r   r   r   )rR   r   r	   r
   Ú)test_returning_value_from_delegated_throwq  s*    	

z=TestPEP380Operation.test_returning_value_from_delegated_throwc                sr   g ‰‡ ‡fdd„}‡fdd„‰ |dƒ |dƒ |dƒ |t dƒƒ |  ˆd	d
ddd	d
ddd	d
ddd	d
ddg¡ dS )z1
        Test next and return with value
        c          
      sh   ˆ | ƒ}t |ƒ y ˆ d¡ t |ƒ ˆ d¡ W n2 tk
rb } zˆ d|f ¡ W d d }~X Y nX d S )Nzf resuming gzf SHOULD NOT BE HEREzf caught %r)r   r   r   )Úrr;   r   )r   r	   r   r
   Úf¡  s    
z>TestPEP380Operation.test_next_and_return_with_value.<locals>.fc             3   s$   ˆ   d¡ d V  ˆ   d| f ¡ | S )Nz
g startingzg returning %r)r   )rU   )r	   r   r
   r   ª  s    
z>TestPEP380Operation.test_next_and_return_with_value.<locals>.gNr   )r"   r6   z
g startingzf resuming gzg returning Nonezf caught StopIteration()zg returning 1zf caught StopIteration(1)zg returning (2,)zf caught StopIteration((2,))zg returning StopIteration(3)z(f caught StopIteration(StopIteration(3)))r   r   )r   rV   r   )r   r	   r
   Útest_next_and_return_with_valueœ  s0    	z3TestPEP380Operation.test_next_and_return_with_valuec                sz   g ‰‡ ‡fdd„}‡fdd„‰ |dƒ |dƒ |dƒ |t dƒƒ |  ˆd	d
dddd	d
dddd	d
dddd	d
dddg¡ dS )z1
        Test send and return with value
        c          
      sj   ˆ | ƒ}t |ƒ y"ˆ d¡ | d¡ ˆ d¡ W n2 tk
rd } zˆ d|f ¡ W d d }~X Y nX d S )Nzf sending spam to gr1   zf SHOULD NOT BE HEREzf caught %r)r   r   r   r   )rU   r;   r   )r   r	   r   r
   rV   Ë  s    

z>TestPEP380Operation.test_send_and_return_with_value.<locals>.fc             3   s4   ˆ   d¡ d V }ˆ   d|f ¡ ˆ   d| f ¡ | S )Nz
g startingzg received %rzg returning %r)r   )rU   r   )r	   r   r
   r   Ô  s
    
z>TestPEP380Operation.test_send_and_return_with_value.<locals>.gNr   )r"   r6   z
g startingzf sending spam to gzg received 'spam'zg returning Nonezf caught StopIteration()zg returning 1zf caught StopIteration(1)zg returning (2,)zf caught StopIteration((2,))zg returning StopIteration(3)z(f caught StopIteration(StopIteration(3)))r   r   )r   rV   r   )r   r	   r
   Útest_send_and_return_with_valueÆ  s8    	z3TestPEP380Operation.test_send_and_return_with_valuec          	      s‚   ‡‡fdd„‰ ‡ ‡fdd„}x`ddt dƒfD ]N‰g ‰|ƒ }ˆ t|ƒ¡ ˆ t| t¡ƒ¡ |  ˆddd	ˆf tˆƒg¡ q,W d
S )ze
        Test catching an exception thrown into a
        subgenerator and returning a value
        c               3   s.   y
dV  W n t k
r(   ˆ  d¡ Y nX ˆS )Nr   zinner caught ValueError)r   r   r   )r	   r/   r   r
   Úinnerú  s
    
zTTestPEP380Operation.test_catching_exception_from_subgen_and_returning.<locals>.innerc              3   s&   ˆ ƒ E d H } ˆ  d| f ¡ | V  d S )Nzinner returned %r to outer)r   )r8   )rY   r	   r   r
   Úouter  s    zTTestPEP380Operation.test_catching_exception_from_subgen_and_returning.<locals>.outerr"   )r"   r   zinner caught ValueErrorzinner returned %r to outerN)r   r   r   Úreprr+   r   r   )r   rZ   r   r   )rY   r	   r/   r
   Ú1test_catching_exception_from_subgen_and_returningõ  s    zETestPEP380Operation.test_catching_exception_from_subgen_and_returningc                sn   g ‰‡fdd„‰ ‡ ‡fdd„}y|ƒ }t |ƒ | t¡ W n tk
rN   Y nX |  d¡ |  ˆddg¡ dS )	zo
        Test throwing GeneratorExit into a subgenerator that
        catches it and returns normally.
        c               3   s8   yˆ   d¡ d V  ˆ   d¡ W n tk
r2   d S X d S )NzEnter fzExit f)r   r)   r   )r	   r   r
   rV     s    
zSTestPEP380Operation.test_throwing_GeneratorExit_into_subgen_that_returns.<locals>.fc               3   s$   ˆ  d¡ ˆ ƒ E d H  ˆ  d¡ d S )NzEnter gzExit g)r   r   )rV   r	   r   r
   r     s    
zSTestPEP380Operation.test_throwing_GeneratorExit_into_subgen_that_returns.<locals>.gz*subgenerator failed to raise GeneratorExitzEnter gzEnter fN)r   r+   r)   r   r   )r   r   r;   r   )rV   r	   r
   Ú4test_throwing_GeneratorExit_into_subgen_that_returns  s    
zHTestPEP380Operation.test_throwing_GeneratorExit_into_subgen_that_returnsc          
      sŽ   g ‰‡fdd„‰ ‡ ‡fdd„}y|ƒ }t |ƒ | t¡ W n4 tk
rn } z|  |jd d¡ W dd}~X Y nX |  d¡ |  ˆd	d
g¡ dS )ze
        Test throwing GeneratorExit into a subgenerator that
        catches it and yields.
        c               3   s>   yˆ   d¡ d V  ˆ   d¡ W n tk
r8   d V  Y nX d S )NzEnter fzExit f)r   r)   r   )r	   r   r
   rV   6  s    
zXTestPEP380Operation.test_throwing_GeneratorExit_into_subgenerator_that_yields.<locals>.fc               3   s$   ˆ  d¡ ˆ ƒ E d H  ˆ  d¡ d S )NzEnter gzExit g)r   r   )rV   r	   r   r
   r   =  s    
zXTestPEP380Operation.test_throwing_GeneratorExit_into_subgenerator_that_yields.<locals>.gr   zgenerator ignored GeneratorExitNz*subgenerator failed to raise GeneratorExitzEnter gzEnter f)r   r+   r)   ÚRuntimeErrorr   r   r   )r   r   r;   r   r   )rV   r	   r
   Ú9test_throwing_GeneratorExit_into_subgenerator_that_yields0  s    $
zMTestPEP380Operation.test_throwing_GeneratorExit_into_subgenerator_that_yieldsc          
      sœ   g ‰‡fdd„‰ ‡ ‡fdd„}y|ƒ }t |ƒ | t¡ W nB tk
r| } z$|  |jd d¡ |  |jt¡ W dd}~X Y nX |  d¡ |  ˆd	d
g¡ dS )z{
        Test throwing GeneratorExit into a subgenerator that
        catches it and raises a different exception.
        c               3   s@   yˆ   d¡ d V  ˆ   d¡ W n tk
r:   tdƒ‚Y nX d S )NzEnter fzExit fzVorpal bunny encountered)r   r)   r   r   )r	   r   r
   rV   T  s    
zRTestPEP380Operation.test_throwing_GeneratorExit_into_subgen_that_raises.<locals>.fc               3   s$   ˆ  d¡ ˆ ƒ E d H  ˆ  d¡ d S )NzEnter gzExit g)r   r   )rV   r	   r   r
   r   [  s    
zRTestPEP380Operation.test_throwing_GeneratorExit_into_subgen_that_raises.<locals>.gr   zVorpal bunny encounteredNz'subgenerator failed to raise ValueErrorzEnter gzEnter f)	r   r+   r)   r   r   r   r'   r(   r   )r   r   r;   r   r   )rV   r	   r
   Ú3test_throwing_GeneratorExit_into_subgen_that_raisesN  s     
zGTestPEP380Operation.test_throwing_GeneratorExit_into_subgen_that_raisesc             C   s   dd„ }|   tt|ƒ ¡ d S )Nc               s   s   dE d H  d S )Nr   r   r   r   r   r
   r   n  s    z4TestPEP380Operation.test_yield_from_empty.<locals>.g)r    r   r   )r   r   r   r   r
   Útest_yield_from_emptym  s    z)TestPEP380Operation.test_yield_from_emptyc                s2  ‡fdd„}‡fdd„‰|ƒ ‰ˆ  tˆƒddddg¡ |ƒ ‰tˆƒg}yx| ˆ d	¡¡ qJW W n tk
rt   Y nX ˆ  |ddddg¡ G d
d„ dtƒ‰ ‡ ‡fdd„}‡ ‡fdd„‰|ƒ ‰tˆƒg}yx| ˆ ˆ ¡¡ qÊW W n tk
rô   Y nX G ‡‡fdd„dtƒ‰‡fdd„}|ƒ ‰tˆƒ ˆ 	¡  d S )Nc               3   s   dV  ˆ ƒ E d H  dV  d S )Nr   r6   r   r   )Útwor   r
   Úonet  s    zOTestPEP380Operation.test_delegating_generators_claim_to_be_running.<locals>.onec               3   s4   dV  yˆ E d H  W n t k
r(   Y nX dV  d S )Nr   r"   )r   r   )r   r   r
   rb   x  s    zOTestPEP380Operation.test_delegating_generators_claim_to_be_running.<locals>.twor   r   r"   r6   r   c               @   s   e Zd ZdS )zQTestPEP380Operation.test_delegating_generators_claim_to_be_running.<locals>.MyErrN)r.   rK   rL   r   r   r   r
   ÚMyErr‹  s   rd   c               3   sP   y
dV  W n ˆ k
r   Y nX ˆƒ E d H  y
dV  W n ˆ k
rJ   Y nX d S )Nr   r6   r   r   )rd   rb   r   r
   rc     s    

c               3   sh   y
dV  W n ˆ k
r   Y nX yˆE d H  W n t k
rB   Y nX y
dV  W n ˆ k
rb   Y nX d S )Nr   r"   )r   r   )rd   r   r   r
   rb   —  s    

c                   s*   e Zd Zdd„ Zdd„ Z‡ ‡fdd„ZdS )zPTestPEP380Operation.test_delegating_generators_claim_to_be_running.<locals>.MyItc             S   s   | S )Nr   )r   r   r   r
   rG   ­  s    zYTestPEP380Operation.test_delegating_generators_claim_to_be_running.<locals>.MyIt.__iter__c             S   s   dS )Nr   r   )r   r   r   r
   rH   ¯  s    zYTestPEP380Operation.test_delegating_generators_claim_to_be_running.<locals>.MyIt.__next__c                s   ˆ  ˆ j¡ ˆ ttˆ ¡ d S )N)Ú
assertTrueÚ
gi_runningr    r   r   )Zself_)r   r   r   r
   r$   ±  s    zVTestPEP380Operation.test_delegating_generators_claim_to_be_running.<locals>.MyIt.closeN)r.   rK   rL   rG   rH   r$   r   )r   r   r   r
   ÚMyIt¬  s   rg   c               3   s   ˆ ƒ E d H  d S )Nr   r   )rg   r   r
   rc   ´  s    )
r   r4   r   r   r   r   rS   r+   Úobjectr$   )r   rc   Zresr   )rd   rg   r   r   rb   r
   Ú.test_delegating_generators_claim_to_be_runningr  s6    


zBTestPEP380Operation.test_delegating_generators_claim_to_be_runningc                sx   dd„ ‰ ‡ fdd„}dd„ }dd„ }x ||ƒ ƒD ]}|   d	|k¡ q0W x,|||ƒ ƒƒD ]}|   d	|kold
|k¡ qVW d S )Nc               S   s   dd„ t  ¡ D ƒS )Nc             S   s   g | ]}|d  ‘qS )r6   r   )Ú.0rV   r   r   r
   ú
<listcomp>¼  s    zaTestPEP380Operation.test_delegator_is_visible_to_debugger.<locals>.call_stack.<locals>.<listcomp>)ÚinspectÚstackr   r   r   r
   Ú
call_stack»  s    zMTestPEP380Operation.test_delegator_is_visible_to_debugger.<locals>.call_stackc               3   s   ˆ ƒ V  ˆ ƒ V  ˆ ƒ V  d S )Nr   r   )rn   r   r
   Úgen¾  s    zFTestPEP380Operation.test_delegator_is_visible_to_debugger.<locals>.genc             s   s   | E d H  d S )Nr   )r   r   r   r
   r1   Ã  s    zGTestPEP380Operation.test_delegator_is_visible_to_debugger.<locals>.spamc             s   s   | E d H  d S )Nr   )r   r   r   r
   r2   Æ  s    zGTestPEP380Operation.test_delegator_is_visible_to_debugger.<locals>.eggsr1   r2   )re   )r   ro   r1   r2   rm   r   )rn   r
   Ú%test_delegator_is_visible_to_debuggerº  s    z9TestPEP380Operation.test_delegator_is_visible_to_debuggerc                s:   G dd„ dƒ‰ ‡ ‡fdd„}d ‰t |ƒ ƒ |  ˆd¡ d S )Nc               @   s   e Zd Zdd„ Zdd„ ZdS )z?TestPEP380Operation.test_custom_iterator_return.<locals>.MyIterc             S   s   | S )Nr   )r   r   r   r
   rG   Ò  s    zHTestPEP380Operation.test_custom_iterator_return.<locals>.MyIter.__iter__c             S   s   t dƒ‚d S )Nr   )r   )r   r   r   r
   rH   Ô  s    zHTestPEP380Operation.test_custom_iterator_return.<locals>.MyIter.__next__N)r.   rK   rL   rG   rH   r   r   r   r
   ÚMyIterÑ  s   rq   c               3   s   ˆ ƒ E d H ‰d S )Nr   r   )rq   r7   r   r
   ro   Ö  s    z<TestPEP380Operation.test_custom_iterator_return.<locals>.genr   )r4   r   )r   ro   r   )rq   r7   r
   Útest_custom_iterator_returnÏ  s
    
z/TestPEP380Operation.test_custom_iterator_returnc          	      sf   dd„ ‰ ‡ fdd„}dd„ }t ƒ : |ƒ }|ƒ }| d ¡ | |¡ | |¡ ~~tƒ  W d Q R X d S )Nc               s   s
   d V  d S )Nr   r   r   r   r
   Ú	innermostð  s    zDTestPEP380Operation.test_close_with_cleared_frame.<locals>.innermostc              3   s   d V } ˆ ƒ E d H  d S )Nr   )Ú	outer_gen)rs   r   r
   rY   ò  s    z@TestPEP380Operation.test_close_with_cleared_frame.<locals>.innerc              s   s   d V } | E d H  d S )Nr   )Ú	inner_genr   r   r
   rZ   õ  s    z@TestPEP380Operation.test_close_with_cleared_frame.<locals>.outer)r   r   r   )r   rY   rZ   ru   rt   r   )rs   r
   Útest_close_with_cleared_frameÝ  s    


z1TestPEP380Operation.test_close_with_cleared_framec                sJ   G ‡fdd„dƒ‰ ‡ fdd„}|ƒ }t |ƒ d ‰| d¡ |  ˆd¡ d S )Nc                   s(   e Zd Zdd„ Zdd„ Z‡ fdd„ZdS )zHTestPEP380Operation.test_send_tuple_with_custom_generator.<locals>.MyGenc             S   s   | S )Nr   )r   r   r   r
   rG     s    zQTestPEP380Operation.test_send_tuple_with_custom_generator.<locals>.MyGen.__iter__c             S   s   dS )Nr   r   )r   r   r   r
   rH   	  s    zQTestPEP380Operation.test_send_tuple_with_custom_generator.<locals>.MyGen.__next__c                s   |‰ d S )Nr   )r   Zwhat)r8   r   r
   r     s    zMTestPEP380Operation.test_send_tuple_with_custom_generator.<locals>.MyGen.sendN)r.   rK   rL   rG   rH   r   r   )r8   r   r
   ÚMyGen  s   rw   c              3   s   ˆ ƒ E d H } d S )Nr   )r8   )rw   r   r
   rZ     s    zHTestPEP380Operation.test_send_tuple_with_custom_generator.<locals>.outer)r   r"   r6   é   )r   r   r   )r   rZ   r   r   )rw   r8   r
   Ú%test_send_tuple_with_custom_generator  s    	
z9TestPEP380Operation.test_send_tuple_with_custom_generatorN)%r.   rK   rL   Ú__doc__r   r   r   r   r   r!   r&   r*   r,   r3   r5   r9   r:   r<   r@   rC   rF   rO   rP   rQ   rT   rW   rX   r\   r]   r_   r`   ra   ri   rp   rr   rv   ry   r   r   r   r
   r      sD   %,)")(3 #+*/H'r   Ú__main__)rz   Zunittestrl   Ztest.supportr   r   r   ZTestCaser   r.   Úmainr   r   r   r
   Ú<module>   s           