@@ -99,10 +99,14 @@ void FUnrealEnginePythonModule::StartupModule()
9999
100100 UESetupPythonInterpeter (true );
101101
102- PyObject * main_module = PyImport_AddModule (" __main__" );
103- main_dict = PyModule_GetDict (main_module);
102+ main_module = PyImport_AddModule (" __main__" );
103+ main_dict = PyModule_GetDict ((PyObject*) main_module);
104104 local_dict = main_dict;// PyDict_New();
105105
106+ // Redirecting stdout
107+ char const * code = " import sys\r\n class StdoutCatcher :\r\n\t def __init__(self) :\r\n\t\t self.data = ''\r\n\t def flush(self) :\r\n\t\t self.data = ''\r\n\t def write(self, stuff) :\r\n\t\t if(stuff.strip()!=''):\r\n\t\t\t self.data = self.data+'python: '+ stuff\r\n catcher = StdoutCatcher()\r\n sys.stdout = catcher" ;
108+ PyRun_SimpleString (code);
109+
106110 if (PyImport_ImportModule (" ue_site" )) {
107111 UE_LOG (LogPython, Log, TEXT (" ue_site Python module successfully imported" ));
108112 }
@@ -139,6 +143,17 @@ void FUnrealEnginePythonModule::RunString(char *str) {
139143 if (!eval_ret) {
140144 unreal_engine_py_log_error ();
141145 return ;
146+ }else {
147+ // Get stdout output information
148+ PyObject* catcher = PyObject_GetAttrString ((PyObject*)main_module, " catcher" );
149+ PyObject* output = PyObject_GetAttrString (catcher, " data" );
150+ char * buffer = PyString_AsString (output);
151+ UE_LOG (LogPython, Log, TEXT (" %s" ), ANSI_TO_TCHAR (buffer));
152+
153+ PyRun_SimpleString (" sys.stdout.flush()" );
154+ Py_DECREF (catcher);
155+ Py_DECREF (output);
156+
142157 }
143158 Py_DECREF (eval_ret);
144159}
0 commit comments