CppLearner
CppLearner

Reputation: 17040

How do you permenantly set number of lines shown in pdb?

I can use l during run time to show more lines before and after the current line to be executed in pdb, but can we do this permanently in a script or via command-line option?

ywong:tmp ywong$ python tests.py
> /private/tmp/tests.py(23)<module>()
-> unittest.main()
(Pdb) l
 18             self.assertEqual(1,1)
 19
 20     if __name__ == "__main__":
 21         import pdb
 22         pdb.set_trace()
 23  ->     unittest.main()
 24         #unittest.main(testRunner=MyRunner)
[EOF]
(Pdb)

Upvotes: 2

Views: 229

Answers (1)

Raghav RV
Raghav RV

Reputation: 4076

You could set up you own function which creates a custom debugger by instantiating the pdb.Pdb object and executing the list command before invoking the pdb interactive prompt.

You can create the custom debugger invocation function as follows :

import pdb, sys

def auto_list_debug():
    # Create an instance of the Pdb class
    my_pdb = pdb.Pdb()
    my_pdb.reset()

    # Execute your list command before invoking the interactive prompt
    my_pdb.cmdqueue.append('l')

    # Invoke the interactive prompt with the current frame
    my_pdb.interaction(sys._getframe().f_back, None)

Use this function instead of pdb.set_trace()

for i in range(5):
    auto_list_debug()

Upvotes: 1

Related Questions