From bf6a457aa9ebdbf5e19e1668af78343ef1399f01 Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Fri, 13 Dec 2024 14:42:56 +0100 Subject: [PATCH] Move from deprecated PyWeakref_GetObject (#3287) --- src/nrnpython/nrnpy_nrn.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/nrnpython/nrnpy_nrn.cpp b/src/nrnpython/nrnpy_nrn.cpp index fb3874bc11..11042bfb8e 100644 --- a/src/nrnpython/nrnpy_nrn.cpp +++ b/src/nrnpython/nrnpy_nrn.cpp @@ -177,6 +177,19 @@ static Object* pysec_cell(Section* sec) { if (auto* pv = sec->prop->dparam[PROP_PY_INDEX].get(); pv) { PyObject* cell_weakref = static_cast(pv)->cell_weakref_; if (cell_weakref) { +#if PY_VERSION_HEX >= 0x030D0000 + PyObject* cell = nullptr; + int err = PyWeakref_GetRef(cell_weakref, &cell); + if (err == -1) { + PyErr_Print(); + hoc_execerror("Error getting cell for", secname(sec)); + } else if (err == 0) { + return nullptr; + } + auto ret = nrnpy_po2ho(cell); + Py_DECREF(cell); + return ret; +#else PyObject* cell = PyWeakref_GetObject(cell_weakref); if (!cell) { PyErr_Print(); @@ -184,6 +197,7 @@ static Object* pysec_cell(Section* sec) { } else if (cell != Py_None) { return nrnpy_po2ho(cell); } +#endif } } return NULL; @@ -215,12 +229,24 @@ static int pysec_cell_equals(Section* sec, Object* obj) { if (auto* pv = sec->prop->dparam[PROP_PY_INDEX].get(); pv) { PyObject* cell_weakref = static_cast(pv)->cell_weakref_; if (cell_weakref) { +#if PY_VERSION_HEX >= 0x030D0000 + PyObject* cell = nullptr; + int err = PyWeakref_GetRef(cell_weakref, &cell); + if (err == -1) { + PyErr_Print(); + hoc_execerror("Error getting cell for", secname(sec)); + } + auto ret = nrnpy_ho_eq_po(obj, cell); + Py_DECREF(cell); + return ret; +#else PyObject* cell = PyWeakref_GetObject(cell_weakref); if (!cell) { PyErr_Print(); hoc_execerror("Error getting cell for", secname(sec)); } return nrnpy_ho_eq_po(obj, cell); +#endif } return nrnpy_ho_eq_po(obj, Py_None); } @@ -1212,7 +1238,17 @@ static PyObject* pysec_wholetree_safe(NPySecObj* const self) { static PyObject* pysec2cell(NPySecObj* self) { nb::object result; if (self->cell_weakref_) { +#if PY_VERSION_HEX >= 0x030D0000 + PyObject* cell = nullptr; + int ret = PyWeakref_GetRef(self->cell_weakref_, &cell); + if (ret > 0) { + result = nb::steal(cell); + } else { + result = nb::none(); + } +#else result = nb::borrow(PyWeakref_GetObject(self->cell_weakref_)); +#endif } else if (auto* o = self->sec_->prop->dparam[6].get(); self->sec_->prop && o) { result = nb::steal(nrnpy_ho2po(o)); } else {