<s> from pypy . conftest import gettestobjspace <EOL> from pypy . objspace . std . celldict import get_global_cache , ModuleCell , ModuleDictImplementation <EOL> from pypy . interpreter import gateway <EOL> class AppTestCellDict ( object ) : <EOL> def setup_class ( cls ) : <EOL> cls . space = gettestobjspace ( ** { "<STR_LIT>" : True } ) <EOL> cls . w_impl_used = cls . space . appexec ( [ ] , """<STR_LIT>""" ) <EOL> def is_in_cache ( space , w_code , w_globals , w_name ) : <EOL> name = space . str_w ( w_name ) <EOL> cache = get_global_cache ( space , w_code , w_globals ) <EOL> index = [ space . str_w ( w_n ) for w_n in w_code . co_names_w ] . index ( name ) <EOL> return space . wrap ( cache [ index ] . w_value is not None ) <EOL> is_in_cache = gateway . interp2app ( is_in_cache ) <EOL> cls . w_is_in_cache = cls . space . wrap ( is_in_cache ) <EOL> stored_builtins = [ ] <EOL> def rescue_builtins ( space ) : <EOL> w_dict = space . builtin . getdict ( ) <EOL> content = { } <EOL> for key , cell in w_dict . implementation . content . iteritems ( ) : <EOL> newcell = ModuleCell ( ) <EOL> newcell . w_value = cell . w_value <EOL> content [ key ] = newcell <EOL> stored_builtins . append ( content ) <EOL> rescue_builtins = gateway . interp2app ( rescue_builtins ) <EOL> cls . w_rescue_builtins = cls . space . wrap ( rescue_builtins ) <EOL> def restore_builtins ( space ) : <EOL> w_dict = space . builtin . getdict ( ) <EOL> if not isinstance ( w_dict . implementation , ModuleDictImplementation ) : <EOL> w_dict . implementation = ModuleDictImplementation ( space ) <EOL> w_dict . implementation . content = stored_builtins . pop ( ) <EOL> restore_builtins = gateway . interp2app ( restore_builtins ) <EOL> cls . w_restore_builtins = cls . space . wrap ( restore_builtins ) <EOL> def test_same_code_in_different_modules ( self ) : <EOL> import sys <EOL> mod1 = type ( sys ) ( "<STR_LIT:abc>" ) <EOL> self . impl_used ( mod1 . __dict__ ) <EOL> glob1 = mod1 . __dict__ <EOL> mod2 = type ( sys ) ( "<STR_LIT:abc>" ) <EOL> self . impl_used ( mod2 . __dict__ ) <EOL> glob2 = mod2 . __dict__ <EOL> def f ( ) : <EOL> return x + <NUM_LIT:1> <EOL> code = f . func_code <EOL> f1 = type ( f ) ( code , glob1 ) <EOL> mod1 . x = <NUM_LIT:1> <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> assert f1 ( ) == <NUM_LIT:2> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> assert f1 ( ) == <NUM_LIT:2> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> mod1 . x = <NUM_LIT:2> <EOL> assert f1 ( ) == <NUM_LIT:3> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> assert f1 ( ) == <NUM_LIT:3> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> f2 = type ( f ) ( code , glob2 ) <EOL> mod2 . x = <NUM_LIT:5> <EOL> assert not self . is_in_cache ( code , glob2 , "<STR_LIT:x>" ) <EOL> assert f2 ( ) == <NUM_LIT:6> <EOL> assert self . is_in_cache ( code , glob2 , "<STR_LIT:x>" ) <EOL> assert f2 ( ) == <NUM_LIT:6> <EOL> assert self . is_in_cache ( code , glob2 , "<STR_LIT:x>" ) <EOL> mod2 . x = <NUM_LIT:7> <EOL> assert f2 ( ) == <NUM_LIT:8> <EOL> assert self . is_in_cache ( code , glob2 , "<STR_LIT:x>" ) <EOL> assert f2 ( ) == <NUM_LIT:8> <EOL> assert self . is_in_cache ( code , glob2 , "<STR_LIT:x>" ) <EOL> def test_override_builtins ( self ) : <EOL> import sys , __builtin__ <EOL> mod1 = type ( sys ) ( "<STR_LIT:abc>" ) <EOL> glob1 = mod1 . __dict__ <EOL> self . impl_used ( mod1 . __dict__ ) <EOL> def f ( ) : <EOL> return len ( x ) <EOL> code = f . func_code <EOL> f1 = type ( f ) ( f . func_code , glob1 ) <EOL> mod1 . x = [ ] <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> assert f1 ( ) == <NUM_LIT:0> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> assert f1 ( ) == <NUM_LIT:0> <EOL> mod1 . x . append ( <NUM_LIT:1> ) <EOL> assert f1 ( ) == <NUM_LIT:1> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> mod1 . len = lambda x : <NUM_LIT:15> <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> mod1 . x . append ( <NUM_LIT:1> ) <EOL> assert f1 ( ) == <NUM_LIT:15> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert f1 ( ) == <NUM_LIT:15> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> del mod1 . len <EOL> mod1 . x . append ( <NUM_LIT:1> ) <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert f1 ( ) == <NUM_LIT:3> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert f1 ( ) == <NUM_LIT:3> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> orig_len = __builtins__ . len <EOL> try : <EOL> __builtins__ . len = lambda x : <NUM_LIT:12> <EOL> mod1 . x . append ( <NUM_LIT:1> ) <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert f1 ( ) == <NUM_LIT:12> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert f1 ( ) == <NUM_LIT:12> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> finally : <EOL> __builtins__ . len = orig_len <EOL> def test_override_builtins2 ( self ) : <EOL> import sys , __builtin__ <EOL> mod1 = type ( sys ) ( "<STR_LIT:abc>" ) <EOL> glob1 = mod1 . __dict__ <EOL> self . impl_used ( mod1 . __dict__ ) <EOL> def f ( ) : <EOL> return l ( x ) <EOL> code = f . func_code <EOL> f1 = type ( f ) ( f . func_code , glob1 ) <EOL> mod1 . x = [ ] <EOL> __builtin__ . l = len <EOL> try : <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT:l>" ) <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> assert f1 ( ) == <NUM_LIT:0> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:l>" ) <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> assert f1 ( ) == <NUM_LIT:0> <EOL> mod1 . x . append ( <NUM_LIT:1> ) <EOL> assert f1 ( ) == <NUM_LIT:1> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:l>" ) <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> del __builtin__ . l <EOL> mod1 . l = len <EOL> mod1 . x . append ( <NUM_LIT:1> ) <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT:l>" ) <EOL> assert f1 ( ) == <NUM_LIT:2> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:l>" ) <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> finally : <EOL> if hasattr ( __builtins__ , "<STR_LIT:l>" ) : <EOL> del __builtins__ . l <EOL> def test_generator ( self ) : <EOL> import sys , __builtin__ <EOL> mod1 = type ( sys ) ( "<STR_LIT:abc>" ) <EOL> glob1 = mod1 . __dict__ <EOL> self . impl_used ( mod1 . __dict__ ) <EOL> def f ( ) : <EOL> yield <NUM_LIT:1> <EOL> yield x <EOL> yield len ( x ) <EOL> code = f . func_code <EOL> f1 = type ( f ) ( f . func_code , glob1 ) <EOL> mod1 . x = [ ] <EOL> gen = f1 ( ) <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> v = gen . next ( ) <EOL> assert v == <NUM_LIT:1> <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> v = gen . next ( ) <EOL> assert v is mod1 . x <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> v = gen . next ( ) <EOL> assert v == <NUM_LIT:0> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> def test_degenerate_to_rdict ( self ) : <EOL> import sys <EOL> mod1 = type ( sys ) ( "<STR_LIT:abc>" ) <EOL> self . impl_used ( mod1 . __dict__ ) <EOL> glob1 = mod1 . __dict__ <EOL> def f ( ) : <EOL> return x + <NUM_LIT:1> <EOL> code = f . func_code <EOL> f1 = type ( f ) ( code , glob1 ) <EOL> mod1 . x = <NUM_LIT:1> <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> assert f1 ( ) == <NUM_LIT:2> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> glob1 [ <NUM_LIT:1> ] = <NUM_LIT:2> <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> assert f1 ( ) == <NUM_LIT:2> <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> def test_degenerate_builtin_to_rdict ( self ) : <EOL> import sys , __builtin__ <EOL> mod1 = type ( sys ) ( "<STR_LIT:abc>" ) <EOL> self . impl_used ( mod1 . __dict__ ) <EOL> glob1 = mod1 . __dict__ <EOL> def f ( ) : <EOL> return len ( x ) <EOL> code = f . func_code <EOL> f1 = type ( f ) ( code , glob1 ) <EOL> mod1 . x = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert f1 ( ) == <NUM_LIT:2> <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT:x>" ) <EOL> assert self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> self . rescue_builtins ( ) <EOL> try : <EOL> __builtin__ . __dict__ [ <NUM_LIT:1> ] = <NUM_LIT:2> <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> assert f1 ( ) == <NUM_LIT:2> <EOL> assert not self . is_in_cache ( code , glob1 , "<STR_LIT>" ) <EOL> finally : <EOL> self . restore_builtins ( ) <EOL> def test_mapping_as_locals ( self ) : <EOL> import sys <EOL> if sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:5> ) or not hasattr ( sys , '<STR_LIT>' ) : <EOL> skip ( "<STR_LIT>" ) <EOL> class M ( object ) : <EOL> def __getitem__ ( self , key ) : <EOL> return key <EOL> def __setitem__ ( self , key , value ) : <EOL> self . result [ key ] = value <EOL> m = M ( ) <EOL> m . result = { } <EOL> exec "<STR_LIT>" in { } , m <EOL> assert m . result == { '<STR_LIT:x>' : '<STR_LIT:m>' } <EOL> exec "<STR_LIT>" in m <EOL> assert m . result == { '<STR_LIT:x>' : '<STR_LIT:m>' , '<STR_LIT:y>' : '<STR_LIT:n>' } <EOL> def test_subclass_of_dict_as_locals ( self ) : <EOL> import sys <EOL> if sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:5> ) or not hasattr ( sys , '<STR_LIT>' ) : <EOL> skip ( "<STR_LIT>" ) <EOL> class M ( dict ) : <EOL> def __getitem__ ( self , key ) : <EOL> return key <EOL> def __setitem__ ( self , key , value ) : <EOL> dict . __setitem__ ( self , key , value ) <EOL> m = M ( ) <EOL> exec "<STR_LIT>" in { } , m <EOL> assert m == { '<STR_LIT:x>' : '<STR_LIT:m>' } <EOL> exec "<STR_LIT>" in m <EOL> assert m == { '<STR_LIT:x>' : '<STR_LIT:m>' , '<STR_LIT:y>' : '<STR_LIT:n>' } </s>
