B
    u9aN                 @   sF   d dl Z d dlZd dlmZmZ G dd de jZedkrBe   dS )    N)check_syntax_errorcpython_onlyc               @   sH  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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ed-d. Zd/d0 Zed1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%dEdF Z&dGdH Z'dIdJ Z(edKdL Z)dMS )N
ScopeTestsc             C   s<   dd }|d}|d}|  |dd |  |dd d S )Nc                s    fdd}|S )Nc                s    |  S )N )y)xr    /usr/lib/python3.7/test_scope.pyadder   s    z?ScopeTests.testSimpleNesting.<locals>.make_adder.<locals>.adderr   )r   r	   r   )r   r   
make_adder   s    z0ScopeTests.testSimpleNesting.<locals>.make_adder   
         )assertEqual)selfr
   incplus10r   r   r   testSimpleNesting	   s
    zScopeTests.testSimpleNestingc             C   s<   dd }|d}|d}|  |dd |  |dd d S )Nc                s    fdd}| S )Nc                 s    fdd} | S )Nc                s    |  S )Nr   )r   )r   r   r   r	      s    zNScopeTests.testExtraNesting.<locals>.make_adder2.<locals>.extra.<locals>.adderr   )r	   )r   r   r   extra   s    z?ScopeTests.testExtraNesting.<locals>.make_adder2.<locals>.extrar   )r   r   r   )r   r   make_adder2   s    z0ScopeTests.testExtraNesting.<locals>.make_adder2r   r   r   r   r   )r   )r   r   r   r   r   r   r   testExtraNesting   s
    zScopeTests.testExtraNestingc             C   s<   dd }|d}|d}|  |dd |  |dd d S )	Nc                s    fdd} d  |S )Nc                s    |  S )Nr   )r   )r   r   r   r	   (   s    zEScopeTests.testSimpleAndRebinding.<locals>.make_adder3.<locals>.adderr   r   )r   r	   r   )r   r   make_adder3'   s    z6ScopeTests.testSimpleAndRebinding.<locals>.make_adder3r   	   r   r   r   r   )r   )r   r   r   r   r   r   r   testSimpleAndRebinding%   s
    z!ScopeTests.testSimpleAndRebindingc                s>    fdd}d | }|  |dd d |  |dd d S )Nc                 s    fdd} |  S )Nc                 s    fdd} |  S )Nc                 s    fdd} | S )Nc                s    |  S )Nr   )r   )global_xr   r   r	   8   s    zbScopeTests.testNestingGlobalNoFree.<locals>.make_adder4.<locals>.nest.<locals>.nest.<locals>.adderr   )r	   )r   r   r   nest7   s    zSScopeTests.testNestingGlobalNoFree.<locals>.make_adder4.<locals>.nest.<locals>.nestr   )r   )r   r   r   r   6   s    zEScopeTests.testNestingGlobalNoFree.<locals>.make_adder4.<locals>.nestr   )r   )r   r   r   make_adder45   s    z7ScopeTests.testNestingGlobalNoFree.<locals>.make_adder4r   r   r   r   r   )r   )r   r   r	   r   )r   r   testNestingGlobalNoFree3   s    	z"ScopeTests.testNestingGlobalNoFreec             C   s<   dd }|d}|d}|  |dd |  |dd d S )Nc                s   G  fddd}| S )Nc                   s   e Zd Z fddZdS )zFScopeTests.testNestingThroughClass.<locals>.make_adder5.<locals>.Adderc                s    | S )Nr   )r   r   )r   r   r   __call__I   s    zOScopeTests.testNestingThroughClass.<locals>.make_adder5.<locals>.Adder.__call__N)__name__
