From b487961cbd5aa82994bf312a15b2ee5b6aa20446 Mon Sep 17 00:00:00 2001 From: Jean-Michel Picod Date: Tue, 6 Dec 2022 18:07:10 +0100 Subject: [PATCH] Moved to non-deprecated API to init Python intepreter --- CHANGELOG.md | 1 + client/src/cmdscript.c | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04916e479..01094bf38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] + - Moved to non-deprecated API to initialize Python interpreter (jmichel@) - Changed `sc upgrade` updated firmware v4.12 (RDV40) (@sentiprox) - Fixed contact interface / smartcard APDU chaining logic and allow 256 bytes ADPU payload. Need SIM firmware 4.12 to work (@jmichel) - Fixed `lf hitag dump` - Should now work as described in the command help (@natmchugh) diff --git a/client/src/cmdscript.c b/client/src/cmdscript.c index c63842ae3..18a0586f8 100644 --- a/client/src/cmdscript.c +++ b/client/src/cmdscript.c @@ -412,22 +412,28 @@ static int CmdScriptRun(const char *Cmd) { // hook Proxmark3 API PyImport_AppendInittab("_pm3", PyInit__pm3); #endif - Py_Initialize(); + PyConfig py_conf; + PyConfig_InitIsolatedConfig(&py_conf); //int argc, char ** argv char *argv[128]; - int argc = split(arguments, argv); - 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); - } + argv[0] = filename; + int argc = split(arguments, &argv[1]); + PyConfig_SetBytesArgv(&py_conf, argc + 1, argv); + // Despite being isolated we probably want to allow users to use + // the Python packages they installed on their user directory as well + // as obey env variables. + py_conf.use_environment = 1; + py_conf.user_site_directory = 1; + // Setting this pre-intializes Python implictly which will change the config + PyConfig_SetString(&py_conf, &py_conf.program_name, program); - PySys_SetArgv(argc + 1, py_args); + Py_InitializeFromConfig(&py_conf); // clean up + PyConfig_Clear(&py_conf); for (int i = 0; i < argc; ++i) { - free(argv[i]); + free(argv[i + 1]); } // setup search paths.