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 nanobind API #3024

Merged
merged 4 commits into from
Aug 7, 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: 16 additions & 20 deletions src/nrnpython/nrnpy_nrn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,13 @@
}

static int NpySObj_contains(PyObject* s, PyObject* obj, const char* string) {
/* Checks is provided PyObject* s contains obj */
if (!PyObject_HasAttrString(obj, string)) {
/* Checks is provided PyObject* s matches obj.<string> */
auto pyobj = nb::borrow(obj); // keep refcount+1 during use
if (!nb::hasattr(pyobj, string)) {
return 0;
}
auto _pyobj = nb::borrow(obj); // keep refcount+1 during use
auto obj_seg = nb::steal(PyObject_GetAttrString(obj, string));
return PyObject_RichCompareBool(s, obj_seg.ptr(), Py_EQ);
auto obj_seg = pyobj.attr(string);
return nb::handle{s}.equal(obj_seg);

Check warning on line 200 in src/nrnpython/nrnpy_nrn.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_nrn.cpp#L200

Added line #L200 was not covered by tests
}

static int NPySecObj_contains(PyObject* sec, PyObject* obj) {
Expand Down Expand Up @@ -1493,29 +1493,27 @@
static PyObject* NPySecObj_insert(NPySecObj* self, PyObject* args) {
CHECK_SEC_INVALID(self->sec_);
char* tname;
PyObject *tpyobj, *tpyobj2;
if (!PyArg_ParseTuple(args, "s", &tname)) {
PyErr_Clear();
// if called with an object that has an insert method, use that
PyObject* tpyobj;
if (PyArg_ParseTuple(args, "O", &tpyobj)) {
ferdonline marked this conversation as resolved.
Show resolved Hide resolved
Py_INCREF(tpyobj);
Py_INCREF((PyObject*) self);
tpyobj2 = PyObject_CallMethod(tpyobj, "insert", "O", (PyObject*) self);
Py_DECREF(tpyobj);
if (tpyobj2 == NULL) {
Py_DECREF((PyObject*) self);
auto _tpyobj_tracker = nb::borrow(tpyobj);
// Returned object to be discarded
auto out_o = nb::steal(PyObject_CallMethod(tpyobj, "insert", "O", (PyObject*) self));
ferdonline marked this conversation as resolved.
Show resolved Hide resolved
if (!out_o.is_valid()) {
PyErr_Clear();
PyErr_SetString(
PyExc_TypeError,
"insert argument must be either a string or an object with an insert method");
return NULL;
return nullptr;

Check warning on line 1509 in src/nrnpython/nrnpy_nrn.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_nrn.cpp#L1509

Added line #L1509 was not covered by tests
}
Py_DECREF(tpyobj2);
Py_INCREF(self);
ferdonline marked this conversation as resolved.
Show resolved Hide resolved
return (PyObject*) self;
}
PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "insert takes a single positional argument");
return NULL;
return nullptr;

Check warning on line 1516 in src/nrnpython/nrnpy_nrn.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_nrn.cpp#L1516

Added line #L1516 was not covered by tests
}
PyObject* otype = PyDict_GetItemString(pmech_types, tname);
if (!otype) {
Expand Down Expand Up @@ -1693,16 +1691,14 @@
Section* sec = self->pysec_->sec_;
CHECK_SEC_INVALID(sec);
Node* nd = node_exact(sec, self->x_);
PyObject* result = PyList_New(0);
nb::list result{};
for (Prop* p = nd->prop; p; p = p->next) {
if (memb_func[p->_type].is_point) {
auto* pp = p->dparam[1].get<Point_process*>();
auto item = nb::steal(nrnpy_ho2po(pp->ob));
int err = PyList_Append(result, item.ptr());
assert(err == 0);
result.append(nb::steal(nrnpy_ho2po(pp->ob)));
}
}
return result;
return result.release().ptr();
ferdonline marked this conversation as resolved.
Show resolved Hide resolved
}

static PyObject* seg_point_processes_safe(NPySegObj* self) {
Expand Down
Loading