1 #include <../../nrnconf.h> 26 extern void* (*nrnpy_save_thread)();
42 extern char* (*nrnpy_po2pickle)(
Object*,
size_t* size);
43 extern Object* (*nrnpy_pickle2po)(
char*,
size_t size);
44 extern char* (*nrnpy_callpicklef)(
char*,
size_t size,
int narg,
80 #if NRNPYTHON_DYNAMICLOAD 81 extern int nrnpy_site_problem;
107 PyObject* args = PyTuple_Pack(1, (PyObject*)
newpysechelp(sec));
119 if (PyErr_Occurred()) {
125 extern void* (*nrnpy_opaque_obj2pyobj_p_)(
Object*);
127 assert(ho && ho->ctemplate->sym == nrnpy_pyobj_sym_);
138 nrnpy_pyobj_sym_ =
s;
160 #if NRNPYTHON_DYNAMICLOAD 178 if (ho->ctemplate->sym == nrnpy_pyobj_sym_) {
185 if (o2->ctemplate->sym == nrnpy_pyobj_sym_) {
221 printf(
"PyObject_CallObject callable\n");
222 PyObject_Print(callable, stdout, 0);
224 PyObject_Print(args, stdout, 0);
225 printf(
"\nreturn %p\n", p);
259 PyObject* head = pn->
po_;
273 if (strcmp(sym->
name,
"_") == 0) {
277 tail = PyObject_GetAttrString(head, sym->
name);
289 args = PyTuple_New(nindex);
290 for (i = 0; i < nindex; ++
i) {
294 if (PyTuple_SetItem(args, nindex - 1 - i, arg)) {
313 if (PyErr_Occurred()) {
321 arg = Py_BuildValue(
"l", (
long)
hoc_xpop());
325 result = PyObject_GetItem(tail, arg);
340 PyObject* pn = PyNumber_Float(result);
372 if (type == NUMBER) {
373 poright = PyFloat_FromDouble(
hoc_xpop());
374 }
else if (type ==
STRING) {
376 }
else if (type == OBJECTVAR || type ==
OBJECTTMP) {
381 hoc_execerror(
"Cannot assign that type to PythonObject", (
char*)0);
390 err = PyObject_SetAttrString(poleft, sym->
name, poright);
391 }
else if (nindex == 1) {
393 PyObject* a = PyObject_GetAttrString(poleft, sym->
name);
395 err = PyObject_SetItem(a, key, poright);
406 "HOC cannot handle PythonObject assignment with more than one index.");
418 PyObject* ps = PyObject_Str(po);
419 printf(
"defer %s\n", PyString_AsString(ps));
430 if (PyTuple_Check(po)) {
431 PyObject* args = PyTuple_GetItem(po, 1);
432 if (!PyTuple_Check(args)) {
433 args = PyTuple_Pack(1, args);
445 PyObject* args = PyTuple_New(0);
463 PyObject* po = Py_BuildValue(
"(OO)", pc, pv);
476 if (PyErr_Occurred()) {
481 PyObject* pn = PyNumber_Float(r);
482 double x = PyFloat_AsDouble(pn);
500 if (PyErr_Occurred()) {
513 PyObject* pn = PyObject_Str(r);
516 strncpy(buf, str.
c_str(), size);
517 buf[size - 1] =
'\0';
527 if (PyErr_Occurred()) {
539 PyObject* args = PyTuple_Pack(4,
PyInt_FromLong(type), PyFloat_FromDouble(x),
552 if (PyErr_Occurred()) {
562 PyObject* args = PyTuple_New((Py_ssize_t)narg);
567 for (
int i = 0;
i <
narg; ++
i) {
574 if (PyTuple_SetItem(args, (Py_ssize_t)(narg -
i - 1), item) != 0) {
581 PyObject* r = PyTuple_New(2);
582 PyTuple_SetItem(r, 1, args);
584 PyTuple_SetItem(r, 0, po);
597 PyObject* args = PyTuple_New((Py_ssize_t)narg);
602 for (
int i = 0;
i <
narg; ++
i) {
609 if (PyTuple_SetItem(args, (Py_ssize_t)(narg -
i - 1), item) != 0) {
626 if (PyErr_Occurred()) {
636 if (err) { *err = 1; }
639 PyObject* pn = PyNumber_Float(r);
640 rval = PyFloat_AsDouble(pn);
644 if (err) { *err = 0; }
653 PyObject*
p = PyTuple_GetItem(po, 0);
654 if (PySequence_Check(p) || PyMapping_Check(p)) {
655 r = PyObject_GetItem(p, PyTuple_GetItem(po, 1));
657 r = PyObject_GetAttr(p, PyTuple_GetItem(po, 1));
659 PyObject* pn = PyNumber_Float(r);
660 double x = PyFloat_AsDouble(pn);
668 PyObject* pn = PyFloat_FromDouble(x);
669 PyObject*
p = PyTuple_GetItem(po, 0);
670 if (PySequence_Check(p) || PyMapping_Check(p)) {
671 PyObject_SetItem(p, PyTuple_GetItem(po, 1), pn);
673 PyObject_SetAttr(p, PyTuple_GetItem(po, 1), pn);
682 PyObject* r = PyObject_GetAttr(PyTuple_GetItem(po, 0),
683 PyTuple_GetItem(po, 1));
684 PyObject* pn = PyObject_Str(r);
687 char* cp = name.
c_str();
688 if (*cpp && strcmp(*cpp, cp) == 0) {
694 *cpp =
new char[strlen(cp) + 1];
705 pickle = PyImport_ImportModule(
"pickle");
708 dumps = PyObject_GetAttrString(pickle,
"dumps");
709 loads = PyObject_GetAttrString(pickle,
"loads");
716 hoc_execerror(
"Neither Python cPickle nor pickle are available", 0);
722 static char*
pickle(PyObject*
p,
size_t* size) {
723 PyObject*
arg = PyTuple_Pack(1, p);
726 if (!r && PyErr_Occurred()) {
731 *size = PyBytes_Size(r);
732 char* buf1 = PyBytes_AsString(r);
733 char*
buf =
new char[*size];
734 for (
size_t i = 0;
i < *size; ++
i) {
743 if (ho && ho->ctemplate->sym == nrnpy_pyobj_sym_) {
753 PyObject* ps = PyBytes_FromStringAndSize(s, size);
754 PyObject*
arg = PyTuple_Pack(1, ps);
774 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_Exception)) {
775 PyObject *ptype, *pvalue, *ptraceback;
776 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
777 PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
779 PyObject* module_name =
NULL;
780 PyObject* pyth_module =
NULL;
781 PyObject* pyth_func =
NULL;
782 PyObject* py_str =
NULL;
788 ptraceback = Py_None;
789 Py_INCREF(ptraceback);
793 pyth_module = PyImport_Import(module_name);
796 pyth_func = PyObject_GetAttrString(pyth_module,
"format_exception");
798 py_str = PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback,
NULL);
804 Fprintf(stderr,
"nrnperr_str: Py2NRNString failed\n");
806 cmes = strdup(mes.
c_str());
808 Fprintf(stderr,
"nrnpyerr_str: strdup failed\n");
815 Fprintf(stderr,
"nrnpyerr_str failed\n");
818 Py_XDECREF(module_name);
819 Py_XDECREF(pyth_func);
820 Py_XDECREF(pyth_module);
823 Py_XDECREF(ptraceback);
840 PyObject* ps = PyBytes_FromStringAndSize(fname, size);
841 args = PyTuple_Pack(1, ps);
847 args = PyTuple_New(narg);
848 for (
int i = 0;
i <
narg; ++
i) {
850 if (PyTuple_SetItem(args, narg - 1 -
i, arg)) {
865 if (PyErr_Occurred()) {
869 char* rs =
pickle(result, retsize);
880 displ[
i + 1] = displ[
i] + cnts[
i];
886 PyObject* plist = PyList_New(np);
888 for (
int i = 0;
i < np; ++
i) {
891 PyList_SetItem(plist,
i, Py_None);
893 PyObject*
p =
unpickle(buf + displ[
i], cnt[i]);
894 PyList_SetItem(plist, i, p);
904 char* sbuf =
pickle(psrc, &sz);
906 int* rcnt =
new int[np];
908 nrnmpi_int_allgather_inplace(rcnt, 1);
910 char* rbuf =
new char[rdispl[np]];
912 nrnmpi_char_allgatherv(sbuf, rbuf, rcnt, rdispl);
915 PyObject* pdest =
char2pylist(rbuf, np, rcnt, rdispl);
925 char* sbuf =
pickle(psrc, &sz);
932 nrnmpi_int_gather(&scnt, rcnt, 1, root);
937 rbuf =
new char[rdispl[np]];
940 nrnmpi_char_gatherv(sbuf, scnt, rbuf, rcnt, rdispl, root);
943 PyObject* pdest = Py_None;
969 PyObject* pdest = psrc;
984 PyObject* psrc =
NULL;
985 PyObject* pdest =
NULL;
987 if (type == 1 || type == 5) {
991 if (!PyList_Check(psrc)) {
994 if (PyList_Size(psrc) != np) {
1006 pdest = PyList_GetItem(psrc, 0);
1025 pdest = PyList_New(1);
1026 PyList_SetItem(pdest, 0, psrc);
1044 }
else if (type != 1 && type != 5) {
1046 if (root < 0 || root >= np) {
1051 }
else if (type == 4) {
1075 for (
int i = 0;
i < np; ++
i) {
1079 PyObject* iterator = PyObject_GetIter(psrc);
1082 size_t bufsz = 100000;
1087 s =
new char[bufsz];
1090 for (
size_t i = 0; (p = PyIter_Next(iterator)) !=
NULL; ++
i) {
1097 char* b =
pickle(p, &sz);
1099 if (curpos + sz >= bufsz) {
1100 bufsz = bufsz * 2 + sz;
1101 char* s2 =
new char[bufsz];
1102 for (
size_t i = 0;
i < curpos; ++
i) {
1108 for (
size_t j = 0;
j < sz; ++
j) {
1109 s[curpos +
j] = b[
j];
1117 Py_DECREF(iterator);
1128 int* ones =
new int[np];
1129 for (
int i = 0;
i < np; ++
i) {
1143 pdest = PyTuple_New(2);
1144 PyTuple_SetItem(pdest, 0, Py_BuildValue(
"l", (
long)sdispl[np]));
1145 PyTuple_SetItem(pdest, 1, Py_BuildValue(
"l", (
long)rdispl[np]));
1151 r =
new char[rdispl[np] + 1];
1152 nrnmpi_char_alltoallv(s, scnt, sdispl, r, rcnt, rdispl);
1168 nrnmpi_int_scatter(scnt, rcnt, 1, root);
1169 r =
new char[rcnt[0] + 1];
1173 nrnmpi_char_scatterv(s, scnt, sdispl, r, rcnt[0], root);
1175 if (scnt)
delete[] scnt;
1176 if (sdispl)
delete[] sdispl;
1179 pdest =
unpickle(r,
size_t(rcnt[0]));
void(* nrnpy_cmdtool)(Object *, int, double, double, int)
static int hoccommand_exec(Object *)
void(* nrnpy_py2n_component)(Object *, Symbol *, int, int)
PyObject * nrnpy_hoc_pop()
int(* nrnpy_guigetstr)(Object *, char **)
#define HocContextRestore
static PyObject * unpickle(char *s, size_t size)
hoc_List * hoc_l_newlist()
static int hoccommand_exec_strret(Object *, char *, int)
static PyObject * hoccommand_exec_help1(PyObject *po)
int * mk_displ(int *cnts)
static void * p_cons(Object *)
static Object * pickle2po(char *, size_t size)
static PyObject * main_module
static int guigetstr(Object *, char **)
void(* nrnpy_hpoasgn)(Object *, int)
Object *(* nrnpy_pickle2po)(char *, size_t size)
Symbol * hoc_lookup(const char *)
void(* nrnpy_restore_thread)(void *)
PyObject * nrnpy_pyCallObject(PyObject *, PyObject *)
static void p_destruct(void *v)
char * hoc_object_name(Object *ob)
static PyObject * main_namespace
static void py2n_component(Object *, Symbol *, int, int)
int(* nrnpy_hoccommand_exec_strret)(Object *, char *, int)
static void nrnmpi_char_broadcast(char *, int, int)
Object *(* nrnpy_callable_with_args)(Object *, int)
static char * pickle(PyObject *p, size_t *size)
static Object ** py_broadcast(void *)
static void hpoasgn(Object *, int)
static int pysame(Object *, Object *)
void(* nrnpy_call_python_with_section)(Object *, Section *)
Object * hoc_pop_object()
sprintf(buf," if (secondorder) {\ " int _i;\" " for(_i=0;_i< %d;++_i) {\" " _p[_slist%d[_i]]+=dt *_p[_dlist%d[_i]];\" " }}\", numeqn, listnum, listnum)
void nrnpy_decref_defer(PyObject *)
int(* nrnpy_pysame)(Object *, Object *)
static void guisetval(Object *, double)
Object * nrnpy_po2ho(PyObject *)
PyObject * hocobj_call_arg(int)
double(* nrnpy_func_call)(Object *, int, int *)
static void * save_thread()
void hoc_tobj_unref(Object **)
static char * call_picklef(char *, size_t size, int narg, size_t *retsize)
char * nrnpyerr_str()
Full python traceback error message returned as string.
static void nrnmpi_int_broadcast(int *, int, int)
void class2oc(const char *, void *(*cons)(Object *), void(*destruct)(void *), Member_func *, int(*checkpoint)(void **), Member_ret_obj_func *, Member_ret_str_func *)
static void call_python_with_section(Object *pyact, Section *sec)
static Object * py_alltoall_type(int, int)
hoc_Item * hoc_l_lappendvoid(hoc_List *, void *)
static PyObject * hoccommand_exec_help(Object *ho)
int nrnpy_numbercheck(PyObject *po)
int nrnpy_ho_eq_po(Object *, PyObject *)
static Object ** py_allgather(void *)
static const char * fname(const char *name)
double(* nrnpy_praxis_efun)(Object *, Object *)
void hoc_execerror(const char *, const char *)
int * nrnpy_site_problem_p
static void nrnmpi_int_alltoallv(int *s, int *scnt, int *sdispl, int *r, int *rcnt, int *rdispl)
bool is_python_string(PyObject *python_string)
PyObject * nrnpy_hoc2pyobject(Object *)
void hoc_push_object(Object *d)
static Object ** py_gather(void *)
Object * hoc_new_object(Symbol *, void *)
char *(* nrnpy_callpicklef)(char *, size_t size, int narg, size_t *retsize)
static void * opaque_obj2pyobj(Object *ho)
static double func_call(Object *, int, int *)
void hoc_obj_ref(Object *obj)
static PyObject * char2pylist(char *buf, int np, int *cnt, int *displ)
PyObject * nrnpy_ho2po(Object *)
static void restore_thread(void *g)
static char * po2pickle(Object *, size_t *size)
static double guigetval(Object *)
#define PyString_FromString
Symbol * nrnpy_pyobj_sym_
double(* nrnpy_guigetval)(Object *)
void(* nrnpy_guisetval)(Object *, double)
void nrnpython_reg_real()
static Object * callable_with_args(Object *, int)
NPySecObj * newpysechelp(Section *sec)
char *(* nrnpy_po2pickle)(Object *, size_t *size)
void hoc_pushstr(char **d)
Object *(* nrnpympi_alltoall_type)(int, int)
int(* nrnpy_hoccommand_exec)(Object *)
char ** hoc_temp_charptr(void)
void *(* nrnpy_save_thread)()
Object ** hoc_objgetarg(int)
static double praxis_efun(Object *, Object *)
void *(* nrnpy_opaque_obj2pyobj_p_)(Object *)
static void grphcmdtool(Object *, int, double, double, int)
Object * nrnpy_pyobject_in_obj(PyObject *)