From 0d2e86f99c1d5598d2fd5ae7d9bdb627babf9bc4 Mon Sep 17 00:00:00 2001 From: Nitraiolo Date: Sat, 17 Dec 2022 15:26:36 +0100 Subject: [PATCH 1/4] Conditional build of cmdscript to allow compilation with old python versions (<3.10.x) --- client/src/cmdscript.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/client/src/cmdscript.c b/client/src/cmdscript.c index b19c5b9df..d326af4d1 100644 --- a/client/src/cmdscript.c +++ b/client/src/cmdscript.c @@ -403,6 +403,7 @@ static int CmdScriptRun(const char *Cmd) { // hook Proxmark3 API PyImport_AppendInittab("_pm3", PyInit__pm3); #endif +#if PY_MINOR_VERSION >= 10 PyConfig py_conf; PyConfig_InitIsolatedConfig(&py_conf); // Despite being isolated we probably want to allow users to use @@ -410,11 +411,15 @@ static int CmdScriptRun(const char *Cmd) { // as system ones. But it seems isolated mode still enforces them off. py_conf.use_environment = 1; py_conf.user_site_directory = 1; +#else + Py_Initialize(); +#endif //int argc, char ** argv char *argv[128]; argv[0] = filename; int argc = split(arguments, &argv[1]); +#if PY_MINOR_VERSION >= 10 // The following line will implicitly pre-initialize Python PyConfig_SetBytesArgv(&py_conf, argc + 1, argv); // This is required by Proxspace to work with an isolated Python configuration @@ -424,6 +429,17 @@ static int CmdScriptRun(const char *Cmd) { // clean up PyConfig_Clear(&py_conf); +#else + wchar_t *py_args[argc + 1]; + py_args[0] = Py_DecodeLocale(filename, NULL); + for (int i = 0; i < argc; i++) { + py_args[i + 1] = Py_DecodeLocale(argv[i], NULL); + } + + PySys_SetArgv(argc + 1, py_args); + + // clean up +#endif for (int i = 0; i < argc; ++i) { free(argv[i + 1]); } From 912d9be2d14201e138cdf4963e0998a3f1bc53b1 Mon Sep 17 00:00:00 2001 From: Nitraiolo Date: Sat, 17 Dec 2022 20:12:12 +0100 Subject: [PATCH 2/4] Added major version into compile condition of cmdscript --- client/src/cmdscript.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/cmdscript.c b/client/src/cmdscript.c index d326af4d1..16d933d76 100644 --- a/client/src/cmdscript.c +++ b/client/src/cmdscript.c @@ -403,7 +403,7 @@ static int CmdScriptRun(const char *Cmd) { // hook Proxmark3 API PyImport_AppendInittab("_pm3", PyInit__pm3); #endif -#if PY_MINOR_VERSION >= 10 +#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 10 PyConfig py_conf; PyConfig_InitIsolatedConfig(&py_conf); // Despite being isolated we probably want to allow users to use @@ -419,7 +419,7 @@ static int CmdScriptRun(const char *Cmd) { char *argv[128]; argv[0] = filename; int argc = split(arguments, &argv[1]); -#if PY_MINOR_VERSION >= 10 +#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 10 // The following line will implicitly pre-initialize Python PyConfig_SetBytesArgv(&py_conf, argc + 1, argv); // This is required by Proxspace to work with an isolated Python configuration From 997e5aac3eb40ad17784c10195ed9a951efc6ab5 Mon Sep 17 00:00:00 2001 From: Nitraiolo Date: Sat, 17 Dec 2022 21:49:04 +0100 Subject: [PATCH 3/4] Fix py_args[] population --- client/src/cmdscript.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/client/src/cmdscript.c b/client/src/cmdscript.c index 16d933d76..b8065950e 100644 --- a/client/src/cmdscript.c +++ b/client/src/cmdscript.c @@ -431,9 +431,8 @@ static int CmdScriptRun(const char *Cmd) { PyConfig_Clear(&py_conf); #else wchar_t *py_args[argc + 1]; - py_args[0] = Py_DecodeLocale(filename, NULL); - for (int i = 0; i < argc; i++) { - py_args[i + 1] = Py_DecodeLocale(argv[i], NULL); + for (int i = 0; i <= argc; i++) { + py_args[i] = Py_DecodeLocale(argv[i], NULL); } PySys_SetArgv(argc + 1, py_args); From 96d8528a0af94f21734124c39481307f3993334f Mon Sep 17 00:00:00 2001 From: Nitraiolo Date: Sun, 18 Dec 2022 09:45:46 +0100 Subject: [PATCH 4/4] Inverted condition in python check so it defaults to new code now. --- client/src/cmdscript.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/client/src/cmdscript.c b/client/src/cmdscript.c index b8065950e..9e19c0959 100644 --- a/client/src/cmdscript.c +++ b/client/src/cmdscript.c @@ -403,7 +403,9 @@ static int CmdScriptRun(const char *Cmd) { // hook Proxmark3 API PyImport_AppendInittab("_pm3", PyInit__pm3); #endif -#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 10 +#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 10 + Py_Initialize(); +#else PyConfig py_conf; PyConfig_InitIsolatedConfig(&py_conf); // Despite being isolated we probably want to allow users to use @@ -411,15 +413,22 @@ static int CmdScriptRun(const char *Cmd) { // as system ones. But it seems isolated mode still enforces them off. py_conf.use_environment = 1; py_conf.user_site_directory = 1; -#else - Py_Initialize(); #endif //int argc, char ** argv char *argv[128]; argv[0] = filename; int argc = split(arguments, &argv[1]); -#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 10 +#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 10 + wchar_t *py_args[argc + 1]; + for (int i = 0; i <= argc; i++) { + py_args[i] = Py_DecodeLocale(argv[i], NULL); + } + + PySys_SetArgv(argc + 1, py_args); + + // clean up +#else // The following line will implicitly pre-initialize Python PyConfig_SetBytesArgv(&py_conf, argc + 1, argv); // This is required by Proxspace to work with an isolated Python configuration @@ -429,15 +438,6 @@ static int CmdScriptRun(const char *Cmd) { // clean up PyConfig_Clear(&py_conf); -#else - wchar_t *py_args[argc + 1]; - for (int i = 0; i <= argc; i++) { - py_args[i] = Py_DecodeLocale(argv[i], NULL); - } - - PySys_SetArgv(argc + 1, py_args); - - // clean up #endif for (int i = 0; i < argc; ++i) { free(argv[i + 1]);