Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more high-level nanobind API. #3244

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions src/nrnpython/nrnpy_hoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,14 @@ static Inst* save_pc(Inst* newpc) {

// also called from nrnpy_nrn.cpp
int hocobj_pushargs(PyObject* args, std::vector<char*>& 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<double>(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<double>(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
Expand All @@ -434,7 +431,7 @@ int hocobj_pushargs(PyObject* args, std::vector<char*>& 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,
Expand All @@ -443,7 +440,7 @@ int hocobj_pushargs(PyObject* args, std::vector<char*>& s2free) {
// Exception ignored on calling ctypes callback function: <function Printf
// thus obscuring the actual error, such as
// nrn.Segment associated with deleted internal Section.
PyHocObject* pho = (PyHocObject*) po;
PyHocObject* pho = (PyHocObject*) po.ptr();
PyHoc::ObjectType tp = pho->type_;
if (tp == PyHoc::HocObject) {
hoc_push_object(pho->ho_);
Expand All @@ -463,28 +460,25 @@ int hocobj_pushargs(PyObject* args, std::vector<char*>& 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<char*>& s2free) {
std::vector<char*>::iterator it = s2free.begin();
for (; it != s2free.end(); ++it) {
if (*it) {
free(*it);
}
for (char* e: s2free) {
free(e);
}
s2free.clear();
}
Expand Down
Loading