1

pdb exits with a BdbQuit exception when I try to use it within my script main.py which is run as follows:

cat input_file.txt | python main.py

You can reproduce the error using the following example:

1) Create a file i.txt containing the following:

1 2 3

2) Create a file main.py containing the following:

a, b, c = map(int, input().split())
import pdb; pdb.set_trace()
print(a, b, c)

3) Use the following command:

cat i.txt | python main.py

and you'll get something similar to this

> /path/to/main.py(3)<module>()
-> print(a, b, c)
(Pdb) 
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(a, b, c)
  File "main.py", line 3, in <module>
    print(a, b, c)
  File "/path/to/anaconda3/lib/python3.7/bdb.py", line 88, in trace_dispatch
    return self.dispatch_line(frame)
  File "/path/to/anaconda3/lib/python3.7/bdb.py", line 113, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit

any clue ?

Stoufa
  • 23
  • 7
  • 1
    pdb can't be interactive if you are feeding data to stdin. – jordanm Feb 13 '20 at 17:35
  • 1
    Basically the data from your input_file went to pdb. – jordanm Feb 13 '20 at 17:35
  • Potential solutions at https://stackoverflow.com/questions/3101777/run-pdb-without-stdin-stdout-using-fifo – tdelaney Feb 13 '20 at 17:39
  • @jordanm Yes I know! ... let me reformulate my question, can I use them both at the same time? If not, what should I change and how? – Stoufa Feb 13 '20 at 17:39
  • If you instantiate the debugger as `pdb.Pdb(...)`, you can set alternate stdin/stdout pipes. Several ways to do this are in the linked answer. – tdelaney Feb 13 '20 at 17:52
  • @tdelaney Well, I found an easier solution :) ```sys.stdin = open('/dev/tty')``` Source : https://stackoverflow.com/questions/9178751/use-pdb-set-trace-in-a-script-that-reads-stdin-via-a-pipe – Stoufa Feb 13 '20 at 18:10
  • @Stoufa - That's interesting. I think it works because you've consumed everything you want from stdin before fiddling with pdb. Glad you found that one. – tdelaney Feb 13 '20 at 18:16
  • @Stoufa - I put `pdb.Pdb(None, open('/dev/tty', 'r'), open('/dev/tty', 'w')).set_trace()` before `a, b, c = map(int, input().split())` and was able to step through it. – tdelaney Feb 13 '20 at 18:19
  • @tdelaney perfect ✌️ – Stoufa Feb 13 '20 at 19:34

0 Answers0