Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support interactive console return value to output #629

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Source/PythonConsole/Private/SPythonLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T
else
{
IsMultiline = false;
PythonModule.RunString(TCHAR_TO_UTF8(*MultilineString));
FString ret_str = PythonModule.RunString(TCHAR_TO_UTF8(*MultilineString));
UE_LOG(LogTemp, Log, TEXT("%s"), *ret_str);
}
}
else if (ExecString.EndsWith(":"))
Expand All @@ -248,15 +249,17 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T
}
else
{
PythonModule.RunString(TCHAR_TO_UTF8(*ExecString));
FString ret_str = PythonModule.RunString(TCHAR_TO_UTF8(*ExecString));
UE_LOG(LogTemp, Log, TEXT("%s"), *ret_str);
}

}
else if (IsMultiline)
{
IsMultiline = false;
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
PythonModule.RunString(TCHAR_TO_UTF8(*MultilineString));
FString ret_str = PythonModule.RunString(TCHAR_TO_UTF8(*MultilineString));
UE_LOG(LogTemp, Log, TEXT("%s"), *ret_str);
}

}
Expand Down
50 changes: 39 additions & 11 deletions Source/UnrealEnginePython/Private/UnrealEnginePython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,22 +547,35 @@ void FUnrealEnginePythonModule::ShutdownModule()
}
}

void FUnrealEnginePythonModule::RunString(char *str)
FString FUnrealEnginePythonModule::RunString(char *str)
{
FScopePythonGIL gil;

PyObject *eval_ret = PyRun_String(str, Py_file_input, (PyObject *)main_dict, (PyObject *)local_dict);
PyObject *eval_ret = PyRun_String(str, Py_single_input, (PyObject *)main_dict, (PyObject *)local_dict);
if (!eval_ret)
{
if (PyErr_ExceptionMatches(PyExc_SystemExit))
{
PyErr_Clear();
return;
return "";
}
unreal_engine_py_log_error();
return;
return "";
}

FString ue4_str_ret;
if (eval_ret != Py_None)
{
PyObject *stringified = PyObject_Str(eval_ret);
if (!stringified)
return "argument cannot be casted to string";

ue4_str_ret = UTF8_TO_TCHAR(UEPyUnicode_AsUTF8(stringified));
}

Py_DECREF(eval_ret);

return ue4_str_ret;
}

#if PLATFORM_MAC
Expand Down Expand Up @@ -626,7 +639,7 @@ FString FUnrealEnginePythonModule::Pep8ize(FString Code)
}


void FUnrealEnginePythonModule::RunFile(char *filename)
FString FUnrealEnginePythonModule::RunFile(char *filename)
{
FScopePythonGIL gil;
FString full_path = UTF8_TO_TCHAR(filename);
Expand All @@ -652,7 +665,7 @@ void FUnrealEnginePythonModule::RunFile(char *filename)
if (!foundFile)
{
UE_LOG(LogPython, Error, TEXT("Unable to find file %s"), UTF8_TO_TCHAR(filename));
return;
return "";
}

#if PY_MAJOR_VERSION >= 3
Expand All @@ -662,14 +675,14 @@ void FUnrealEnginePythonModule::RunFile(char *filename)
if (fopen_s(&fd, TCHAR_TO_UTF8(*full_path), "r") != 0)
{
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), *full_path);
return;
return "";
}
#else
fd = fopen(TCHAR_TO_UTF8(*full_path), "r");
if (!fd)
{
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), *full_path);
return;
return "";
}
#endif

Expand All @@ -680,12 +693,20 @@ void FUnrealEnginePythonModule::RunFile(char *filename)
if (PyErr_ExceptionMatches(PyExc_SystemExit))
{
PyErr_Clear();
return;
return "";
}
unreal_engine_py_log_error();
return;
return "";
}

PyObject *stringified = PyObject_Str(eval_ret);
if (!stringified)
return "argument cannot be casted to string";

FString ue4_string = UTF8_TO_TCHAR(UEPyUnicode_AsUTF8(stringified));
Py_DECREF(eval_ret);

return ue4_string;
#else
// damn, this is horrible, but it is the only way i found to avoid the CRT error :(
FString command = FString::Printf(TEXT("execfile(\"%s\")"), *full_path);
Expand All @@ -698,8 +719,15 @@ void FUnrealEnginePythonModule::RunFile(char *filename)
return;
}
unreal_engine_py_log_error();
return;
return "";
}

PyObject *stringified = PyObject_Str(eval_ret);
if (!stringified)
return "argument cannot be casted to string";

FString ue4_string = UTF8_TO_TCHAR(UEPyUnicode_AsUTF8(stringified));
return ue4_string;
#endif

}
Expand Down
4 changes: 2 additions & 2 deletions Source/UnrealEnginePython/Public/UnrealEnginePython.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ class UNREALENGINEPYTHON_API FUnrealEnginePythonModule : public IModuleInterface
virtual void StartupModule() override;
virtual void ShutdownModule() override;

void RunString(char *);
void RunFile(char *);
FString RunString(char *);
FString RunFile(char *);

#if PLATFORM_MAC
void RunStringInMainThread(char *);
Expand Down