私はpython C++ APIを使用して、C++プログラムからpythonコマンドを実行しています。すべてのpython出力を文字列にキャッチしたいのですが、pythonのstdoutおよびstderr出力をキャッチするために、次のリダイレクトで管理しました。
#python script , redirect_python_stdout_stderr.py
class CatchOutput:
def __init__(self):
self.value = ''
def write(self, txt):
self.value += txt
catchOutput = CatchOutput()
sys.stdout = catchOutput
sys.stderr = catchOutput
#C++ code
PyObject *pModule = PyImport_AddModule("__main__");
PyRun_SimpleString("execfile('redirect_python_stdout_stderr.py')");
PyObject *catcher = PyObject_GetAttrString(pModule,"catchOutput");
PyObject *output = PyObject_GetAttrString(catcher,"value");
char* pythonOutput = PyString_AsString(output);
しかし、pythonsインタープリターの出力もキャッチするために何をすべきかわかりません....