お返事ありがとうございます:) 私はあなたが何を示唆しているか理解していると思いますが、それは私が必要とするものではありません。正直言って、私は必要なことをするための正しい技法が何か分かりません。PythonをC++で埋め込む - C++プログラムからPythonを実行する
私は実行中に時々いくつかのタスクを実行するためにpythonを呼び出す必要があるプログラムを持っています。 Pythonを呼び出してpythonsをキャッチする関数が必要です。
pythonCallBackFunc(const char* pythonInput)
この関数はこれを行う関数です。例えば
:
pythonCallBackFunc("5**2") needs to print on stdin(or some other file):
PythonResult: 25
pythonCallBackFunc("print 5**2") needs to print on stdin(or some other file):
PythonResult: 25
pythonCallBackFunc("'a'+'b'") needs to print on stdin(or some other file):
PythonResult: 'ab'
pythonCallBackFunc("print 'a'+'b'") needs to print on stdin(or some other file):
PythonResult: ab
pythonCallBackFunc("execfile('temp.py')")
it should print nothing but is needs to run the temp.py script
the next 2 calls need to print the value of result, meaning 4.
pythonCallBackFunc("result = 4")
pythonCallBackFunc("print result")
私の問題は、与えられたコマンド(pythonInput)についてすべてのpython出力をキャッチすることです。私が試した最初のものは、このスクリプトを使用してファイルにPythonのsdtoutとstderrをリダイレクトすることです:
#stdout.py
import sys
saveout = sys.stdout
fsock = open('out.log', 'w')
sys.stdout = fsock
#stdout_close.py
sys.stdout = saveout
fsock.close()
#stdout_close.py
fsock = open('error.log', 'w')
sys.stderr = fsock
とリダイレクトした後、私はそれはOKだったが、この機能は無視
をPy_run_SimpleString機能を使用しましたこのコマンドの種類:
Py_run_SimpleString("'a'+'b'")
出力は空だった....
アレックス
私はそれを取得しません。あなたはeval - boostOutputStrに格納されている "5 * 5 + 9"を返します。値は34で、あなたはcout << "Python:" << boostOutputStrを実行します。 – pyfunc
Pythonを組み込むことは、選択肢があるときには、通常は最適ではない設計であることに注意してください。 http://twistedmatrix.com/users/glyph/rant/extendit.html –