void embed_init_python(void) { FENTER; #ifndef PYTHON_SO_LIB #error "Python version needs passing in with -DPYTHON_SO_VERSION=libpython<ver>.so" #else #define PY_SO_LIB xstr(PYTHON_SO_LIB) #endif // Don't initialise python if already running if (gtstate) return; void * ret = utils_dyn_open(PY_SO_LIB); if (!ret) { fprintf(stderr, "Failed to find python lib\n"); } Py_SetProgramName(progname); Py_Initialize(); /* Initialize the interpreter */ PySys_SetArgvEx(1, argv, 0); PyEval_InitThreads(); /* Create (and acquire) the interpreter lock */ /* Swap out and return current thread state and release the GIL */ gtstate = PyEval_SaveThread(); FEXIT; }
void embed_init_python(void) { FENTER; #ifndef PYTHON_SO_LIB #error "Python version needs passing in with -DPYTHON_SO_VERSION=libpython<ver>.so" #else #define PY_SO_LIB xstr(PYTHON_SO_LIB) #endif // Don't initialise python if already running if (gtstate) return; void * ret = utils_dyn_open(PY_SO_LIB); if (!ret) { fprintf(stderr, "Failed to find python lib\n"); } Py_SetProgramName(progname); Py_Initialize(); /* Initialize the interpreter */ PySys_SetArgvEx(1, argv, 0); PyEval_InitThreads(); /* Create (and acquire) the interpreter lock */ /* Swap out and return current thread state and release the GIL */ gtstate = PyEval_SaveThread(); /* Before returning we check if the user wants pause the simulator thread such that they can attach */ const char *pause = getenv("COCOTB_ATTACH"); if (pause) { long sleep_time = strtol(pause, NULL, 10); if (errno == ERANGE && (sleep_time == LONG_MAX || sleep_time == LONG_MIN)) { fprintf(stderr, "COCOTB_ATTACH only needs to be set to ~30 seconds"); goto out; } if ((errno != 0 && sleep_time == 0) || (sleep_time <= 0)) { fprintf(stderr, "COCOTB_ATTACH must be set to an integer base 10 or omitted"); goto out; } fprintf(stderr, "Waiting for %lu seconds - Attach to %d\n", sleep_time, getpid()); sleep(sleep_time); } out: FEXIT; }
void embed_init_python(void) { FENTER; #ifndef PYTHON_SO_LIB #error "Python version needs passing in with -DPYTHON_SO_VERSION=libpython<ver>.so" #else #define PY_SO_LIB xstr(PYTHON_SO_LIB) #endif // Don't initialise python if already running if (gtstate) return; void * ret = utils_dyn_open(PY_SO_LIB); if (!ret) { fprintf(stderr, "Failed to find python lib\n"); } to_python(); // reset Program Name (i.e. argv[0]) if we are in a virtual environment char *venv_path_home = getenv("VIRTUAL_ENV"); if (venv_path_home) { char venv_path[strlen(venv_path_home)+64]; strcpy(venv_path, venv_path_home); strcat(venv_path, "/bin/python"); // this is universal in any VIRTUAL_ENV the python interpreter #if PY_MAJOR_VERSION >= 3 static wchar_t venv_path_w[1024]; #if PY_MINOR_VERSION >= 5 // Python3.5 + provides the locale decoder wcscpy(venv_path_w, Py_DecodeLocale(venv_path, NULL)); #else // for lesser python versions, we just hope the user specified his locales correctly setlocale (LC_ALL, ""); mbstowcs(venv_path_w, venv_path, sizeof(venv_path_w)); #endif LOG_INFO("Using virtualenv at %ls.", venv_path_w); Py_SetProgramName(venv_path_w); #else // Python2 case LOG_INFO("Using virtualenv at %s.", venv_path); Py_SetProgramName(venv_path); #endif } else { LOG_INFO("Did not detect virtual environment. Using system-wide Python interpreter."); } Py_Initialize(); /* Initialize the interpreter */ PySys_SetArgvEx(1, argv, 0); PyEval_InitThreads(); /* Create (and acquire) the interpreter lock */ /* Swap out and return current thread state and release the GIL */ gtstate = PyEval_SaveThread(); to_simulator(); /* Before returning we check if the user wants pause the simulator thread such that they can attach */ const char *pause = getenv("COCOTB_ATTACH"); if (pause) { long sleep_time = strtol(pause, NULL, 10); if (errno == ERANGE && (sleep_time == LONG_MAX || sleep_time == LONG_MIN)) { fprintf(stderr, "COCOTB_ATTACH only needs to be set to ~30 seconds"); goto out; } if ((errno != 0 && sleep_time == 0) || (sleep_time <= 0)) { fprintf(stderr, "COCOTB_ATTACH must be set to an integer base 10 or omitted"); goto out; } fprintf(stderr, "Waiting for %lu seconds - Attach to %d\n", sleep_time, getpid()); sleep(sleep_time); } out: FEXIT; }