2 #include <structmember.h>
12 #include "../nrniv/shapeplt.h"
16 #if defined(NRNPYTHON_DYNAMICLOAD) && NRNPYTHON_DYNAMICLOAD > 0
18 #undef NRNPYTHON_DYNAMICLOAD
19 #define NRNPYTHON_DYNAMICLOAD PY_MAJOR_VERSION
34 extern Object** (*nrnpy_gui_helper_)(
const char*,
Object*);
35 extern Object** (*nrnpy_gui_helper3_)(
const char*,
Object*, int);
36 extern char** (*nrnpy_gui_helper3_str_)(
const char*,
Object*, int);
38 extern void* (*nrnpy_get_pyobj)(
Object* obj);
69 extern PyObject*
nrnpy_cas(PyObject*, PyObject*);
81 extern IvocVect* (*nrnpy_vec_from_python_p_)(
void*);
82 extern Object** (*nrnpy_vec_to_python_p_)(
void*);
83 extern Object** (*nrnpy_vec_as_numpy_helper_)(int,
double*);
128 #define HocTopContextSet \
129 if (hoc_thisobject) { \
132 assert(hoc_thisobject == 0);
133 #define HocContextRestore
162 static PyObject*
nrnexec(PyObject*
self, PyObject* args) {
164 if (!PyArg_ParseTuple(args,
"s", &cmd)) {
168 return Py_BuildValue(
"i", b ? 1 : 0);
171 static PyObject*
hoc_ac(PyObject*
self, PyObject* args) {
172 PyArg_ParseTuple(args,
"|d", &
hoc_ac_);
173 return Py_BuildValue(
"d",
hoc_ac_);
177 {
"execute",
nrnexec, METH_VARARGS,
"Execute a hoc command, return 1 on success, 0 on failure."},
178 {
"hoc_ac",
hoc_ac, METH_VARARGS,
"Get (or set) the scalar hoc_ac_."},
193 if (self->indices_) {
194 delete[]
self->indices_;
199 ((PyObject*)
self)->ob_type->tp_free((PyObject*)
self);
211 if (subself ==
NULL) {
218 self->indices_ =
NULL;
222 if (kwds && PyDict_Check(kwds)) {
223 PyObject* base = PyDict_GetItemString(kwds,
"hocbase");
232 PyDict_DelItemString(kwds,
"hocbase");
248 PyErr_SetString(PyExc_TypeError,
"HOC base class not valid");
256 static int hocobj_init(PyObject* subself, PyObject* args, PyObject* kwds) {
260 if (subself !=
NULL) {
266 self->indices_ =
NULL;
294 self->ho_ ?
"." :
"",
297 for (
int i = 0;
i <
self->nindex_; ++
i) {
298 sprintf(cp += strlen(cp),
"[%d]", self->indices_[
i]);
300 sprintf(cp += strlen(cp),
"[?]");
302 sprintf(cp += strlen(cp),
"()");
305 sprintf(cp,
"<hoc ref value %g>", self->u.x_);
307 sprintf(cp,
"<hoc ref str \"%s\">", self->u.s_);
309 sprintf(cp,
"<hoc ref pstr \"%s\">", *self->u.pstr_);
313 sprintf(cp,
"<all section iterator next>");
315 sprintf(cp,
"<SectionList iterator>");
317 sprintf(cp,
"<pointer to hoc scalar %g>", self->u.px_ ? *self->u.px_ : -1e100);
319 sprintf(cp,
"<incomplete pointer to hoc array %s>", self->sym_->name);
321 sprintf(cp,
"<TopLevelHocInterpreter>");
323 po = Py_BuildValue(
"s",
buf);
338 int i,
narg = PyTuple_Size(args);
340 PyObject* po = PyTuple_GetItem(args,
i);
344 PyObject* pn = PyNumber_Float(po);
357 s2free.push_back(*ts);
358 hoc_execerr_ext(
"python string arg cannot decode into c_str. Pyerr message: %s",
362 s2free.push_back(*ts);
407 std::vector<char*>::iterator it = s2free.begin();
408 for (; it != s2free.end(); ++it) {
420 if (!sym && strcmp(
name,
"delay") == 0) {
422 }
else if (!sym && ho->
aliases) {
431 if (sym && sym->
type == UNDEF) {
436 sprintf(
e,
"'%s' is not a defined hoc variable name.",
name);
437 PyErr_SetString(PyExc_LookupError,
e);
461 fc[3].
i = po->
ho_->ctemplate->id;
480 int rval = PyNumber_Check(po);
482 if (rval == 1 && po->ob_type->tp_as_sequence) {
488 PyObject* tmp = PyNumber_Float(po);
507 po = Py_BuildValue(
"");
560 result = Py_BuildValue(
"d", *px);
562 PyErr_SetString(PyExc_AttributeError,
"POINTER is NULL");
587 if (PyArg_Parse(po,
"s", &s) == 1) {
596 if (PyArg_Parse(po,
"d", &x) == 1) {
603 PyErr_SetString(PyExc_AttributeError,
"POINTER is NULL");
615 PyErr_SetString(PyExc_TypeError,
"argument cannot be a hoc object intermediate");
636 return (
void*) Py_BuildValue(
"i", (
long)
hoc_xpop());
640 return (
void*) PyBool_FromLong((
long)
hoc_xpop());
643 static void*
fcall(
void* vself,
void* vargs) {
651 std::vector<char*> strings_to_free;
656 self->nindex_ =
narg;
668 if (self->sym_->type == BLTIN) {
674 }
else if (self->sym_->type == TEMPLATE) {
685 fc[1].sym =
self->sym_;
711 PyObject* section = 0;
713 if (kwrds && PyDict_Check(kwrds)) {
715 PyObject* keys = PyDict_Keys(kwrds);
716 assert(PyList_Check(keys));
717 int n = PyList_Size(keys);
718 for (
int i = 0;
i <
n; ++
i) {
719 PyObject*
key = PyList_GetItem(keys,
i);
720 PyObject*
value = PyDict_GetItem(kwrds,
key);
721 printf(
"%s %s\n", PyUnicode_AsUTF8(
key), PyUnicode_AsUTF8(PyObject_Str(
value)));
724 section = PyDict_GetItemString(kwrds,
"sec");
725 int num_kwargs = PyDict_Size(kwrds);
726 if (num_kwargs > 1) {
727 PyErr_SetString(PyExc_RuntimeError,
"invalid keyword argument");
741 PyErr_SetString(PyExc_TypeError,
"sec is not a Section");
747 PyErr_SetString(PyExc_RuntimeError,
"invalid keyword argument");
762 PyErr_SetString(PyExc_RuntimeError,
"hocobj_call error");
765 result = (PyObject*)
fcall((
void*)
self, (
void*) args);
769 PyErr_SetString(PyExc_TypeError,
"object is not callable");
792 if (sym->
type == VAR &&
831 for (
i = 0;
i <
n; ++
i) {
891 PyObject* nn = Py_BuildValue(
"");
892 for (
Symbol* s =
sl->first; s; s = s->next) {
893 if (s->type == UNDEF) {
897 if (strcmp(s->name,
"del") == 0) {
898 PyDict_SetItemString(dict,
"delay", nn);
900 PyDict_SetItemString(dict, s->name, nn);
912 pdoc = PyImport_ImportModule(
"neuron.doc");
914 PyErr_SetString(PyExc_ImportError,
"Failed to import neuron.doc documentation module.");
920 PyErr_SetString(PyExc_AttributeError,
921 "neuron.doc module does not have attribute 'get_docstring'!");
934 descrgetfunc f = descr->ob_type->tp_descr_get;
936 result = f(descr, subself, (PyObject*) Py_TYPE(subself));
947 PyErr_SetString(PyExc_TypeError,
"not a compound type");
954 char*
n =
name.c_str();
956 name.set_pyerr(PyExc_TypeError,
"attribute name must be a string");
964 return PyObject_GenericGetAttr(
p, pyname);
972 if (strcmp(
n,
"__dict__") == 0) {
976 sl =
self->ho_->ctemplate->symtable;
977 }
else if (self->sym_ && self->sym_->type == TEMPLATE) {
978 sl =
self->sym_->u.ctemplate->symtable;
980 PyObject* dict = PyDict_New();
992 PyDict_SetItemString(dict,
"__array_interface__", Py_None);
993 }
else if (
is_obj_type(self->ho_,
"RangeVarPlot") ||
995 PyDict_SetItemString(dict,
"plot", Py_None);
998 }
else if (strncmp(
n,
"_ref_", 5) == 0) {
1000 PyErr_SetString(PyExc_TypeError,
"not a HocTopLevelInterpreter or HocObject");
1003 sym =
getsym(
n + 5, self->ho_, 0);
1005 return PyObject_GenericGetAttr((PyObject*) subself, pyname);
1011 }
else if (self->type_ ==
PyHoc::HocObject && !self->ho_->ctemplate->constructor) {
1018 char** cpp =
OPSTR(sym);
1025 "Hoc pointer error, %s is not a hoc variable or range variable or strdef",
1027 PyErr_SetString(PyExc_TypeError,
buf);
1032 }
else if (
is_obj_type(self->ho_,
"Vector") && strcmp(
n,
"__array_interface__") == 0) {
1035 Vect*
v = (
Vect*) self->ho_->u.this_pointer;
1036 int size =
v->size();
1039 return Py_BuildValue(
"{s:(i),s:s,s:i,s:(N,O)}",
1047 PyLong_FromVoidPtr(x),
1050 }
else if (
is_obj_type(self->ho_,
"RangeVarPlot") && strcmp(
n,
"plot") == 0) {
1051 return PyObject_CallFunctionObjArgs(
rvp_plot, (PyObject*)
self,
NULL);
1052 }
else if (
is_obj_type(self->ho_,
"PlotShape") && strcmp(
n,
"plot") == 0) {
1054 }
else if (strcmp(
n,
"__doc__") == 0) {
1056 PyObject* docobj =
NULL;
1058 docobj = Py_BuildValue(
"s s",
1059 self->ho_->ctemplate->sym->name,
1060 self->sym_ ? self->sym_->name :
"");
1061 }
else if (self->sym_) {
1063 docobj = Py_BuildValue(
"s s",
"", self->sym_->name);
1067 docobj = Py_BuildValue(
"s s",
"",
"");
1077 strncmp(
n,
"__nrnsec_0x", 11) == 0) {
1080 PyErr_SetString(PyExc_NameError,
n);
1093 PyErr_SetString(PyExc_NameError,
n);
1106 return PyObject_GenericGetAttr((PyObject*) subself, pyname);
1114 PyErr_SetString(PyExc_TypeError,
1115 "No hoc method for a callable. Missing parentheses before the '.'?");
1119 PyErr_SetString(PyExc_TypeError,
"Missing array index");
1125 po->
ho_ =
self->ho_;
1131 if (
t == VAR ||
t ==
STRING ||
t == OBJECTVAR ||
t == RANGEVAR ||
t == SECTION ||
1139 PyErr_SetString(PyExc_TypeError,
"No value");
1145 if (
t == SECTION ||
t == SECTIONREF) {
1180 PyErr_SetString(PyExc_TypeError,
"Section access unspecified");
1191 PyErr_SetString(PyExc_TypeError,
"Cannot be a reference");
1243 case HOCOBJFUNCTION:
1250 po->
ho_ =
self->ho_;
1258 case SETPOINTERKEYWORD:
1267 PyErr_Format(PyExc_TypeError,
1268 "Cannot access %s directly; it is a range variable and may be accessed "
1269 "via a section or segment.",
1272 PyErr_Format(PyExc_TypeError,
1273 "Cannot access %s (NEURON type %d) directly.",
1285 if (!PyArg_ParseTuple(args,
"O", &
name)) {
1296 result = PyObject_GenericGetAttr(subself,
name);
1315 int issub = ((PyTypeObject*) PyObject_Type(subself) !=
hocobject_type);
1319 if (PyObject_HasAttr(subself, pyname)) {
1322 return PyObject_GenericSetAttr(subself, pyname,
value);
1334 char*
n =
name.c_str();
1336 name.set_pyerr(PyExc_TypeError,
"attribute name must be a string");
1343 return PyObject_GenericSetAttr(subself, pyname,
value);
1347 return PyObject_GenericSetAttr(
p, pyname,
value);
1348 }
else if (strncmp(
n,
"_ref_", 5) == 0) {
1351 if (rvsym && rvsym->
type == RANGEVAR) {
1354 PyErr_SetString(PyExc_TypeError,
"Point_process not located in a section");
1360 sym =
getsym(
n, self->ho_, 1);
1362 sym =
getsym(
n, self->ho_, 1);
1371 po->
ho_ =
self->ho_;
1385 PyErr_SetString(PyExc_TypeError,
"No value");
1393 sprintf(
e,
"'%s' requires subscript for assignment",
n);
1394 PyErr_SetString(PyExc_TypeError,
e);
1399 PyErr_SetString(PyExc_TypeError,
"not assignable");
1407 PyErr_SetString(PyExc_TypeError,
"wrong number of subscripts");
1414 PyErr_SetString(PyExc_TypeError,
"Section access unspecified");
1421 if (PyArg_Parse(
value,
"i", &
i) != 0 &&
i > 0 &&
i <= 32767) {
1424 PyErr_SetString(PyExc_ValueError,
1425 "nseg must be an integer in range 1 to 32767");
1429 err = PyArg_Parse(
value,
"d", &x) == 0;
1441 "Assignment to %s value of physical constant %s",
1444 err = PyErr_WarnEx(PyExc_Warning, mes, 1);
1455 if (PyArg_Parse(
value,
"s", &s) == 1) {
1468 if (PyArg_Parse(
value,
"O", &po) == 1) {
1469 if (po == Py_None) {
1475 PyErr_SetString(PyExc_TypeError,
1476 "argument cannot be a hoc object intermediate");
1492 PyErr_SetString(PyExc_TypeError,
"not assignable");
1526 if (ix < 0 ||
n <= ix) {
1533 (po->
ho_ && po->
sym_) ?
"." :
"",
1535 PyErr_SetString(PyExc_IndexError,
e);
1549 PyErr_SetString(PyExc_TypeError,
"hoc.SectionList has no len()");
1555 }
else if (po->
sym_ && po->
sym_->type == TEMPLATE) {
1556 return po->
sym_->u.ctemplate->count;
1558 PyErr_SetString(PyExc_TypeError,
"hoc all section iterator() has no len()");
1561 PyErr_SetString(PyExc_TypeError,
"hoc SectionList iterator() has no len()");
1564 PyErr_SetString(PyExc_TypeError,
"Most HocObject have no len()");
1581 }
else if (po->
sym_ && po->
sym_->type == TEMPLATE) {
1601 return PySeqIter_New(
self);
1603 return PySeqIter_New(
self);
1619 return PySeqIter_New(
self);
1620 }
else if (po->
sym_ && po->
sym_->type == TEMPLATE) {
1625 PyErr_SetString(PyExc_TypeError,
"Not an iterable HocObject");
1690 if (curitem != ql) {
1748 }
else if (po->
sym_->type == TEMPLATE) {
1750 if (
q != po->
sym_->u.ctemplate->olist) {
1770 PyErr_SetString(PyExc_IndexError,
"index for hoc ref must be 0");
1792 PyErr_SetString(PyExc_IndexError,
e);
1795 return PyFloat_FromDouble(
vector_vec(hv)[ix]);
1799 if (ix < 0 || ix >= hl->
count()) {
1802 PyErr_SetString(PyExc_IndexError,
e);
1808 PyErr_SetString(PyExc_TypeError,
"unsubscriptable object");
1815 PyErr_SetString(PyExc_TypeError,
"unsubscriptable object");
1817 }
else if (po->
sym_->type == TEMPLATE) {
1822 if (ob->
index == ix) {
1827 sprintf(
e,
"%s[%ld] instance does not exist", po->
sym_->name, ix);
1828 PyErr_SetString(PyExc_IndexError,
e);
1834 PyErr_SetString(PyExc_TypeError,
e);
1843 result = (PyObject*) ponew;
1847 if (po->
sym_->type == SECTION || po->
sym_->type == SECTIONREF) {
1896 PyErr_SetString(PyExc_TypeError,
"incomplete hoc pointer");
1900 PyErr_SetString(PyExc_IndexError,
"index for hoc ref must be 0");
1904 PyArg_Parse(
arg,
"d", po->
u.
px_ +
i);
1906 PyArg_Parse(
arg,
"d", &po->
u.
x_);
1909 PyArg_Parse(
arg,
"s", &ts);
1913 PyArg_Parse(
arg,
"s", &ts);
1917 PyArg_Parse(
arg,
"O", &tp);
1930 if (
i >= vec_size ||
i < 0) {
1931 PyErr_SetString(PyExc_IndexError,
"index out of bounds");
1939 PyErr_SetString(PyExc_TypeError,
"unsubscriptable object");
1944 PyErr_SetString(PyExc_TypeError,
"wrong number of subscripts");
1951 if (po->
sym_->type == SECTION) {
1952 PyErr_SetString(PyExc_TypeError,
"not assignable");
1972 if (PyArg_Parse(
arg,
"O", &pyo) == 1) {
1983 PyErr_SetString(PyExc_TypeError,
"not assignable");
1991 static PyObject*
mkref(PyObject*
self, PyObject* args) {
1994 if (PyArg_ParseTuple(args,
"O", &pa) == 1) {
1998 PyObject* pn = PyNumber_Float(pa);
1999 result->u.x_ = PyFloat_AsDouble(pn);
2006 str.
set_pyerr(PyExc_TypeError,
"string arg must have only ascii characters");
2010 char* cpa = str.
c_str();
2016 return (PyObject*)
result;
2018 PyErr_SetString(PyExc_TypeError,
"single arg must be number, string, or Object");
2040 return (PyObject*)
result;
2059 str.
set_pyerr(PyExc_TypeError,
"POINTER name can contain only ascii characters");
2068 PyErr_SetString(PyExc_TypeError,
"Point_process not located in a section");
2071 ppd = &
prop->dparam[sym->
u.
rng.index].pval;
2084 PyErr_SetString(PyExc_TypeError,
2085 "setpointer(_ref_hocvar, 'POINTER_name', point_process or "
2093 PyObject* po =
NULL;
2095 po = Py_BuildValue(
"O", PyLong_FromVoidPtr(ho));
2098 PyErr_SetString(PyExc_TypeError,
"HocObject does not wrap a Hoc Object");
2111 result = self_ptr < other_ptr;
2114 result = self_ptr <= other_ptr;
2117 result = self_ptr == other_ptr;
2120 result = self_ptr != other_ptr;
2123 result = self_ptr > other_ptr;
2126 result = self_ptr >= other_ptr;
2137 void* self_ptr = (
void*) (self->ho_);
2138 void* other_ptr = (
void*) other;
2139 bool are_equal =
true;
2141 if (((
PyHocObject*) other)->type_ == self->type_) {
2142 switch (self->type_) {
2148 self_ptr = (
void*)
self;
2151 if (self->ho_ != (
void*) (((
PyHocObject*) other)->ho_)) {
2154 }
else if (op == Py_EQ) {
2158 PyErr_SetString(PyExc_TypeError,
"this comparison is undefined");
2161 self_ptr = (
void*) self->sym_;
2162 other_ptr = (
void*) (((
PyHocObject*) other)->sym_);
2165 self_ptr =
self->u.px_;
2166 other_ptr = (
void*) (((
PyHocObject*) other)->u.px_);
2170 if (op != Py_EQ && op != Py_NE) {
2172 PyErr_SetString(PyExc_TypeError,
"this comparison is undefined");
2175 if (self->ho_ != (
void*) (((
PyHocObject*) other)->ho_)) {
2177 other_ptr = (
void*) (((
PyHocObject*) other)->ho_);
2180 if (self->nindex_ != (((
PyHocObject*) other)->nindex_) ||
2187 for (
int i = 0;
i <
self->nindex_;
i++) {
2188 if (self->indices_[
i] != ((
PyHocObject*) other)->indices_[
i]) {
2192 if (are_equal == (op == Py_EQ)) {
2197 other_ptr = (
void*) (((
PyHocObject*) other)->ho_);
2202 }
else if (op == Py_NE) {
2206 PyErr_SetString(PyExc_TypeError,
"this comparison is undefined");
2215 if (PyArg_ParseTuple(args,
"O", &po)) {
2230 if (PyObject_HasAttrString(po,
"__array_interface__")) {
2231 PyObject* ai = PyObject_GetAttrString(po,
"__array_interface__");
2232 Py2NRNString typestr(PyDict_GetItemString(ai,
"typestr"));
2234 data = PyLong_AsVoidPtr(PyTuple_GetItem(PyDict_GetItemString(ai,
"data"), 0));
2236 if (PyErr_Occurred()) {
2239 pstride = PyDict_GetItemString(ai,
"strides");
2240 if (pstride == Py_None) {
2242 }
else if (PyTuple_Check(pstride)) {
2243 if (PyTuple_Size(pstride) == 1) {
2244 psize = PyTuple_GetItem(pstride, 0);
2245 if (PyLong_Check(psize)) {
2246 stride = PyLong_AsLong(psize);
2251 PyErr_SetString(PyExc_TypeError,
2252 "array_interface stride element of invalid type.");
2259 PyErr_SetString(PyExc_TypeError,
"array_interface stride object of invalid type.");
2265 return static_cast<char*
>(
data);
2277 if (!PySequence_Check(po)) {
2278 if (!PyIter_Check(po)) {
2280 " does not support the Python Sequence or Iterator protocol");
2282 PyObject* iterator = PyObject_GetIter(po);
2286 while ((
p = PyIter_Next(iterator)) !=
NULL) {
2287 if (!PyNumber_Check(
p)) {
2292 hv->push_back(PyFloat_AsDouble(
p));
2296 Py_DECREF(iterator);
2298 int size = PySequence_Size(po);
2305 for (
int i = 0,
j = 0;
i < size; ++
i,
j +=
stride) {
2306 x[
i] = *(
double*) (y +
j);
2309 for (
int i = 0;
i < size; ++
i) {
2310 PyObject*
p = PySequence_GetItem(po,
i);
2311 if (!PyNumber_Check(
p)) {
2316 x[
i] = PyFloat_AsDouble(
p);
2325 static PyObject* (*vec_as_numpy)(int,
double*);
2338 PyObject* args = PyTuple_New(0);
2347 delete[](*save_data);
2349 *save_data_size = PyByteArray_Size(
result);
2350 *save_data =
new char[*save_data_size];
2351 memcpy(*save_data, PyByteArray_AsString(
result), *save_data_size);
2354 *save_data_size = 0;
2360 PyObject* args = PyTuple_New(1);
2361 PyObject* py_data = PyByteArray_FromStringAndSize(
data, size);
2363 if (py_data ==
NULL) {
2367 PyTuple_SetItem(args, 0, py_data);
2375 hoc_execerror(
"SaveState:",
"Missing data restore function.");
2381 PyObject* plotshape_plot0,
2382 PyObject* get_mech_object_0,
2383 PyObject* store_savestate,
2384 PyObject* restore_savestate) {
2404 const double result = PyFloat_AsDouble(pyobj);
2420 Py_DECREF((PyObject*) pyobj);
2430 PyObject* args = PyTuple_New(
narg + 3);
2432 PyTuple_SetItem(args, 0, pyname);
2433 for (
int iarg = 0; iarg <
narg; iarg++) {
2434 const int iiarg = iarg + 1;
2437 PyTuple_SetItem(args, iarg + 3, active_obj);
2442 PyObject* py_ptr = (PyObject*) ptr_nrn;
2444 PyTuple_SetItem(args, iarg + 3, py_ptr);
2446 if (handle_strptr > 0) {
2450 PyTuple_SetItem(args, iarg + 3, py_ptr);
2453 PyTuple_SetItem(args, iarg + 3, py_str);
2456 PyObject* py_double = PyFloat_FromDouble(*
getarg(iiarg));
2457 PyTuple_SetItem(args, iarg + 3, py_double);
2469 PyTuple_SetItem(args, 1, my_obj);
2479 PyTuple_SetItem(args, 2, my_obj2);
2480 PyObject* po = PyObject_CallObject(
gui_callback, args);
2481 if (PyErr_Occurred()) {
2485 po = PyLong_FromLong(0);
2525 PyObject* po = (*vec_as_numpy)(size,
data);
2526 if (po != Py_None) {
2539 int size = hv->size();
2557 if (!PySequence_Check(po)) {
2560 if (size != PySequence_Size(po)) {
2564 if ((po = PyList_New(size)) ==
NULL) {
2565 hoc_execerror(
"Could not create new Python List with correct size.", 0);
2576 for (
int i = 0,
j = 0;
i < size; ++
i,
j +=
stride) {
2577 *(
double*) (y +
j) = x[
i];
2579 }
else if (PyList_Check(po)) {
2580 for (
int i = 0;
i < size; ++
i) {
2581 PyObject* pn = PyFloat_FromDouble(x[
i]);
2582 if (!pn || PyList_SetItem(po,
i, pn) == -1) {
2589 for (
int i = 0;
i < size; ++
i) {
2590 PyObject* pn = PyFloat_FromDouble(x[
i]);
2591 if (!pn || PySequence_SetItem(po,
i, pn) == -1) {
2621 PyErr_SetString(PyExc_TypeError,
"get_plotshape_variable only takes PlotShape objects");
2624 void* that = pho->
ho_->u.this_pointer;
2627 spi = ((ShapePlot*) that);
2636 PyObject* py_obj = (PyObject*) spi->
varobj();
2641 return Py_BuildValue(
"sOffN", spi->
varname(), py_obj, spi->
low(), spi->
high(), py_sl);
2651 PyErr_SetString(PyExc_TypeError,
"HocObject: Only Vector instance can be pickled");
2657 PyObject* mod = PyImport_ImportModule(
"neuron");
2661 PyObject* obj = PyObject_GetAttrString(mod,
"_pkl");
2664 PyErr_SetString(PyExc_Exception,
"neuron module has no _pkl method.");
2668 PyObject*
ret = PyTuple_New(3);
2672 PyTuple_SET_ITEM(
ret, 0, obj);
2680 PyObject* state = PyTuple_New(4);
2681 if (state ==
NULL) {
2687 PyObject* str = PyBytes_FromStringAndSize((
const char*) (&x),
sizeof(
double));
2693 PyTuple_SET_ITEM(state, 1, str);
2695 str = PyBytes_FromStringAndSize((
const char*)
vector_vec(vec), vec->size() *
sizeof(
double));
2701 PyTuple_SET_ITEM(state, 3, str);
2702 PyTuple_SET_ITEM(
ret, 2, state);
2707 #define BYTEHEADER \
2711 int BYTESWAP_FLAG = 0;
2712 #define BYTESWAP(_X__, _TYPE__) \
2713 if (BYTESWAP_FLAG == 1) { \
2714 _IN__ = (char*) &(_X__); \
2715 for (_II__ = 0; _II__ < sizeof(_TYPE__); _II__++) { \
2716 _OUT__[_II__] = _IN__[sizeof(_TYPE__) - _II__ - 1]; \
2718 (_X__) = *((_TYPE__*) &_OUT__); \
2725 PyObject* rawdata =
NULL;
2726 PyObject* endian_data;
2730 if (!PyArg_ParseTuple(args,
"(iOiO)", &version, &endian_data, &size, &rawdata)) {
2733 Py_INCREF(endian_data);
2737 if (!PyBytes_Check(rawdata) || !PyBytes_Check(endian_data)) {
2738 PyErr_SetString(PyExc_TypeError,
"pickle not returning string");
2739 Py_DECREF(endian_data);
2745 if (PyBytes_AsStringAndSize(endian_data, &datastr, &len) < 0) {
2746 Py_DECREF(endian_data);
2750 if (len !=
sizeof(
double)) {
2751 PyErr_SetString(PyExc_ValueError,
"endian_data size is not sizeof(double)");
2752 Py_DECREF(endian_data);
2757 if (*((
double*) datastr) != 2.0) {
2760 Py_DECREF(endian_data);
2762 if (PyBytes_AsStringAndSize(rawdata, &datastr, &len) < 0) {
2766 if (len != size *
sizeof(
double)) {
2767 PyErr_SetString(PyExc_ValueError,
"buffer size does not match array size");
2771 if (BYTESWAP_FLAG) {
2772 double* x = (
double*) datastr;
2773 for (
int i = 0;
i < size; ++
i) {
2777 memcpy((
char*)
vector_vec(vec), datastr, len);
2784 #if defined(HAVE_DLFCN_H) && !defined(MINGW)
2786 int rval =
dladdr((
const void*) Py_Initialize, &
info);
2788 PyErr_SetString(PyExc_Exception,
2789 "dladdr: Py_Initialize could not be matched to a shared object");
2792 if (!
info.dli_fname) {
2793 PyErr_SetString(PyExc_Exception,
2794 "dladdr: No symbol matching Py_Initialize could be found.");
2797 return Py_BuildValue(
"s",
info.dli_fname);
2806 {
"baseattr",
hocobj_baseattr, METH_VARARGS,
"To allow use of an overrided base method"},
2807 {
"hocobjptr",
hocobj_vptr, METH_NOARGS,
"Hoc Object pointer as a long int"},
2811 "o1.same(o2) return True if o1 and o2 wrap the same internal HOC Object"},
2812 {
"hname",
hocobj_name, METH_NOARGS,
"More specific than __str__() or __attr__()."},
2819 {
"ref",
mkref, METH_VARARGS,
"Wrap to allow call by reference in a hoc function"},
2820 {
"cas",
nrnpy_cas, METH_VARARGS,
"Return the currently accessed section."},
2821 {
"allsec",
nrnpy_forall, METH_VARARGS,
"Return iterator over all sections."},
2824 METH_VARARGS | METH_KEYWORDS,
2825 "Return a new Section"},
2826 {
"setpointer",
setpointer, METH_VARARGS,
"Assign hoc variable address to NMODL POINTER"},
2830 "Return full path to file that contains Py_Initialize()"},
2836 PyObject* nn = Py_BuildValue(
"s", meth->ml_doc);
2840 err = PyDict_SetItemString(dict, meth->ml_name, nn);
2874 bool potentially_valid =
false;
2877 potentially_valid =
true;
2879 potentially_valid =
true;
2882 if (!potentially_valid) {
2883 Py_INCREF(Py_NotImplemented);
2884 return Py_NotImplemented;
2887 return PyObject_CallFunction(
nrnpy_vec_math, strcpy(
buf,
"siOO"), op, reversed, obj1, obj2);
2895 Py_INCREF(Py_NotImplemented);
2896 return Py_NotImplemented;
2933 char endian_character = 0;
2935 PyObject* psys = PyImport_ImportModule(
"sys");
2937 PyErr_SetString(PyExc_ImportError,
"Failed to import sys to determine system byteorder.");
2941 PyObject* pbo = PyObject_GetAttrString(psys,
"byteorder");
2943 PyErr_SetString(PyExc_AttributeError,
"sys module does not have attribute 'byteorder'!");
2952 if (strcmp(byteorder.
c_str(),
"little") == 0) {
2953 endian_character =
'<';
2954 }
else if (strcmp(byteorder.
c_str(),
"big") == 0) {
2955 endian_character =
'>';
2957 PyErr_SetString(PyExc_RuntimeError,
"Unknown system native byteorder.");
2960 return endian_character;
2969 PyObject* iterator = PyObject_GetIter(pargs);
2972 if (iterator ==
NULL) {
2977 while ((item = PyIter_Next(iterator))) {
2979 hoc_execerror(
"iterable must contain only Section objects", 0);
2986 Py_DECREF(iterator);
2987 if (PyErr_Occurred()) {
3007 PyObject* modules = PyImport_GetModuleDict();
3009 PyObject* module = PyDict_GetItemString(modules,
"neuron.coreneuron");
3011 PyObject* val = PyObject_GetAttrString(module, option);
3013 long enable = PyLong_AsLong(val);
3021 if (PyErr_Occurred()) {
3043 extern char* (*nrnpy_nrncore_arg_p_)(
double tstop);
3045 PyObject* modules = PyImport_GetModuleDict();
3047 PyObject* module = PyDict_GetItemString(modules,
"neuron.coreneuron");
3049 PyObject* callable = PyObject_GetAttrString(module,
"nrncore_arg");
3051 PyObject* ts = Py_BuildValue(
"(d)", tstop);
3053 PyObject*
arg = PyObject_CallObject(callable, ts);
3061 "neuron.coreneuron.nrncore_arg() must return an ascii string");
3064 if (strlen(str.
c_str()) > 0) {
3065 return strdup(str.
c_str());
3072 if (PyErr_Occurred()) {
3096 char endian_character = 0;
3099 PyObject* modules = PyImport_GetModuleDict();
3100 if ((m = PyDict_GetItemString(modules,
"hoc")) !=
NULL && PyModule_Check(m)) {
3119 err = PyDict_SetItemString(
topmethdict, meth->ml_name, descr);
3148 if (endian_character == 0)
3154 err = PyDict_SetItemString(modules,
"hoc", m);
void nrn_pushsec(Section *sec)
void cable_prop_assign(Symbol *sym, double *pd, int op)
void * fpycall(void *(*)(void *, void *), void *, void *)
void set_pyerr(PyObject *type, const char *message)
virtual const char * varname() const =0
virtual Object * neuron_section_list()=0
virtual void * varobj() const =0
Symbol * hoc_table_lookup(const char *, Symlist *)
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 int dladdr(const void *addr, Dl_info *info)
void hoc_execerror(const char *, const char *)
int hoc_is_object_arg(int narg)
void hoc_pushstr(char **d)
void hoc_execerr_ext(const char *fmt,...)
printf style specification of hoc_execerror message.
double hoc_call_func(Symbol *s, int narg)
void hoc_pushpx(double *d)
void hoc_pushobj(Object **d)
void * hoc_sec_internal_name2ptr(const char *s, int eflag)
int hoc_is_str_arg(int narg)
int hoc_is_temp_charptr(char **cpp)
Objectdata * hoc_objectdata
void hoc_assign_str(char **cpp, const char *buf)
int is_obj_type(Object *obj, const char *type_name)
int hoc_is_double_arg(int narg)
char ** hoc_temp_charptr(void)
void hoc_obj_ref(Object *obj)
char * hoc_object_name(Object *ob)
void * hoc_pysec_name2ptr(const char *s, int eflag)
Symbol * hoc_lookup(const char *)
int hoc_is_pdouble_arg(int narg)
double * hoc_pgetarg(int narg)
void hoc_obj_unref(Object *obj)
void hoc_push_object(Object *d)
char ** hoc_pgargstr(int narg)
Objectdata * hoc_objectdata_restore(Objectdata *obdsav)
Objectdata * hoc_objectdata_save(void)
#define HocContextRestore
void hoc_l_delete(hoc_Item *)
Object ** hoc_objgetarg(int)
int vector_capacity(Vect *v)
void vector_resize(Vect *v, int n)
double * vector_vec(Vect *v)
Object ** hoc_temp_objptr(Object *)
#define ITERATE(itm, lst)
@ HocForallSectionIterator
Point_process * ob2pntproc_0(Object *)
void section_unref(Section *)
int const size_t const size_t n
Object **(* nrnpy_gui_helper3_)(const char *, Object *, int)
Object * nrnpy_pyobject_in_obj(PyObject *)
static void hocobj_dealloc(PyHocObject *self)
int nrnpy_numbercheck(PyObject *po)
static PyObject * plotshape_plot
static char * double_array_interface(PyObject *po, long &stride)
static Symbol * getsym(char *name, Object *ho, int fail)
static PyObject * cpp2refstr(char **cpp)
double ** nrnpy_setpointer_helper(PyObject *, PyObject *)
static PyObject * hocobj_name(PyObject *pself, PyObject *args)
static PyObject * hocobj_same(PyHocObject *pself, PyObject *args)
static PyObject * hocobj_getattro(PyObject *subself, PyObject *name)
static Symbol * sym_mat_x
static PyObject * py_hocobj_add(PyObject *obj1, PyObject *obj2)
static cTemplate * hoc_vec_template_
static cTemplate * hoc_list_template_
static cTemplate * hoc_sectionlist_template_
static IvocVect * nrnpy_vec_from_python(void *v)
static int nrncore_file_mode_value()
return value of neuron.coreneuron.file_mode
Object * nrnpy_po2ho(PyObject *)
int nrnpy_vec_math_register(PyObject *callback)
PyTypeObject * psection_type
static void hocobj_pushargs_free_strings(std::vector< char * > &s2free)
static PyObject * curargs_
int nrnpy_set_toplevel_callbacks(PyObject *rvp_plot0, PyObject *plotshape_plot0, PyObject *get_mech_object_0, PyObject *store_savestate, PyObject *restore_savestate)
Object * hoc_obj_look_inside_stack(int)
static PyObject * py_hocobj_uabs(PyObject *obj)
static PyObject * nrnexec(PyObject *self, PyObject *args)
void(* nrnpy_decref)(void *pyobj)
static int hocobj_init(PyObject *subself, PyObject *args, PyObject *kwds)
static int hocobj_pushargs(PyObject *args, std::vector< char * > &s2free)
static PyObject * store_savestate_
static PyObject * gui_helper_3_helper_(const char *name, Object *obj, int handle_strptr)
static PyObject * hocobj_call(PyHocObject *self, PyObject *args, PyObject *kwrds)
int nrnpy_rvp_pyobj_callback_register(PyObject *callback)
static void * nrnpy_hoc_bool_pop()
static PyObject * gui_callback
static Inst * save_pc(Inst *newpc)
Symlist * hoc_top_level_symlist
Symbol * nrnpy_pyobj_sym_
Section * nrn_noerr_access()
void(* nrnpy_sectionlist_helper_)(void *, Object *)
static void hocobj_objectvar(Symbol *sym)
static PyObject * setpointer(PyObject *self, PyObject *args)
static Object ** vec_as_numpy_helper(int size, double *data)
int ivoc_list_count(Object *)
static void * nrnpy_hoc_int_pop()
static Object ** gui_helper_3_(const char *name, Object *obj, int handle_strptr)
int hoc_max_builtin_class_id
double cable_prop_eval(Symbol *sym)
Symbol * ivoc_alias_lookup(const char *name, Object *ob)
static PyObject * hocpickle_reduce(PyObject *self, PyObject *args)
static PyObject * hocobj_iter(PyObject *self)
static PyObject * py_hocobj_uneg(PyObject *obj)
double(* nrnpy_object_to_double_)(Object *)
static PyObject * hoc_ac(PyObject *self, PyObject *args)
static PyObject *(* vec_as_numpy)(int, double *)
PyObject * nrnpy_hoc_pop()
static void add2topdict(PyObject *)
static PyObject * py_hocobj_upos(PyObject *obj)
static void * fcall(void *vself, void *vargs)
PyObject * nrnpy_forall(PyObject *, PyObject *)
static void pyobject_in_objptr(Object **, PyObject *)
static int araylen(Arrayinfo *a, PyHocObject *po)
static PyObject * libpython_path(PyObject *self, PyObject *args)
PyObject * nrnpy_cas(PyObject *, PyObject *)
static double object_to_double_(Object *obj)
static hoc_Item * next_valid_secitem(hoc_Item *q, hoc_Item *ql)
static void eval_component(PyHocObject *po, int ix)
void *(* nrnpy_get_pyobj)(Object *obj)
static int hocobj_setattro(PyObject *subself, PyObject *pyname, PyObject *value)
static char * nrncore_arg(double tstop)
static PyObject * mkref(PyObject *self, PyObject *args)
static PyObject * pfunc_get_docstring
static PyObject * py_hocobj_div(PyObject *obj1, PyObject *obj2)
static PyObject * hocobj_baseattr(PyObject *subself, PyObject *args)
double * hoc_evalpointer()
Object **(* nrnpy_vec_as_numpy_helper_)(int, double *)
int nrn_is_hocobj_ptr(PyObject *po, double *&pd)
static int hocobj_setitem(PyObject *self, Py_ssize_t i, PyObject *arg)
static int setup_doc_system()
int(* nrnpy_nrncore_file_mode_value_p_)()
value of neuron.coreneuron.file_mode as 0, 1 (-1 if error)
PyObject * nrnpy_ho2po(Object *)
void hoc_tobj_unref(Object **)
static PyMethodDef hocobj_methods[]
static void nrnpy_restore_savestate_(int64_t size, char *data)
static void hocobj_pushtop(PyHocObject *po, Symbol *sym, int ix)
static PyMethodDef toplevel_methods[]
void hoc_object_component()
void lvappendsec_and_ref(void *sl, Section *sec)
static PyObject * nrnpy_rvp_pyobj_callback
static PyObject * hocobj_richcmp(PyHocObject *self, PyObject *other, int op)
static bool pyobj_is_vector(PyObject *obj)
static PyObject * py_hocobj_math_unary(const char *op, PyObject *obj)
static PyObject * hocobj_repr(PyObject *p)
static Symbol * sym_vec_x
PyObject * nrnpy_pushsec(PyObject *)
static PyObject * hocobj_vptr(PyObject *pself, PyObject *args)
static int get_nrncore_opt_value(const char *option)
Object **(* nrnpy_vec_to_python_p_)(void *)
static PyObject * rvp_plot
static PyObject * py_hocobj_sub(PyObject *obj1, PyObject *obj2)
int nrnpy_set_gui_callback(PyObject *)
Object * hoc_newobj1(Symbol *, int)
static PyHocObject * intermediate(PyHocObject *po, Symbol *sym, int ix)
static PyObject * py_hocobj_math(const char *op, PyObject *obj1, PyObject *obj2)
void(* nrnpy_restore_savestate)(int64_t, char *)
static int refuse_to_look
static PyMethodDef HocMethods[]
int nrn_netcon_weight(NetCon *, double **)
static PyObject * hocobj_getitem(PyObject *self, Py_ssize_t ix)
static char array_interface_typestr[5]
char *(* nrnpy_nrncore_arg_p_)(double tstop)
Gets the python string returned by neuron.coreneuron.nrncore_arg(tstop) return a strdup() copy of the...
PyObject * hocobj_call_arg(int i)
PyObject * toplevel_get(PyObject *subself, const char *n)
static int hocobj_nonzero(PyObject *self)
char **(* nrnpy_gui_helper3_str_)(const char *, Object *, int)
static PyObject * restore_savestate_
static PyObject * nrnpy_vec_math
static Arrayinfo * hocobj_aray(Symbol *sym, Object *ho)
static Object ** nrnpy_vec_to_python(void *v)
static void nrnpy_decref_(void *pyobj)
static Py_ssize_t hocobj_len(PyObject *self)
PyObject * nrn_hocobj_ptr(double *pd)
static PyObject * hocobj_iternext(PyObject *self)
static int set_final_from_stk(PyObject *po)
static void symlist2dict(Symlist *sl, PyObject *dict)
int nrnpy_set_vec_as_numpy(PyObject *(*p)(int, double *))
static int araychk(Arrayinfo *a, PyHocObject *po, int ix)
static long hocobj_hash(PyHocObject *self)
static int nrncore_enable_value()
return value of neuron.coreneuron.enable
Object **(* nrnpy_gui_helper_)(const char *, Object *)
HAVE_IV.
static void nrnpy_store_savestate_(char **save_data, uint64_t *save_data_size)
int(* nrnpy_nrncore_enable_value_p_)()
value of neuron.coreneuron.enable as 0, 1 (-1 if error)
static PyObject * hocobj_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
static Object ** gui_helper_(const char *name, Object *obj)
Objectdata * hoc_top_level_data
char get_endian_character()
static PyObject * iternext_sl(PyHocObject *po, hoc_Item *ql)
int nrn_secref_nchild(Section *)
static Symbol * sym_netcon_weight
Symlist * hoc_built_in_symlist
PyObject * get_plotshape_data(PyObject *sp)
PyObject * nrn_ptr_richcmp(void *self_ptr, void *other_ptr, int op)
static int component(PyHocObject *po)
static void sectionlist_helper_(void *sl, Object *args)
PyTypeObject * hocobject_type
static PyObject * py_hocobj_mul(PyObject *obj1, PyObject *obj2)
void nrn_change_nseg(Section *, int)
static PyObject * hocobj_getsec(Symbol *sym)
static PyObject * hocobj_getattr(PyObject *subself, PyObject *pyname)
static char ** gui_helper_3_str_(const char *name, Object *obj, int handle_strptr)
static void * nrnpy_get_pyobj_(Object *obj)
NPySecObj * newpysechelp(Section *sec)
static PyObject * hocpickle_setstate(PyObject *self, PyObject *args)
static Object * rvp_rxd_to_callable_(Object *obj)
PyObject * nrnpy_hoc2pyobject(Object *)
#define BYTESWAP(_X__, _TYPE__)
static const char * hocobj_docstring
static PyObject * topmethdict
int nrn_matrix_dim(void *, int)
Object *(* nrnpy_rvp_rxd_to_callable)(Object *)
bool hoc_valid_stmt(const char *, Object *)
IvocVect *(* nrnpy_vec_from_python_p_)(void *)
static PyObject * get_mech_object_
static PyMemberDef hocobj_members[]
PyObject * nrnpy_newsecobj(PyObject *, PyObject *, PyObject *)
void(* nrnpy_store_savestate)(char **save_data, uint64_t *save_data_size)
static PyType_Spec nrnpy_HocObjectType_spec
static struct PyModuleDef hocmodule
int nrn_pointer_assign(Prop *prop, Symbol *sym, PyObject *value)
bool is_python_string(PyObject *python_string)
#define PyString_FromString
static double ref(void *v)
static double done(void *v)
PyObject_HEAD Section * sec_
PyObject_HEAD Object * ho_
PyHoc::IteratorState its_
struct Symbol::@37::@38 rng
HocStruct cTemplate * ctemplate
union hoc_Item::@40 element