I'm trying to debug a Python CLI I wrote that can take its arguments from stdin. A simple test case would have the output of
echo "test" | python mytool.py
be equivalent to the output of
python mytool.py test
I'd like to debug some issues with this tool, so I tried to run this:
echo "test" | pdb mytool.py
But I get this output, then pdb exits:
> /path/to/mytool.py(5)<module>()
-> '''
(Pdb) *** NameError: name 'test' is not defined
(Pdb)
The same thing occurs when I add -m python
to the shebang, and if I run pdb.set_trace()
inside the script.
What's going on here?
Another option is to create you own Pdb object, and set there the stdin and stdout. My proof of concept involves 2 terminals, but for sure some work can be merged some kind of very unsecure network server.
Create two fifos:
mkfifo fifo_stdin mkfifo fifo_stdout
In one terminal, open stdout on background, and write to stdin:
cat fifo_stdout & cat > fifo_stdin
import pdb mypdb=pdb.Pdb(stdin=open('fifo_stdin','r'), stdout=open('fifo_stdout','w')) ... mypdb.set_trace() ...
You should be able to use pdb on the first console.
The only drawback is having to use your custom pdb, but some monkey patching at init (PYTHONSTARTUP or similar) can help:
import pdb mypdb=pdb.Pdb(stdin=open('fifo_stdin','r'), stdout=open('fifo_stdout','w')) pdb.set_trace=mydbp.set_trace
You can use another file descriptor. With bash you can create a new file descriptor with:
And then on your python file have something like:
Just runing your script will use that test.txt as input, and you can use stdin on stdin. It can be used as well with pipes if you need.
When you use pdb(or any other python debugger) it acquires
stdin
for debug commands, that's why you getNameError: name 'test' is not defined
.For example this command will quit debugger at the begging of a runtime and you wont get this error(nor interactive debugging) for one run:
Your controlling TTY is still a terminal, right? Use this instead of
pdb.set_trace
.Haven't gotten readline to autocomplete under these circumstances. Up arrow won't work either, or any other of the readline niceties.