@@ -192,30 +192,34 @@ bool CPythonManager::Initialize( void )
192192 // This fix currently only works for dedicated servers, but crashes on listen servers.
193193 if (engine->IsDedicatedServer ()) {
194194 object sys = python::import (" sys" );
195- object io_open = python::import (" io" ).attr (" open" );
196-
197- object stdin_ = sys.attr (" stdin" );
198- if (stdin_.is_none ())
199- {
200- DevMsg (1 , MSG_PREFIX " stdin is None... reconnecting.\n " );
201- sys.attr (" stdin" ) = sys.attr (" __stdin__" ) = io_open (" CONIN$" , " rt" );
202- }
203-
204- object stdout_ = sys.attr (" stdout" );
205- if (stdout_.is_none ())
206- {
207- DevMsg (1 , MSG_PREFIX " stdout is None... reconnecting.\n " );
208- sys.attr (" stdout" ) = sys.attr (" __stdout__" ) = io_open (" CONOUT$" , " wt" );
209- }
210-
211- object stderr_ = sys.attr (" stderr" );
212- if (stderr_.is_none ())
213- {
214- DevMsg (1 , MSG_PREFIX " stderr is None... reconnecting.\n " );
215195
216- // Use CONOUT$, because CONERR$ has no effect:
217- // https://github.com/Source-Python-Dev-Team/Source.Python/issues/237
218- sys.attr (" stderr" ) = sys.attr (" __stderr__" ) = io_open (" CONOUT$" , " wt" );
196+ // Only reconnect the streams if the server was launched with a console (issue #392).
197+ if (extract<bool >(sys.attr (" argv" ).attr (" __contains__" )(str (" -console" )))) {
198+ object io_open = python::import (" io" ).attr (" open" );
199+
200+ object stdin_ = sys.attr (" stdin" );
201+ if (stdin_.is_none ())
202+ {
203+ DevMsg (1 , MSG_PREFIX " stdin is None... reconnecting.\n " );
204+ sys.attr (" stdin" ) = sys.attr (" __stdin__" ) = io_open (" CONIN$" , " rt" );
205+ }
206+
207+ object stdout_ = sys.attr (" stdout" );
208+ if (stdout_.is_none ())
209+ {
210+ DevMsg (1 , MSG_PREFIX " stdout is None... reconnecting.\n " );
211+ sys.attr (" stdout" ) = sys.attr (" __stdout__" ) = io_open (" CONOUT$" , " wt" );
212+ }
213+
214+ object stderr_ = sys.attr (" stderr" );
215+ if (stderr_.is_none ())
216+ {
217+ DevMsg (1 , MSG_PREFIX " stderr is None... reconnecting.\n " );
218+
219+ // Use CONOUT$, because CONERR$ has no effect:
220+ // https://github.com/Source-Python-Dev-Team/Source.Python/issues/237
221+ sys.attr (" stderr" ) = sys.attr (" __stderr__" ) = io_open (" CONOUT$" , " wt" );
222+ }
219223 }
220224 }
221225#endif
0 commit comments