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

Arguments for nrnpy_pyCallObject are created with nanobind #3140

Merged
merged 3 commits into from
Oct 24, 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
43 changes: 15 additions & 28 deletions src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,28 +177,22 @@
PyErr_Print();
hoc_execerror("No attribute:", sym->name);
}
PyObject* args = 0;
Object* on;
PyObject* result = 0;
if (isfunc) {
args = PyTuple_New(nindex);
nb::list args{};
for (i = 0; i < nindex; ++i) {
PyObject* arg = nrnpy_hoc_pop("isfunc py2n_component");
nb::object arg = nb::steal(nrnpy_hoc_pop("isfunc py2n_component"));
if (!arg) {
PyErr2NRNString e;
e.get_pyerr();
Py_DECREF(args);
hoc_execerr_ext("arg %d error: %s", i, e.c_str());
}
// PyObject_Print(arg, stdout, 0);
// printf(" %d arg %d\n", arg->ob_refcnt, i);
if (PyTuple_SetItem(args, nindex - 1 - i, arg)) {
assert(0);
}
args.append(arg);
}
args.reverse();
// printf("PyObject_CallObject %s %p\n", sym->name, tail);
result = nrnpy_pyCallObject(nb::borrow<nb::callable>(tail), nb::borrow(args)).release().ptr();
Py_DECREF(args);
result = nrnpy_pyCallObject(nb::borrow<nb::callable>(tail), args).release().ptr();
// PyObject_Print(result, stdout, 0);
// printf(" result of call\n");
if (!result) {
Expand All @@ -216,7 +210,7 @@
return;
}
} else if (nindex) {
PyObject* arg;
nb::object arg;
int n = hoc_pop_ndim();
if (n > 1) {
hoc_execerr_ext(
Expand All @@ -226,15 +220,15 @@
n);
}
if (hoc_stack_type() == NUMBER) {
arg = Py_BuildValue("l", (long) hoc_xpop());
arg = nb::int_((long) hoc_xpop());
} else {
// I don't think it is syntactically possible
// for this to be a VAR. It is possible for it to
// be an Object but the GetItem below will raise
// TypeError: list indices must be integers or slices, not hoc.HocObject
arg = nrnpy_hoc_pop("nindex py2n_component");
arg = nb::steal(nrnpy_hoc_pop("nindex py2n_component"));

Check warning on line 229 in src/nrnpython/nrnpy_p2h.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L229

Added line #L229 was not covered by tests
}
result = PyObject_GetItem(tail, arg);
result = PyObject_GetItem(tail, arg.ptr());
if (!result) {
PyErr_Print();
hoc_execerror("Python get item failed:", hoc_object_name(ob));
Expand Down Expand Up @@ -468,24 +462,17 @@
nb::callable po = nb::borrow<nb::callable>(((Py2Nrn*) ho->u.this_pointer)->po_);
nanobind::gil_scoped_acquire lock{};

PyObject* args = PyTuple_New((Py_ssize_t) narg);
if (args == NULL) {
hoc_execerror("PyTuple_New failed", 0);
}
nb::list args{};
for (int i = 0; i < narg; ++i) {
PyObject* item = nrnpy_hoc_pop("func_call");
if (item == NULL) {
Py_XDECREF(args);
nb::object item = nb::steal(nrnpy_hoc_pop("func_call"));
if (!item) {
hoc_execerror("nrnpy_hoc_pop failed", 0);
}
if (PyTuple_SetItem(args, (Py_ssize_t) (narg - i - 1), item) != 0) {
Py_XDECREF(args);
hoc_execerror("PyTuple_SetItem failed", 0);
}
args.append(item);
}
args.reverse();

nb::object r = nrnpy_pyCallObject(po, nb::borrow(args));
Py_XDECREF(args);
nb::object r = nrnpy_pyCallObject(po, args);
double rval = 0.0;
if (!r.is_valid()) {
if (!err || *err) {
Expand Down
Loading