|
1 | 1 | /* Minimal main program -- everything is loaded from the library. */ |
2 | 2 |
|
| 3 | +#define WINDOWS_LEAN_AND_MEAN |
3 | 4 | #include <windows.h> |
| 5 | +#include <fcntl.h> |
| 6 | +#include <sys/stat.h> |
| 7 | + |
4 | 8 | #include "Python.h" |
5 | 9 |
|
6 | 10 | extern int Py_Main(); |
7 | 11 |
|
8 | 12 | int WINAPI WinMain( |
9 | | - HINSTANCE hInstance, // handle to current instance |
10 | | - HINSTANCE hPrevInstance, // handle to previous instance |
11 | | - LPSTR lpCmdLine, // pointer to command line |
12 | | - int nCmdShow // show state of window |
| 13 | + HINSTANCE hInstance, /* handle to current instance */ |
| 14 | + HINSTANCE hPrevInstance, /* handle to previous instance */ |
| 15 | + LPSTR lpCmdLine, /* pointer to command line */ |
| 16 | + int nCmdShow /* show state of window */ |
13 | 17 | ) |
14 | 18 | { |
15 | | - return Py_Main(__argc, __argv); |
| 19 | + int null_file; |
| 20 | + |
| 21 | + /* |
| 22 | + * make sure that the C RTL has valid file descriptors for |
| 23 | + * stdin, stdout, stderr. Use the NUL device if necessary. |
| 24 | + * This allows popen to work under pythonw. |
| 25 | + * |
| 26 | + * When pythonw.exe starts the C RTL function _ioinit is called |
| 27 | + * first. WinMain is called later hence the need to check for |
| 28 | + * invalid handles. |
| 29 | + * |
| 30 | + * Note: FILE stdin, stdout, stderr do not use the file descriptors |
| 31 | + * setup here. They are already initialised before WinMain was called. |
| 32 | + */ |
| 33 | + |
| 34 | + null_file = open("NUL", _O_RDWR); |
| 35 | + |
| 36 | + if (_get_osfhandle(0) == -1) |
| 37 | + dup2(null_file, 0); |
| 38 | + |
| 39 | + if (_get_osfhandle(1) == -1) |
| 40 | + dup2(null_file, 1); |
| 41 | + |
| 42 | + if (_get_osfhandle(2) == -1) |
| 43 | + dup2(null_file, 2); |
| 44 | + |
| 45 | + close(null_file); |
| 46 | + |
| 47 | + return Py_Main(__argc, __argv); |
16 | 48 | } |
0 commit comments