<s> """<STR_LIT>""" <EOL> import sys <EOL> from functools import wraps <EOL> def is_same_array ( a , b ) : <EOL> """<STR_LIT>""" <EOL> if not a . flags [ '<STR_LIT>' ] and not b . flags [ '<STR_LIT>' ] : <EOL> return a . base is b . base <EOL> if not a . flags [ '<STR_LIT>' ] and b . flags [ '<STR_LIT>' ] : <EOL> return a . base is b <EOL> if not b . flags [ '<STR_LIT>' ] and a . flags [ '<STR_LIT>' ] : <EOL> return b . base is a <EOL> return a is b <EOL> class surrogate ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path ) : <EOL> self . path = path <EOL> self . elements = self . path . split ( '<STR_LIT:.>' ) <EOL> def __call__ ( self , func ) : <EOL> @ wraps ( func ) <EOL> def _wrapper ( * args , ** kwargs ) : <EOL> prepared = self . prepare ( ) <EOL> result = func ( * args , ** kwargs ) <EOL> if prepared : <EOL> self . restore ( ) <EOL> return result <EOL> return _wrapper <EOL> @ property <EOL> def nothing_to_stub ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . elements ) == <NUM_LIT:0> <EOL> def prepare ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _determine_existing_modules ( ) <EOL> if self . nothing_to_stub : <EOL> return False <EOL> self . _create_module_stubs ( ) <EOL> self . _save_base_module ( ) <EOL> self . _add_module_stubs ( ) <EOL> return True <EOL> def restore ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _remove_module_stubs ( ) <EOL> self . _restore_base_module ( ) <EOL> def _get_importing_path ( self , elements ) : <EOL> """<STR_LIT>""" <EOL> ip = '<STR_LIT:.>' . join ( elements ) <EOL> if self . known_path : <EOL> ip = self . known_path + '<STR_LIT:.>' + ip <EOL> return ip <EOL> def _create_module_stubs ( self ) : <EOL> """<STR_LIT>""" <EOL> last_module = type ( self . elements [ - <NUM_LIT:1> ] , ( object , ) , { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : self . _get_importing_path ( self . elements ) } ) <EOL> modules = [ last_module ] <EOL> for element in reversed ( self . elements [ : - <NUM_LIT:1> ] ) : <EOL> next_module = modules [ - <NUM_LIT:1> ] <EOL> module = type ( element , ( object , ) , { <EOL> next_module . __name__ : next_module , <EOL> '<STR_LIT>' : [ next_module . __name__ ] } ) <EOL> modules . append ( module ) <EOL> self . modules = list ( reversed ( modules ) ) <EOL> self . modules [ <NUM_LIT:0> ] . __path__ = [ ] <EOL> def _determine_existing_modules ( self ) : <EOL> """<STR_LIT>""" <EOL> known = <NUM_LIT:0> <EOL> while known < len ( self . elements ) and '<STR_LIT:.>' . join ( self . elements [ : known + <NUM_LIT:1> ] ) in sys . modules : <EOL> known += <NUM_LIT:1> <EOL> self . known_path = '<STR_LIT:.>' . join ( self . elements [ : known ] ) <EOL> self . elements = self . elements [ known : ] <EOL> def _save_base_module ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . base_module = sys . modules [ self . known_path ] <EOL> except KeyError : <EOL> self . base_module = None <EOL> self . base_all = [ ] <EOL> if hasattr ( self . base_module , '<STR_LIT>' ) : <EOL> self . base_all = list ( self . base_module . __all__ ) <EOL> if self . base_module : <EOL> self . base_module . __all__ = self . base_all + [ self . elements [ <NUM_LIT:0> ] ] <EOL> setattr ( self . base_module , self . elements [ <NUM_LIT:0> ] , self . modules [ <NUM_LIT:0> ] ) <EOL> def _add_module_stubs ( self ) : <EOL> """<STR_LIT>""" <EOL> for i , module in enumerate ( self . modules ) : <EOL> module . _importing_path = self . _get_importing_path ( self . elements [ : i + <NUM_LIT:1> ] ) <EOL> sys . modules [ module . _importing_path ] = module <EOL> def _remove_module_stubs ( self ) : <EOL> """<STR_LIT>""" <EOL> for module in reversed ( self . modules ) : <EOL> if module . _importing_path in sys . modules : <EOL> del sys . modules [ module . _importing_path ] <EOL> def _restore_base_module ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . base_module : <EOL> self . base_module . __all__ = self . base_all <EOL> if not self . base_all : <EOL> del self . base_module . __all__ <EOL> if hasattr ( self . base_module , self . elements [ <NUM_LIT:0> ] ) : <EOL> delattr ( self . base_module , self . elements [ <NUM_LIT:0> ] ) </s>
