a
    zeSO                     @   sR   d dl Z d dlZd dlmZmZ d dlmZ G dd de jZedkrNe 	  dS )    N)check_syntax_errorcpython_only)
gc_collectc                   @   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.9/test/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 r   r   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 r   r   r   r
   r   r   r   )   s    zEScopeTests.testSimpleAndRebinding.<locals>.make_adder3.<locals>.adderr   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 r   r   r   Zglobal_xr   r   r   9   s    zbScopeTests.testNestingGlobalNoFree.<locals>.make_adder4.<locals>.nest.<locals>.nest.<locals>.adderr   r   r"   r   r   nest8   s    zSScopeTests.testNestingGlobalNoFree.<locals>.make_adder4.<locals>.nest.<locals>.nestr   r#   r"   r   r   r#   7   s    zEScopeTests.testNestingGlobalNoFree.<locals>.make_adder4.<locals>.nestr   r$   r"   r   r   make_adder46   s    z7ScopeTests.testNestingGlobalNoFree.<locals>.make_adder4r   r   r   r   r   r   )r   r%   r   r   r"   r   testNestingGlobalNoFree4   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 r   r   )r   r	   r
   r   r   __call__J   s    zOScopeTests.testNestingThroughClass.<locals>.make_adder5.<locals>.Adder.__call__N)__name__
__module____qualname__r'   r   r
   r   r   AdderI   s   r+   r   )r   r+   r   r
   r   make_adder5H   s    z7ScopeTests.testNestingThroughClass.<locals>.make_adder5r   r   r   r   r   r   )r   r,   r   r   r   r   r   testNestingThroughClassF   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 r   Zglobal_nest_xr   r   r   r   r   X   s    zMScopeTests.testNestingPlusFreeRefToGlobal.<locals>.make_adder6.<locals>.adderr.   r   r   r   r   make_adder6V   s    z>ScopeTests.testNestingPlusFreeRefToGlobal.<locals>.make_adder6r   r      r   r   r   )r   r/   r   r   r   r   r   testNestingPlusFreeRefToGlobalT   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 r   r   zr
   r   r   hh   s    zEScopeTests.testNearestEnclosingScope.<locals>.f.<locals>.g.<locals>.hr   )r	   r5   r   r
   r   gf   s    z:ScopeTests.testNearestEnclosingScope.<locals>.f.<locals>.gr   r   r   r6   r   r   r   fe   s    z/ScopeTests.testNearestEnclosingScope.<locals>.fr      /   r   )r   r8   Z	test_funcr   r   r   testNearestEnclosingScopec   s    z$ScopeTests.testNearestEnclosingScopec                    s>   dd   fdd}|ddd}|ddd	}|  | d
 d S )Nc                 S   s   | S r   r   r
   r   r   r   identityr   s    z9ScopeTests.testMixedFreevarsAndCellvars.<locals>.identityc                    s    fdd}|S )Nc                    s&   |  }  fdd}| |S )Nc                      s      S r   r   r   )br<   r	   r4   r   r   r5   x   s    zHScopeTests.testMixedFreevarsAndCellvars.<locals>.f.<locals>.g.<locals>.hr   )ar=   cr5   )r<   r   r4   )r=   r	   r   r6   v   s    z=ScopeTests.testMixedFreevarsAndCellvars.<locals>.f.<locals>.gr   )r   r	   r4   r6   r<   )r   r4   r   r8   u   s    z2ScopeTests.testMixedFreevarsAndCellvars.<locals>.fr   r            '   r   r   r8   r6   r5   r   r@   r   testMixedFreevarsAndCellvarsp   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 r   r   rJ   rK   r   r   test   s    z?ScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Test.testc                 S   s   t dS NglobalstrrJ   r   r   r   actual_global   s    zHScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Test.actual_globalc                 S   s   t | S r   rP   rJ   r   r   r   rQ      s    z>ScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Test.strNr(   r)   r*   rK   rM   rR   rQ   r   rL   r   r   Test   s   rT   r   )rT   r   rL   r   rM      s    	z,ScopeTests.testFreeVarInMethod.<locals>.testrG   rI   rO   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 rH   r   rJ   r   r   r   rK      s    z;ScopeTests.testFreeVarInMethod.<locals>.Test.method_and_varc                    s    S r   r   rJ   rL   r   r   rM      s    z1ScopeTests.testFreeVarInMethod.<locals>.Test.testc                 S   s   t dS rN   rP   rJ   r   r   r   rR      s    z:ScopeTests.testFreeVarInMethod.<locals>.Test.actual_globalc                 S   s   t | S r   rP   rJ   r   r   r   rQ      s    z0ScopeTests.testFreeVarInMethod.<locals>.Test.strNrS   r   rL   r   r   rT      s   rT   )r   rM   rK   rR   )r   rM   trT   r   rL   r   testFreeVarInMethod   s    zScopeTests.testFreeVarInMethodc                 C   s2   dddd}|  |ddd |  | d d S )N   r>   c                    s    fdd}| d S )Nc                      s    d S )Nr9   r   r   rX   r   r   bar   s    z8ScopeTests.testCellIsKwonlyArg.<locals>.foo.<locals>.barrA   r   )r>   rY   r   rX   r   foo   s    z+ScopeTests.testCellIsKwonlyArg.<locals>.foor2   2      r   )r   rZ   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   r`      s    z1ScopeTests.testRecursion.<locals>.f.<locals>.factr   zx must be >= 0)
