python-remote-pdb icon indicating copy to clipboard operation
python-remote-pdb copied to clipboard

Add `post_mortem()`

Open danielgafni opened this issue 1 year ago • 1 comments

It would be useful to have a post_mortem function like pdb has. This would allow calling remote_pdb right after an exception is raise (if it's raised). Like this:

    try:
        code_which_can_fail()
    except:
        extype, value, tb = sys.exc_info()
        traceback.print_exc()
        remote_pdb.post_mortem(tb)

This is useful in environments where remote_pdb can't be called directly

Something like this would work:

def post_mortem(t=None):
    # handling the default
    if t is None:
        # sys.exc_info() returns (type, value, traceback) if an exception is
        # being handled, otherwise it returns None
        t = sys.exc_info()[2]
    if t is None:
        raise ValueError("A valid traceback must be passed if no " "exception is being handled")

    p = remote_pdb.RemotePdb(
        host=os.environ.get("REMOTE_PDB_HOST", "127.0.0.1"), port=int(os.environ.get("REMOTE_PDB_PORT", "0")),
        # other args
    )
    p.reset()
    p.interaction(None, t)

I can make a PR if this feature is welcome

danielgafni avatar Sep 18 '23 11:09 danielgafni

Found a similar issue at #12, the implementation above is a working post_mortem

danielgafni avatar Sep 18 '23 12:09 danielgafni