From 43a25c3abcdad380ef5fc0284207da052c5e7d98 Mon Sep 17 00:00:00 2001 From: dave Date: Mon, 18 Feb 2019 08:38:30 -0700 Subject: [PATCH] manually applied https://github.com/20tab/UnrealEnginePython/pull/629 --- Source/PythonConsole/Private/SPythonLog.cpp | 9 ++- .../Private/UnrealEnginePython.cpp | 56 ++++++++++++++----- .../Public/UnrealEnginePython.h | 4 +- 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/Source/PythonConsole/Private/SPythonLog.cpp b/Source/PythonConsole/Private/SPythonLog.cpp index 555141be4..15ed11503 100644 --- a/Source/PythonConsole/Private/SPythonLog.cpp +++ b/Source/PythonConsole/Private/SPythonLog.cpp @@ -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(":")) @@ -248,7 +249,8 @@ 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); } } @@ -256,7 +258,8 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T { IsMultiline = false; FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked("UnrealEnginePython"); - PythonModule.RunString(TCHAR_TO_UTF8(*MultilineString)); + FString ret_str = PythonModule.RunString(TCHAR_TO_UTF8(*MultilineString)); + UE_LOG(LogTemp, Log, TEXT("%s"), *ret_str); } } diff --git a/Source/UnrealEnginePython/Private/UnrealEnginePython.cpp b/Source/UnrealEnginePython/Private/UnrealEnginePython.cpp index cf9c438ca..6135bc9ab 100644 --- a/Source/UnrealEnginePython/Private/UnrealEnginePython.cpp +++ b/Source/UnrealEnginePython/Private/UnrealEnginePython.cpp @@ -121,9 +121,9 @@ void FUnrealEnginePythonModule::UESetupPythonInterpreter(bool verbose) for (int32 i = 0; i < Args.Num(); i++) { -#if PY_MAJOR_VERSION >= 3 - argv[i] = (wchar_t *)(*Args[i]); - #if ENGINE_MINOR_VERSION >= 20 + #if PY_MAJOR_VERSION >= 3 + argv[i] = (wchar_t *)(*Args[i]); + #if ENGINE_MINOR_VERSION >= 20 argv[i] = (wchar_t *)(TCHAR_TO_WCHAR(*Args[i])); #else argv[i] = (wchar_t *)(*Args[i]); @@ -550,22 +550,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 @@ -629,7 +642,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); @@ -655,7 +668,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 @@ -665,14 +678,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 @@ -683,12 +696,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); @@ -701,8 +722,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 } diff --git a/Source/UnrealEnginePython/Public/UnrealEnginePython.h b/Source/UnrealEnginePython/Public/UnrealEnginePython.h index a29eaa4e9..5a7d20e1c 100644 --- a/Source/UnrealEnginePython/Public/UnrealEnginePython.h +++ b/Source/UnrealEnginePython/Public/UnrealEnginePython.h @@ -106,8 +106,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 *);