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 4). #3208

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
72 changes: 28 additions & 44 deletions src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@
#endif
int i;
Py2Nrn* pn = (Py2Nrn*) ob->u.this_pointer;
PyObject* head = pn->po_;
PyObject* tail;
auto head = nb::borrow(pn->po_);
nb::object tail;
nanobind::gil_scoped_acquire lock{};
if (pn->type_ == 0) { // top level
if (!main_module) {
Expand All @@ -163,22 +163,21 @@
Py_INCREF(main_module);
Py_INCREF(main_namespace);
}
tail = PyRun_String(sym->name, Py_eval_input, main_namespace, main_namespace);
tail = nb::steal(PyRun_String(sym->name, Py_eval_input, main_namespace, main_namespace));
} else {
Py_INCREF(head);
head.inc_ref();
if (strcmp(sym->name, "_") == 0) {
tail = head;
Py_INCREF(tail);
} else {
tail = PyObject_GetAttrString(head, sym->name);
tail = nb::steal(PyObject_GetAttrString(head.ptr(), sym->name));
}
}
if (!tail) {
PyErr_Print();
hoc_execerror("No attribute:", sym->name);
}
Object* on;
PyObject* result = 0;
nb::object result;
if (isfunc) {
nb::list args{};
for (i = 0; i < nindex; ++i) {
Expand All @@ -192,13 +191,11 @@
}
args.reverse();
// printf("PyObject_CallObject %s %p\n", sym->name, tail);
result = nrnpy_pyCallObject(nb::borrow<nb::callable>(tail), args).release().ptr();
result = nrnpy_pyCallObject(nb::borrow<nb::callable>(tail), args);
// PyObject_Print(result, stdout, 0);
// printf(" result of call\n");
if (!result) {
char* mes = nrnpyerr_str();
Py_XDECREF(tail);
Py_XDECREF(head);
if (mes) {
Fprintf(stderr, "%s\n", mes);
free(mes);
Expand Down Expand Up @@ -228,41 +225,36 @@
// TypeError: list indices must be integers or slices, not hoc.HocObject
arg = nb::steal(nrnpy_hoc_pop("nindex py2n_component"));
}
result = PyObject_GetItem(tail, arg.ptr());
result = nb::steal(PyObject_GetItem(tail.ptr(), arg.ptr()));
if (!result) {
PyErr_Print();
hoc_execerror("Python get item failed:", hoc_object_name(ob));
}
} else {
result = tail;
Py_INCREF(result);
}
// printf("py2n_component %s %d %s result refcount=%d\n", hoc_object_name(ob),
// ob->refcount, sym->name, result->ob_refcnt);
// if numeric, string, or python HocObject return those
if (nrnpy_numbercheck(result)) {
if (nrnpy_numbercheck(result.ptr())) {
hoc_pop_defer();
PyObject* pn = PyNumber_Float(result);
hoc_pushx(PyFloat_AsDouble(pn));
Py_XDECREF(pn);
} else if (is_python_string(result)) {
auto pn = nb::steal(PyNumber_Float(result.ptr()));
hoc_pushx(PyFloat_AsDouble(pn.ptr()));
} else if (is_python_string(result.ptr())) {
char** ts = hoc_temp_charptr();
Py2NRNString str(result, true);
Py2NRNString str(result.ptr(), true);
*ts = str.c_str();
hoc_pop_defer();
hoc_pushstr(ts);
} else {
// PyObject_Print(result, stdout, 0);
on = nrnpy_po2ho(result);
on = nrnpy_po2ho(result.ptr());
hoc_pop_defer();
hoc_push_object(on);
if (on) {
on->refcount--;
}
}
Py_XDECREF(result);
Py_XDECREF(head);
Py_DECREF(tail);
}

static void hpoasgn(Object* o, int type) {
Expand Down Expand Up @@ -617,31 +609,31 @@
PyObject *ptype, *pvalue, *ptraceback;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);

auto type = nb::steal(ptype);
auto value = nb::steal(pvalue);
auto traceback = nb::steal(ptraceback);

// try for full backtrace
PyObject* module_name = NULL;
PyObject* pyth_module = NULL;
PyObject* pyth_func = NULL;
PyObject* py_str = NULL;
nb::object py_str;
char* cmes = NULL;

// Since traceback.format_exception returns list of strings, wrap
// in neuron.format_exception that returns a string.
if (!ptraceback) {
ptraceback = Py_None;
Py_INCREF(ptraceback);
}
module_name = PyString_FromString("neuron");
if (module_name) {
pyth_module = PyImport_Import(module_name);
if (!traceback) {
traceback = nb::none();

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

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L624

Added line #L624 was not covered by tests
}
auto pyth_module = nb::steal(PyImport_ImportModule("neuron"));
if (pyth_module) {
pyth_func = PyObject_GetAttrString(pyth_module, "format_exception");
auto pyth_func = nb::steal(
PyObject_GetAttrString(pyth_module.ptr(), "format_exception"));
if (pyth_func) {
py_str = PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback, NULL);
py_str = nb::steal(PyObject_CallFunctionObjArgs(
pyth_func.ptr(), type.ptr(), value.ptr(), traceback.ptr(), NULL));
}
}
if (py_str) {
Py2NRNString mes(py_str);
Py2NRNString mes(py_str.ptr());
if (mes.err()) {
Fprintf(stderr, "nrnperr_str: Py2NRNString failed\n");
} else {
Expand All @@ -657,14 +649,6 @@
Fprintf(stderr, "nrnpyerr_str failed\n");
}

Py_XDECREF(module_name);
Py_XDECREF(pyth_func);
Py_XDECREF(pyth_module);
Py_XDECREF(ptype);
Py_XDECREF(pvalue);
Py_XDECREF(ptraceback);
Py_XDECREF(py_str);

return cmes;
}
return NULL;
Expand Down
Loading