Debug Interactively a python code from another program

128 views Asked by At

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.

1

There are 1 answers

0
J. P. Petersen On BEST ANSWER

From Subprocess documentation on communicate:

Popen.communicate(input=None)

Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.

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 and stderr 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 process stdin.

You might also want to look into the pexpect module here. However it does not have good Windows support yet.