Skip to content

Commit 43a25c3

Browse files
committedFeb 18, 2019
manually applied 20tab#629
1 parent 4ce60ba commit 43a25c3

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed
 

‎Source/PythonConsole/Private/SPythonLog.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T
238238
else
239239
{
240240
IsMultiline = false;
241-
PythonModule.RunString(TCHAR_TO_UTF8(*MultilineString));
241+
FString ret_str = PythonModule.RunString(TCHAR_TO_UTF8(*MultilineString));
242+
UE_LOG(LogTemp, Log, TEXT("%s"), *ret_str);
242243
}
243244
}
244245
else if (ExecString.EndsWith(":"))
@@ -248,15 +249,17 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T
248249
}
249250
else
250251
{
251-
PythonModule.RunString(TCHAR_TO_UTF8(*ExecString));
252+
FString ret_str = PythonModule.RunString(TCHAR_TO_UTF8(*ExecString));
253+
UE_LOG(LogTemp, Log, TEXT("%s"), *ret_str);
252254
}
253255

254256
}
255257
else if (IsMultiline)
256258
{
257259
IsMultiline = false;
258260
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
259-
PythonModule.RunString(TCHAR_TO_UTF8(*MultilineString));
261+
FString ret_str = PythonModule.RunString(TCHAR_TO_UTF8(*MultilineString));
262+
UE_LOG(LogTemp, Log, TEXT("%s"), *ret_str);
260263
}
261264

262265
}

‎Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

+42-14
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ void FUnrealEnginePythonModule::UESetupPythonInterpreter(bool verbose)
121121

122122
for (int32 i = 0; i < Args.Num(); i++)
123123
{
124-
#if PY_MAJOR_VERSION >= 3
125-
argv[i] = (wchar_t *)(*Args[i]);
126-
#if ENGINE_MINOR_VERSION >= 20
124+
#if PY_MAJOR_VERSION >= 3
125+
argv[i] = (wchar_t *)(*Args[i]);
126+
#if ENGINE_MINOR_VERSION >= 20
127127
argv[i] = (wchar_t *)(TCHAR_TO_WCHAR(*Args[i]));
128128
#else
129129
argv[i] = (wchar_t *)(*Args[i]);
@@ -550,22 +550,35 @@ void FUnrealEnginePythonModule::ShutdownModule()
550550
}
551551
}
552552

553-
void FUnrealEnginePythonModule::RunString(char *str)
553+
FString FUnrealEnginePythonModule::RunString(char *str)
554554
{
555555
FScopePythonGIL gil;
556556

557-
PyObject *eval_ret = PyRun_String(str, Py_file_input, (PyObject *)main_dict, (PyObject *)local_dict);
557+
PyObject *eval_ret = PyRun_String(str, Py_single_input, (PyObject *)main_dict, (PyObject *)local_dict);
558558
if (!eval_ret)
559559
{
560560
if (PyErr_ExceptionMatches(PyExc_SystemExit))
561561
{
562562
PyErr_Clear();
563-
return;
563+
return "";
564564
}
565565
unreal_engine_py_log_error();
566-
return;
566+
return "";
567+
}
568+
569+
FString ue4_str_ret;
570+
if (eval_ret != Py_None)
571+
{
572+
PyObject *stringified = PyObject_Str(eval_ret);
573+
if (!stringified)
574+
return "argument cannot be casted to string";
575+
576+
ue4_str_ret = UTF8_TO_TCHAR(UEPyUnicode_AsUTF8(stringified));
567577
}
578+
568579
Py_DECREF(eval_ret);
580+
581+
return ue4_str_ret;
569582
}
570583

571584
#if PLATFORM_MAC
@@ -629,7 +642,7 @@ FString FUnrealEnginePythonModule::Pep8ize(FString Code)
629642
}
630643

631644

632-
void FUnrealEnginePythonModule::RunFile(char *filename)
645+
FString FUnrealEnginePythonModule::RunFile(char *filename)
633646
{
634647
FScopePythonGIL gil;
635648
FString full_path = UTF8_TO_TCHAR(filename);
@@ -655,7 +668,7 @@ void FUnrealEnginePythonModule::RunFile(char *filename)
655668
if (!foundFile)
656669
{
657670
UE_LOG(LogPython, Error, TEXT("Unable to find file %s"), UTF8_TO_TCHAR(filename));
658-
return;
671+
return "";
659672
}
660673

661674
#if PY_MAJOR_VERSION >= 3
@@ -665,14 +678,14 @@ void FUnrealEnginePythonModule::RunFile(char *filename)
665678
if (fopen_s(&fd, TCHAR_TO_UTF8(*full_path), "r") != 0)
666679
{
667680
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), *full_path);
668-
return;
681+
return "";
669682
}
670683
#else
671684
fd = fopen(TCHAR_TO_UTF8(*full_path), "r");
672685
if (!fd)
673686
{
674687
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), *full_path);
675-
return;
688+
return "";
676689
}
677690
#endif
678691

@@ -683,12 +696,20 @@ void FUnrealEnginePythonModule::RunFile(char *filename)
683696
if (PyErr_ExceptionMatches(PyExc_SystemExit))
684697
{
685698
PyErr_Clear();
686-
return;
699+
return "";
687700
}
688701
unreal_engine_py_log_error();
689-
return;
702+
return "";
690703
}
704+
705+
PyObject *stringified = PyObject_Str(eval_ret);
706+
if (!stringified)
707+
return "argument cannot be casted to string";
708+
709+
FString ue4_string = UTF8_TO_TCHAR(UEPyUnicode_AsUTF8(stringified));
691710
Py_DECREF(eval_ret);
711+
712+
return ue4_string;
692713
#else
693714
// damn, this is horrible, but it is the only way i found to avoid the CRT error :(
694715
FString command = FString::Printf(TEXT("execfile(\"%s\")"), *full_path);
@@ -701,8 +722,15 @@ void FUnrealEnginePythonModule::RunFile(char *filename)
701722
return;
702723
}
703724
unreal_engine_py_log_error();
704-
return;
725+
return "";
705726
}
727+
728+
PyObject *stringified = PyObject_Str(eval_ret);
729+
if (!stringified)
730+
return "argument cannot be casted to string";
731+
732+
FString ue4_string = UTF8_TO_TCHAR(UEPyUnicode_AsUTF8(stringified));
733+
return ue4_string;
706734
#endif
707735

708736
}

‎Source/UnrealEnginePython/Public/UnrealEnginePython.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ class UNREALENGINEPYTHON_API FUnrealEnginePythonModule : public IModuleInterface
106106
virtual void StartupModule() override;
107107
virtual void ShutdownModule() override;
108108

109-
void RunString(char *);
110-
void RunFile(char *);
109+
FString RunString(char *);
110+
FString RunFile(char *);
111111

112112
#if PLATFORM_MAC
113113
void RunStringInMainThread(char *);

0 commit comments

Comments
 (0)