1 #include <../../nrnconf.h>
7 #include <../nrnpython/nrnpython_config.h>
31 extern void nrn_possible_mismatched_arch(
const char*);
36 #if defined(NRNPYTHON_DYNAMICLOAD)
39 #if !defined(RTLD_NOLOAD)
45 #if NRNPYTHON_DYNAMICLOAD >= 30
48 static const char*
ver[] = {
"3.7", 0};
50 static const char*
ver[] = {
"3.10",
"3.9",
"3.8",
"3.7", 0};
55 static const char*
ver[] = {0};
60 static void* python_already_loaded();
61 static void* load_python();
62 static void load_nrnpython(
int,
const char*);
74 (*p_nrnpython_real)();
88 #if NRNPYTHON_DYNAMICLOAD
89 static char* nrnpy_pylib;
91 static void siteprob(
void) {
93 printf(
"Py_Initialize exited. PYTHONHOME probably needs to be set correctly.\n");
96 "The value of PYTHONHOME or our automatic guess based on the output of "
97 "nrnpyenv.sh:\n export PYTHONHOME=%s\ndid not work.\n",
101 "It will help to examine the output of:\nnrnpyenv.sh\n\
102 and set the indicated environment variables, or avoid python by adding\n\
104 to %s/lib/nrn.defaults (or .nrn.defaults in your $HOME directory)\n",
109 static void set_nrnpylib() {
110 nrnpy_pylib =
getenv(
"NRN_PYLIB");
117 nrnpy_pylib = strdup(nrnpy_pylib);
127 char*
line =
new char[linesz + 1];
133 "%s\\mingw\\usr\\bin\\bash %s/bin/nrnpyenv.sh %s --NEURON_HOME=%s",
141 char*
line =
new char[linesz + 1];
143 "bash %s/../../bin/nrnpyenv.sh %s",
151 if (!fgets(
line, linesz,
p)) {
154 while (fgets(
line, linesz,
p)) {
158 cp += strlen(
"export NRN_PYTHONHOME=") + 1;
159 cp[strlen(cp) - 2] =
'\0';
164 }
else if (!nrnpy_pylib && (cp =
strstr(
line,
"export NRN_PYLIB="))) {
165 cp += strlen(
"export NRN_PYLIB=") + 1;
166 cp[strlen(cp) - 2] =
'\0';
170 nrnpy_pylib = strdup(cp);
179 nrnmpi_char_broadcast_world(&nrnpy_pylib, 0);
186 static void set_pythonhome(
void* handle){
198 void* s =
dlsym(handle,
"Py_Initialize");
207 for (
int i =
n-1;
i > 0; --
i) {
215 int res = setenv(
"PYTHONHOME",
p, 1);
231 #if NRNPYTHON_DYNAMICLOAD
242 fprintf(stderr,
"Could not dlopen NRN_PYLIB: %s\n", nrnpy_pylib);
244 nrn_possible_mismatched_arch(nrnpy_pylib);
250 python_already_loaded();
253 handle = load_python();
261 set_pythonhome(handle);
283 (*p_nrnpython_reg_real)();
293 #if NRNPYTHON_DYNAMICLOAD
298 static void* ver_dlo(
int flag) {
299 for (
int i = 0;
ver[
i]; ++
i) {
320 static void* python_already_loaded() {
326 static void* load_python() {
332 static void* load_sym(
void* handle,
const char*
name) {
341 static void* load_nrnpython_helper(
const char* npylib) {
357 static int pylib2pyver10(std::string pylib) {
359 const auto pos = pylib.find_last_of(
"/\\");
360 if (pos != std::string::npos) {
361 pylib = pylib.substr(pos + 1);
365 pylib.erase(std::remove_if(pylib.begin(), pylib.end(), [](
char c) { return !std::isdigit(c); }),
369 return std::atoi(pylib.c_str());
372 static void load_nrnpython(
int pyver10,
const char* pylib) {
374 #if (defined(__MINGW32__) || \
375 (defined(USE_LIBNRNPYTHON_MAJORMINOR) && USE_LIBNRNPYTHON_MAJORMINOR == 1))
378 if (pyver10 < 1 && pylib) {
379 pv10 = pylib2pyver10(pylib);
382 handle = load_nrnpython_helper(
name);
385 printf(
"pyver10=%d pylib=%s\n", pyver10, pylib ? pylib :
"NULL");
389 handle = load_nrnpython_helper(
"libnrnpython3");
391 printf(
"Could not load libnrnpython3\n");
392 printf(
"pyver10=%d pylib=%s\n", pyver10, pylib ? pylib :
"NULL");
sprintf(buf, " if (secondorder) {\n" " int _i;\n" " for (_i = 0; _i < %d; ++_i) {\n" " _p[_slist%d[_i]] += dt*_p[_dlist%d[_i]];\n" " }}\n", numeqn, listnum, listnum)
DLFCN_EXPORT void * dlopen(const char *file, int mode)
DLFCN_EXPORT int dladdr(const void *addr, Dl_info *info)
DLFCN_NOINLINE DLFCN_EXPORT void * dlsym(void *handle, const char *name)
void hoc_retpushx(double x)
static char line[MAXLINE]
char * getenv(const char *s)
FILE * popen(char *s1, char *s2)
int const size_t const size_t n
int nrnmpi_numprocs_world
void class2oc(const char *, void *(*cons)(Object *), void(*destruct)(void *), Member_func *, int(*checkpoint)(void **), Member_ret_obj_func *, Member_ret_str_func *)
int nrnpython_start(int)
defined(NRNPYTHON_DYNAMICLOAD)
int nrn_is_python_extension
int * nrnpy_site_problem_p
int(* p_nrnpython_start)(int)
static void p_destruct(void *v)
char * hoc_back2forward(char *s)
void nrnpython_reg_real()
static void(* p_nrnpython_reg_real)()
static void(* p_nrnpython_real)()
static void * p_cons(Object *)
char * hoc_forward2back(char *s)
static Member_func p_members[]
char * strstr(char *cs, char *ct)