Tôi chủ yếu cố gắng viết giao diện điều khiển với đầu vào và đầu ra cho tập lệnh python được nhúng. Theo các hướng dẫn here, tôi đã có thể nắm bắt stdout:Chuyển hướng không đồng bộ stdout/stdin từ python được nhúng sang C++?
Py_Initialize();
PyRun_SimpleString("\
class StdoutCatcher:\n\
def __init__(self):\n\
self.data = ''\n\
def write(self, stuff):\n\
self.data = self.data + stuff\n\
import sys\n\
sys.stdout = StdoutCatcher()");
PyRun_SimpleString("some script");
PyObject *sysmodule;
PyObject *pystdout;
PyObject *pystdoutdata;
char *string;
sysmodule = PyImport_ImportModule("sys");
pystdout = PyObject_GetAttrString(sysmodule, "stdout");
pystdoutdata = PyObject_GetAttrString(pystdout, "data");
stdoutstring = PyString_AsString(pystdoutdata);
Py_Finalize();
Vấn đề ở đây là tôi chỉ nhận được những stdout sau kịch bản đã chạy xong, trong khi lý tưởng cho một giao diện điều khiển các stdoutstring sẽ cập nhật như python script cập nhật nó. Có cách nào để làm việc này không?
Ngoài ra, làm thế nào tôi sẽ đi về chụp stdin?
Nếu có ích, tôi đang làm việc với trình biên dịch chấp nhận Mục tiêu-C. Tôi cũng có sẵn thư viện tăng cường.
Tôi đã tìm ra phần stdout của câu hỏi. Cho hậu thế, công trình này:
static PyObject*
redirection_stdoutredirect(PyObject *self, PyObject *args)
{
const char *string;
if(!PyArg_ParseTuple(args, "s", &string))
return NULL;
//pass string onto somewhere
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef RedirectionMethods[] = {
{"stdoutredirect", redirection_stdoutredirect, METH_VARARGS,
"stdout redirection helper"},
{NULL, NULL, 0, NULL}
};
//in main...
Py_Initialize();
Py_InitModule("redirection", RedirectionMethods);
PyRun_SimpleString("\
import redirection\n\
import sys\n\
class StdoutCatcher:\n\
def write(self, stuff):\n\
redirection.stdoutredirect(stuff)\n\
sys.stdout = StdoutCatcher()");
PyRun_SimpleString("some script");
Py_Finalize();
Vẫn gặp sự cố với thiết bị nhập chuẩn ...
PS. cảm ơn các giải pháp của bạn ở trên, tôi thấy chúng khá hữu ích! – Dave