Skip to content

Commit

Permalink
Updated python interpreter intialization code for new API. (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
feldergast authored Dec 9, 2022
1 parent 76974cb commit 5ce052c
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/sst/core/model/python/pymodel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1046,24 +1046,43 @@ SSTPythonModelDefinition::initModel(
CALL_INFO, 2, 0, "SST loading a Python model from script: %s / [%s]\n", script_file.c_str(),
local_script_name.c_str());


#if PY_MAJOR_VERSION >= 3
// Add sst module to the Python interpreter as a built in
PyImport_AppendInittab("sst", &PyInit_sst);
#endif

// Get the Python scripting engine started
Py_Initialize();

#if PY_MAJOR_VERSION >= 3
#if PY_MINOR_VERSION >= 11
{
PyConfig config;
PyConfig_InitPythonConfig(&config);
// Set to zero so that we can get environment variables in the
// script
config.isolated = 0;
// Set to zero so it won't parse out the first argument of
// argv
config.parse_argv = 0;
// Set argc and argv
PyConfig_SetBytesArgv(&config, argc, argv);
// Get the Python scripting engine started
Py_InitializeFromConfig(&config);
}
#else
// Set arguments; Python3 takes wchar_t* arg instead of char*
wchar_t** wargv = (wchar_t**)PyMem_Malloc(sizeof(wchar_t*) * argc);
for ( int i = 0; i < argc; i++ )
for ( int i = 0; i < argc; i++ ) {
wargv[i] = Py_DecodeLocale(argv[i], nullptr);
}

// Get the Python scripting engine started
Py_Initialize();
PySys_SetArgv(argc, wargv);
#endif
PyRun_SimpleString("import sys\n"
"import sst\n"
"sys.meta_path.append(sst.ModuleLoader())\n");
#else
// Get the Python scripting engine started
Py_Initialize();
PySys_SetArgv(argc, argv);
// Add sst module to the Python interpreter as a built in
PyInit_sst();
Expand Down

0 comments on commit 5ce052c

Please sign in to comment.