Merge pull request #1873 from jmichelp/master

Fix python initialization to have SWIG work
This commit is contained in:
Iceman 2023-01-22 16:49:16 +01:00 committed by GitHub
commit 0a2517feb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -147,7 +147,7 @@ static int split(char *str, char **arr) {
return word_cnt; return word_cnt;
} }
static void set_python_path(char *path) { static void set_python_path(const char *path) {
PyObject *syspath = PySys_GetObject("path"); PyObject *syspath = PySys_GetObject("path");
if (syspath == 0) { if (syspath == 0) {
PrintAndLogEx(WARNING, "Python failed to getobject"); PrintAndLogEx(WARNING, "Python failed to getobject");
@ -407,12 +407,16 @@ static int CmdScriptRun(const char *Cmd) {
Py_Initialize(); Py_Initialize();
#else #else
PyConfig py_conf; PyConfig py_conf;
PyConfig_InitIsolatedConfig(&py_conf); // We need to use Python mode instead of isolated to avoid breaking stuff.
// Despite being isolated we probably want to allow users to use PyConfig_InitPythonConfig(&py_conf);
// the Python packages they installed on their user directory as well // Let's still make things bit safer by being as close as possible to isolated mode.
// as system ones. But it seems isolated mode still enforces them off. py_conf.configure_c_stdio = -1;
py_conf.use_environment = 1; py_conf.faulthandler = 0;
py_conf.use_hash_seed = 0;
py_conf.install_signal_handlers = 0;
py_conf.parse_argv = 0;
py_conf.user_site_directory = 1; py_conf.user_site_directory = 1;
py_conf.use_environment = 0;
#endif #endif
//int argc, char ** argv //int argc, char ** argv
@ -429,8 +433,13 @@ static int CmdScriptRun(const char *Cmd) {
#else #else
// The following line will implicitly pre-initialize Python // The following line will implicitly pre-initialize Python
PyConfig_SetBytesArgv(&py_conf, argc + 1, argv); PyConfig_SetBytesArgv(&py_conf, argc + 1, argv);
// We disallowed in py_conf environment variables interfering with python interpreter's behavior.
// Let's manually enable the ones we truly need.
// This is required by Proxspace to work with an isolated Python configuration // This is required by Proxspace to work with an isolated Python configuration
PyConfig_SetBytesString(&py_conf, &py_conf.home, getenv("PYTHONHOME")); PyConfig_SetBytesString(&py_conf, &py_conf.home, getenv("PYTHONHOME"));
// This is required for allowing `import pm3` in python scripts
PyConfig_SetBytesString(&py_conf, &py_conf.pythonpath_env, getenv("PYTHONPATH"));
Py_InitializeFromConfig(&py_conf); Py_InitializeFromConfig(&py_conf);