
    <eT                         d dl Z d dlZd dlmZmZ d dlmZ  G d de j                  Zedk    r e j	                     dS dS )    N)check_syntax_errorcpython_only)
gc_collectc                   .   e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zed             Zd Zed             Zd Zd Z d Z!d Z"d  Z#d! Z$d" Z%d# Z&d$ Z'd% Z(d& Z)d' Z*ed(             Z+d)S )*
ScopeTestsc                     d } |d          } |d          }|                       |d          d           |                       |d          d           d S )Nc                       fd}|S )Nc                     | z   S N yxs    &/usr/lib/python3.11/test/test_scope.pyadderz?ScopeTests.testSimpleNesting.<locals>.make_adder.<locals>.adder       1u    r   r   r   s   ` r   
make_adderz0ScopeTests.testSimpleNesting.<locals>.make_adder   s!        Lr      
         assertEqual)selfr   incplus10s       r   testSimpleNestingzScopeTests.testSimpleNesting
   so    	 	 	
 jmmBQ###Q'''''r   c                     d } |d          } |d          }|                       |d          d           |                       |d          d           d S )Nc                 "      fd} |            S )Nc                      fd} | S )Nc                     | z   S r   r   r   s    r   r   zNScopeTests.testExtraNesting.<locals>.make_adder2.<locals>.extra.<locals>.adder       q5Lr   r   )r   r   s    r   extraz?ScopeTests.testExtraNesting.<locals>.make_adder2.<locals>.extra   s!    ! ! ! ! !r   r   )r   r&   s   ` r   make_adder2z0ScopeTests.testExtraNesting.<locals>.make_adder2   s)         577Nr   r   r   r   r   r   r   )r   r'   r   r   s       r   testExtraNestingzScopeTests.testExtraNesting   so    	 	 	 k!nnRQ###Q'''''r   c                     d } |d          } |d          }|                       |d          d           |                       |d          d           d S )Nc                       fd} dz    |S )Nc                     | z   S r   r   r   s    r   r   zEScopeTests.testSimpleAndRebinding.<locals>.make_adder3.<locals>.adder)   r   r   r   r   r   s   ` r   make_adder3z6ScopeTests.testSimpleAndRebinding.<locals>.make_adder3(   s*        AALr   r   	   r   r   r   r   r   )r   r,   r   r   s       r   testSimpleAndRebindingz!ScopeTests.testSimpleAndRebinding&   so    	 	 	 k!nnQQ###Q'''''r   c                     fd}d |            }|                       |d          d           d|                       |d          d           d S )Nc                  "    fd}  |             S )Nc                  "    fd}  |             S )Nc                      fd} | S )Nc                     | z   S r   r   r   global_xs    r   r   zbScopeTests.testNestingGlobalNoFree.<locals>.make_adder4.<locals>.nest.<locals>.nest.<locals>.adder9   s    '!|+r   r   )r   r5   s    r   nestzSScopeTests.testNestingGlobalNoFree.<locals>.make_adder4.<locals>.nest.<locals>.nest8   s!    , , , , , Lr   r   r6   r5   s    r   r6   zEScopeTests.testNestingGlobalNoFree.<locals>.make_adder4.<locals>.nest7   s)    ! ! ! ! ! tvvr   r   r7   s    r   make_adder4z7ScopeTests.testNestingGlobalNoFree.<locals>.make_adder46   s)         466Mr   r   r   r   r   r   r   )r   r8   r   r5   s      @r   testNestingGlobalNoFreez"ScopeTests.testNestingGlobalNoFree4   su    	 	 	 	 	 q1%%%rA&&&&&r   c                     d } |d          } |d          }|                       |d          d           |                       |d          d           d S )Nc                 6      G  fdd          } |            S )Nc                       e Zd Z fdZdS )FScopeTests.testNestingThroughClass.<locals>.make_adder5.<locals>.Adderc                     |z   S r   r   )r   r   r   s     r   __call__zOScopeTests.testNestingThroughClass.<locals>.make_adder5.<locals>.Adder.__call__J   r%   r   N)__name__
