views:

392

answers:

1

I am having some trouble getting Python IO redirected to a console that I've allocated for my Win32 app. Is there a Python-specific stream that I need to redirect?

Here's more-or-less what I'm doing now (error checking removed, etc.):

int __stdcall WinMain(/*Usual stuff here*/) {
    // Create the console
    AllocConsole();
    SetConsoleTitle(L"My Console");

    // Redirect Standard IO Streams to the new console
    freopen("CONOUT$","w",stdout);
    freopen("CONOUT$","w",stderr);
    freopen("CONIN$","r",stdin);

    // Test the console:
    printf("This Works.\r\n");
    cout << "So Does this" << endl;

    // Python Stuff (This is where it fails)
    Py_Initialize();
    PyRun_SimpleString("print('I don't work.')\n");
    Py_Finalize();
}

If I run the same thing but as a console app (Visual Studio 05, BTW) and remove the AllocConsole call everything works. Anyone know what I'm missing?

EDIT: Just for clarification, I am looking for a way to do it from the C API.

YET ANOTHER EDIT: Alex's solution is correct, but for anyone out there using Python 3.x you'll probably notice that the PyFile_FromString function is missing in the new API. While it may not be the best alternative, I found that this works fine in Python 3.x:

PyObject* sys = PyImport_ImportModule("sys");
PyObject* io = PyImport_ImportModule("io");
PyObject* pystdout = PyObject_CallMethod(io, "open", "ss", "CONOUT$", "wt");
if (-1 == PyObject_SetAttrString(sys, "stdout", pystdout)) {
    /* Announce your error to the world */
}
Py_DECREF(sys);
Py_DECREF(io);
Py_DECREF(pystdout);
+2  A: 

Set sys.stdout on the Python side (presumably to an open('CONOUT$', 'wt')) to make Python's print work, and similarly for sys.stderr and sys.stdin. (There are faster ways to make this happen from a C extension, but the simplest way is to just execute the Python statements, with a import sys in front;-).

Why: because Python's runtime, on startup, found the standard FDs closed, set sys.stdout and friends accordingly, and is not going to check again and set them differently -- so you just set them yourself, explicitly, and it will be fine.

If you're keen to do it all at C-API level, it will take a few lines, but of course it can be done...

PyObject* sys = PyImport_ImportModule("sys");
PyObject* pystdout = PyFile_FromString("CONOUT$", "wt");
if (-1 == PyObject_SetAttrString(sys, "stdout", pystdout)) {
  /* raise errors and wail very loud */
}
Py_DECREF(sys);
Py_DECREF(pystdout);

this is the exact equivalent of the single Python line:

sys.stdout = open('CONOUT$', 'wt')
Alex Martelli
Thanks, I appreciate the hint, but I guess I should have been more explicit: I am looking for a way to do this via the C api. The documentation for the file/sys API's is, well, spotty at best. As such I haven't had much luck figuring it out on my own.
Toji
OK, let me edit the answer then.
Alex Martelli
There -- showed how to translate the one Python line into 7+ lines with the C API (maybe more, depending on error-diagnosis reqs). (See why experienced Python and C-API coders often suggest just running the Python code string from C instead?-).
Alex Martelli
I do, and I prefer it that way too. In this case the need to do it in the C API is due to security concerns, so there's not much I can do about it. Thank you very much!
Toji