i know that there is similar question but my efforts to resolve this problem were not successful. I want to redirect Python interpreter I/O, but i have only succeeded to redirect stdout. I have still problem with stdin and stderr. Based on Redirect Embedded Python IO to a console created with AllocConsole i have done this:
PyObject* sys = PyImport_ImportModule("sys");
PyObject* pystdout = PyFile_FromString("CONOUT$", "wt");
if (-1 == PyObject_SetAttrString(sys, "stdout", pystdout)) {
/* raise errors and wail very loud */
}
PyObject* pystdin = PyFile_FromString("CONIN$", "rb");
if (-1 == PyObject_SetAttrString(sys, "stdin", pystdin)) {
/* raise errors and wail very loud */
}
//cout << "no error" << endl;
Py_DECREF(sys);
Py_DECREF(pystdout);
Py_DECREF(pystdin);
and i have simple script for testing purposes:
print 'Hello'
guess = int(raw_input('Take a guess: '))
print quess
When my script is executed only first print is show on console. Second and third commands are not shown at all. So, instead of output:
Hello
Take a guess: "my guess"
"my guess"
i have only
Hello
I would appreciate any help and it needs to be solved using Python C API. Thanks.