<s> from pypy . objspace . std . dictmultiobject import DictImplementation <EOL> from pypy . objspace . std . dictmultiobject import IteratorImplementation <EOL> from pypy . objspace . std . dictmultiobject import W_DictMultiObject , _is_sane_hash <EOL> from pypy . rlib import jit <EOL> class ModuleCell ( object ) : <EOL> def __init__ ( self , w_value = None ) : <EOL> self . w_value = w_value <EOL> def invalidate ( self ) : <EOL> w_value = self . w_value <EOL> self . w_value = None <EOL> return w_value <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . w_value , ) <EOL> class ModuleDictImplementation ( DictImplementation ) : <EOL> def __init__ ( self , space ) : <EOL> self . space = space <EOL> self . content = { } <EOL> self . unshadowed_builtins = { } <EOL> def getcell ( self , key , make_new = True ) : <EOL> res = self . content . get ( key , None ) <EOL> if res is not None : <EOL> return res <EOL> if not make_new : <EOL> return None <EOL> result = self . content [ key ] = ModuleCell ( ) <EOL> return result <EOL> def add_unshadowed_builtin ( self , name , builtin_impl ) : <EOL> assert isinstance ( builtin_impl , ModuleDictImplementation ) <EOL> self . unshadowed_builtins [ name ] = builtin_impl <EOL> def invalidate_unshadowed_builtin ( self , name ) : <EOL> impl = self . unshadowed_builtins [ name ] <EOL> try : <EOL> cell = impl . content [ name ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> w_value = cell . invalidate ( ) <EOL> cell = impl . content [ name ] = ModuleCell ( w_value ) <EOL> def setitem ( self , w_key , w_value ) : <EOL> space = self . space <EOL> if space . is_w ( space . type ( w_key ) , space . w_str ) : <EOL> return self . setitem_str ( w_key , w_value ) <EOL> else : <EOL> return self . _as_rdict ( ) . setitem ( w_key , w_value ) <EOL> def setitem_str ( self , w_key , w_value , shadows_type = True ) : <EOL> name = self . space . str_w ( w_key ) <EOL> self . getcell ( name ) . w_value = w_value <EOL> if name in self . unshadowed_builtins : <EOL> self . invalidate_unshadowed_builtin ( name ) <EOL> del self . unshadowed_builtins [ name ] <EOL> return self <EOL> def delitem ( self , w_key ) : <EOL> space = self . space <EOL> w_key_type = space . type ( w_key ) <EOL> if space . is_w ( w_key_type , space . w_str ) : <EOL> key = space . str_w ( w_key ) <EOL> cell = self . getcell ( key , False ) <EOL> if cell is None : <EOL> raise KeyError <EOL> cell . invalidate ( ) <EOL> del self . content [ key ] <EOL> return self <EOL> elif _is_sane_hash ( space , w_key_type ) : <EOL> raise KeyError <EOL> else : <EOL> return self . _as_rdict ( ) . delitem ( w_key ) <EOL> def length ( self ) : <EOL> return len ( self . content ) <EOL> def get ( self , w_lookup ) : <EOL> space = self . space <EOL> w_lookup_type = space . type ( w_lookup ) <EOL> if space . is_w ( w_lookup_type , space . w_str ) : <EOL> res = self . getcell ( space . str_w ( w_lookup ) , False ) <EOL> if res is None : <EOL> return None <EOL> return res . w_value <EOL> elif _is_sane_hash ( space , w_lookup_type ) : <EOL> return None <EOL> else : <EOL> return self . _as_rdict ( ) . get ( w_lookup ) <EOL> def iteritems ( self ) : <EOL> return ModuleDictItemIteratorImplementation ( self . space , self ) <EOL> def iterkeys ( self ) : <EOL> return ModuleDictKeyIteratorImplementation ( self . space , self ) <EOL> def itervalues ( self ) : <EOL> return ModuleDictValueIteratorImplementation ( self . space , self ) <EOL> def keys ( self ) : <EOL> space = self . space <EOL> return [ space . wrap ( key ) for key in self . content . iterkeys ( ) ] <EOL> def values ( self ) : <EOL> return [ cell . w_value for cell in self . content . itervalues ( ) ] <EOL> def items ( self ) : <EOL> space = self . space <EOL> return [ space . newtuple ( [ space . wrap ( key ) , cell . w_value ] ) <EOL> for ( key , cell ) in self . content . iteritems ( ) ] <EOL> def _as_rdict ( self ) : <EOL> newimpl = self . space . DefaultDictImpl ( self . space ) <EOL> for k , cell in self . content . iteritems ( ) : <EOL> newimpl . setitem ( self . space . wrap ( k ) , cell . w_value ) <EOL> cell . invalidate ( ) <EOL> for k in self . unshadowed_builtins : <EOL> self . invalidate_unshadowed_builtin ( k ) <EOL> return newimpl <EOL> class ModuleDictKeyIteratorImplementation ( IteratorImplementation ) : <EOL> def __init__ ( self , space , dictimplementation ) : <EOL> IteratorImplementation . __init__ ( self , space , dictimplementation ) <EOL> self . iterator = dictimplementation . content . iterkeys ( ) <EOL> def next_entry ( self ) : <EOL> for key in self . iterator : <EOL> return self . space . wrap ( key ) <EOL> else : <EOL> return None <EOL> class ModuleDictValueIteratorImplementation ( IteratorImplementation ) : <EOL> def __init__ ( self , space , dictimplementation ) : <EOL> IteratorImplementation . __init__ ( self , space , dictimplementation ) <EOL> self . iterator = dictimplementation . content . itervalues ( ) <EOL> def next_entry ( self ) : <EOL> for cell in self . iterator : <EOL> return cell . w_value <EOL> else : <EOL> return None <EOL> class ModuleDictItemIteratorImplementation ( IteratorImplementation ) : <EOL> def __init__ ( self , space , dictimplementation ) : <EOL> IteratorImplementation . __init__ ( self , space , dictimplementation ) <EOL> self . iterator = dictimplementation . content . iteritems ( ) <EOL> def next_entry ( self ) : <EOL> for key , cell in self . iterator : <EOL> return self . space . newtuple ( [ self . space . wrap ( key ) , cell . w_value ] ) <EOL> else : <EOL> return None <EOL> class State ( object ) : <EOL> def __init__ ( self , space ) : <EOL> self . space = space <EOL> self . invalidcell = ModuleCell ( ) <EOL> self . always_invalid_cache = [ ] <EOL> self . neverused_dictimpl = ModuleDictImplementation ( space ) <EOL> class GlobalCacheHolder ( object ) : <EOL> def __init__ ( self , space ) : <EOL> self . cache = None <EOL> state = space . fromcache ( State ) <EOL> self . dictimpl = state . neverused_dictimpl <EOL> def getcache ( self , space , code , w_globals ) : <EOL> implementation = getimplementation ( w_globals ) <EOL> if self . dictimpl is implementation : <EOL> return self . cache <EOL> return self . getcache_slow ( space , code , w_globals , implementation ) <EOL> getcache . _always_inline_ = True <EOL> def getcache_slow ( self , space , code , w_globals , implementation ) : <EOL> state = space . fromcache ( State ) <EOL> if not isinstance ( implementation , ModuleDictImplementation ) : <EOL> cache = state . always_invalid_cache <EOL> if len ( code . co_names_w ) > len ( cache ) : <EOL> cache = [ state . invalidcell ] * len ( code . co_names_w ) <EOL> state . always_invalid_cache = cache <EOL> else : <EOL> cache = [ state . invalidcell ] * len ( code . co_names_w ) <EOL> self . cache = cache <EOL> self . dictimpl = implementation <EOL> return cache <EOL> getcache_slow . _dont_inline_ = True <EOL> def init_code ( code ) : <EOL> code . globalcacheholder = GlobalCacheHolder ( code . space ) <EOL> def get_global_cache ( space , code , w_globals ) : <EOL> from pypy . interpreter . pycode import PyCode <EOL> assert isinstance ( code , PyCode ) <EOL> holder = code . globalcacheholder <EOL> return holder . getcache ( space , code , w_globals ) <EOL> def getimplementation ( w_dict ) : <EOL> if type ( w_dict ) is W_DictMultiObject : <EOL> return w_dict . implementation <EOL> else : <EOL> return None <EOL> def LOAD_GLOBAL ( f , nameindex , * ignored ) : <EOL> cell = f . cache_for_globals [ nameindex ] <EOL> w_value = cell . w_value <EOL> if w_value is None : <EOL> w_value = load_global_fill_cache ( f , nameindex ) <EOL> f . pushvalue ( w_value ) <EOL> LOAD_GLOBAL . _always_inline_ = True <EOL> def find_cell_from_dict ( implementation , name ) : <EOL> if isinstance ( implementation , ModuleDictImplementation ) : <EOL> return implementation . getcell ( name , False ) <EOL> return None <EOL> @ jit . dont_look_inside <EOL> def load_global_fill_cache ( f , nameindex ) : <EOL> name = f . space . str_w ( f . getname_w ( nameindex ) ) <EOL> implementation = getimplementation ( f . w_globals ) <EOL> if isinstance ( implementation , ModuleDictImplementation ) : <EOL> cell = implementation . getcell ( name , False ) <EOL> if cell is None : <EOL> builtin_impl = getimplementation ( f . get_builtin ( ) . getdict ( ) ) <EOL> cell = find_cell_from_dict ( builtin_impl , name ) <EOL> if cell is not None : <EOL> implementation . add_unshadowed_builtin ( name , builtin_impl ) <EOL> if cell is not None : <EOL> f . cache_for_globals [ nameindex ] = cell <EOL> return cell . w_value <EOL> return f . _load_global ( f . getname_w ( nameindex ) ) <EOL> load_global_fill_cache . _dont_inline_ = True </s>
