How to use the 'debug' command inside pdb (python) - python

I would like to know how to use the debug command in pdb?
(Pdb) help
Documented commands (type help <topic>):
========================================
EOF c d h list q rv undisplay
a cl debug help ll quit s unt
alias clear disable ignore longlist r source until
args commands display interact n restart step up
b condition down j next return tbreak w
break cont enable jump p retval u whatis
bt continue exit l pp run unalias where
Miscellaneous help topics:
==========================
pdb exec
(Pdb) help debug
debug code
Enter a recursive debugger that steps through the code
argument (which is an arbitrary expression or statement to be
executed in the current environment).
(Pdb) debug print('hello')
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()->None
((Pdb)) n
hello
--Return--
> <string>(1)<module>()->None
((Pdb)) n
LEAVING RECURSIVE DEBUGGER
(Pdb)

The question confused me years. I always forgot to search the final answer. But today I get it, and share what I find here.
How can I debug manually typed expression and statements in pdb?
When you are in the recursive debug mode first time, type s , you will kwon what to do the next.

Let, you have a bunch of code. You put pdb, say line 3.
In this case, when you run the program, line 1 and line 2 is executed automatically and you can see the result by putting the variable name and from line 4 is not executed.
If you wanna see after line 3 result you have to write code which results you want to see or you can go next line using n, and c for continuing that means exit from debugging mode.

Related

How to run Anki-Qt script without requiring a breakpoint?

If I run the toy script below as shown:
import sys
sys.path.append('/usr/share/anki')
import aqt
app = aqt._run(argv=['/usr/bin/anki', '--profile=test'], exec=False)
# breakpoint()
print(repr(aqt.mw.col))
aqt.mw.cleanupAndExit()
...I get the following output, which is not right:
$ python3 /tmp/ankiq.py
None
If I uncomment the commented statement, however, and re-run the modified script, I get the correct output (eventually):
$ python3 /tmp/ankiq.py
> /tmp/ankiq.py(8)<module>()
-> print(repr(aqt.mw.col))
(Pdb) c
<anki.collection._Collection object at 0x7f32ec1417b8>
I would like to avoid the need for the breakpoint() statement (and for having to hit c whenever I want to run such code).
My guess is that, when the breakpoint() statement is commented out, the print statement happens before aqt.mw has been fully initialized.
(I tried replacing the breakpoint() statement with time.sleep(1), but when I run the script with this modification, it hangs before ever printing any output.)
Q: How can I modify the toy script above so that, by the time the print statement executes, aqt.mw.col has its correct value?
It seems that calling aqt._run(*args, exec=False) returns a QApplication object - but without starting its event-loop. To manually process pending events, you could therefore try calling app.processEvents().
From the comments, it appears the exact solution is as follows:
while aqt.mw.col is None:
app.processEvents()

Why do my pdb breakpoints disappear when the debugger goes to another file?

I think I figured how this is happening but I just want to know why it's happening.
I accidentally found that every time I call pdb.set_trace(), it seems to erase all the breakpoints that I've set. I was just wondering why that is. I couldn't find any explanation on Google. I had set several breakpoints but midway through debugging I noticed they had all disappeared. It wasn't until I ran a simple script with 2 pdb.set_trace() statements did I notice that if I ran pdb.set_trace(), breakpoints seemed to get deleted. Hopefully someone has an explanation for me.
Example pseudocode:
import pdb
pdb.set_trace()
print (a)
print (b)
print (c)
print (d)
pdb.set_trace()
print (e)
print (f)
At print (a), I set a break point for line print (c) and line
(print f).
I hit 'b' so that it shows my breakpoints (both of which
are there)
I hit 'c' for it to continue, which it does until print
(c), where it stops. I check again and the 2 breakpoints are still
there I hit 'c' and it proceeds until it reaches the second
pdb.set_trace().
At this point when I check again, I have 0
breakpoints.
After playing around some more with this, it seems like every time I execute pdb.set_trace() in my script, it will reset the breakpoints in the script, which is why they disappear like this. If I just have 1 pdb.set_trace() line in my script, the breakpoints appear to stick until the end of the execution of the script so I will be more mindful and not put multiple pdb.set_trace() statements in my script when debugging.

Does pdb have a "comm" command like gdb?

In GDB, users can define some actions that run automatically when a breakpoint is hit. For example,
b foo.cpp:100
comm 1
p x
end
Does pdb have the similar function?
Yes. In https://docs.python.org/2/library/pdb.html, you can find:
commands [bpnumber]
Specify a list of commands for breakpoint number bpnumber. The commands themselves appear on the following lines. Type a line containing just ‘end’ to terminate the commands

setting breakpoint in pdb python

I want to set breakpoints using pdb and then run the loop in the program until that breakpoint. and then after checking the values keep continuing (only stopping at that point) until the loop ends. how do i do it?
You can run your program into pdb from the command line by running
python -m pdb your_script.py
This will stop execution at line 1, now set breakpoint with b linenumber e.g. b 25. Now type run it will run the program until that break point.
If you can modify the code, one way is to add this line where you want to break:
import pdb; pdb.set_trace()

Python call makefile compiling and capture make's output

A job in Jenkins calls my python script, in which I want to call make to compile my UT codes, and raise sys.exit(1) if a compiling error like "make: *** [ ] Error 1" occurs.
I also need to print output in real time.
Here's my python script:
make_process = subprocess.Popen("make clean all;", shell=True, stdout=subprocess.PIPE, stderr=sys.stdout.fileno())
while True:
line = make_process.stdout.readline()
if not line:break
print line, #output to console in time
sys.stdout.flush()
But how do I capture the make error and let this python script fail?
Note:
make_process.wait() is 0 when make error happens.
this answer doesn't work for me:
Running shell command from Python and capturing the output
Update:
It turned out to be a makefile issue. See the comments on the accepted answer. But for this python question, pentadecagon gave the right answer.
You can check the return value of the make process by
make_process.poll()
This returns "None" if the process is still running, or the error code if it's finished. If you just want the output to end up on the console there is no need to do this manually
The output goes to the console anyway, and can do it like this:
make_process = subprocess.Popen("make clean all", stderr=subprocess.STDOUT)
if make_process.wait() != 0:
something_went_wrong();

Categories

Resources