@@ -128,27 +128,30 @@ static void setup_stdout_stderr() {
128128}
129129
130130namespace {
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}
145145FAutoConsoleCommand 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
150150void 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
269274void 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
318323void 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))
0 commit comments