mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2024-11-15 06:09:13 +08:00
some PyStatus
This commit is contained in:
parent
2562e0b8c9
commit
96f85d38bc
1 changed files with 31 additions and 6 deletions
|
@ -440,6 +440,7 @@ static int CmdScriptRun(const char *Cmd) {
|
|||
Py_Initialize();
|
||||
#else
|
||||
PyConfig py_conf;
|
||||
PyStatus status;
|
||||
// We need to use Python mode instead of isolated to avoid breaking stuff.
|
||||
PyConfig_InitPythonConfig(&py_conf);
|
||||
// Let's still make things bit safer by being as close as possible to isolated mode.
|
||||
|
@ -465,8 +466,10 @@ static int CmdScriptRun(const char *Cmd) {
|
|||
PySys_SetArgv(argc + 1, py_args);
|
||||
#else
|
||||
// The following line will implicitly pre-initialize Python
|
||||
PyConfig_SetBytesArgv(&py_conf, argc + 1, argv);
|
||||
|
||||
status = PyConfig_SetBytesArgv(&py_conf, argc + 1, argv);
|
||||
if (PyStatus_Exception(status)) {
|
||||
goto pyexception;
|
||||
}
|
||||
// We disallowed in py_conf environment variables interfering with python interpreter's behavior.
|
||||
// Let's manually enable the ones we truly need.
|
||||
const char *virtual_env = getenv("VIRTUAL_ENV");
|
||||
|
@ -474,15 +477,27 @@ static int CmdScriptRun(const char *Cmd) {
|
|||
size_t length = strlen(virtual_env) + strlen("/bin/python3") + 1;
|
||||
char python_executable_path[length];
|
||||
snprintf(python_executable_path, length, "%s/bin/python3", virtual_env);
|
||||
PyConfig_SetBytesString(&py_conf, &py_conf.executable, python_executable_path);
|
||||
status = PyConfig_SetBytesString(&py_conf, &py_conf.executable, python_executable_path);
|
||||
if (PyStatus_Exception(status)) {
|
||||
goto pyexception;
|
||||
}
|
||||
} else {
|
||||
// This is required by Proxspace to work with an isolated Python configuration
|
||||
PyConfig_SetBytesString(&py_conf, &py_conf.home, getenv("PYTHONHOME"));
|
||||
status = PyConfig_SetBytesString(&py_conf, &py_conf.home, getenv("PYTHONHOME"));
|
||||
if (PyStatus_Exception(status)) {
|
||||
goto pyexception;
|
||||
}
|
||||
}
|
||||
// This is required for allowing `import pm3` in python scripts
|
||||
PyConfig_SetBytesString(&py_conf, &py_conf.pythonpath_env, getenv("PYTHONPATH"));
|
||||
status = PyConfig_SetBytesString(&py_conf, &py_conf.pythonpath_env, getenv("PYTHONPATH"));
|
||||
if (PyStatus_Exception(status)) {
|
||||
goto pyexception;
|
||||
}
|
||||
|
||||
Py_InitializeFromConfig(&py_conf);
|
||||
status = Py_InitializeFromConfig(&py_conf);
|
||||
if (PyStatus_Exception(status)) {
|
||||
goto pyexception;
|
||||
}
|
||||
|
||||
// clean up
|
||||
PyConfig_Clear(&py_conf);
|
||||
|
@ -516,6 +531,16 @@ static int CmdScriptRun(const char *Cmd) {
|
|||
PrintAndLogEx(SUCCESS, "\nfinished " _YELLOW_("%s"), filename);
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
pyexception:
|
||||
PyConfig_Clear(&py_conf);
|
||||
if (PyStatus_IsExit(status)) {
|
||||
PrintAndLogEx(WARNING, "\nPython initialization failed with exitcode=%i", status.exitcode);
|
||||
}
|
||||
if (PyStatus_IsError(status)) {
|
||||
PrintAndLogEx(WARNING, "\nPython initialization failed with exception: %s", status.err_msg);
|
||||
}
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in a new issue