From 14deb53bafd17c8d8ed202994caa297cf938eee6 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 12 Dec 2018 18:35:35 +0100 Subject: [PATCH] Restore Windows stdio flags after Python initialisation. Python forcibly sets the file mode for stdin, stdout, and stderr to O_BINARY for several reasons. This causes wprintf() to output UTF-16 instead of going through the proper text conversion. As a result, standard output was all messed up after the module was initialised. The Python developers do not really consider this a bug because they are not familiar with wprintf() (https://bugs.python.org/issue16587). Unfortunately this is what UE4 uses for logging. --- .../Private/UnrealEnginePython.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Source/UnrealEnginePython/Private/UnrealEnginePython.cpp b/Source/UnrealEnginePython/Private/UnrealEnginePython.cpp index bb79b8c8f..8d8e2c1b2 100644 --- a/Source/UnrealEnginePython/Private/UnrealEnginePython.cpp +++ b/Source/UnrealEnginePython/Private/UnrealEnginePython.cpp @@ -34,9 +34,14 @@ const char *ue4_module_options = "linux_global_symbols"; #include "Runtime/Core/Public/Misc/CommandLine.h" #include "Runtime/Core/Public/Misc/ConfigCacheIni.h" #include "Runtime/Core/Public/GenericPlatform/GenericPlatformFile.h" +#include "Runtime/Core/Public/GenericPlatform/GenericPlatformMisc.h" #include "Runtime/Core/Public/HAL/FileManagerGeneric.h" +#if PLATFORM_WINDOWS +#include +#endif + #if PLATFORM_ANDROID #include "Android/AndroidJNI.h" #include "Android/AndroidApplication.h" @@ -447,6 +452,22 @@ void FUnrealEnginePythonModule::StartupModule() Py_Initialize(); +#if PLATFORM_WINDOWS + // Restore stdio state after Py_Initialize set it to O_BINARY, otherwise + // everything that the engine will output is going to be encoded in UTF-16. + // The behaviour is described here: https://bugs.python.org/issue16587 + _setmode(fileno(stdin), O_TEXT); + _setmode(fileno(stdout), O_TEXT); + _setmode(fileno(stderr), O_TEXT); + + // Also restore the user-requested UTF-8 flag if relevant (behaviour copied + // from LaunchEngineLoop.cpp). + if (FParse::Param(FCommandLine::Get(), TEXT("UTF8Output"))) + { + FPlatformMisc::SetUTF8Output(); + } +#endif + PyEval_InitThreads(); #if WITH_EDITOR