diff --git a/src/nrnpython/nrnpy_hoc.cpp b/src/nrnpython/nrnpy_hoc.cpp index 42cdcd7945..99c7572161 100644 --- a/src/nrnpython/nrnpy_hoc.cpp +++ b/src/nrnpython/nrnpy_hoc.cpp @@ -409,17 +409,14 @@ static Inst* save_pc(Inst* newpc) { // also called from nrnpy_nrn.cpp int hocobj_pushargs(PyObject* args, std::vector& s2free) { - int i, narg = PyTuple_Size(args); - for (i = 0; i < narg; ++i) { - PyObject* po = PyTuple_GetItem(args, i); - // PyObject_Print(po, stdout, 0); - // printf(" pushargs %d\n", i); - if (nrnpy_numbercheck(po)) { - nb::float_ pn(po); - hoc_pushx(static_cast(pn)); - } else if (is_python_string(po)) { + const nb::tuple tup(args); + for (int i = 0; i < tup.size(); ++i) { + nb::object po(tup[i]); + if (nrnpy_numbercheck(po.ptr())) { + hoc_pushx(nb::cast(po)); + } else if (is_python_string(po.ptr())) { char** ts = hoc_temp_charptr(); - Py2NRNString str(po, /* disable_release */ true); + Py2NRNString str(po.ptr(), /* disable_release */ true); if (str.err()) { // Since Python error has been set, need to clear, or hoc_execerror // printing with Printf will generate a @@ -434,7 +431,7 @@ int hocobj_pushargs(PyObject* args, std::vector& s2free) { *ts = str.c_str(); s2free.push_back(*ts); hoc_pushstr(ts); - } else if (PyObject_TypeCheck(po, hocobject_type)) { + } else if (PyObject_TypeCheck(po.ptr(), hocobject_type)) { // The PyObject_TypeCheck above used to be PyObject_IsInstance. The // problem with the latter is that it calls the __class__ method of // the object which can raise an error for nrn.Section, nrn.Segment, @@ -443,7 +440,7 @@ int hocobj_pushargs(PyObject* args, std::vector& s2free) { // Exception ignored on calling ctypes callback function: type_; if (tp == PyHoc::HocObject) { hoc_push_object(pho->ho_); @@ -463,28 +460,25 @@ int hocobj_pushargs(PyObject* args, std::vector& s2free) { } else { // make a hoc python object and push that Object* ob = NULL; - pyobject_in_objptr(&ob, po); + pyobject_in_objptr(&ob, po.ptr()); hoc_push_object(ob); hoc_obj_unref(ob); } } else { // make a hoc PythonObject and push that? Object* ob = NULL; - if (po != Py_None) { - pyobject_in_objptr(&ob, po); + if (!po.is_none()) { + pyobject_in_objptr(&ob, po.ptr()); } hoc_push_object(ob); hoc_obj_unref(ob); } } - return narg; + return tup.size(); } void hocobj_pushargs_free_strings(std::vector& s2free) { - std::vector::iterator it = s2free.begin(); - for (; it != s2free.end(); ++it) { - if (*it) { - free(*it); - } + for (char* e: s2free) { + free(e); } s2free.clear(); }