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

Modernize DECREF (part 2). #3205

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
6 changes: 2 additions & 4 deletions src/nrnpython/nrnpy_nrn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,10 +995,8 @@ static PyObject* nrnpy_set_psection(PyObject* self, PyObject* args) {
PyErr_SetString(PyExc_TypeError, "argument must be a callable");
return NULL;
}
if (nrnpy_psection) {
Py_DECREF(nrnpy_psection);
nrnpy_psection = NULL;
}

Py_XDECREF(nrnpy_psection);
nrnpy_psection = po;
Py_INCREF(po);
return po;
Expand Down
67 changes: 27 additions & 40 deletions src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,53 +269,48 @@
int err = 0;
int nindex;
Symbol* sym;
PyObject* poleft;
PyObject* poright;
nb::object poright;
if (type == NUMBER) {
poright = PyFloat_FromDouble(hoc_xpop());
poright = nb::steal(PyFloat_FromDouble(hoc_xpop()));
} else if (type == STRING) {
poright = Py_BuildValue("s", *hoc_strpop());
poright = nb::steal(Py_BuildValue("s", *hoc_strpop()));

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L276

Added line #L276 was not covered by tests
} else if (type == OBJECTVAR || type == OBJECTTMP) {
Object** po2 = hoc_objpop();
poright = nrnpy_ho2po(*po2);
poright = nb::steal(nrnpy_ho2po(*po2));

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L279

Added line #L279 was not covered by tests
hoc_tobj_unref(po2);
} else {
hoc_execerror("Cannot assign that type to PythonObject", (char*) 0);
}
auto stack_value = hoc_pop_object();
assert(o == stack_value.get());
poleft = nrnpy_hoc2pyobject(o);
auto poleft = nb::borrow(nrnpy_hoc2pyobject(o));
sym = hoc_spop();
nindex = hoc_ipop();
// printf("hpoasgn %s %s %d\n", hoc_object_name(o), sym->name, nindex);
if (nindex == 0) {
err = PyObject_SetAttrString(poleft, sym->name, poright);
err = PyObject_SetAttrString(poleft.ptr(), sym->name, poright.ptr());
} else if (nindex == 1) {
int ndim = hoc_pop_ndim();
assert(ndim == 1);
PyObject* key = PyLong_FromDouble(hoc_xpop());
PyObject* a;
auto key = nb::steal(PyLong_FromDouble(hoc_xpop()));
nb::object a;
if (strcmp(sym->name, "_") == 0) {
a = poleft;
Py_INCREF(a);
a = nb::borrow(poleft);
} else {
a = PyObject_GetAttrString(poleft, sym->name);
a = nb::steal(PyObject_GetAttrString(poleft.ptr(), sym->name));
}
if (a) {
err = PyObject_SetItem(a, key, poright);
Py_DECREF(a);
err = PyObject_SetItem(a.ptr(), key.ptr(), poright.ptr());
} else {
err = -1;
}
Py_DECREF(key);
} else {
hoc_execerr_ext(
"%d dimensional python objects "
"can't be accessed from hoc with var._[i1][i2]... syntax. "
"Must use var._[i1]._[i2]... hoc syntax.",
nindex);
}
Py_DECREF(poright);
if (err) {
PyErr_Print();
hoc_execerror("Assignment to PythonObject failed", NULL);
Expand Down Expand Up @@ -343,13 +338,10 @@
static double praxis_efun(Object* ho, Object* v) {
nanobind::gil_scoped_acquire lock{};

PyObject* pc = nrnpy_ho2po(ho);
PyObject* pv = nrnpy_ho2po(v);
PyObject* po = Py_BuildValue("(OO)", pc, pv);
Py_XDECREF(pc);
Py_XDECREF(pv);
nb::object r = hoccommand_exec_help1(nb::borrow(po));
Py_XDECREF(po);
auto pc = nb::steal(nrnpy_ho2po(ho));
auto pv = nb::steal(nrnpy_ho2po(v));
auto po = nb::steal(Py_BuildValue("(OO)", pc.ptr(), pv.ptr()));
nb::object r = hoccommand_exec_help1(po);
if (!r.is_valid()) {
char* mes = nrnpyerr_str();
if (mes) {
Expand Down Expand Up @@ -427,33 +419,29 @@
}

static Object* callable_with_args(Object* ho, int narg) {
PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_;
auto po = nb::borrow(((Py2Nrn*) ho->u.this_pointer)->po_);
nanobind::gil_scoped_acquire lock{};

PyObject* args = PyTuple_New((Py_ssize_t) narg);
if (args == NULL) {
auto args = nb::steal(PyTuple_New((Py_ssize_t) narg));
if (!args) {
hoc_execerror("PyTuple_New failed", 0);
}
for (int i = 0; i < narg; ++i) {
// not used with datahandle args.
PyObject* item = nrnpy_hoc_pop("callable_with_args");
if (item == NULL) {
Py_XDECREF(args);
auto item = nb::steal(nrnpy_hoc_pop("callable_with_args"));
if (!item) {
hoc_execerror("nrnpy_hoc_pop failed", 0);
}
if (PyTuple_SetItem(args, (Py_ssize_t) (narg - i - 1), item) != 0) {
Py_XDECREF(args);
if (PyTuple_SetItem(args.ptr(), (Py_ssize_t) (narg - i - 1), item.release().ptr()) != 0) {
hoc_execerror("PyTuple_SetItem failed", 0);
}
}

PyObject* r = PyTuple_New(2);
PyTuple_SetItem(r, 1, args);
Py_INCREF(po); // when r is destroyed, do not want po refcnt to go to 0
PyTuple_SetItem(r, 0, po);
auto r = nb::steal(PyTuple_New(2));
PyTuple_SetItem(r.ptr(), 1, args.release().ptr());
PyTuple_SetItem(r.ptr(), 0, po.release().ptr());

Object* hr = nrnpy_po2ho(r);
Py_XDECREF(r);
Object* hr = nrnpy_po2ho(r.release().ptr());

return hr;
}
Expand Down Expand Up @@ -514,9 +502,8 @@
} else {
r = PyObject_GetAttr(p, PyTuple_GetItem(po, 1));
}
PyObject* pn = PyNumber_Float(r);
double x = PyFloat_AsDouble(pn);
Py_XDECREF(pn);
auto pn = nb::steal(PyNumber_Float(r));
double x = PyFloat_AsDouble(pn.ptr());

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L505-L506

Added lines #L505 - L506 were not covered by tests
return x;
}

Expand Down
Loading