<s> import imp <EOL> import sys <EOL> import logging <EOL> import importlib <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> __exclude = { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' } <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> class SysModulesSaver : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , module_names = ( ) ) : <EOL> """<STR_LIT>""" <EOL> self . _saved = { } <EOL> imp . acquire_lock ( ) <EOL> self . save ( * module_names ) <EOL> def save ( self , * module_names ) : <EOL> """<STR_LIT>""" <EOL> for modname in module_names : <EOL> self . _saved [ modname ] = sys . modules . get ( modname , None ) <EOL> def restore ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> for modname , mod in self . _saved . items ( ) : <EOL> if mod is not None : <EOL> sys . modules [ modname ] = mod <EOL> else : <EOL> try : <EOL> del sys . modules [ modname ] <EOL> except KeyError : <EOL> pass <EOL> finally : <EOL> imp . release_lock ( ) <EOL> def inject ( module_name , new_globals , * additional_modules ) : <EOL> """<STR_LIT>""" <EOL> patched_name = '<STR_LIT>' + module_name <EOL> if patched_name in sys . modules : <EOL> return sys . modules [ patched_name ] <EOL> if not additional_modules : <EOL> additional_modules = ( <EOL> _green_os_modules ( ) + <EOL> _green_select_modules ( ) + <EOL> _green_socket_modules ( ) + <EOL> _green_thread_modules ( ) + <EOL> _green_time_modules ( ) <EOL> ) <EOL> saver = SysModulesSaver ( [ name for name , m in additional_modules ] ) <EOL> saver . save ( module_name ) <EOL> for name , mod in additional_modules : <EOL> sys . modules [ name ] = mod <EOL> sys . modules . pop ( module_name , None ) <EOL> try : <EOL> module = __import__ ( module_name , { } , { } , module_name . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) <EOL> if new_globals is not None : <EOL> for name in dir ( module ) : <EOL> if name not in __exclude : <EOL> new_globals [ name ] = getattr ( module , name ) <EOL> sys . modules [ patched_name ] = module <EOL> finally : <EOL> saver . restore ( ) <EOL> return module <EOL> def import_patched ( module_name , * additional_modules , ** kw_additional_modules ) : <EOL> """<STR_LIT>""" <EOL> return inject ( module_name , None , * additional_modules + tuple ( kw_additional_modules . items ( ) ) ) <EOL> def patch_function ( func , * additional_modules ) : <EOL> """<STR_LIT>""" <EOL> if not additional_modules : <EOL> additional_modules = ( <EOL> _green_os_modules ( ) + <EOL> _green_select_modules ( ) + <EOL> _green_socket_modules ( ) + <EOL> _green_thread_modules ( ) + <EOL> _green_time_modules ( ) <EOL> ) <EOL> def patched ( * args , ** kw ) : <EOL> saver = SysModulesSaver ( ) <EOL> for name , mod in additional_modules : <EOL> saver . save ( name ) <EOL> sys . modules [ name ] = mod <EOL> try : <EOL> return func ( * args , ** kw ) <EOL> finally : <EOL> saver . restore ( ) <EOL> return patched <EOL> def _original_patch_function ( func , * module_names ) : <EOL> """<STR_LIT>""" <EOL> def patched ( * args , ** kw ) : <EOL> saver = SysModulesSaver ( module_names ) <EOL> for name in module_names : <EOL> sys . modules [ name ] = original ( name ) <EOL> try : <EOL> return func ( * args , ** kw ) <EOL> finally : <EOL> saver . restore ( ) <EOL> return patched <EOL> def original ( modname ) : <EOL> """<STR_LIT>""" <EOL> original_name = '<STR_LIT>' + modname <EOL> if original_name in sys . modules : <EOL> return sys . modules . get ( original_name ) <EOL> saver = SysModulesSaver ( ( modname , ) ) <EOL> sys . modules . pop ( modname , None ) <EOL> deps = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> if modname in deps : <EOL> dependency = deps [ modname ] <EOL> saver . save ( dependency ) <EOL> sys . modules [ dependency ] = original ( dependency ) <EOL> try : <EOL> real_mod = __import__ ( modname , { } , { } , modname . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) <EOL> sys . modules [ original_name ] = real_mod <EOL> finally : <EOL> saver . restore ( ) <EOL> return sys . modules [ original_name ] <EOL> already_patched = { } <EOL> def monkey_patch ( ** modules ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( '<STR_LIT>' ) <EOL> accepted_args = { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:time>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' } <EOL> default_modules = modules . pop ( '<STR_LIT:all>' , None ) <EOL> for k in modules . keys ( ) : <EOL> if k not in accepted_args : <EOL> raise TypeError ( '<STR_LIT>' % k ) <EOL> if default_modules is None : <EOL> default_modules = not ( True in modules . values ( ) ) <EOL> for modname in accepted_args : <EOL> if modname == '<STR_LIT>' : <EOL> modules . setdefault ( modname , False ) <EOL> modules . setdefault ( modname , default_modules ) <EOL> modules_to_patch = [ ] <EOL> if modules [ '<STR_LIT>' ] and not already_patched . get ( '<STR_LIT>' ) : <EOL> modules_to_patch += _green_os_modules ( ) <EOL> already_patched [ '<STR_LIT>' ] = True <EOL> if modules [ '<STR_LIT>' ] and not already_patched . get ( '<STR_LIT>' ) : <EOL> modules_to_patch += _green_select_modules ( ) <EOL> already_patched [ '<STR_LIT>' ] = True <EOL> if modules [ '<STR_LIT>' ] and not already_patched . get ( '<STR_LIT>' ) : <EOL> modules_to_patch += _green_socket_modules ( ) <EOL> already_patched [ '<STR_LIT>' ] = True <EOL> if modules [ '<STR_LIT>' ] and not already_patched . get ( '<STR_LIT>' ) : <EOL> modules_to_patch += _green_thread_modules ( ) <EOL> already_patched [ '<STR_LIT>' ] = True <EOL> if modules [ '<STR_LIT:time>' ] and not already_patched . get ( '<STR_LIT:time>' ) : <EOL> modules_to_patch += _green_time_modules ( ) <EOL> already_patched [ '<STR_LIT:time>' ] = True <EOL> if modules . get ( '<STR_LIT>' ) and not already_patched . get ( '<STR_LIT>' ) : <EOL> modules_to_patch += _green_builtins ( ) <EOL> already_patched [ '<STR_LIT>' ] = True <EOL> if modules [ '<STR_LIT>' ] and not already_patched . get ( '<STR_LIT>' ) : <EOL> try : <EOL> from guv . support import psycopg2_patcher <EOL> psycopg2_patcher . make_psycopg_green ( ) <EOL> already_patched [ '<STR_LIT>' ] = True <EOL> except ImportError : <EOL> pass <EOL> imp . acquire_lock ( ) <EOL> try : <EOL> for name , mod in modules_to_patch : <EOL> log . debug ( '<STR_LIT>' . format ( name , mod ) ) <EOL> orig_mod = sys . modules . get ( name ) <EOL> if orig_mod is None : <EOL> orig_mod = importlib . import_module ( name ) <EOL> for attr_name in mod . __patched__ : <EOL> patched_attr = getattr ( mod , attr_name , None ) <EOL> if patched_attr is not None : <EOL> setattr ( orig_mod , attr_name , patched_attr ) <EOL> finally : <EOL> imp . release_lock ( ) <EOL> if modules [ '<STR_LIT>' ] and not already_patched . get ( '<STR_LIT>' ) : <EOL> try : <EOL> import cassandra . cluster <EOL> from guv . support . cassandra import GuvConnection <EOL> cassandra . cluster . DefaultConnection = GuvConnection <EOL> cassandra . cluster . Cluster . connection_class = GuvConnection <EOL> already_patched [ '<STR_LIT>' ] = True <EOL> except ImportError : <EOL> pass <EOL> def is_monkey_patched ( module ) : <EOL> """<STR_LIT>""" <EOL> return module in already_patched or getattr ( module , '<STR_LIT>' , None ) in already_patched <EOL> def copy_attributes ( source , destination , ignore = [ ] , srckeys = None ) : <EOL> """<STR_LIT>""" <EOL> if srckeys is None : <EOL> srckeys = source . __all__ <EOL> d = { name : getattr ( source , name ) for name in srckeys <EOL> if not ( name . startswith ( '<STR_LIT>' ) or name in ignore ) } <EOL> destination . update ( d ) <EOL> def _green_os_modules ( ) : <EOL> from guv . green import os <EOL> return [ ( '<STR_LIT>' , os ) ] <EOL> def _green_select_modules ( ) : <EOL> from guv . green import select <EOL> return [ ( '<STR_LIT>' , select ) ] <EOL> def _green_socket_modules ( ) : <EOL> from guv . green import socket <EOL> try : <EOL> from guv . green import ssl <EOL> return [ ( '<STR_LIT>' , socket ) , ( '<STR_LIT>' , ssl ) ] <EOL> except ImportError : <EOL> return [ ( '<STR_LIT>' , socket ) ] <EOL> def _green_thread_modules ( ) : <EOL> from guv . green import queue , thread , threading <EOL> return [ ( '<STR_LIT>' , queue ) , ( '<STR_LIT>' , thread ) , ( '<STR_LIT>' , threading ) ] <EOL> def _green_time_modules ( ) : <EOL> from guv . green import time <EOL> return [ ( '<STR_LIT:time>' , time ) ] <EOL> def _green_builtins ( ) : <EOL> try : <EOL> from guv . green import builtin <EOL> return [ ( '<STR_LIT>' , builtin ) ] <EOL> except ImportError : <EOL> return [ ] </s>
