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