Skip to content

Commit ea7d72f

Browse files
author
Roberto De Ioris
committed
allow to brutally finalize the python vm
1 parent 6966dcc commit ea7d72f

3 files changed

Lines changed: 30 additions & 22 deletions

File tree

Source/UnrealEnginePython/Private/PyCommandlet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ int32 UPyCommandlet::Main(const FString& CommandLine)
9090
PySys_SetArgv(PyArgv.Num(), argv);
9191

9292
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
93+
PythonModule.BrutalFinalize = true;
9394
PythonModule.RunFile(TCHAR_TO_UTF8(*Filepath));
9495
return 0;
9596
}

Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,27 +128,30 @@ static void setup_stdout_stderr() {
128128
}
129129

130130
namespace {
131-
static void consoleExecScript(const TArray<FString>& Args)
132-
{
133-
if (Args.Num() != 1)
134-
{
135-
UE_LOG(LogPython, Warning, TEXT("Usage: 'py.exec <scriptname>'."));
136-
UE_LOG(LogPython, Warning, TEXT(" scriptname: Name of script, must reside in Scripts folder. Ex: myscript.py"));
137-
}
138-
else
139-
{
140-
UPythonBlueprintFunctionLibrary::ExecutePythonScript(Args[0]);
141-
}
142-
}
131+
static void consoleExecScript(const TArray<FString>& Args)
132+
{
133+
if (Args.Num() != 1)
134+
{
135+
UE_LOG(LogPython, Warning, TEXT("Usage: 'py.exec <scriptname>'."));
136+
UE_LOG(LogPython, Warning, TEXT(" scriptname: Name of script, must reside in Scripts folder. Ex: myscript.py"));
137+
}
138+
else
139+
{
140+
UPythonBlueprintFunctionLibrary::ExecutePythonScript(Args[0]);
141+
}
142+
}
143143

144144
}
145145
FAutoConsoleCommand ExecPythonScriptCommand(
146-
TEXT("py.exec"),
147-
*NSLOCTEXT("UnrealEnginePython", "CommandText_Exec", "Execute python script").ToString(),
148-
FConsoleCommandWithArgsDelegate::CreateStatic(consoleExecScript));
146+
TEXT("py.exec"),
147+
*NSLOCTEXT("UnrealEnginePython", "CommandText_Exec", "Execute python script").ToString(),
148+
FConsoleCommandWithArgsDelegate::CreateStatic(consoleExecScript));
149149

150150
void FUnrealEnginePythonModule::StartupModule()
151151
{
152+
153+
BrutalFinalize = false;
154+
152155
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
153156
FString IniValue;
154157
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("Home"), IniValue, GEngineIni)) {
@@ -158,10 +161,10 @@ void FUnrealEnginePythonModule::StartupModule()
158161
char *home = TCHAR_TO_UTF8(*IniValue);
159162
#endif
160163
Py_SetPythonHome(home);
161-
}
164+
}
162165

163166
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("RelativeHome"), IniValue, GEngineIni)) {
164-
IniValue = FPaths::Combine(*FPaths::GameContentDir(), *IniValue);
167+
IniValue = FPaths::Combine(*FPaths::GameContentDir(), *IniValue);
165168
#if PY_MAJOR_VERSION >= 3
166169
wchar_t *home = (wchar_t *)*IniValue;
167170
#else
@@ -207,7 +210,7 @@ void FUnrealEnginePythonModule::StartupModule()
207210

208211
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("ZipPath"), IniValue, GEngineIni)) {
209212
ZipPath = IniValue;
210-
}
213+
}
211214

212215
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("RelativeZipPath"), IniValue, GEngineIni)) {
213216
ZipPath = FPaths::Combine(*FPaths::GameContentDir(), *IniValue);
@@ -262,8 +265,10 @@ void FUnrealEnginePythonModule::ShutdownModule()
262265
// we call this function before unloading the module.
263266

264267
UE_LOG(LogPython, Log, TEXT("Goodbye Python"));
265-
PythonGILAcquire();
266-
Py_Finalize();
268+
if (!BrutalFinalize) {
269+
PythonGILAcquire();
270+
Py_Finalize();
271+
}
267272
}
268273

269274
void FUnrealEnginePythonModule::RunString(char *str) {
@@ -313,7 +318,7 @@ void FUnrealEnginePythonModule::RunStringSandboxed(char *str) {
313318

314319
Py_EndInterpreter(py_new_state);
315320
PyThreadState_Swap(_main);
316-
}
321+
}
317322

318323
void FUnrealEnginePythonModule::RunFile(char *filename) {
319324
FScopePythonGIL gil;
@@ -358,7 +363,7 @@ void FUnrealEnginePythonModule::RunFile(char *filename) {
358363
}
359364

360365
// run a python script in a new sub interpreter (useful for unit tests)
361-
void FUnrealEnginePythonModule::RunFileSandboxed(char *filename, void (*callback)(void *arg), void *arg) {
366+
void FUnrealEnginePythonModule::RunFileSandboxed(char *filename, void(*callback)(void *arg), void *arg) {
362367
FScopePythonGIL gil;
363368
char *full_path = filename;
364369
if (!FPaths::FileExists(filename))

Source/UnrealEnginePython/Public/UnrealEnginePython.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class UNREALENGINEPYTHON_API FUnrealEnginePythonModule : public IModuleInterface
3636
FString ZipPath;
3737
FString AdditionalModulesPath;
3838

39+
bool BrutalFinalize;
40+
3941
private:
4042
void *ue_python_gil;
4143
// used by console

0 commit comments

Comments
 (0)