__module____qualname__r   r   )r   r   r   AdderH   s   r#   r   )r   r#   r   )r   r   make_adder5G   s    z7ScopeTests.testNestingThroughClass.<locals>.make_adder5r   r   r   r   r   )r   )r   r$   r   r   r   r   r   testNestingThroughClassE   s
    z"ScopeTests.testNestingThroughClassc             C   s<   dd }|d}|d}|  |dd |  |dd d S )Nc             S   s   dd }| a |S )Nc             S   s   t |  S )N)global_nest_x)r   r   r   r   r	   W   s    zMScopeTests.testNestingPlusFreeRefToGlobal.<locals>.make_adder6.<locals>.adder)r&   )r   r	   r   r   r   make_adder6U   s    z>ScopeTests.testNestingPlusFreeRefToGlobal.<locals>.make_adder6r   r      r   r   )r   )r   r'   r   r   r   r   r   testNestingPlusFreeRefToGlobalS   s
    z)ScopeTests.testNestingPlusFreeRefToGlobalc             C   s$   dd }|d}|  |dd d S )Nc             S   s   dd }|dS )Nc                s   d  fdd}|S )N*   c                s    |  S )Nr   )z)r   r   r   hg   s    zEScopeTests.testNearestEnclosingScope.<locals>.f.<locals>.g.<locals>.hr   )r   r,   r   )r   r   ge   s    z:ScopeTests.testNearestEnclosingScope.<locals>.f.<locals>.gr   r   )r   r-   r   r   r   fd   s    z/ScopeTests.testNearestEnclosingScope.<locals>.fr      /   )r   )r   r.   Z	test_funcr   r   r   testNearestEnclosingScopeb   s    z$ScopeTests.testNearestEnclosingScopec                s>   dd   fdd}|ddd}|ddd	}|  | d
 d S )Nc             S   s   | S )Nr   )r   r   r   r   identityq   s    z9ScopeTests.testMixedFreevarsAndCellvars.<locals>.identityc                s    fdd}|S )Nc                s&   |  }  fdd}| |S )Nc                  s      S )Nr   r   )br2   r   r+   r   r   r,   w   s    zHScopeTests.testMixedFreevarsAndCellvars.<locals>.f.<locals>.g.<locals>.hr   )ar3   cr,   )r2   r   r+   )r3   r   r   r-   u   s    z=ScopeTests.testMixedFreevarsAndCellvars.<locals>.f.<locals>.gr   )r   r   r+   r-   )r2   )r   r+   r   r.   t   s    z2ScopeTests.testMixedFreevarsAndCellvars.<locals>.fr   r            '   )r   )r   r.   r-   r,   r   )r2   r   testMixedFreevarsAndCellvarso   s
    z'ScopeTests.testMixedFreevarsAndCellvarsc                s   dd }| }|  | d |  | d |  | d d G  fddd}| }|  | d |  | d |  | d d S )Nc                 s   d G  fddd} |  S )Nvarc                   s0   e Zd Zdd Z fddZdd Zdd Zd	S )
z:ScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Testc             S   s   dS )Nmethodr   )r   r   r   r   method_and_var   s    zIScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Test.method_and_varc                s    S )Nr   )r   )r=   r   r   test   s    z?ScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Test.testc             S   s   t dS )Nglobal)str)r   r   r   r   actual_global   s    zHScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Test.actual_globalc             S   s   t | S )N)r@   )r   r   r   r   r@      s    z>ScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Test.strN)r    r!   r"   r=   r>   rA   r@   r   )r=   r   r   Test   s   rB   r   )rB   r   )r=   r   r>      s    	z,ScopeTests.testFreeVarInMethod.<locals>.testr;   r<   r?   c                   s0   e Zd Zdd Z fddZdd Zdd Zd	S )
z,ScopeTests.testFreeVarInMethod.<locals>.Testc             S   s   dS )Nr<   r   )r   r   r   r   r=      s    z;ScopeTests.testFreeVarInMethod.<locals>.Test.method_and_varc                s    S )Nr   )r   )r=   r   r   r>      s    z1ScopeTests.testFreeVarInMethod.<locals>.Test.testc             S   s   t dS )Nr?   )r@   )r   r   r   r   rA      s    z:ScopeTests.testFreeVarInMethod.<locals>.Test.actual_globalc             S   s   t | S )N)r@   )r   r   r   r   r@      s    z0ScopeTests.testFreeVarInMethod.<locals>.Test.strN)r    r!   r"   r=   r>   rA   r@   r   )r=   r   r   rB      s   rB   )r   r>   r=   rA   )r   r>   trB   r   )r=   r   testFreeVarInMethod   s    zScopeTests.testFreeVarInMethodc             C   s2   dddd}|  |ddd |  | d d S )N   )r4   c                s    fdd}| d S )Nc                  s    d S )Nr/   r   r   )r4   r   r   bar   s    z8ScopeTests.testCellIsKwonlyArg.<locals>.foo.<locals>.barr6   r   )r4   rF   r   )r4   r   foo   s    z+ScopeTests.testCellIsKwonlyArg.<locals>.foor*   2      )r   )r   rG   r   r   r   testCellIsKwonlyArg   s    zScopeTests.testCellIsKwonlyArgc             C   s   dd }|  |dd d S )Nc                s(    fdd | dkr | S t dd S )Nc                s    | dkrdS |  | d  S d S )Nr   r   r   )n)factr   r   rL      s    z1ScopeTests.testRecursion.<locals>.f.<locals>.factr   zx must be >= 0)
ValueError)r   r   )rL   r   r.      s    z#ScopeTests.testRecursion.<locals>.fr8   i  )r   )r   r.   r   r   r   testRecursion   s    zScopeTests.testRecursionc             C   s,   t | d t | d t | d t | d d S )Nzif 1:
            def unoptimized_clash1(strip):
                def f(s):
                    from sys import *
                    return getrefcount(s) # ambiguity: free or local
                return f
            zif 1:
            def unoptimized_clash2():
                from sys import *
                def f(s):
                    return getrefcount(s) # ambiguity: global or local
                return f
            zif 1:
            def unoptimized_clash2():
                from sys import *
                def g():
                    def f(s):
                        return getrefcount(s) # ambiguity: global or local
                    return f
            zif 1:
            def f():
                def g():
                    from sys import *
                    return getrefcount # global or local?
            )r   )r   r   r   r   testUnoptimizedNamespaces   s    z$ScopeTests.testUnoptimizedNamespacesc       	         s   dd }|d}|d}|  |dd |  |dd dd }|d}|d}|  |dd |  |dd  fd	d}d |d }|  |dd
 dd }|ddd
}|ddd}|  | d d S )Nc                s    fddS )Nc                s    |  S )Nr   )r   )r   r   r   <lambda>       z:ScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>r   )r   r   )r   r   rP      rQ   z(ScopeTests.testLambdas.<locals>.<lambda>r   r   r   r/      c                s    fdd S )Nc                  s    fddS )Nc                s    |  S )Nr   )r   )r   r   r   rP      rQ   zLScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>r   r   )r   r   r   rP      rQ   z:ScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>r   )r   r   )r   r   rP      rQ   c                s    fddS )Nc                s    |  S )Nr   )r   )r   r   r   rP      rQ   z:ScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>r   )r   )r   r   r   rP      rQ   r6   c                s    fddS )Nc                s    fddS )Nc                  s      S )Nr   r   )r3   r   r+   r   r   rP      rQ   zLScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>r   )r4   r3   r5   )r   r+   )r3   r   rP      rQ   z:ScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>r   )r   r   r+   r   )r   r+   r   rP      rQ   r7   r8      )r   )	r   f1r   r   f2Zf3Zf8r-   r,   r   )r   r   testLambdas   s$    zScopeTests.testLambdasc             C   s,   dd }dd }|  t| |  t| d S )Nc                 s   t    fdd} d d S )Nc                  s    S )Nr   r   )r   r   r   inner  s    z@ScopeTests.testUnboundLocal.<locals>.errorInOuter.<locals>.innerr   )print)rW   r   )r   r   errorInOuter   s    z1ScopeTests.testUnboundLocal.<locals>.errorInOuterc                 s    fdd} |   d d S )Nc                  s    S )Nr   r   )r   r   r   rW     s    z@ScopeTests.testUnboundLocal.<locals>.errorInInner.<locals>.innerr   r   )rW   r   )r   r   errorInInner  s    z1ScopeTests.testUnboundLocal.<locals>.errorInInner)assertRaisesUnboundLocalError	NameError)r   rY   rZ   r   r   r   testUnboundLocal   s    zScopeTests.testUnboundLocalc             C   s,   dd }dd }|  t| |  t| d S )Nc                 s   d  t    fdd} d S )Nr   c                  s    S )Nr   r   )r   r   r   rW     s    zIScopeTests.testUnboundLocal_AfterDel.<locals>.errorInOuter.<locals>.inner)rX   )rW   r   )r   r   rY     s    z:ScopeTests.testUnboundLocal_AfterDel.<locals>.errorInOuterc                 s    fdd} d  |   d S )Nc                  s    S )Nr   r   )r   r   r   rW     s    zIScopeTests.testUnboundLocal_AfterDel.<locals>.errorInInner.<locals>.innerr   r   )rW   r   )r   r   rZ     s    z:ScopeTests.testUnboundLocal_AfterDel.<locals>.errorInInner)r[   r\   r]   )r   rY   rZ   r   r   r   testUnboundLocal_AfterDel  s    z$ScopeTests.testUnboundLocal_AfterDelc             C   s   t dd| ji d S )Na  if 1:
            global_x = 1
            def f():
                global_x += 1
            try:
                f()
            except UnboundLocalError:
                pass
            else:
                fail('scope of global_x not correctly determined')
            fail)execr`   )r   r   r   r   testUnboundLocal_AugAssign#  s    
z%ScopeTests.testUnboundLocal_AugAssignc             C   sB   dd }|  |ddd d dd }|  |d	d
 d d	 d S )Nc                 s    fdd}|S )Nc                  s    S )Nr   r   )lstr   r   returner4  s    zIScopeTests.testComplexDefinitions.<locals>.makeReturner.<locals>.returnerr   )rc   rd   r   )rc   r   makeReturner3  s    z7ScopeTests.testComplexDefinitions.<locals>.makeReturnerr   r   r6   )r   r   r6   c                 s    fdd}|S )Nc                  s    S )Nr   r   )kwargsr   r   rd   ;  s    zJScopeTests.testComplexDefinitions.<locals>.makeReturner2.<locals>.returnerr   )rf   rd   r   )rf   r   makeReturner2:  s    z8ScopeTests.testComplexDefinitions.<locals>.makeReturner2r(   )r4   r4   )r   )r   re   rg   r   r   r   testComplexDefinitions1  s    z!ScopeTests.testComplexDefinitionsc             C   s   t d d S )Na;  if 1:
            # I
            x = 7
            def f():
                x = 1
                def g():
                    global x
                    def i():
                        def h():
                            return x
                        return h()
                    return i()
                return g()
            self.assertEqual(f(), 7)
            self.assertEqual(x, 7)

            # II
            x = 7
            def f():
                x = 1
                def g():
                    x = 2
                    def i():
                        def h():
                            return x
                        return h()
                    return i()
                return g()
            self.assertEqual(f(), 2)
            self.assertEqual(x, 7)

            # III
            x = 7
            def f():
                x = 1
                def g():
                    global x
                    x = 2
                    def i():
                        def h():
                            return x
                        return h()
                    return i()
                return g()
            self.assertEqual(f(), 2)
            self.assertEqual(x, 2)

            # IV
            x = 7
            def f():
                x = 3
                def g():
                    global x
                    x = 2
                    def i():
                        def h():
                            return x
                        return h()
                    return i()
                return g()
            self.assertEqual(f(), 2)
            self.assertEqual(x, 2)

            # XXX what about global statements in class blocks?
            # do they affect methods?

            x = 12
            class Global:
                global x
                x = 13
                def set(self, val):
                    x = val
                def get(self):
                    return x

            g = Global()
            self.assertEqual(g.get(), 13)
            g.set(15)
            self.assertEqual(g.get(), 13)
            )ra   )r   r   r   r   testScopeOfGlobalStmtA  s    Oz ScopeTests.testScopeOfGlobalStmtc                sH   G  fddd  fdd}xt dD ]
}|  q(W |  jd d S )Nc                   s(   e Zd ZdZ fddZ fddZdS )z!ScopeTests.testLeaks.<locals>.Foor   c                s     j d7  _ d S )Nr   )count)r   )Foor   r   __init__  s    z*ScopeTests.testLeaks.<locals>.Foo.__init__c                s     j d8  _ d S )Nr   )rj   )r   )rk   r   r   __del__  s    z)ScopeTests.testLeaks.<locals>.Foo.__del__N)r    r!   r"   rj   rl   rm   r   )rk   r   r   rk     s   rk   c                 s      fdd} |   d S )Nc                  s    S )Nr   r   )r   r   r   rU     s    z,ScopeTests.testLeaks.<locals>.f1.<locals>.f2r   )rU   )rk   )r   r   rT     s    z ScopeTests.testLeaks.<locals>.f1d   r   )ranger   rj   )r   rT   ir   )rk   r   	testLeaks  s
    	
zScopeTests.testLeaksc             C   s   t d d S )Na  if 1:
            def test(x):
                class Foo:
                    global x
                    def __call__(self, y):
                        return x + y
                return Foo()

            x = 0
            self.assertEqual(test(6)(2), 8)
            x = -1
            self.assertEqual(test(3)(2), 5)

            looked_up_by_load_name = False
            class X:
                # Implicit globals inside classes are be looked up by LOAD_NAME, not
                # LOAD_GLOBAL.
                locals()['looked_up_by_load_name'] = True
                passed = looked_up_by_load_name

            self.assertTrue(X.passed)
            )ra   )r   r   r   r   testClassAndGlobal  s    zScopeTests.testClassAndGlobalc             C   s>   dd }|dd}|  d| |d= | |dddd d S )	Nc                s    fdd}|S )Nc                s"    fdd}  } d7  t  S )Nc                s    |  S )Nr   )r+   )r   r   r   r,     s    z>ScopeTests.testLocalsFunction.<locals>.f.<locals>.g.<locals>.hr6   )locals)r   r,   w)r   )r   r   r-     s    z3ScopeTests.testLocalsFunction.<locals>.f.<locals>.gr   )r   r-   r   )r   r   r.     s    z(ScopeTests.testLocalsFunction.<locals>.fr   r7   r,      r8   )r   r   rt   )assertInr   )r   r.   dr   r   r   testLocalsFunction  s
    	zScopeTests.testLocalsFunctionc             C   sH   dd }|  |djd dd }|dj}| d| | d| d S )Nc                s   G  fddd}|S )Nc                   s"   e Zd ZdZ fddZe  dS )z0ScopeTests.testLocalsClass.<locals>.f.<locals>.C   c                s    S )Nr   )r   )r   r   r   m  s    z2ScopeTests.testLocalsClass.<locals>.f.<locals>.C.mN)r    r!   r"   r   rz   rs   r   )r   r   r   C  s   r{   r   )r   r{   r   )r   r   r.     s    z%ScopeTests.testLocalsClass.<locals>.fr   ry   c                s   G  fddd}|S )Nc                   s&   e Zd Z Z fddZee ZdS )z0ScopeTests.testLocalsClass.<locals>.f.<locals>.Cc                s    S )Nr   )r   )r   r   r   rz     s    z2ScopeTests.testLocalsClass.<locals>.f.<locals>.C.mN)r    r!   r"   r   rz   listrs   r+   r   )r   r   r   r{     s   r{   r   )r   r{   r   )r   r   r.     s    r   r   )r   r   r+   assertNotInrv   )r   r.   Zvarnamesr   r   r   testLocalsClass  s    
zScopeTests.testLocalsClassc                sN   dd l }| |j|  |dd  d G  fddd}|  d d S )Nr   c             S   s   d S )Nr   )r4   r3   r5   r   r   r   rP     rQ   z6ScopeTests.testLocalsClass_WithTrace.<locals>.<lambda>ry   c                   s   e Zd Z fddZdS )z/ScopeTests.testLocalsClass_WithTrace.<locals>.Cc                s    S )Nr   )r   )r   r   r   r.     s    z1ScopeTests.testLocalsClass_WithTrace.<locals>.C.fN)r    r!   r"   r.   r   )r   r   r   r{      s   r{   )sys
addCleanupsettracegettracer   )r   r   r{   r   )r   r   testLocalsClass_WithTrace  s    z$ScopeTests.testLocalsClass_WithTracec             C   s(   dd }|d }|  |j|  d S )Nc                s   G  fddd}|S )Nc                   s   e Zd Z fddZ ZdS )z1ScopeTests.testBoundAndFree.<locals>.f.<locals>.Cc                s    S )Nr   )r   )r   r   r   rz     s    z3ScopeTests.testBoundAndFree.<locals>.f.<locals>.C.mN)r    r!   r"   rz   r4   r   )r   r   r   r{   
  s   r{   r   )r   r{   r   )r   r   r.   	  s    z&ScopeTests.testBoundAndFree.<locals>.fr6   )r   r4   rz   )r   r.   instr   r   r   testBoundAndFree  s    
zScopeTests.testBoundAndFreec                sn   dd l } fdd dd }G dd d}| |j|  |  |d|d	 |d  | t|j d S )
Nr   c                s    S )Nr   )r4   r3   r5   )tracerr   r   r     s    z7ScopeTests.testInteractionWithTraceFunc.<locals>.tracerc                s6   |\} |dkr2 dkr&d|j | f   fddS d S )Nr    z_%s__%sc                s
   t |  S )N)getattr)obj)desr   r   rP     rQ   zNScopeTests.testInteractionWithTraceFunc.<locals>.adaptgetter.<locals>.<lambda>)r    )nameklassgetterkindr   )r   r   adaptgetter  s
    z<ScopeTests.testInteractionWithTraceFunc.<locals>.adaptgetterc               @   s   e Zd ZdS )z:ScopeTests.testInteractionWithTraceFunc.<locals>.TestClassN)r    r!   r"   r   r   r   r   	TestClass!  s   r   rG   )r   r   )r   r   r   r   r[   	TypeError)r   r   r   r   r   )r   r   testInteractionWithTraceFunc  s    

z'ScopeTests.testInteractionWithTraceFuncc             C   sT   dd }|d}|  tt|j yt|ji  W n tk
rD   Y nX | d d S )Nc                s    fddS )Nc                  s    d S )Nr   r   r   )r   r   r   rP   .  rQ   z<ScopeTests.testEvalExecFreeVars.<locals>.f.<locals>.<lambda>r   )r   r   )r   r   r.   -  s    z*ScopeTests.testEvalExecFreeVars.<locals>.fr6   z9exec should have failed, because code contained free vars)r[   r   eval__code__ra   r`   )r   r.   r-   r   r   r   testEvalExecFreeVars+  s    zScopeTests.testEvalExecFreeVarsc             C   s^   yt t W n tk
r    Y n
X t d dd }|  yt t W n tk
rX   Y nX d S )Nzbad should not be definedc               S   s   dd dD  d S )Nc             S   s   g | ]}|  D ]}|qqS r   )split).0sbadr   r   r   
<listcomp>D  s    z?ScopeTests.testListCompLocalVars.<locals>.x.<locals>.<listcomp>za br   r   r   r   r   r   C  s    z+ScopeTests.testListCompLocalVars.<locals>.x)rX   r   r]   )r   r   r   r   r   testListCompLocalVars:  s    z ScopeTests.testListCompLocalVarsc             C   s   dd }|d  d S )Nc                s    fdd}|S )Nc                  s     t d d S )Nzx + 1)r   r   )r   r   r   r-   O  s    z1ScopeTests.testEvalFreeVars.<locals>.f.<locals>.gr   )r   r-   r   )r   r   r.   N  s    z&ScopeTests.testEvalFreeVars.<locals>.fr7   r   )r   r.   r   r   r   testEvalFreeVarsL  s    zScopeTests.testEvalFreeVarsc             C   s   G dd d}d S )Nc               @   s   e Zd Zdd ZdS )z+ScopeTests.testFreeingCell.<locals>.Specialc             S   s
   t   d S )N)Znestedcell_get)r   r   r   r   rm   Z  s    z3ScopeTests.testFreeingCell.<locals>.Special.__del__N)r    r!   r"   rm   r   r   r   r   SpecialY  s   r   r   )r   r   r   r   r   testFreeingCellV  s    zScopeTests.testFreeingCellc             C   sP   dd }|d\}}|  | d |  | d |  | d |  | d d S )Nc                s     fdd} fdd}||fS )Nc                  s    d7   S )Nr   r   r   )r   r   r   r   `  s    z7ScopeTests.testNonLocalFunction.<locals>.f.<locals>.incc                  s    d8   S )Nr   r   r   )r   r   r   decd  s    z7ScopeTests.testNonLocalFunction.<locals>.f.<locals>.decr   )r   r   r   r   )r   r   r.   _  s    z*ScopeTests.testNonLocalFunction.<locals>.fr   r   r   )r   )r   r.   r   r   r   r   r   testNonLocalFunction]  s    zScopeTests.testNonLocalFunctionc             C   sT   dd }|d}|  | d |  | d |  | d |  | d d S )Nc                s   G  fddd}| S )Nc                   s$   e Zd Z fddZ fddZdS )z3ScopeTests.testNonLocalMethod.<locals>.f.<locals>.cc                s    d7   S )Nr   r   )r   )r   r   r   r   s  s    z7ScopeTests.testNonLocalMethod.<locals>.f.<locals>.c.incc                s    d8   S )Nr   r   )r   )r   r   r   r   w  s    z7ScopeTests.testNonLocalMethod.<locals>.f.<locals>.c.decN)r    r!   r"   r   r   r   )r   r   r   r5   r  s   r5   r   )r   r5   r   )r   r   r.   q  s    	z(ScopeTests.testNonLocalMethod.<locals>.fr   r   r   )r   r   r   )r   r.   r5   r   r   r   testNonLocalMethodp  s    zScopeTests.testNonLocalMethodc             C   s8   i }i }t d|| | d|d  | d|d  d S )NaC  if 1:
            def f():
                y = 1
                def g():
                    global y
                    return y
                def h():
                    return y + 1
                return g, h
            y = 9
            g, h = f()
            result9 = g()
            result2 = h()
            r   Zresult2r   Zresult9)ra   r   )r   Zlocal_nsZ	global_nsr   r   r   #testGlobalInParallelNestedFunctions  s    
