Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the output of 'l' in python pdb after every command entered

I would like to have the output of the python pdb 'l' command printed to the screen after every command I enter in an interactive debugging session.

Is there a way to setup python pdb to do this?

like image 609
Paul D. Eden Avatar asked Mar 02 '09 14:03

Paul D. Eden


People also ask

How do I display a variable in pdb?

pdb is a fully featured python shell, so you can execute arbitrary commands. locals() and globals() will display all the variables in scope with their values. You can use dir() if you're not interested in the values.

How do you get out of a loop in pdb?

Once you get you pdb prompt . Just hit n (next) 10 times to exit the loop.

How do you continue pdb?

To start execution, you use the continue or c command. If the program executes successfully, you will be taken back to the (Pdb) prompt where you can restart the execution again. At this point, you can use quit / q or Ctrl+D to exit the debugger.

What does pdb Set_trace () do?

There are many different ways to use pdb, but the simplest is by using its pdb. set_trace() function. Wherever you put this line in your code, the Python interpreter will stop and present an interactive debugger command prompt.


2 Answers

One way to do this is to alias your favourite commands to run the command and then l.

e.g.

(Pdb) alias s step ;; l
(Pdb) s
> /usr/lib/python2.5/distutils/core.py(14)<module>()
-> from types import *
 9      # This module should be kept compatible with Python 2.1.
10      
11      __revision__ = "$Id: core.py 38672 2005-03-20 22:19:47Z fdrake $"
12      
13      import sys, os
14  ->  from types import *
15      
16      from distutils.debug import DEBUG
17      from distutils.errors import *
18      from distutils.util import grok_environment_error
19      

In your ~/.pdbrc you can add the aliases so you have them every time:

alias s step ;; l
like image 99
Michael Twomey Avatar answered Nov 15 '22 00:11

Michael Twomey


';;' allow to separate commands


[crchemist@test tmp]$ python t.py
> /home/crchemist/tmp/t.py(7)()
-> a()
(Pdb) p a ;; l
function a at 0xb7e96df4
  2         b = 49 + 45
  3         v = 'fff'
  4         return v
  5
  6     import pdb; pdb.set_trace()
  7  -> a() [EOF]
(Pdb) s ;; l
--Call--
> /home/crchemist/tmp/t.py(1)a()
-> def a():
  1  -> def a():
  2         b = 49 + 45
  3         v = 'fff'
  4         return v
  5
  6     import pdb; pdb.set_trace()
  7     a() [EOF]
(Pdb) s ;; l
> /home/crchemist/tmp/t.py(2)a()
-> b = 49 + 45
  1     def a():
  2  ->     b = 49 + 45
  3         v = 'fff'
  4         return v
  5
  6     import pdb; pdb.set_trace()
  7     a() [EOF]
(Pdb)

like image 35
Mykola Kharechko Avatar answered Nov 15 '22 00:11

Mykola Kharechko