def wrap_function_for_post_mortem_debugging(session: Session, task: Task) -> None: """Wrap the function for post-mortem debugging.""" task_function = task.function @functools.wraps(task_function) def wrapper(*args: Any, **kwargs: Any) -> None: capman = session.config["pm"].get_plugin("capturemanager") live_manager = session.config["pm"].get_plugin("live_manager") try: task_function(*args, **kwargs) except Exception: # Order is important! Pausing the live object before the capturemanager # would flush the table to stdout and it will be visible in the captured # output. capman.suspend(in_=True) out, err = capman.read() live_manager.pause() if out or err: console.print() if out: console.rule("Captured stdout", style=None) console.print(out) if err: console.rule("Captured stderr", style=None) console.print(err) exc_info = remove_internal_traceback_frames_from_exc_info( sys.exc_info()) console.print() console.rule("Traceback", characters=">", style=None) console.print( render_exc_info(*exc_info, session.config["show_locals"])) post_mortem(exc_info[2]) live_manager.resume() capman.resume() raise task.function = wrapper
def wrap_function_for_tracing(session: Session, task: Task) -> None: """Wrap the task function for tracing.""" _pdb = PytaskPDB._init_pdb("runcall") task_function = task.function # We can't just return `partial(pdb.runcall, task_function)` because (on python < # 3.7.4) runcall's first param is `func`, which means we'd get an exception if one # of the kwargs to task_function was called `func`. @functools.wraps(task_function) def wrapper(*args: Any, **kwargs: Any) -> None: capman = session.config["pm"].get_plugin("capturemanager") live_manager = session.config["pm"].get_plugin("live_manager") # Order is important! Pausing the live object before the capturemanager would # flush the table to stdout and it will be visible in the captured output. capman.suspend(in_=True) out, err = capman.read() live_manager.stop() if out or err: console.print() if out: console.rule("Captured stdout", style=None) console.print(out) if err: console.rule("Captured stderr", style=None) console.print(err) _pdb.runcall(task_function, *args, **kwargs) live_manager.resume() capman.resume() task.function = wrapper