Skip to content

Commit a639bd4

Browse files
author
Roberto De Ioris
committed
fixed py_exec() #336
1 parent 7dccc39 commit a639bd4

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -488,30 +488,30 @@ void FUnrealEnginePythonModule::RunStringSandboxed(char *str)
488488
void FUnrealEnginePythonModule::RunFile(char *filename)
489489
{
490490
FScopePythonGIL gil;
491-
char *full_path = filename;
491+
FString full_path = UTF8_TO_TCHAR(filename);
492492
if (!FPaths::FileExists(filename))
493493
{
494-
full_path = TCHAR_TO_UTF8(*FPaths::Combine(*ScriptsPath, UTF8_TO_TCHAR(filename)));
494+
full_path = FPaths::Combine(*ScriptsPath, full_path);
495495
}
496496
#if PY_MAJOR_VERSION >= 3
497497
FILE *fd = nullptr;
498498

499499
#if PLATFORM_WINDOWS
500-
if (fopen_s(&fd, full_path, "r") != 0)
500+
if (fopen_s(&fd, TCHAR_TO_UTF8(*full_path), "r") != 0)
501501
{
502-
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), UTF8_TO_TCHAR(full_path));
502+
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), *full_path);
503503
return;
504504
}
505505
#else
506-
fd = fopen(full_path, "r");
506+
fd = fopen(TCHAR_TO_UTF8(*full_path), "r");
507507
if (!fd)
508508
{
509-
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), UTF8_TO_TCHAR(full_path));
509+
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), *full_path);
510510
return;
511511
}
512512
#endif
513513

514-
PyObject *eval_ret = PyRun_File(fd, full_path, Py_file_input, (PyObject *)main_dict, (PyObject *)local_dict);
514+
PyObject *eval_ret = PyRun_File(fd, TCHAR_TO_UTF8(*full_path), Py_file_input, (PyObject *)main_dict, (PyObject *)local_dict);
515515
fclose(fd);
516516
if (!eval_ret)
517517
{
@@ -521,7 +521,7 @@ void FUnrealEnginePythonModule::RunFile(char *filename)
521521
Py_DECREF(eval_ret);
522522
#else
523523
// damn, this is horrible, but it is the only way i found to avoid the CRT error :(
524-
FString command = FString::Printf(TEXT("execfile(\"%s\")"), UTF8_TO_TCHAR(full_path));
524+
FString command = FString::Printf(TEXT("execfile(\"%s\")"), *full_path);
525525
PyObject *eval_ret = PyRun_String(TCHAR_TO_UTF8(*command), Py_file_input, (PyObject *)main_dict, (PyObject *)local_dict);
526526
if (!eval_ret)
527527
{
@@ -536,10 +536,10 @@ void FUnrealEnginePythonModule::RunFile(char *filename)
536536
void FUnrealEnginePythonModule::RunFileSandboxed(char *filename, void(*callback)(void *arg), void *arg)
537537
{
538538
FScopePythonGIL gil;
539-
char *full_path = filename;
539+
FString full_path = filename;
540540
if (!FPaths::FileExists(filename))
541541
{
542-
full_path = TCHAR_TO_UTF8(*FPaths::Combine(*ScriptsPath, UTF8_TO_TCHAR(filename)));
542+
full_path = FPaths::Combine(*ScriptsPath, full_path);
543543
}
544544

545545
PyThreadState *_main = PyThreadState_Get();
@@ -571,21 +571,21 @@ void FUnrealEnginePythonModule::RunFileSandboxed(char *filename, void(*callback)
571571
FILE *fd = nullptr;
572572

573573
#if PLATFORM_WINDOWS
574-
if (fopen_s(&fd, full_path, "r") != 0)
574+
if (fopen_s(&fd, TCHAR_TO_UTF8(*full_path), "r") != 0)
575575
{
576-
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), UTF8_TO_TCHAR(full_path));
576+
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), *full_path);
577577
return;
578578
}
579579
#else
580-
fd = fopen(full_path, "r");
580+
fd = fopen(TCHAR_TO_UTF8(*full_path), "r");
581581
if (!fd)
582582
{
583-
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), UTF8_TO_TCHAR(full_path));
583+
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), *full_path);
584584
return;
585585
}
586586
#endif
587587

588-
PyObject *eval_ret = PyRun_File(fd, full_path, Py_file_input, global_dict, global_dict);
588+
PyObject *eval_ret = PyRun_File(fd, TCHAR_TO_UTF8(*full_path), Py_file_input, global_dict, global_dict);
589589
fclose(fd);
590590
if (!eval_ret)
591591
{
@@ -597,7 +597,7 @@ void FUnrealEnginePythonModule::RunFileSandboxed(char *filename, void(*callback)
597597
Py_DECREF(eval_ret);
598598
#else
599599
// damn, this is horrible, but it is the only way i found to avoid the CRT error :(
600-
FString command = FString::Printf(TEXT("execfile(\"%s\")"), UTF8_TO_TCHAR(full_path));
600+
FString command = FString::Printf(TEXT("execfile(\"%s\")"), *full_path);
601601
PyObject *eval_ret = PyRun_String(TCHAR_TO_UTF8(*command), Py_file_input, global_dict, global_dict);
602602
if (!eval_ret)
603603
{

0 commit comments

Comments
 (0)