My goal is ultimately to execute a python script that manipulates values in my C++ program, one line at a time, returning execution to my C++ program between statements in the python script. Right now, I'm been attempting to feed the python interpreter my script one line at a time. But it wants a context, and I have no idea how to construct that. Can anyone point me to some good tutorials (the documentation is not very good for this).
I chose the answer that most closely answered my question, but I believe this may not be enough control for some applications. An answer that works for those applications might involve lower-level calls in the Python API. Please answer the question if you have an answer that grants more control over execution.
I Asked another question following this one, because I encountered different problems afterwards which are very closely related. Link: Python C API - Stopping Execution (and continuing it later)
I think it will be difficult to feed a script one line at a time. Look into sys.settrace()
to set a function that is invoked at each line of execution. You can also set it in the C API using PyEval_SetTrace
, in a slightly different form.
I am not 100% sure that I fully understand your goal, but some 8 months ago I wanted to do something similar. I wanted to be able to drive my c++ application from python scripts. I was on win32, Qt, gcc. It turns out that these days gdb, the debugger for gcc, can be scripted via python. It took me a lot of reading and few days of work, but it worked well. I did not have to add any extra code into my c++ sources!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With