1 #include <../../nrnconf.h> 7 #include <../nrnpython/nrnpython_config.h> 31 extern void nrn_possible_mismatched_arch(
const char*);
36 #if defined(NRNPYTHON_DYNAMICLOAD) 43 extern void* dlopen_noerr(
const char*
name,
int mode);
44 #define dlopen dlopen_noerr 45 extern void*
dlsym(
void* handle,
const char*
name);
46 extern int dlclose(
void* handle);
56 #if NRNPYTHON_DYNAMICLOAD >= 30 59 static const char*
ver[] = {
"3.6", 0};
61 static const char*
ver[] = {
"3.9",
"3.8",
"3.7",
"3.6", 0};
66 static const char*
ver[] = {0};
71 static void* python_already_loaded();
72 static void* load_python();
73 static void load_nrnpython(
int,
const char*);
75 extern "C" int nrnpython_start(int);
78 #endif //defined(NRNPYTHON_DYNAMICLOAD) 85 (*p_nrnpython_real)();
100 #if NRNPYTHON_DYNAMICLOAD 101 static char* nrnpy_pylib;
103 static void siteprob(
void) {
105 printf(
"Py_Initialize exited. PYTHONHOME probably needs to be set correctly.\n");
107 printf(
"The value of PYTHONHOME or our automatic guess based on the output of nrnpyenv.sh:\n export PYTHONHOME=%s\ndid not work.\n",
nrnpy_pyhome);
109 printf(
"It will help to examine the output of:\nnrnpyenv.sh\n\ 110 and set the indicated environment variables, or avoid python by adding\n\ 112 to %s/lib/nrn.defaults (or .nrn.defaults in your $HOME directory)\n",
117 static void set_nrnpylib() {
118 nrnpy_pylib =
getenv(
"NRN_PYLIB");
122 if (nrnpy_pylib) { nrnpy_pylib = strdup(nrnpy_pylib); }
129 char*
line =
new char[linesz+1];
134 sprintf(line,
"%s\\mingw\\usr\\bin\\bash %s/bin/nrnpyenv.sh %s --NEURON_HOME=%s",
142 char* line =
new char[linesz+1];
143 sprintf(line,
"bash %s/../../bin/nrnpyenv.sh %s",
147 FILE*
p =
popen(line,
"r");
149 printf(
"could not popen '%s'\n", line);
151 if (!fgets(line, linesz, p)) {
152 printf(
"failed: %s\n", line);
154 while(fgets(line, linesz, p)) {
158 cp += strlen(
"export NRN_PYTHONHOME=") + 1;
159 cp[strlen(cp) - 2] =
'\0';
162 }
else if (!nrnpy_pylib && (cp =
strstr(line,
"export NRN_PYLIB="))) {
163 cp += strlen(
"export NRN_PYLIB=") + 1;
164 cp[strlen(cp) - 2] =
'\0';
165 if (nrnpy_pylib) { free(nrnpy_pylib); }
166 nrnpy_pylib = strdup(cp);
175 nrnmpi_char_broadcast_world(&nrnpy_pylib, 0);
182 static void set_pythonhome(
void* handle){
194 void*
s =
dlsym(handle,
"Py_Initialize");
196 int success =
dladdr(s, &dl_info);
203 for (
int i = n-1;
i > 0; --
i) {
211 int res = setenv(
"PYTHONHOME", p, 1);
227 #if NRNPYTHON_DYNAMICLOAD 238 fprintf(stderr,
"Could not dlopen NRN_PYLIB: %s\n", nrnpy_pylib);
240 nrn_possible_mismatched_arch(nrnpy_pylib);
245 if (!handle) { python_already_loaded();}
247 handle = load_python();
255 set_pythonhome(handle);
277 (*p_nrnpython_reg_real)();
287 #if NRNPYTHON_DYNAMICLOAD // to end of file 292 static void* ver_dlo(
int flag) {
293 for (
int i = 0;
ver[
i]; ++
i) {
304 void* handle =
dlopen(name, flag);
314 static void* python_already_loaded() {
320 static void* load_python() {
326 static void* load_sym(
void* handle,
const char*
name) {
327 void*
p =
dlsym(handle, name);
329 printf(
"Could not load %s\n", name);
335 static void* load_nrnpython_helper(
const char* npylib) {
338 sprintf(name,
"%s.dll", npylib);
351 static int pylib2pyver10(std::string pylib) {
353 const auto pos = pylib.find_last_of(
"/\\");
354 if (pos != std::string::npos) {
355 pylib = pylib.substr(pos + 1);
360 std::remove_if(pylib.begin(),
363 return !std::isdigit(
c);
367 return std::atoi(pylib.c_str());
370 static void load_nrnpython(
int pyver10,
const char* pylib) {
372 #if (defined(__MINGW32__) || (defined(USE_LIBNRNPYTHON_MAJORMINOR) && USE_LIBNRNPYTHON_MAJORMINOR == 1)) 375 if (pyver10 < 1 && pylib) {
376 pv10 = pylib2pyver10(pylib);
378 sprintf(name,
"libnrnpython%d", pv10);
379 handle = load_nrnpython_helper(name);
381 printf(
"Could not load %s\n", name);
382 printf(
"pyver10=%d pylib=%s\n", pyver10, pylib ? pylib :
"NULL");
386 handle = load_nrnpython_helper(
"libnrnpython3");
388 printf(
"Could not load libnrnpython3\n");
389 printf(
"pyver10=%d pylib=%s\n", pyver10, pylib ? pylib :
"NULL");
char * strstr(cs, ct) char *cs
char * hoc_back2forward(char *s)
const char * dlerror(void)
int nrnmpi_numprocs_world
int dladdr(const void *dl_restrict, Dl_info *dl_restrict)
char * hoc_forward2back(char *s)
sprintf(buf," if (secondorder) {\ " int _i;\" " for(_i=0;_i< %d;++_i) {\" " _p[_slist%d[_i]]+=dt *_p[_dlist%d[_i]];\" " }}\", numeqn, listnum, listnum)
int dlclose(void *handle)
int const size_t const size_t n
void class2oc(const char *, void *(*cons)(Object *), void(*destruct)(void *), Member_func *, int(*checkpoint)(void **), Member_ret_obj_func *, Member_ret_str_func *)
FILE * popen(char *s1, char *s2)
int(* p_nrnpython_start)(int)
int * nrnpy_site_problem_p
char * getenv(const char *s)
static void(* p_nrnpython_real)()
static Member_func p_members[]
fprintf(stderr, "Don't know the location of params at %p\, pp)
void * dlopen(const char *path, int mode)
static void(* p_nrnpython_reg_real)()
int nrnpython_start(int)
defined(NRNPYTHON_DYNAMICLOAD)
static void * p_cons(Object *)
void nrnpython_reg_real()
static char line[MAXLINE]
int nrn_is_python_extension
void * dlsym(void *handle, const char *symbol)
static void p_destruct(void *v)