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 nanobind to simplify functions gui{get,set} #3217

Merged
merged 23 commits into from
Nov 21, 2024
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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: 16 additions & 27 deletions src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,48 +482,37 @@
}

static double guigetval(Object* ho) {
PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_;
nanobind::gil_scoped_acquire lock{};
PyObject* r = NULL;
PyObject* p = PyTuple_GetItem(po, 0);
if (PySequence_Check(p) || PyMapping_Check(p)) {
r = PyObject_GetItem(p, PyTuple_GetItem(po, 1));
nb::gil_scoped_acquire lock{};
nb::tuple po(((Py2Nrn*) ho->u.this_pointer)->po_);
if (nb::sequence::check_(po[0]) || nb::mapping::check_(po[0])) {
return nb::cast<double>(po[0][po[1]]);

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L485-L488

Added lines #L485 - L488 were not covered by tests
} else {
r = PyObject_GetAttr(p, PyTuple_GetItem(po, 1));
return nb::cast<double>(po[0].attr(po[1]));

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L490

Added line #L490 was not covered by tests
}
auto pn = nb::steal(PyNumber_Float(r));
double x = PyFloat_AsDouble(pn.ptr());
return x;
}

static void guisetval(Object* ho, double x) {
PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_;
nanobind::gil_scoped_acquire lock{};
auto pn = nb::steal(PyFloat_FromDouble(x));
PyObject* p = PyTuple_GetItem(po, 0);
if (PySequence_Check(p) || PyMapping_Check(p)) {
PyObject_SetItem(p, PyTuple_GetItem(po, 1), pn.ptr());
nb::gil_scoped_acquire lock{};
nb::tuple po(((Py2Nrn*) ho->u.this_pointer)->po_);
if (nb::sequence::check_(po[0]) || nb::mapping::check_(po[0])) {
po[0][po[1]] = x;

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L495-L498

Added lines #L495 - L498 were not covered by tests
} else {
PyObject_SetAttr(p, PyTuple_GetItem(po, 1), pn.ptr());
po[0].attr(po[1]) = x;

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L500

Added line #L500 was not covered by tests
}
}

static int guigetstr(Object* ho, char** cpp) {
PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_;
nanobind::gil_scoped_acquire lock{};

PyObject* r = PyObject_GetAttr(PyTuple_GetItem(po, 0), PyTuple_GetItem(po, 1));
auto pn = nb::steal(PyObject_Str(r));
Py2NRNString name(pn.ptr());
char* cp = name.c_str();
if (*cpp && strcmp(*cpp, cp) == 0) {
nb::gil_scoped_acquire lock{};
nb::tuple po(((Py2Nrn*) ho->u.this_pointer)->po_);
auto name = nb::cast<std::string>(po[0].attr(po[1]));
if (*cpp && name == *cpp) {

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L505-L508

Added lines #L505 - L508 were not covered by tests
return 0;
}
if (*cpp) {
delete[] * cpp;
}
*cpp = new char[strlen(cp) + 1];
strcpy(*cpp, cp);
*cpp = new char[name.size() + 1];
strcpy(*cpp, name.c_str());

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L514-L515

Added lines #L514 - L515 were not covered by tests
return 1;
}

Expand Down
Loading