<s> import sys <EOL> import types <EOL> import abc <EOL> from python_toolbox import decorator_tools <EOL> from . base_classes . decorating_context_manager import DecoratingContextManager <EOL> from . context_manager_type import ContextManagerType <EOL> from . self_hook import SelfHook <EOL> class ContextManager ( DecoratingContextManager ) : <EOL> '''<STR_LIT>''' <EOL> __metaclass__ = ContextManagerType <EOL> @ abc . abstractmethod <EOL> def __enter__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> @ abc . abstractmethod <EOL> def __exit__ ( self , exc_type , exc_value , exc_traceback ) : <EOL> '''<STR_LIT>''' <EOL> def __init_lone_manage_context ( self , * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> self . _ContextManager__args = args <EOL> self . _ContextManager__kwargs = kwargs <EOL> self . _ContextManager__generators = [ ] <EOL> def __enter_using_manage_context ( self ) : <EOL> '''<STR_LIT>''' <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _ContextManager__generators = [ ] <EOL> new_generator = self . manage_context ( <EOL> * getattr ( self , '<STR_LIT>' , ( ) ) , <EOL> ** getattr ( self , '<STR_LIT>' , { } ) <EOL> ) <EOL> assert isinstance ( new_generator , types . GeneratorType ) <EOL> self . _ContextManager__generators . append ( new_generator ) <EOL> try : <EOL> generator_return_value = next ( new_generator ) <EOL> return self if ( generator_return_value is SelfHook ) else generator_return_value <EOL> except StopIteration : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def __exit_using_manage_context ( self , exc_type , exc_value , exc_traceback ) : <EOL> '''<STR_LIT>''' <EOL> generator = self . _ContextManager__generators . pop ( ) <EOL> assert isinstance ( generator , types . GeneratorType ) <EOL> if exc_type is None : <EOL> try : <EOL> next ( generator ) <EOL> except StopIteration : <EOL> return <EOL> else : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> if exc_value is None : <EOL> exc_value = exc_type ( ) <EOL> try : <EOL> generator . throw ( exc_type , exc_value , exc_traceback ) <EOL> except StopIteration as stop_iteration : <EOL> return stop_iteration is not exc_value <EOL> except : <EOL> if sys . exc_info ( ) [ <NUM_LIT:1> ] is not exc_value : <EOL> raise <EOL> else : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) </s>
