I'm writing a python editor with support to debugging. I have to debug interactively a python code from my application like a IDE, but without many options.
I know bdb and pdb, but I have to execute this script saved into a file and send commands like step over, continue, quit, etc.
I'm trying something using subprocess lib with pdb but I didn't achive a good result.
p = subprocess.Popen(args=[sys.executable, '-m', 'pdb', 'mide.py'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
p.communicate('s')
p.communicate('s')# I know why this line doesn't work, it's just a example how i wanted to do it.
How to make this works in python 3.x? I just need a path to follow, but preferably just with python 3 without external dependences.
P.S. I'm using PyQt5.
From Subprocess documentation on
communicate
:So when
communicate
is called it will wait for the process to terminate. It can not be called a second time because the process has already terminated.You should probably instead read directly from the
stdout
andstderr
yourself. This has to be done in different threads, or you have to poll once in a while to see if data is available. Each command can take an unknown time to be executed by pdb, so you don't know when the output will be ready. You send command by writing to the processstdin
.You might also want to look into the
pexpect
module here. However it does not have good Windows support yet.