<s> import gc <EOL> import threading <EOL> import unittest <EOL> from doctest import DocTestSuite <EOL> from test import test_support <EOL> class ThreadingLocalTest ( unittest . TestCase ) : <EOL> def test_derived ( self ) : <EOL> import time <EOL> class Local ( threading . local ) : <EOL> def __init__ ( self ) : <EOL> time . sleep ( <NUM_LIT> ) <EOL> local = Local ( ) <EOL> def f ( i ) : <EOL> local . x = i <EOL> self . assertEqual ( local . x , i ) <EOL> threads = [ ] <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> t = threading . Thread ( target = f , args = ( i , ) ) <EOL> t . start ( ) <EOL> threads . append ( t ) <EOL> for t in threads : <EOL> t . join ( ) <EOL> def test_derived_cycle_dealloc ( self ) : <EOL> class Local ( threading . local ) : <EOL> pass <EOL> locals = None <EOL> passed = [ False ] <EOL> e1 = threading . Event ( ) <EOL> e2 = threading . Event ( ) <EOL> def f ( ) : <EOL> cycle = [ Local ( ) ] <EOL> cycle . append ( cycle ) <EOL> cycle [ <NUM_LIT:0> ] . foo = '<STR_LIT:bar>' <EOL> del cycle <EOL> gc . collect ( ) <EOL> e1 . set ( ) <EOL> e2 . wait ( ) <EOL> passed [ <NUM_LIT:0> ] = all ( not hasattr ( local , '<STR_LIT:foo>' ) for local in locals ) <EOL> t = threading . Thread ( target = f ) <EOL> t . start ( ) <EOL> e1 . wait ( ) <EOL> locals = [ Local ( ) for i in range ( <NUM_LIT:10> ) ] <EOL> e2 . set ( ) <EOL> t . join ( ) <EOL> self . assertTrue ( passed [ <NUM_LIT:0> ] ) <EOL> def test_main ( ) : <EOL> suite = DocTestSuite ( '<STR_LIT>' ) <EOL> try : <EOL> from thread import _local <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> import _threading_local <EOL> local_orig = _threading_local . local <EOL> def setUp ( test ) : <EOL> _threading_local . local = _local <EOL> def tearDown ( test ) : <EOL> _threading_local . local = local_orig <EOL> suite . addTest ( DocTestSuite ( '<STR_LIT>' , <EOL> setUp = setUp , tearDown = tearDown ) <EOL> ) <EOL> suite . addTest ( unittest . makeSuite ( ThreadingLocalTest ) ) <EOL> test_support . run_suite ( suite ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_main ( ) </s>