__module____qualname__r?   r   s   r   Adderr=   I   s.        ! ! ! ! ! ! !r   rD   r   )r   rD   s   ` r   make_adder5z7ScopeTests.testNestingThroughClass.<locals>.make_adder5H   sB    ! ! ! ! ! ! ! ! ! ! 577Nr   r   r   r   r   r   r   )r   rE   r   r   s       r   testNestingThroughClassz"ScopeTests.testNestingThroughClassF   so    	 	 	 k!nnRQ###Q'''''r   c                     d } |d          } |d          }|                       |d          d           |                       |d          d           d S )Nc                     d }| a |S )Nc                     t           | z   S r   global_nest_xr   s    r   r   zMScopeTests.testNestingPlusFreeRefToGlobal.<locals>.make_adder6.<locals>.adderX   s    $q((r   rJ   r   s     r   make_adder6z>ScopeTests.testNestingPlusFreeRefToGlobal.<locals>.make_adder6V   s    ) ) )MLr   r   r      r   r   r   )r   rM   r   r   s       r   testNestingPlusFreeRefToGlobalz)ScopeTests.testNestingPlusFreeRefToGlobalT   so    	 	 	 k!nnRQ$$$Q'''''r   c                 `    d } |d          }|                       |d          d           d S )Nc                     d } |d          S )Nc                     dfd}|S )N*   c                     | z   S r   r   )zr   s    r   hzEScopeTests.testNearestEnclosingScope.<locals>.f.<locals>.g.<locals>.hh   r%   r   r   )r   rV   r   s     @r   gz:ScopeTests.testNearestEnclosingScope.<locals>.f.<locals>.gf   s&    ! ! ! ! !r   r   r   r   rW   s     r   fz/ScopeTests.testNearestEnclosingScope.<locals>.fe   s       
 1Q44Kr   r      /   r   )r   rY   	test_funcs      r   testNearestEnclosingScopez$ScopeTests.testNearestEnclosingScopec   sD    	 	 	 AbEE	1r*****r   c                     d fd} |ddd          } |ddd          }|                       |            d           d S )	Nc                     | S r   r   rC   s    r   identityz9ScopeTests.testMixedFreevarsAndCellvars.<locals>.identityr   s    Hr   c                       fd}|S )Nc                 0    | z   } fd}|z   |S )Nc                  &      z   z            S r   r   )br`   r   rU   s   r   rV   zHScopeTests.testMixedFreevarsAndCellvars.<locals>.f.<locals>.g.<locals>.hx   s     $8AQK000r   r   )ard   crV   r   r`   r   rU   s    `  @r   rW   z=ScopeTests.testMixedFreevarsAndCellvars.<locals>.f.<locals>.gv   sF    E1 1 1 1 1 1 1 1 Er   r   )r   r   rU   rW   r`   s   ` ` r   rY   z2ScopeTests.testMixedFreevarsAndCellvars.<locals>.fu   s/           Hr   r   r            '   r   )r   rY   rW   rV   r`   s       @r   testMixedFreevarsAndCellvarsz'ScopeTests.testMixedFreevarsAndCellvarsp   sq    	 	 			 		 		 		 		 AaAJJAaAJJb!!!!!r   c                 8   d } |            }|                      |                                d           |                      |                                d           |                      |                                d           d G fdd          } |            }|                      |                                d           |                      |                                d           |                      |                                d           d S )Nc                  :    d G fdd          }  |             S )Nvarc                   ,    e Zd Zd Z fdZd Zd ZdS ):ScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Testc                     dS Nmethodr   r   s    r   method_and_varzIScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Test.method_and_var   s    #8r   c                     S r   r   r   ru   s    r   testz?ScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Test.test   s	    ))r   c                      t          d          S Nglobalstrrt   s    r   actual_globalzHScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Test.actual_global   s    x==(r   c                      t          |           S r   r|   rt   s    r   r}   z>ScopeTests.testFreeVarInMethod.<locals>.test.<locals>.Test.str   s    t99$r   Nr@   rA   rB   ru   rx   r~   r}   ru   s   r   Testrp      s[        $ $ $* * * * *) ) )% % % % %r   r   r   )r   ru   s    @r   rx   z,ScopeTests.testFreeVarInMethod.<locals>.test   sG    "N% % % % % % % % % % 466Mr   rn   rs   r{   c                   ,    e Zd Zd Z fdZd Zd ZdS ),ScopeTests.testFreeVarInMethod.<locals>.Testc                     dS rr   r   rt   s    r   ru   z;ScopeTests.testFreeVarInMethod.<locals>.Test.method_and_var   s    xr   c                     S r   r   rw   s    r   rx   z1ScopeTests.testFreeVarInMethod.<locals>.Test.test   s	    %%r   c                      t          d          S rz   r|   rt   s    r   r~   z:ScopeTests.testFreeVarInMethod.<locals>.Test.actual_global   s    8}}$r   c                      t          |           S r   r|   rt   s    r   r}   z0ScopeTests.testFreeVarInMethod.<locals>.Test.str   s    4yy r   Nr   r   s   r   r   r      s[             & & & & &% % %! ! ! ! !r   r   )r   rx   ru   r~   )r   rx   tr   ru   s       @r   testFreeVarInMethodzScopeTests.testFreeVarInMethod   s)   	 	 	 DFF5)))))++X666**H555		! 		! 		! 		! 		! 		! 		! 		! 		! 		! DFF5)))))++X666**H55555r   c                     ddd}|                       |d          d           |                       |            d           d S )N   re   c                 (      fd} |            dz   S )Nc                       dz   S )NrZ   r   r   s   r   barz8ScopeTests.testCellIsKwonlyArg.<locals>.foo.<locals>.bar   r   r   rg   r   )re   r   s   ` r   fooz+ScopeTests.testCellIsKwonlyArg.<locals>.foo   s,        35519r   rS   2      r   )r   r   s     r   testCellIsKwonlyArgzScopeTests.testCellIsKwonlyArg   sd      	 	 	 	 	
 	rB'''#####r   c                     d } |            }d } ||          }|j         \  } |            }|                     ||           |                     ||           d S )Nc                  (    dfd} | j         \  }|S )NrS   c                       S r   r   values   r   innerzCScopeTests.testCellIsArgAndEscapes.<locals>.external.<locals>.inner       r   __closure__r   cellr   s     @r   externalz4ScopeTests.testCellIsArgAndEscapes.<locals>.external   0    E    %EDKr   c                       fd}|S )Nc                       S r   r   )args   r   eggsz>ScopeTests.testCellIsArgAndEscapes.<locals>.spam.<locals>.eggs       
r   r   )r   r   s   ` r   spamz0ScopeTests.testCellIsArgAndEscapes.<locals>.spam   s!        Kr   r   assertIsassertIsNotr   r   cell_extr   r   cell_closure	cell_eggss          r   testCellIsArgAndEscapesz"ScopeTests.testCellIsArgAndEscapes   s    
	 	 	 8::	 	 	
 tH~~(DFF	i***L11111r   c                     d } |            }d } ||          }|j         \  } |            }|                     ||           |                     ||           d S )Nc                  (    dfd} | j         \  }|S )NrS   c                       S r   r   r   s   r   r   zEScopeTests.testCellIsLocalAndEscapes.<locals>.external.<locals>.inner   r   r   r   r   s     @r   r   z6ScopeTests.testCellIsLocalAndEscapes.<locals>.external   r   r   c                     | fd}|S )Nc                       S r   r   )r   s   r   r   z@ScopeTests.testCellIsLocalAndEscapes.<locals>.spam.<locals>.eggs   s    r   r   )r   r   r   s     @r   r   z2ScopeTests.testCellIsLocalAndEscapes.<locals>.spam   s&    D    Kr   r   r   s          r   testCellIsLocalAndEscapesz$ScopeTests.testCellIsLocalAndEscapes   s    
	 	 	 8::	 	 	 tH~~(DFF	i***L11111r   c                 J    d }|                       |d          d           d S )Nc                 N    fd| dk    r |           S t          d          )Nc                 6    | dk    rdS |  | dz
            z  S )Nr   r   r   )nfacts    r   r   z1ScopeTests.testRecursion.<locals>.f.<locals>.fact   s'    661ttAE{{?*r   r   zx must be >= 0)
ValueError)r   r   s    @r   rY   z#ScopeTests.testRecursion.<locals>.f   sC    + + + + +
 AvvtAww !1222r   ri   i  r   r   rY   s     r   testRecursionzScopeTests.testRecursion   s7    		3 		3 		3 	1s#####r   c                     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   rt   s    r   testUnoptimizedNamespacesz$ScopeTests.testUnoptimizedNamespaces   sv    4 " 	 	 	 	4 " 	 	 	 	4 " 	 	 	 	4 " 	 	 	 	 	r   c                 <  	 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 S )Nc                       fdS )Nc                     | z   S r   r   r   s    r   <lambda>z:ScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>  s    Q r   r   rC   s   `r   r   z(ScopeTests.testLambdas.<locals>.<lambda>  s     r   r   r   r   rZ      c                        fd            S )Nc                       fdS )Nc                     | z   S r   r   r   s    r   r   zLScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>!  s    1q5 r   r   rC   s   r   r   z:ScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>!  s     r   r   rC   s   `r   r   z(ScopeTests.testLambdas.<locals>.<lambda>!  s    0000033 r   c                     fdS )Nc                     | z   S r   r   r4   s    r   r   z:ScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>'  s    A r   r   )r   r5   s    r   r   z(ScopeTests.testLambdas.<locals>.<lambda>'  s    ---- r   rg   c                     fdS )Nc                     fdS )Nc                       z   z  S r   r   )rd   r   rU   s   r   r   zLScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>,  s    a1q5k r   r   )re   rd   rf   r   rU   s    ` r   r   z:ScopeTests.testLambdas.<locals>.<lambda>.<locals>.<lambda>,  s    -A-A-A-A-A-A r   r   )r   r   rU   s    ``r   r   z(ScopeTests.testLambdas.<locals>.<lambda>,  s    AAAAA r   rh   ri      r   )
r   f1r   r   f2f3f8rW   rV   r5   s
            @r   testLambdaszScopeTests.testLambdas  sI   &&beeBQ###B'''33beeBQ###B'''----bhhQ###AABq!QKKAaAJJb!!!!!r   c                 ~    d }d }|                      t          |           |                      t          |           d S )Nc                  4    t                     fd} dd S )Nc                       S r   r   rL   s   r   r   z@ScopeTests.testUnboundLocal.<locals>.errorInOuter.<locals>.inner5      r   r   printr   r   s    @r   errorInOuterz1ScopeTests.testUnboundLocal.<locals>.errorInOuter3  s0    !HHH    AAAr   c                  *    fd}  |              dd S )Nc                       S r   r   rL   s   r   r   z@ScopeTests.testUnboundLocal.<locals>.errorInInner.<locals>.inner:  r   r   r   r   r   s    @r   errorInInnerz1ScopeTests.testUnboundLocal.<locals>.errorInInner9  s0        EGGGAAAr   assertRaisesUnboundLocalError	NameErrorr   r   r   s      r   testUnboundLocalzScopeTests.testUnboundLocal1  sU    	 	 		 	 	 	+\:::)\22222r   c                 ~    d }d }|                      t          |           |                      t          |           d S )Nc                  6    dt                     fd} d S )Nr   c                       S r   r   rL   s   r   r   zIScopeTests.testUnboundLocal_AfterDel.<locals>.errorInOuter.<locals>.innerJ  r   r   r   r   s    @r   r   z:ScopeTests.testUnboundLocal_AfterDel.<locals>.errorInOuterF  s9    A!HHH      r   c                  ,    fd} d |              d S )Nc                       S r   r   rL   s   r   r   zIScopeTests.testUnboundLocal_AfterDel.<locals>.errorInInner.<locals>.innerN  r   r   r   r   r   s    @r   r   z:ScopeTests.testUnboundLocal_AfterDel.<locals>.errorInInnerM  s3        AEGGGGGr   r   r   s      r   testUnboundLocal_AfterDelz$ScopeTests.testUnboundLocal_AfterDelB  sU    	 	 		 	 	 	+\:::)\22222r   c                 4    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   rt   s    r   testUnboundLocal_AugAssignz%ScopeTests.testUnboundLocal_AugAssignW  s/     
 $)$
	& 
	& 
	& 
	& 
	&r   c                     d }|                        |ddd                      d           d }|                        |d                      d	         d           d S )
