@contextmanager
def replace_trace(trace=None):
 oldtrace = sys.gettrace()
 sys.settrace(trace)
 try:
  (yield)
 finally:
  if ((oldtrace is not None) and (not callable(oldtrace)) and hasattr(oldtrace, 'pytrace')):
   oldtrace = oldtrace.pytrace
  sys.settrace(oldtrace)

@ contextmanager def replace_trace ( trace = None ) : oldtrace = sys . gettrace ( ) sys . settrace ( trace ) try : ( yield ) finally : if ( ( oldtrace is not None ) and ( not callable ( oldtrace ) ) and hasattr ( oldtrace , 'pytrace' ) ) : oldtrace = oldtrace . pytrace sys . settrace ( oldtrace )
a context manager that temporary replaces the trace function .