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 18 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
45 changes: 21 additions & 24 deletions src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,40 +493,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::tuple po(((Py2Nrn*) ho->u.this_pointer)->po_);
nb::gil_scoped_acquire lock{};

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L496-L497

Added lines #L496 - L497 were not covered by tests
nb::float_ x{};
if (nb::sequence::check_(po[0]) || nb::mapping::check_(po[0])) {
x = po[0][po[1]];

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#L499-L500

Added lines #L499 - L500 were not covered by tests
} else {
r = PyObject_GetAttr(p, PyTuple_GetItem(po, 1));
x = po[0].attr(po[1]);

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L502

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

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L504

Added line #L504 was not covered by tests
}

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::tuple po(((Py2Nrn*) ho->u.this_pointer)->po_);
nb::gil_scoped_acquire lock{};
if (nb::sequence::check_(po[0]) || nb::mapping::check_(po[0])) {
po[0][po[1]] = x;

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L508-L511

Added lines #L508 - L511 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 513 in src/nrnpython/nrnpy_p2h.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L513

Added line #L513 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();
nb::tuple po(((Py2Nrn*) ho->u.this_pointer)->po_);
nb::gil_scoped_acquire lock{};

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L518-L519

Added lines #L518 - L519 were not covered by tests
nb::str name{};
if (nb::sequence::check_(po[0]) || nb::mapping::check_(po[0])) {
name = po[0][po[1]];

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L521-L522

Added lines #L521 - L522 were not covered by tests
} else {
name = po[0].attr(po[1]);

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L524

Added line #L524 was not covered by tests
}
const char* cp = name.c_str();

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L526

Added line #L526 was not covered by tests
alkino marked this conversation as resolved.
Show resolved Hide resolved
if (*cpp && strcmp(*cpp, cp) == 0) {
return 0;
}
Expand Down
Loading