Nc                        fd}|S )Nc                       S r   r   )lsts   r   returnerzIScopeTests.testComplexDefinitions.<locals>.makeReturner.<locals>.returnerh  r   r   r   )r   r   s   ` r   makeReturnerz7ScopeTests.testComplexDefinitions.<locals>.makeReturnerg  s!        Or   r   r   rg   )r   r   rg   c                        fd}|S )Nc                       S r   r   )kwargss   r   r   zJScopeTests.testComplexDefinitions.<locals>.makeReturner2.<locals>.returnero  s    r   r   )r   r   s   ` r   makeReturner2z8ScopeTests.testComplexDefinitions.<locals>.makeReturner2n  s!        Or   rN   r   re   r   )r   r   r   s      r   testComplexDefinitionsz!ScopeTests.testComplexDefinitionse  s    	 	 	
 	,a!,,..888	 	 	
 	,,,,..s3R88888r   c                 $    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   rt   s    r   testScopeOfGlobalStmtz ScopeTests.testScopeOfGlobalStmtu  s+     	 O O	 O	 O	 O	 O	r   c                      G fdd          fd}t          d          D ]} |             t                       |                     j        d           d S )Nc                   (    e Zd ZdZ fdZ fdZdS )!ScopeTests.testLeaks.<locals>.Foor   c                 (    xj         dz  c_         d S Nr   countr   Foos    r   __init__z*ScopeTests.testLeaks.<locals>.Foo.__init__      		Q				r   c                 (    xj         dz  c_         d S r   r  r  s    r   __del__z)ScopeTests.testLeaks.<locals>.Foo.__del__  r  r   N)r@   rA   rB   r  r  r  )r  s   r   r  r     sL        E          r   r  c                  <                 fd}  |              d S )Nc                       S r   r   rC   s   r   r   z,ScopeTests.testLeaks.<locals>.f1.<locals>.f2  r   r   r   )r   r   r  s    @r   r   z ScopeTests.testLeaks.<locals>.f1  s7    A    BDDDDDr   d   r   )ranger   r   r  )r   r   ir  s      @r   	testLeakszScopeTests.testLeaks  s    	 	 	 	 	 	 	 	 	 		 	 	 	 	 s 	 	ABDDDDA&&&&&r   c                 $    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   rt   s    r   testClassAndGlobalzScopeTests.testClassAndGlobal  s#      	 	 	 	 	r   c                     d }  |d          d          }|                      d|           |d= |                     |dddd           d S )Nc                       fd}|S )Nc                 @      fd} z   } dz   t                      S )Nc                     | z   S r   r   )rU   r   s    r   rV   z>ScopeTests.testLocalsFunction.<locals>.f.<locals>.g.<locals>.h  r%   r   rg   )locals)r   rV   wr   s   `  r   rW   z3ScopeTests.testLocalsFunction.<locals>.f.<locals>.g  s8    ! ! ! ! !EQxxr   r   rX   s   ` r   rY   z(ScopeTests.testLocalsFunction.<locals>.f  s#              Hr   r   rh   rV      ri   )r   r   r  )assertInr   )r   rY   ds      r   testLocalsFunctionzScopeTests.testLocalsFunction  si    	 	 	 AAaDDGGc1cF!!!4455555r   c                     d }|                       |d          j        d           d } |d          j        }|                     d|           |                     d|           d S )Nc                 &      G  fdd          }|S )Nc                   2    e Zd ZdZ fdZ e             dS )0ScopeTests.testLocalsClass.<locals>.f.<locals>.C   c                     S r   r   r   r   s    r   mz2ScopeTests.testLocalsClass.<locals>.f.<locals>.C.m      Hr   N)r@   rA   rB   r   r"  r  rC   s   r   Cr    s8            r   r$  r   r   r$  s   ` r   rY   z%ScopeTests.testLocalsClass.<locals>.f  s<             
 Hr   r   r  c                 &      G  fdd          }|S )Nc                   D    e Zd Z Z fdZ e e                      ZdS )r  c                     S r   r   r!  s    r   r"  z2ScopeTests.testLocalsClass.<locals>.f.<locals>.C.m!  r#  r   N)r@   rA   rB   r   r"  listr  rU   rC   s   r   r$  z0ScopeTests.testLocalsClass.<locals>.f.<locals>.C  s@            DNNr   r$  r   r%  s   ` r   rY   z%ScopeTests.testLocalsClass.<locals>.f  s<    # # # # # # # # # #
 Hr   r   r   )r   r   rU   assertNotInr  )r   rY   varnamess      r   testLocalsClasszScopeTests.testLocalsClass	  s    	 	 	 	1$$$	 	 	 1Q446h'''c8$$$$$r   c                     dd l }|                     |j        |                                           |                    d            d G fdd          }|                     d           d S )Nr   c                     d S r   r   )re   rd   rf   s      r   r   z6ScopeTests.testLocalsClass_WithTrace.<locals>.<lambda>2  s    $ r   r  c                       e Zd Z fdZdS )/ScopeTests.testLocalsClass_WithTrace.<locals>.Cc                     S r   r   r!  s    r   rY   z1ScopeTests.testLocalsClass_WithTrace.<locals>.C.f6  r   r   N)r@   rA   rB   rY   rC   s   r   r$  r0  5  s.              r   r$  )sys
addCleanupsettracegettracer   )r   r2  r$  r   s      @r   testLocalsClass_WithTracez$ScopeTests.testLocalsClass_WithTrace*  s     	


cllnn555&&'''	 	 	 	 	 	 	 	 	 	 	Br   c                     d }  |d                      }|                      |j        |                                           d S )Nc                 &      G  fdd          }|S )Nc                       e Zd Z fdZ ZdS )1ScopeTests.testBoundAndFree.<locals>.f.<locals>.Cc                     S r   r   r!  s    r   r"  z3ScopeTests.testBoundAndFree.<locals>.f.<locals>.C.m@  r#  r   N)r@   rA   rB   r"  re   rC   s   r   r$  r:  ?  s-            r   r$  r   r%  s   ` r   rY   z&ScopeTests.testBoundAndFree.<locals>.f>  s<              Hr   rg   )r   re   r"  )r   rY   insts      r   testBoundAndFreezScopeTests.testBoundAndFree;  sL    	 	 	 qqttvv*****r   c                 B   dd l }fdd } G d d          }|                     |j        |                                           |                                |d|d           |                    d            |                     t
          |j                   d S )Nr   c                     S r   r   )re   rd   rf   tracers      r   r@  z7ScopeTests.testInteractionWithTraceFunc.<locals>.tracerL  s    Mr   c                 N    |\  }|dk    rdk    rd|j         d| fdS d S )Nr    ___c                 $    t          |           S r   )getattr)objdess    r   r   zNScopeTests.testInteractionWithTraceFunc.<locals>.adaptgetter.<locals>.<lambda>T  s    73#4#4 r   )r@   )nameklassgetterkindrH  s       @r   adaptgetterz<ScopeTests.testInteractionWithTraceFunc.<locals>.adaptgetterO  sH    ID#qyy"999',~~~tt<C44444 yr   c                       e Zd ZdS ):ScopeTests.testInteractionWithTraceFunc.<locals>.TestClassN)r@   rA   rB   r   r   r   	TestClassrO  V  s        Dr   rP  r   )r   rB  )r2  r3  r4  r5  r   	TypeError)r   r2  rM  rP  r@  s       @r   testInteractionWithTraceFuncz'ScopeTests.testInteractionWithTraceFuncH  s     	


	 	 	 	 		5 	5 	5	 	 	 	 	 	 	 	 	cllnn555VE9g...T)S\22222r   c                     d } |d          }|                      t          t          |j                   	 t	          |j        i            |                     d           d S # t          $ r Y d S w xY w)Nc                       fdS )Nc                       dz   S r   r   rC   s   r   r   z<ScopeTests.testEvalExecFreeVars.<locals>.f.<locals>.<lambda>c  s    1q5 r   r   rC   s   `r   rY   z*ScopeTests.testEvalExecFreeVars.<locals>.fb  s     === r   rg   z9exec should have failed, because code contained free vars)r   rQ  eval__code__r   r   r   rY   rW   s      r   testEvalExecFreeVarszScopeTests.testEvalExecFreeVars`  s    	! 	! 	! AaDD)T1:666	SR    IIQRRRRR  	 	 	DD	s   A" "
