Skip to content

Commit

Permalink
manually applied 20tab#629
Browse files Browse the repository at this point in the history
  • Loading branch information
dfb committed Feb 18, 2019
1 parent 4ce60ba commit 43a25c3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
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
56 changes: 42 additions & 14 deletions Source/UnrealEnginePython/Private/UnrealEnginePython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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);
Expand All @@ -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

}
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 @@ -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 *);
Expand Down

0 comments on commit 43a25c3

Please sign in to comment.