ValueErrorr
   r   r_   r   r8      s    z#ScopeTests.testRecursion.<locals>.frC   i  r   r   r8   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   rJ   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 r   r   r   r
   r   r   <lambda>       :ScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>r   r
   r   r
   r   re      rf   z(ScopeTests.testLambdas.<locals>.<lambda>r   r   r   r9      c                    s    fdd S )Nc                      s    fddS )Nc                    s    |  S r   r   r   r
   r   r   re      rf   LScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>r   r   r
   r   r   re      rf   rg   r   r
   r   r
   r   re      rf   c                    s    fddS )Nc                    s    |  S r   r   r   r"   r   r   re      rf   rg   r   r
   r"   r   r   re      rf   rA   c                    s    fddS )Nc                    s    fddS )Nc                      s      S r   r   r   )r=   r	   r4   r   r   re      rf   ri   r   r>   r=   r?   r	   r4   )r=   r   re      rf   rg   r   )r   r	   r4   r   rk   r   re      rf   rB   rC      r   )	r   f1r   r   f2Zf3Zf8r6   r5   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 r   r   r   r   r   r   inner  s    z@ScopeTests.testUnboundLocal.<locals>.errorInOuter.<locals>.innerr   printrp   r   r   r   errorInOuter   s    z1ScopeTests.testUnboundLocal.<locals>.errorInOuterc                     s    fdd} |   d d S )Nc                      s    S r   r   r   r   r   r   rp     s    z@ScopeTests.testUnboundLocal.<locals>.errorInInner.<locals>.innerr   r   rs   r   r   r   errorInInner  s    z1ScopeTests.testUnboundLocal.<locals>.errorInInnerassertRaisesUnboundLocalError	NameErrorr   rt   ru   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 r   r   r   r   r   r   rp     s    zIScopeTests.testUnboundLocal_AfterDel.<locals>.errorInOuter.<locals>.innerrq   rs   r   r   r   rt     s    z:ScopeTests.testUnboundLocal_AfterDel.<locals>.errorInOuterc                     s    fdd} d  |   d S )Nc                      s    S r   r   r   r   r   r   rp     s    zIScopeTests.testUnboundLocal_AfterDel.<locals>.errorInInner.<locals>.innerr   r   rs   r   r   r   ru     s    z:ScopeTests.testUnboundLocal_AfterDel.<locals>.errorInInnerrv   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}   rJ   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 r   r   r   lstr   r   returner5  s    zIScopeTests.testComplexDefinitions.<locals>.makeReturner.<locals>.returnerr   )r   r   r   r   r   makeReturner4  s    z7ScopeTests.testComplexDefinitions.<locals>.makeReturnerr   r   rA   )r   r   rA   c                     s    fdd}|S )Nc                      s    S r   r   r   kwargsr   r   r   <  s    zJScopeTests.testComplexDefinitions.<locals>.makeReturner2.<locals>.returnerr   )r   r   r   r   r   makeReturner2;  s    z8ScopeTests.testComplexDefinitions.<locals>.makeReturner2r0   rX   r>   r   )r   r   r   r   r   r   testComplexDefinitions2  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)
            r~   rJ   r   r   r   testScopeOfGlobalStmtB  s    z ScopeTests.testScopeOfGlobalStmtc                    sJ   G  fddd  fdd}t dD ]
}|  q&t  |  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   countrJ   Foor   r   __init__  s    z*ScopeTests.testLeaks.<locals>.Foo.__init__c                    s     j d8  _ d S r   r   rJ   r   r   r   __del__  s    z)ScopeTests.testLeaks.<locals>.Foo.__del__N)r(   r)   r*   r   r   r   r   r   r   r   r     s   r   c                     s      fdd} |   d S )Nc                      s    S r   r   r   r
   r   r   rn     s    z,ScopeTests.testLeaks.<locals>.f1.<locals>.f2r   )rn   r   r
   r   rm     s    z ScopeTests.testLeaks.<locals>.f1d   r   )ranger   r   r   )r   rm   ir   r   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)
            r   rJ   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 r   r   r3   r   r   r   r5     s    z>ScopeTests.testLocalsFunction.<locals>.f.<locals>.g.<locals>.hrA   )locals)r	   r5   wr
   r   r   r6     s    z3ScopeTests.testLocalsFunction.<locals>.f.<locals>.gr   r7   r   r
   r   r8     s    z(ScopeTests.testLocalsFunction.<locals>.fr   rB   r5      rC   )r   r	   r   )assertInr   )r   r8   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 )0ScopeTests.testLocalsClass.<locals>.f.<locals>.C   c                    s    S r   r   rJ   r
   r   r   m  s    2ScopeTests.testLocalsClass.<locals>.f.<locals>.C.mN)r(   r)   r*   r   r   r   r   r
   r   r   C  s   r   r   r   r   r   r
   r   r8     s    z%ScopeTests.testLocalsClass.<locals>.fr   r   c                    s   G  fddd}|S )Nc                       s&   e Zd Z Z fddZee ZdS )r   c                    s    S r   r   rJ   r
   r   r   r     s    r   N)r(   r)   r*   r	   r   listr   r4   r   r
   r   r   r     s   r   r   r   r   r
   r   r8     s    r   r	   )r   r   r4   assertNotInr   )r   r8   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 r   r   rj   r   r   r   re     rf   z6ScopeTests.testLocalsClass_WithTrace.<locals>.<lambda>r   c                       s   e Zd Z fddZdS )z/ScopeTests.testLocalsClass_WithTrace.<locals>.Cc                    s    S r   r   rJ   r
   r   r   r8     s    z1ScopeTests.testLocalsClass_WithTrace.<locals>.C.fN)r(   r)   r*   r8   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 r   r   rJ   r
   r   r   r     s    z3ScopeTests.testBoundAndFree.<locals>.f.<locals>.C.mN)r(   r)   r*   r   r>   r   r
   r   r   r     s   r   r   r   r   r
   r   r8     s    z&ScopeTests.testBoundAndFree.<locals>.frA   )r   r>   r   )r   r8   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 r   r   rj   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 r   )getattr)objZdesr   r   re   !  rf   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   rZ   )r   r   )r   r   r   r   rw   	TypeError)r   r   r   r   r   r   r   testInteractionWithTraceFunc  s    

