1 #include <../../nrnconf.h>
26 extern void* (*nrnpy_save_thread)();
29 return PyEval_SaveThread();
32 PyEval_RestoreThread((PyThreadState*)
g);
42 extern Object* (*nrnpy_callable_with_args)(
Object*, int);
46 extern char* (*nrnpy_po2pickle)(
Object*,
size_t* size);
47 extern Object* (*nrnpy_pickle2po)(
char*,
size_t size);
48 extern char* (*nrnpy_callpicklef)(
char*,
size_t size,
int narg,
size_t* retsize);
50 extern Object* (*nrnpympi_alltoall_type)(int, int);
83 #if NRNPYTHON_DYNAMICLOAD
84 extern int nrnpy_site_problem;
124 if (PyErr_Occurred()) {
130 extern void* (*nrnpy_opaque_obj2pyobj_p_)(
Object*);
165 #if NRNPYTHON_DYNAMICLOAD
226 printf(
"PyObject_CallObject callable\n");
227 PyObject_Print(callable, stdout, 0);
229 PyObject_Print(args, stdout, 0);
265 PyObject* head = pn->
po_;
278 if (strcmp(sym->
name,
"_") == 0) {
282 tail = PyObject_GetAttrString(head, sym->
name);
294 args = PyTuple_New(nindex);
295 for (
i = 0;
i < nindex; ++
i) {
299 if (PyTuple_SetItem(args, nindex - 1 -
i,
arg)) {
318 if (PyErr_Occurred()) {
345 PyObject* pn = PyNumber_Float(
result);
377 if (
type == NUMBER) {
378 poright = PyFloat_FromDouble(
hoc_xpop());
386 hoc_execerror(
"Cannot assign that type to PythonObject", (
char*) 0);
395 err = PyObject_SetAttrString(poleft, sym->
name, poright);
396 }
else if (nindex == 1) {
398 PyObject* a = PyObject_GetAttrString(poleft, sym->
name);
400 err = PyObject_SetItem(a,
key, poright);
409 hoc_execerror(
buf,
"HOC cannot handle PythonObject assignment with more than one index.");
421 PyObject* ps = PyObject_Str(po);
422 printf(
"defer %s\n", PyString_AsString(ps));
433 if (PyTuple_Check(po)) {
434 PyObject* args = PyTuple_GetItem(po, 1);
435 if (!PyTuple_Check(args)) {
436 args = PyTuple_Pack(1, args);
448 PyObject* args = PyTuple_New(0);
466 PyObject* po = Py_BuildValue(
"(OO)",
pc,
pv);
479 if (PyErr_Occurred()) {
484 PyObject* pn = PyNumber_Float(r);
485 double x = PyFloat_AsDouble(pn);
503 if (PyErr_Occurred()) {
516 PyObject* pn = PyObject_Str(r);
520 buf[size - 1] =
'\0';
530 if (PyErr_Occurred()) {
542 PyObject* args = PyTuple_Pack(
555 if (PyErr_Occurred()) {
565 PyObject* args = PyTuple_New((Py_ssize_t)
narg);
570 for (
int i = 0;
i <
narg; ++
i) {
577 if (PyTuple_SetItem(args, (Py_ssize_t) (
narg -
i - 1), item) != 0) {
584 PyObject* r = PyTuple_New(2);
585 PyTuple_SetItem(r, 1, args);
587 PyTuple_SetItem(r, 0, po);
600 PyObject* args = PyTuple_New((Py_ssize_t)
narg);
605 for (
int i = 0;
i <
narg; ++
i) {
612 if (PyTuple_SetItem(args, (Py_ssize_t) (
narg -
i - 1), item) != 0) {
629 if (PyErr_Occurred()) {
644 PyObject* pn = PyNumber_Float(r);
645 rval = PyFloat_AsDouble(pn);
660 PyObject*
p = PyTuple_GetItem(po, 0);
661 if (PySequence_Check(
p) || PyMapping_Check(
p)) {
662 r = PyObject_GetItem(
p, PyTuple_GetItem(po, 1));
664 r = PyObject_GetAttr(
p, PyTuple_GetItem(po, 1));
666 PyObject* pn = PyNumber_Float(r);
667 double x = PyFloat_AsDouble(pn);
675 PyObject* pn = PyFloat_FromDouble(x);
676 PyObject*
p = PyTuple_GetItem(po, 0);
677 if (PySequence_Check(
p) || PyMapping_Check(
p)) {
678 PyObject_SetItem(
p, PyTuple_GetItem(po, 1), pn);
680 PyObject_SetAttr(
p, PyTuple_GetItem(po, 1), pn);
689 PyObject* r = PyObject_GetAttr(PyTuple_GetItem(po, 0), PyTuple_GetItem(po, 1));
690 PyObject* pn = PyObject_Str(r);
693 char* cp =
name.c_str();
694 if (*cpp && strcmp(*cpp, cp) == 0) {
700 *cpp =
new char[strlen(cp) + 1];
711 pickle = PyImport_ImportModule(
"pickle");
722 hoc_execerror(
"Neither Python cPickle nor pickle are available", 0);
728 static char*
pickle(PyObject*
p,
size_t* size) {
729 PyObject*
arg = PyTuple_Pack(1,
p);
732 if (!r && PyErr_Occurred()) {
737 *size = PyBytes_Size(r);
738 char* buf1 = PyBytes_AsString(r);
739 char*
buf =
new char[*size];
740 for (
size_t i = 0;
i < *size; ++
i) {
759 PyObject* ps = PyBytes_FromStringAndSize(s, size);
760 PyObject*
arg = PyTuple_Pack(1, ps);
780 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_Exception)) {
781 PyObject *ptype, *pvalue, *ptraceback;
782 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
783 PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
785 PyObject* module_name =
NULL;
786 PyObject* pyth_module =
NULL;
787 PyObject* pyth_func =
NULL;
788 PyObject* py_str =
NULL;
794 ptraceback = Py_None;
795 Py_INCREF(ptraceback);
799 pyth_module = PyImport_Import(module_name);
802 pyth_func = PyObject_GetAttrString(pyth_module,
"format_exception");
804 py_str = PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback,
NULL);
810 Fprintf(stderr,
"nrnperr_str: Py2NRNString failed\n");
812 cmes = strdup(mes.
c_str());
814 Fprintf(stderr,
"nrnpyerr_str: strdup failed\n");
821 Fprintf(stderr,
"nrnpyerr_str failed\n");
824 Py_XDECREF(module_name);
825 Py_XDECREF(pyth_func);
826 Py_XDECREF(pyth_module);
829 Py_XDECREF(ptraceback);
846 PyObject* ps = PyBytes_FromStringAndSize(
fname, size);
847 args = PyTuple_Pack(1, ps);
853 args = PyTuple_New(
narg);
854 for (
int i = 0;
i <
narg; ++
i) {
856 if (PyTuple_SetItem(args,
narg - 1 -
i,
arg)) {
871 if (PyErr_Occurred()) {
886 displ[
i + 1] = displ[
i] + cnts[
i];
892 PyObject* plist = PyList_New(np);
894 for (
int i = 0;
i < np; ++
i) {
897 PyList_SetItem(plist,
i, Py_None);
900 PyList_SetItem(plist,
i,
p);
910 char* sbuf =
pickle(psrc, &sz);
912 int* rcnt =
new int[np];
914 nrnmpi_int_allgather_inplace(rcnt, 1);
916 char* rbuf =
new char[rdispl[np]];
918 nrnmpi_char_allgatherv(sbuf, rbuf, rcnt, rdispl);
921 PyObject* pdest =
char2pylist(rbuf, np, rcnt, rdispl);
931 char* sbuf =
pickle(psrc, &sz);
938 nrnmpi_int_gather(&scnt, rcnt, 1,
root);
943 rbuf =
new char[rdispl[np]];
946 nrnmpi_char_gatherv(sbuf, scnt, rbuf, rcnt, rdispl,
root);
949 PyObject* pdest = Py_None;
975 PyObject* pdest = psrc;
990 PyObject* psrc =
NULL;
991 PyObject* pdest =
NULL;
997 if (!PyList_Check(psrc)) {
1000 if (PyList_Size(psrc) != np) {
1012 pdest = PyList_GetItem(psrc, 0);
1031 pdest = PyList_New(1);
1032 PyList_SetItem(pdest, 0, psrc);
1050 }
else if (
type != 1 &&
type != 5) {
1052 if (root < 0 || root >= np) {
1057 }
else if (
type == 4) {
1080 for (
int i = 0;
i < np; ++
i) {
1084 PyObject* iterator = PyObject_GetIter(psrc);
1087 size_t bufsz = 100000;
1092 s =
new char[bufsz];
1095 for (
size_t i = 0; (
p = PyIter_Next(iterator)) !=
NULL; ++
i) {
1104 if (curpos + sz >= bufsz) {
1105 bufsz = bufsz * 2 + sz;
1106 char* s2 =
new char[bufsz];
1107 for (
size_t i = 0;
i < curpos; ++
i) {
1113 for (
size_t j = 0;
j < sz; ++
j) {
1114 s[curpos +
j] = b[
j];
1122 Py_DECREF(iterator);
1133 int* ones =
new int[np];
1134 for (
int i = 0;
i < np; ++
i) {
1148 pdest = PyTuple_New(2);
1149 PyTuple_SetItem(pdest, 0, Py_BuildValue(
"l", (
long) sdispl[np]));
1150 PyTuple_SetItem(pdest, 1, Py_BuildValue(
"l", (
long) rdispl[np]));
1156 r =
new char[rdispl[np] + 1];
1157 nrnmpi_char_alltoallv(s, scnt, sdispl, r, rcnt, rdispl);
1173 nrnmpi_int_scatter(scnt, rcnt, 1,
root);
1174 r =
new char[rcnt[0] + 1];
1180 nrnmpi_char_scatterv(s, scnt, sdispl, r, rcnt[0],
root);
1189 pdest =
unpickle(r,
size_t(rcnt[0]));
static void nrnmpi_int_alltoallv(int *s, int *scnt, int *sdispl, int *r, int *rcnt, int *rdispl)
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)
void hoc_execerror(const char *, const char *)
void hoc_pushstr(char **d)
char ** hoc_temp_charptr(void)
void hoc_obj_ref(Object *obj)
char * hoc_object_name(Object *ob)
Symbol * hoc_lookup(const char *)
void hoc_push_object(Object *d)
#define HocContextRestore
hoc_List * hoc_l_newlist()
hoc_Item * hoc_l_lappendvoid(hoc_List *, void *)
Object ** hoc_objgetarg(int)
void class2oc(const char *, void *(*cons)(Object *), void(*destruct)(void *), Member_func *, int(*checkpoint)(void **), Member_ret_obj_func *, Member_ret_str_func *)
int * nrnpy_site_problem_p
int nrnpy_numbercheck(PyObject *po)
PyObject * nrnpy_hoc_pop()
static int hoccommand_exec(Object *)
static Object * py_alltoall_type(int, int)
static void * save_thread()
static void guisetval(Object *, double)
static double praxis_efun(Object *, Object *)
void(* nrnpy_hpoasgn)(Object *, int)
double(* nrnpy_func_call)(Object *, int, int *)
PyObject * hocobj_call_arg(int)
Object * nrnpy_pyobject_in_obj(PyObject *)
static int pysame(Object *, Object *)
Object * nrnpy_po2ho(PyObject *)
Object * hoc_new_object(Symbol *, void *)
static double func_call(Object *, int, int *)
void *(* nrnpy_save_thread)()
Symbol * nrnpy_pyobj_sym_
Object *(* nrnpy_callable_with_args)(Object *, int)
Object *(* nrnpy_pickle2po)(char *, size_t size)
static PyObject * hoccommand_exec_help1(PyObject *po)
PyObject * nrnpy_hoc2pyobject(Object *)
void(* nrnpy_py2n_component)(Object *, Symbol *, int, int)
void nrnpy_decref_defer(PyObject *)
static char * po2pickle(Object *, size_t *size)
int * mk_displ(int *cnts)
void *(* nrnpy_opaque_obj2pyobj_p_)(Object *)
static int hoccommand_exec_strret(Object *, char *, int)
static void grphcmdtool(Object *, int, double, double, int)
char *(* nrnpy_po2pickle)(Object *, size_t *size)
char *(* nrnpy_callpicklef)(char *, size_t size, int narg, size_t *retsize)
int(* nrnpy_guigetstr)(Object *, char **)
PyObject * nrnpy_ho2po(Object *)
void hoc_tobj_unref(Object **)
static PyObject * char2pylist(char *buf, int np, int *cnt, int *displ)
void(* nrnpy_restore_thread)(void *)
static void p_destruct(void *v)
void(* nrnpy_cmdtool)(Object *, int, double, double, int)
double(* nrnpy_guigetval)(Object *)
static void restore_thread(void *g)
static char * call_picklef(char *, size_t size, int narg, size_t *retsize)
Object *(* nrnpympi_alltoall_type)(int, int)
static void py2n_component(Object *, Symbol *, int, int)
int(* nrnpy_hoccommand_exec_strret)(Object *, char *, int)
static PyObject * main_module
static PyObject * hoccommand_exec_help(Object *ho)
int nrnpy_ho_eq_po(Object *, PyObject *)
static void * opaque_obj2pyobj(Object *ho)
void(* nrnpy_guisetval)(Object *, double)
void nrnpython_reg_real()
int(* nrnpy_pysame)(Object *, Object *)
char * nrnpyerr_str()
Full python traceback error message returned as string.
static PyObject * main_namespace
PyObject * nrnpy_pyCallObject(PyObject *, PyObject *)
static double guigetval(Object *)
static void call_python_with_section(Object *pyact, Section *sec)
static void * p_cons(Object *)
int(* nrnpy_hoccommand_exec)(Object *)
static void hpoasgn(Object *, int)
double(* nrnpy_praxis_efun)(Object *, Object *)
static int guigetstr(Object *, char **)
void(* nrnpy_call_python_with_section)(Object *, Section *)
static PyObject * unpickle(char *s, size_t size)
Object * hoc_pop_object()
static Object * pickle2po(char *, size_t size)
NPySecObj * newpysechelp(Section *sec)
static Object * callable_with_args(Object *, int)
static char * pickle(PyObject *p, size_t *size)
bool is_python_string(PyObject *python_string)
#define PyString_FromString
static Object ** py_gather(void *)
static void nrnmpi_char_broadcast(char *, int, int)
static Object ** py_broadcast(void *)
static void nrnmpi_int_broadcast(int *, int, int)
static Object ** py_allgather(void *)
static const char * fname(const char *name)