z.ScopeTests.testGlobalInParallelNestedFunctionsc             C   s4   dd }|d}|  | d | d|jj d S )Nc                s   G  fddd}| S )Nc                   s    e Zd Z d7   fddZdS )z2ScopeTests.testNonLocalClass.<locals>.f.<locals>.cr   c                s    S )Nr   )r   )r   r   r   get  s    z6ScopeTests.testNonLocalClass.<locals>.f.<locals>.c.getN)r    r!   r"   r   r   )r   r   r   r5     s   r5   r   )r   r5   r   )r   r   r.     s    z'ScopeTests.testNonLocalClass.<locals>.fr   r   r   )r   r   r}   	__class____dict__)r   r.   r5   r   r   r   testNonLocalClass  s    zScopeTests.testNonLocalClassc             C   s2   dd }|d}|  t|ddddddg d S )	Nc                s    fdd}|S )Nc             3   s$   xt | D ]} d7   V  q
W d S )Nr   )ro   )r   rp   )r   r   r   r-     s    z6ScopeTests.testNonLocalGenerator.<locals>.f.<locals>.gr   )r   r-   r   )r   r   r.     s    z+ScopeTests.testNonLocalGenerator.<locals>.fr   r/   r   r   r6   r7   )r   r|   )r   r.   r-   r   r   r   testNonLocalGenerator  s    z ScopeTests.testNonLocalGeneratorc             C   s(   dd }|d}| }|  | d d S )Nc                s    fdd}|S )Nc                 s    d8   fdd} | S )Nr   c                  s    d7   S )Nr7   r   r   )r   r   r   r,     s    z>ScopeTests.testNestedNonLocal.<locals>.f.<locals>.g.<locals>.hr   )r,   )r   r   r   r-     s    z3ScopeTests.testNestedNonLocal.<locals>.f.<locals>.gr   )r   r-   r   )r   r   r.     s    z(ScopeTests.testNestedNonLocal.<locals>.fr   r6   )r   )r   r.   r-   r,   r   r   r   testNestedNonLocal  s    zScopeTests.testNestedNonLocalc             C   s   dd }dd }d S )Nc             S   s   d S )Nr   )r4   r   r   r   top  s    z/ScopeTests.testTopIsNotSignificant.<locals>.topc               S   s   d S )Nr   r   r   r   r   r3     s    z-ScopeTests.testTopIsNotSignificant.<locals>.br   )r   r   r3   r   r   r   testTopIsNotSignificant  s    z"ScopeTests.testTopIsNotSignificantc                sR   d G  fddd}|  |jd G dd d}| t|d |   d d S )Nr*   c                   s   e Zd Zde d<  ZdS )z8ScopeTests.testClassNamespaceOverridesClosure.<locals>.X+   r   N)r    r!   r"   rs   r   r   )r   r   r   X  s   
r   r   c               @   s   e Zd Zde d< [dS )z8ScopeTests.testClassNamespaceOverridesClosure.<locals>.Xr   r   N)r    r!   r"   rs   r   r   r   r   r   r     s   
r   )r   r   ZassertFalsehasattr)r   r   r   )r   r   "testClassNamespaceOverridesClosure  s    z-ScopeTests.testClassNamespaceOverridesClosurec             C   s8   G dd d}| }|   t|}~| |  d S )Nc               @   s   e Zd Zdd ZdS )z'ScopeTests.testCellLeak.<locals>.Testerc          
      s>   ydd  W n( t k
r4 } z
| _W d d }~X Y nX d  d S )Nr   r   )	Exceptionexc)r   r   r   )r   r   dig  s
    z+ScopeTests.testCellLeak.<locals>.Tester.digN)r    r!   r"   r   r   r   r   r   Tester  s   r   )r   weakrefrefZassertIsNone)r   r   Ztesterr   r   r   r   testCellLeak  s    	
zScopeTests.testCellLeakN)*r    r!   r"   r   r   r   r   r%   r)   r1   r:   rD   rJ   rN   rO   rV   r^   r_   rb   rh   ri   rq   rr   rx   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sL   %"T!
r   __main__)	Zunittestr   Ztest.supportr   r   ZTestCaser   r    mainr   r   r   r   <module>   s        v