z'ScopeTests.testInteractionWithTraceFuncc                 C   sR   dd }|d}|  tt|j zt|ji  W n tyB   Y n0 | d d S )Nc                    s    fddS )Nc                      s    d S r   r   r   r
   r   r   re   0  rf   z<ScopeTests.testEvalExecFreeVars.<locals>.f.<locals>.<lambda>r   r
   r   r
   r   r8   /  s    z*ScopeTests.testEvalExecFreeVars.<locals>.frA   z9exec should have failed, because code contained free vars)rw   r   eval__code__r~   r}   r   r8   r6   r   r   r   testEvalExecFreeVars-  s    zScopeTests.testEvalExecFreeVarsc                 C   sZ   zt t W n ty   Y n
0 t d dd }|  zt t W n tyT   Y n0 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>F  rf   z?ScopeTests.testListCompLocalVars.<locals>.x.<locals>.<listcomp>za br   r   r   r   r   r   E  s    z+ScopeTests.testListCompLocalVars.<locals>.x)rr   r   ry   )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   r6   Q  s    z1ScopeTests.testEvalFreeVars.<locals>.f.<locals>.gr   r7   r   r
   r   r8   P  s    z&ScopeTests.testEvalFreeVars.<locals>.frB   r   rb   r   r   r   testEvalFreeVarsN  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 r   )Znestedcell_getrJ   r   r   r   r   \  s    z3ScopeTests.testFreeingCell.<locals>.Special.__del__N)r(   r)   r*   r   r   r   r   r   Special[  s   r   r   )r   r   r   r   r   testFreeingCellX  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 r   r   r   r
   r   r   r   b  s    z7ScopeTests.testNonLocalFunction.<locals>.f.<locals>.incc                      s    d8   S r   r   r   r
   r   r   decf  s    z7ScopeTests.testNonLocalFunction.<locals>.f.<locals>.decr   )r   r   r   r   r
   r   r8   a  s    z*ScopeTests.testNonLocalFunction.<locals>.fr   r   r   r   )r   r8   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 r   r   rJ   r
   r   r   r   u  s    z7ScopeTests.testNonLocalMethod.<locals>.f.<locals>.c.incc                    s    d8   S r   r   rJ   r
   r   r   r   y  s    z7ScopeTests.testNonLocalMethod.<locals>.f.<locals>.c.decN)r(   r)   r*   r   r   r   r
   r   r   r?   t  s   r?   r   r   r?   r   r
   r   r8   s  s    	z(ScopeTests.testNonLocalMethod.<locals>.fr   r   r   )r   r   r   r   r8   r?   r   r   r   testNonLocalMethodr  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)r~   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 r   r   rJ   r
   r   r   get  s    z6ScopeTests.testNonLocalClass.<locals>.f.<locals>.c.getN)r(   r)   r*   r   r   r
   r   r   r?     s   r?   r   r   r   r
   r   r8     s    z'ScopeTests.testNonLocalClass.<locals>.fr   r   r   )r   r   r   	__class____dict__r   r   r   r   testNonLocalClass  s    zScopeTests.testNonLocalClassc                 C   s,   dd }|d}|  t|dg d d S )Nc                    s    fdd}|S )Nc                 3   s    t | D ]} d7   V  qd S r   )r   )r	   r   r
   r   r   r6     s    z6ScopeTests.testNonLocalGenerator.<locals>.f.<locals>.gr   r7   r   r
   r   r8     s    z+ScopeTests.testNonLocalGenerator.<locals>.fr   r9   )r   r   rA   rB   r9   )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 )NrB   r   r   r
   r   r   r5     s    z>ScopeTests.testNestedNonLocal.<locals>.f.<locals>.g.<locals>.hr   )r5   r
   r   r   r6     s    z3ScopeTests.testNestedNonLocal.<locals>.f.<locals>.gr   r7   r   r
   r   r8     s    z(ScopeTests.testNestedNonLocal.<locals>.fr   rA   r   rE   r   r   r   testNestedNonLocal  s    zScopeTests.testNestedNonLocalc                 C   s   dd }dd }d S )Nc                 S   s   d S r   r   rX   r   r   r   top  s    z/ScopeTests.testTopIsNotSignificant.<locals>.topc                   S   s   d S r   r   r   r   r   r   r=     s    z-ScopeTests.testTopIsNotSignificant.<locals>.br   )r   r   r=   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 )Nr2   c                       s   e Zd Zde d<  ZdS z8ScopeTests.testClassNamespaceOverridesClosure.<locals>.X+   r   N)r(   r)   r*   r   r	   r   r
   r   r   X  s   
r   r   c                   @   s   e Zd Zde d< [dS r   )r(   r)   r*   r   r   r   r   r   r   r     s   
r   )r   r	   ZassertFalsehasattr)r   r   r   r
   r   "testClassNamespaceOverridesClosure  s    z-ScopeTests.testClassNamespaceOverridesClosurec                 C   s>   G dd d}| }|   t|}~t  | |  d S )Nc                   @   s   e Zd Zdd ZdS )z'ScopeTests.testCellLeak.<locals>.Testerc              
      s@   zdd  W n* t y6 } z| _W Y d }~n
d }~0 0 d  d S )Nr   r   )	Exceptionexc)r   r   r   rJ   r   dig  s
    z+ScopeTests.testCellLeak.<locals>.Tester.digN)r(   r)   r*   r   r   r   r   r   Tester  s   r   )r   weakrefrefr   ZassertIsNone)r   r   Ztesterr   r   r   r   testCellLeak  s    	
zScopeTests.testCellLeakN)*r(   r)   r*   r   r   r!   r&   r-   r1   r;   rF   rV   r]   rc   rd   ro   r{   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sR   %"T!


r   __main__)
Zunittestr   Ztest.supportr   r   r   ZTestCaser   r(   mainr   r   r   r   <module>   s        x