<s> from __future__ import absolute_import <EOL> from sentry . utils . imports import import_string <EOL> class PatchContext ( object ) : <EOL> def __init__ ( self , target , callback ) : <EOL> target , attr = target . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> target = import_string ( target ) <EOL> self . func = getattr ( target , attr ) <EOL> self . target = target <EOL> self . attr = attr <EOL> self . callback = callback <EOL> def __enter__ ( self ) : <EOL> self . patch ( ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> self . unpatch ( ) <EOL> def patch ( self ) : <EOL> func = getattr ( self . target , self . attr ) <EOL> def wrapped ( * args , ** kwargs ) : <EOL> __traceback_hide__ = True <EOL> return self . callback ( self . func , * args , ** kwargs ) <EOL> wrapped . __name__ = func . __name__ <EOL> if hasattr ( func , '<STR_LIT>' ) : <EOL> wrapped . __doc__ = func . __doc__ <EOL> if hasattr ( func , '<STR_LIT>' ) : <EOL> wrapped . __module__ = func . __module__ <EOL> setattr ( self . target , self . attr , wrapped ) <EOL> def unpatch ( self ) : <EOL> setattr ( self . target , self . attr , self . func ) </s>