A0/A0c                     	 t          t                     t          d           n# t          $ r Y nw xY wd } |             	 t          t                     d S # t          $ r Y d S w xY w)Nzbad should not be definedc                      d dD              d S )Nc                 @    g | ]}|                                 D ]}|S r   )split).0sbads      r   
<listcomp>z?ScopeTests.testListCompLocalVars.<locals>.x.<locals>.<listcomp>y  s-    555Q1779955CS5555r   za br   r   r   r   r   z+ScopeTests.testListCompLocalVars.<locals>.xx  s    55%555555r   )r   r`  r   r!  s     r   testListCompLocalVarsz ScopeTests.testListCompLocalVarso  s    	/#JJJ -....  	 	 	D	
	6 	6 	6 	
	#JJJJJ 	 	 	DD	s   & 
33A 
A('A(c                 2    d }  |d                       d S )Nc                       fd}|S )Nc                  *      t          d           d S )Nzx + 1)rV  rC   s   r   rW   z1ScopeTests.testEvalFreeVars.<locals>.f.<locals>.g  s    Wr   r   rX   s   ` r   rY   z&ScopeTests.testEvalFreeVars.<locals>.f  s#         Hr   rh   r   r   s     r   testEvalFreeVarszScopeTests.testEvalFreeVars  s*    	 	 	 	!r   c                       G d d          }d S )Nc                       e Zd Zd ZdS )+ScopeTests.testFreeingCell.<locals>.Specialc                 "    t                       d S r   )nestedcell_getrt   s    r   r  z3ScopeTests.testFreeingCell.<locals>.Special.__del__  s         r   N)r@   rA   rB   r  r   r   r   Specialri    s#        ! ! ! ! !r   rl  r   )r   rl  s     r   testFreeingCellzScopeTests.testFreeingCell  s4    	! 	! 	! 	! 	! 	! 	! 	! 	! 	!r   c                    d } |d          \  }}|                       |            d           |                       |            d           |                       |            d           |                       |            d           d S )Nc                        fd} fd}||fS )Nc                       dz    S r   r   rC   s   r   r   z7ScopeTests.testNonLocalFunction.<locals>.f.<locals>.inc      Qr   c                       dz    S r   r   rC   s   r   decz7ScopeTests.testNonLocalFunction.<locals>.f.<locals>.dec  rq  r   r   )r   r   rs  s   `  r   rY   z*ScopeTests.testNonLocalFunction.<locals>.f  s@             8Or   r   r   r   r   )r   rY   r   rs  s       r   testNonLocalFunctionzScopeTests.testNonLocalFunction  s    		 		 		 1Q44S""""""""""""""r   c                 b   d } |d          }|                      |                                d           |                      |                                d           |                      |                                d           |                      |                                d           d S )Nc                 6      G  fdd          } |            S )Nc                   $    e Zd Z fdZ fdZdS )3ScopeTests.testNonLocalMethod.<locals>.f.<locals>.cc                     dz  S r   r   r!  s    r   r   z7ScopeTests.testNonLocalMethod.<locals>.f.<locals>.c.inc      FAHr   c                     dz  S r   r   r!  s    r   rs  z7ScopeTests.testNonLocalMethod.<locals>.f.<locals>.c.dec  rz  r   N)r@   rA   rB   r   rs  rC   s   r   rf   rx    sG                  r   rf   r   r   rf   s   ` r   rY   z(ScopeTests.testNonLocalMethod.<locals>.f  sB              133Jr   r   r   r   )r   r   rs  r   rY   rf   s      r   testNonLocalMethodzScopeTests.testNonLocalMethod  s    
	 
	 
	 AaDD!$$$!$$$!$$$!$$$$$r   c                     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   result2r-   result9)r   r   )r   local_ns	global_nss      r   #testGlobalInParallelNestedFunctionsz.ScopeTests.testGlobalInParallelNestedFunctions  sj    
 	  9	& 	& 	& 	Ii0111Ii011111r   c                     d } |d          }|                      |                                d           |                     d|j        j                   d S )Nc                 6      G  fdd          } |            S )Nc                   $    e Zd Z dz    fdZdS )2ScopeTests.testNonLocalClass.<locals>.f.<locals>.cr   c                     S r   r   r!  s    r   getz6ScopeTests.testNonLocalClass.<locals>.f.<locals>.c.get  r#  r   N)r@   rA   rB   r  rC   s   r   rf   r    s7        Q      r   rf   r   r|  s   ` r   rY   z'ScopeTests.testNonLocalClass.<locals>.f  sB             
 133Jr   r   r   r   )r   r  r*  	__class____dict__r}  s      r   testNonLocalClasszScopeTests.testNonLocalClass  s_    	 	 	 AaDD!$$$ak233333r   c                 ~    d } |d          }|                      t           |d                    g d           d S )Nc                       fd}|S )Nc              3   B   K   t          |           D ]}dz  V  d S r   )r  )r   r  r   s     r   rW   z6ScopeTests.testNonLocalGenerator.<locals>.f.<locals>.g  s:      q  AFAGGGG r   r   rX   s   ` r   rY   z+ScopeTests.testNonLocalGenerator.<locals>.f  s#        
 Hr   r   rZ   )r   r   rg   rh   rZ   )r   r)  rX  s      r   testNonLocalGeneratorz ScopeTests.testNonLocalGenerator  sN    	 	 	 AaDDaadd___55555r   c                 r    d } |d          } |            }|                       |            d           d S )Nc                       fd}|S )Nc                      dz  fd} | S )Nr   c                       dz    S )Nrh   r   rC   s   r   rV   z>ScopeTests.testNestedNonLocal.<locals>.f.<locals>.g.<locals>.h  rz  r   r   )rV   r   s    r   rW   z3ScopeTests.testNestedNonLocal.<locals>.f.<locals>.g  s,    Q     r   r   rX   s   ` r   rY   z(ScopeTests.testNestedNonLocal.<locals>.f  s#         Hr   r   rg   r   )r   rY   rW   rV   s       r   testNestedNonLocalzScopeTests.testNestedNonLocal  sM    		 		 		 AaDDACCa     r   c                     d }d }d S )Nc                     d S r   r   r   s    r   topz/ScopeTests.testTopIsNotSignificant.<locals>.top  s    Dr   c                      d S r   r   r   r   r   rd   z-ScopeTests.testTopIsNotSignificant.<locals>.b   s      r   r   )r   r  rd   s      r   testTopIsNotSignificantz"ScopeTests.testTopIsNotSignificant  s*    	 	 		 	 	 	 	r   c                     d G fdd          }|                      |j        d            G d d          }|                     t          |d                     |                      d           d S )NrS   c                   .    e Zd Zd e            d<    ZdS 8ScopeTests.testClassNamespaceOverridesClosure.<locals>.X+   r   N)r@   rA   rB   r  r   rC   s   r   Xr    s#        FFHHSMAAAr   r  r  c                   *    e Zd Zd e            d<   [dS r  )r@   rA   rB   r  r   r   r   r   r  z8ScopeTests.testClassNamespaceOverridesClosure.<locals>.X
  s         FFHHSMr   r   )r   r   assertFalsehasattr)r   r  r   s     @r   "testClassNamespaceOverridesClosurez-ScopeTests.testClassNamespaceOverridesClosure  s    	 	 	 	 	 	 	 	 	 	 	b!!!	 	 	 	 	 	 	 	 	C)))Br   c                      G d d          } |            }|                                  t          j        |          }~t                       |                      |                       d S )Nc                       e Zd Zd ZdS )'ScopeTests.testCellLeak.<locals>.Testerc                 X     	 	 ddz   n# t           $ r}| _        Y d }~nd }~ww xY wd  d S )Nr   c                       S r   r   rt   s   r   r   z=ScopeTests.testCellLeak.<locals>.Tester.dig.<locals>.<lambda>   s    D r   r   )	Exceptionexc)r   r  s   ` r   digz+ScopeTests.testCellLeak.<locals>.Tester.dig  sQ    !#aCCC  # # #"DHHHHHH#s   
 
% %N)r@   rA   rB   r  r   r   r   Testerr    s#            r   r  )r  weakrefrefr   assertIsNone)r   r  testerr  s       r   testCellLeakzScopeTests.testCellLeak  s    	 	 	 	 	 	 	 	 

k&!!##%%     r   N),r@   rA   rB   r    r(   r.   r9   rF   rO   r]   rk   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r,  r   r6  r=  rR  rY  rb  rf  rm  rt  r~  r  r  r  r  r  r  r  r   r   r   r   r      s       ( ( (( ( (( ( (' ' '$( ( (( ( (+ + +" " "(#6 #6 #6J	$ 	$ 	$2 2 222 2 24$ $ $      D" " "03 3 3"3 3 3*& & &9 9 9 R R Rh' ' '.  26 6 6 % % %B     \  + + + 3 3 \3.S S S  $  ! ! !# # #&% % %$2 2 204 4 46 6 6! ! !"        ! ! \! ! !r   r   __main__)
unittestr  test.supportr   r   r   TestCaser   r@   mainr   r   r   <module>r     s      9 9 9 9 9 9 9 9 # # # # # #c! c! c! c! c!" c! c! c!L zHMOOOOO r   