Skip to content

Commit

Permalink
Move from deprecated PyWeakref_GetObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Cornu committed Dec 12, 2024
1 parent 5b6ff83 commit d3937cc
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/nrnpython/nrnpy_nrn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,25 @@ static Object* pysec_cell(Section* sec) {
if (auto* pv = sec->prop->dparam[PROP_PY_INDEX].get<void*>(); pv) {
PyObject* cell_weakref = static_cast<NPySecObj*>(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;
}
#else
PyObject* cell = PyWeakref_GetObject(cell_weakref);
if (!cell) {
PyErr_Print();
hoc_execerror("Error getting cell for", secname(sec));
} else if (cell != Py_None) {
return nrnpy_po2ho(cell);
} else if (cell == Py_None) {
return nullptr;
}
#endif
return nrnpy_po2ho(cell);
}
}
return NULL;
Expand Down Expand Up @@ -215,11 +227,20 @@ static int pysec_cell_equals(Section* sec, Object* obj) {
if (auto* pv = sec->prop->dparam[PROP_PY_INDEX].get<void*>(); pv) {
PyObject* cell_weakref = static_cast<NPySecObj*>(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
PyObject* cell = PyWeakref_GetObject(cell_weakref);
if (!cell) {
PyErr_Print();
hoc_execerror("Error getting cell for", secname(sec));
}
#endif
return nrnpy_ho_eq_po(obj, cell);
}
return nrnpy_ho_eq_po(obj, Py_None);
Expand Down Expand Up @@ -1212,7 +1233,13 @@ 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;
PyWeakref_GetRef(self->cell_weakref_, &cell);
result = nb::steal(cell);
#else
result = nb::borrow(PyWeakref_GetObject(self->cell_weakref_));
#endif
} else if (auto* o = self->sec_->prop->dparam[6].get<Object*>(); self->sec_->prop && o) {
result = nb::steal(nrnpy_ho2po(o));
} else {
Expand Down

0 comments on commit d3937cc

Please sign in to comment.