5

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)

Community
  • 1
  • 1
Miles
  • 1,858
  • 1
  • 21
  • 34

3 Answers3

4

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.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
1

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!

Radim Cernej
  • 875
  • 1
  • 10
  • 21
  • Can GDB accept scripts at runtime and be included in a release build? – Miles Oct 15 '11 at 00:17
  • guy above had solution: python API had a solution I was blind to because of inexperience in programming: didn't know what a debug trace was. Thanks anyway. – Miles Oct 15 '11 at 00:28
  • In my post I should have emphasized that my suggestion, while very cost-effective, will always suffer from serious drawbacks - like the need to compile/link with debug symbols. In my case it did not matter, in many real-worlds cases it WOULD matter. Beyond that I can only add that python documentation discusses interfacing python and c/c++ in both directions, I vaguely remember a chapter "Embedding Python Interpreter". Too rusty, sorry, Ned above probably pointed you in the right direction. – Radim Cernej Oct 16 '11 at 04:52
0

well i tell you what.. i think the best thing to do is to convert your python script to c++ app. this how you'll have working and useful code to embed in your source.
if your c app is not that big, maybe you prefer to make a c-extension for python and turn the python code to the main app..
either way, i would do it in Cython. you can use the following question to understand how to do this job if you'd like: Convert Python program to C/C++ code?

Community
  • 1
  • 1
RoeeK
  • 1,112
  • 12
  • 23
  • I don't want to simply port my Python code. I'm trying to make it so I can write scripts that are read by my program in real time, in the fashion described in my initial post. – Miles Oct 15 '11 at 00:14