@@ -108,14 +108,53 @@ std::unique_ptr<PyConfig> ScriptEngine::prepareScriptingHome (
108108 zip.uncompressTo (pythonFolder.getParentDirectory ());
109109 }
110110
111+ PyPreConfig preconfig;
112+ PyPreConfig_InitIsolatedConfig (&preconfig);
113+ preconfig.utf8_mode = 1 ;
114+
115+ if (PyStatus status = Py_PreInitialize (&preconfig); PyStatus_IsError (status))
116+ {
117+ YUP_DBG (" Failed Py_PreInitialize" );
118+ return nullptr ;
119+ }
120+
111121 auto config = std::make_unique<PyConfig>();
112122
113123 PyConfig_InitPythonConfig (config.get ());
114124 config->parse_argv = 0 ;
115125 config->isolated = 1 ;
116126 config->install_signal_handlers = 0 ;
117- config->program_name = Py_DecodeLocale (programName.toRawUTF8 (), nullptr );
118- config->home = Py_DecodeLocale (destinationFolder.getFullPathName ().toRawUTF8 (), nullptr );
127+
128+ if (auto status = PyConfig_SetBytesString (config.get (), &config->program_name , programName.toRawUTF8 ()); PyStatus_IsError (status))
129+ {
130+ YUP_DBG (" Failed config->program_name" );
131+ return nullptr ;
132+ }
133+
134+ const auto homePath = destinationFolder.getFullPathName ();
135+ if (auto status = PyConfig_SetBytesString (config.get (), &config->home , homePath.toRawUTF8 ()); PyStatus_IsError (status))
136+ {
137+ YUP_DBG (" Failed config->home" );
138+ return nullptr ;
139+ }
140+
141+ #if YUP_WINDOWS
142+ config->module_search_paths_set = 1 ;
143+
144+ const auto prefixPath = destinationFolder.getChildFile (" lib" ).getFullPathName ();
145+
146+ if (auto status = PyConfig_SetBytesString (config.get (), &config->prefix , prefixPath.toRawUTF8 ()); PyStatus_IsError (status))
147+ {
148+ YUP_DBG (" Failed config->prefix" );
149+ return nullptr ;
150+ }
151+
152+ if (auto status = PyConfig_SetBytesString (config.get (), &config->exec_prefix , prefixPath.toRawUTF8 ()); PyStatus_IsError (status))
153+ {
154+ YUP_DBG (" Failed config->exec_prefix" );
155+ return nullptr ;
156+ }
157+ #endif
119158
120159 return config;
121160}
0 commit comments