<s> import sys <EOL> import unittest <EOL> from pikos . _internal . profile_function_manager import ProfileFunctionManager <EOL> from pikos . tests . compat import TestCase <EOL> class MockNativeMonitor ( ) : <EOL> def __init__ ( self ) : <EOL> self . _profile = ProfileFunctionManager ( ) <EOL> def __enter__ ( self ) : <EOL> self . _profile . replace ( self . event ) <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> self . _profile . recover ( ) <EOL> def event ( self , frame , event , arg ) : <EOL> pass <EOL> class TestProfileFunctionsManager ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . old = sys . getprofile ( ) <EOL> def foo ( ) : <EOL> pass <EOL> def bar ( frame , event , arg ) : <EOL> pass <EOL> self . foo = foo <EOL> self . bar = bar <EOL> self . monitor = MockNativeMonitor ( ) <EOL> def test_preserving_previous_function ( self ) : <EOL> sys . setprofile ( self . bar ) <EOL> with self . monitor : <EOL> self . foo ( ) <EOL> self . assertIs ( sys . getprofile ( ) , self . bar ) <EOL> def test_error_when_set_multiple ( self ) : <EOL> self . monitor . _profile . replace ( self . bar ) <EOL> self . assertIs ( sys . getprofile ( ) , self . bar ) <EOL> self . monitor . _profile . replace ( self . bar ) <EOL> self . assertIs ( sys . getprofile ( ) , self . bar ) <EOL> self . monitor . _profile . recover ( ) <EOL> self . monitor . _profile . replace ( self . bar ) <EOL> self . assertIs ( sys . getprofile ( ) , self . bar ) <EOL> with self . assertRaises ( RuntimeError ) : <EOL> self . monitor . _profile . replace ( None ) <EOL> self . assertIs ( sys . getprofile ( ) , self . bar ) <EOL> self . monitor . _profile . recover ( ) <EOL> def test_error_when_unset ( self ) : <EOL> with self . assertRaises ( RuntimeError ) : <EOL> self . monitor . _profile . recover ( ) <EOL> def tearDown ( self ) : <EOL> sys . setprofile ( self . old ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
