Skip to content

Monkeypatching pre/postcmd does not work as expected. #87

@Gigahawk

Description

@Gigahawk

Monkeypatching Pdb.postcmd is a common way to have pdb automatically run some code between pdb commands.

https://stackoverflow.com/questions/8520550/python-pdb-debugger-disp-equivalent

However, it seems like pdbpp is not calling postcmd/precmd in the same way that regular pdb does.

def wrap(func):
    def _wrap(self, *args, **kwargs):
        result = func(self, *args, **kwargs)
        print("WRAPPED")
        _locals = self.curframe.f_locals
        print({k: v for k, v in _locals.items() if k.startswith("local")})
        return result

    return _wrap


import pdb

pdb.Pdb.precmd = wrap(pdb.Pdb.precmd)


def test_pdb():
    import pdb

    pdb.set_trace()
    local_3 = 3
    local_4 = 4
    return


test_pdb()

When this is run with normal pdb:

[nix-shell:~/repos/ipython-b3d]$ python test.py
WRAPPED
{}
> /home/jasper/repos/ipython-b3d/test.py(20)test_pdb()
-> pdb.set_trace()
(Pdb) n
WRAPPED
{}
WRAPPED
{}
> /home/jasper/repos/ipython-b3d/test.py(21)test_pdb()
-> local_3 = 3
(Pdb) n
WRAPPED
{}
WRAPPED
{'local_3': 3}
> /home/jasper/repos/ipython-b3d/test.py(22)test_pdb()
-> local_4 = 4
(Pdb) n
WRAPPED
{'local_3': 3}
WRAPPED
{'local_3': 3, 'local_4': 4}
> /home/jasper/repos/ipython-b3d/test.py(23)test_pdb()
-> return
(Pdb) c
WRAPPED
{'local_3': 3, 'local_4': 4}

Notice how the precmd is called once prior to running the cmd, and then again after running the cmd (i.e. after the next command on the local_4 = 4, there is first a print showing the frame locals before the assignment, and then a second print after the assignment has completed).

When I run this in an environment with pdbpp:

[jasper@arios:~/repos/ipython-b3d]$ python test.py
[1] > /home/jasper/repos/ipython-b3d/test.py(21)test_pdb()
-> local_3 = 3
(Pdb++) n
WRAPPED
{}
[1] > /home/jasper/repos/ipython-b3d/test.py(22)test_pdb()
-> local_4 = 4
(Pdb++) n
WRAPPED
{'local_3': 3}
[1] > /home/jasper/repos/ipython-b3d/test.py(23)test_pdb()
-> return
(Pdb++) n
WRAPPED
{'local_3': 3, 'local_4': 4}
--Return--
[1] > /home/jasper/repos/ipython-b3d/test.py(23)test_pdb()->None
-> return
(Pdb++) c
WRAPPED
{'local_3': 3, 'local_4': 4}

The precmd is only called once, before the indicated line is actually run, leading to this confusing behavior where variables seem to only show up 1 line after.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions