@@ -168,6 +168,41 @@ bool CPythonManager::Initialize( void )
168168 return false ;
169169 }
170170
171+ // Patch for issues #175.
172+ // For unknown reasons the streams might be unable to connect due to io.OpenWrapper failing
173+ // to get their handle from their fileno and internally raise an "OSError":
174+ //
175+ // [WinError 6] The handle is invalid"...
176+ //
177+ // Same error occurs when trying to retrieve a python file object from their descriptor
178+ // using PyFile_FromFd. This is kinda weird since GetStdHandle is returning valid handles for them
179+ // but oh well... reconnecting them seems to fix the issues from my testings.
180+ #ifdef _WIN32
181+ object sys = python::import (" sys" );
182+ object io_open = python::import (" io" ).attr (" open" );
183+
184+ object stdin_ = sys.attr (" stdin" );
185+ if (stdin_.is_none ())
186+ {
187+ DevMsg (1 , MSG_PREFIX " stdin is None... reconnecting.\n " );
188+ sys.attr (" stdin" ) = sys.attr (" __stdin__" ) = io_open (" CONIN$" , " rt" );
189+ }
190+
191+ object stdout_ = sys.attr (" stdout" );
192+ if (stdout_.is_none ())
193+ {
194+ DevMsg (1 , MSG_PREFIX " stdout is None... reconnecting.\n " );
195+ sys.attr (" stdout" ) = sys.attr (" __stdout__" ) = io_open (" CONOUT$" , " wt" );
196+ }
197+
198+ object stderr_ = sys.attr (" stderr" );
199+ if (stderr_.is_none ())
200+ {
201+ DevMsg (1 , MSG_PREFIX " stderr is None... reconnecting.\n " );
202+ sys.attr (" stderr" ) = sys.attr (" __stderr__" ) = io_open (" CONERR$" , " wt" );
203+ }
204+ #endif
205+
171206 // Import the main module file.
172207 DevMsg (1 , MSG_PREFIX " Loading main module...\n " );
173208
0 commit comments