Skip to content

Commit d1c7a89

Browse files
committed
setlocale
1 parent 31d55a8 commit d1c7a89

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

Source/UnrealEnginePython/Private/Slate/UEPySPythonEditorViewport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
#include "UEPySPythonEditorViewport.h"
3-
#if ENGINE_MAJOR_VERSION == 5
3+
#if WITH_EDITOR && ENGINE_MAJOR_VERSION == 5
44
#include "UnrealWidget.h"
55
#endif
66

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,9 @@ static PyMethodDef ue_PyUObject_methods[] = {
843843

844844

845845
{ "get_class", (PyCFunction)py_ue_get_class, METH_VARARGS, "" },
846+
#if WITH_EDITOR
846847
{ "class_generated_by", (PyCFunction)py_ue_class_generated_by, METH_VARARGS, "" },
848+
#endif
847849
{ "class_get_flags", (PyCFunction)py_ue_class_get_flags, METH_VARARGS, "" },
848850
{ "class_set_flags", (PyCFunction)py_ue_class_set_flags, METH_VARARGS, "" },
849851
{ "get_obj_flags", (PyCFunction)py_ue_get_obj_flags, METH_VARARGS, "" },

Source/UnrealEnginePython/Private/UObject/UEPyObject.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ PyObject *py_ue_get_class(ue_PyUObject * self, PyObject * args)
3030
Py_RETURN_UOBJECT(self->ue_object->GetClass());
3131
}
3232

33+
#if WITH_EDITOR
3334
PyObject *py_ue_class_generated_by(ue_PyUObject * self, PyObject * args)
3435
{
3536

@@ -45,6 +46,7 @@ PyObject *py_ue_class_generated_by(ue_PyUObject * self, PyObject * args)
4546

4647
Py_RETURN_UOBJECT(u_object);
4748
}
49+
#endif
4850

4951
PyObject *py_ue_class_get_flags(ue_PyUObject * self, PyObject * args)
5052
{

Source/UnrealEnginePython/Private/UObject/UEPyWorld.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ PyObject *py_ue_set_current_level(ue_PyUObject *self, PyObject * args)
316316
if (!level)
317317
return PyErr_Format(PyExc_Exception, "argument is not a ULevel");
318318

319-
#if WITH_EDITOR || ENGINE_MINOR_VERSION < 22
319+
#if WITH_EDITOR || !(ENGINE_MAJOR_VERSION == 5 || (ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 22))
320320

321321
if (world->SetCurrentLevel(level))
322322
Py_RETURN_TRUE;

Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ void FUnrealEnginePythonModule::StartupModule()
262262
{
263263
BrutalFinalize = false;
264264

265+
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 7
266+
// Python 3.7+ changes the C locale which affects functions using C string APIs
267+
// So change the C locale back to its current setting after Py_Initialize has been called
268+
FString CurrentLocale;
269+
270+
if (const char* CurrentLocalePtr = setlocale(LC_ALL, nullptr))
271+
{
272+
CurrentLocale = ANSI_TO_TCHAR(CurrentLocalePtr);
273+
}
274+
#endif
275+
265276
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
266277
FString PythonHome;
267278
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("Home"), PythonHome, GEngineIni))
@@ -523,6 +534,14 @@ void FUnrealEnginePythonModule::StartupModule()
523534
#endif
524535
#endif
525536

537+
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 7
538+
// We call setlocale here to restore the previous state
539+
if (!CurrentLocale.IsEmpty())
540+
{
541+
setlocale(LC_ALL, TCHAR_TO_ANSI(*CurrentLocale));
542+
}
543+
#endif
544+
526545
UESetupPythonInterpreter(true);
527546

528547
main_module = PyImport_AddModule("__main__");

0 commit comments

Comments
 (0)