Skip to content

Commit ddbc118

Browse files
author
Fredrik Lundh
committed
- this is a tentative checkin of the python#100764 patch (by
Barry Scott). it appears to solve the problem on NT and 2000, but not on Windows 95. in other words, it's better than before, but not per- fect. I'll leave the patch open for now.
1 parent 2a1e060 commit ddbc118

1 file changed

Lines changed: 37 additions & 5 deletions

File tree

PC/WinMain.c

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
11
/* Minimal main program -- everything is loaded from the library. */
22

3+
#define WINDOWS_LEAN_AND_MEAN
34
#include <windows.h>
5+
#include <fcntl.h>
6+
#include <sys/stat.h>
7+
48
#include "Python.h"
59

610
extern int Py_Main();
711

812
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 */
1317
)
1418
{
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);
1648
}

0 commit comments

Comments
 (0)