Skip to content

Commit

Permalink
Rename to HocTopContextManager
Browse files Browse the repository at this point in the history
  • Loading branch information
JCGoran committed Dec 10, 2024
1 parent 702bd72 commit 318dd73
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/nrnpython/hoccontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ static void hc_restore_(HocContext* hc) {
hoc_symlist = hc->sl;
}

class HocTopContext {
// RAII guard for the top HOC context
class HocTopContextManager {
private:
HocContext hcref;
HocContext* hc_ = nullptr;

public:
HocTopContext() {
HocTopContextManager() {
// ``hoc_thisobject`` is global
if (hoc_thisobject) {
hc_ = hc_save_and_set_to_top_(&hcref);
}
}
~HocTopContext() {
~HocTopContextManager() {
if (hc_) {
hc_restore_(hc_);
}
Expand Down
10 changes: 5 additions & 5 deletions src/nrnpython/nrnpy_hoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ static void* fcall(void* vself, void* vargs) {

return result;
} else {
auto interp = HocTopContext();
auto interp = HocTopContextManager();
Inst fc[4];
// ugh. so a potential call of hoc_get_last_pointer_symbol will return nullptr.
fc[0].in = STOP;
Expand Down Expand Up @@ -1262,7 +1262,7 @@ static PyObject* hocobj_getattr(PyObject* subself, PyObject* pyname) {
}
}
// top level interpreter fork
auto interp = HocTopContext();
auto interp = HocTopContextManager();
switch (sym->type) {
case VAR: // double*
if (!is_array(*sym)) {
Expand Down Expand Up @@ -1497,7 +1497,7 @@ static int hocobj_setattro(PyObject* subself, PyObject* pyname, PyObject* value)
return -1;
}
}
auto interp = HocTopContext();
auto interp = HocTopContextManager();
switch (sym->type) {
case VAR: // double*
if (is_array(*sym)) {
Expand Down Expand Up @@ -1991,7 +1991,7 @@ static PyObject* hocobj_getitem(PyObject* self, Py_ssize_t ix) {
}
}
} else { // must be a top level intermediate
auto interp = HocTopContext();
auto interp = HocTopContextManager();
switch (po->sym_->type) {
case VAR:
hocobj_pushtop(po, po->sym_, ix);
Expand Down Expand Up @@ -2138,7 +2138,7 @@ static int hocobj_setitem(PyObject* self, Py_ssize_t i, PyObject* arg) {
err = set_final_from_stk(arg);
}
} else { // must be a top level intermediate
auto interp = HocTopContext();
auto interp = HocTopContextManager();
switch (po->sym_->type) {
case VAR:
hocobj_pushtop(po, po->sym_, i);
Expand Down
2 changes: 1 addition & 1 deletion src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static nb::object nrnpy_pyCallObject(nb::callable callable, nb::object args) {
// When hoc calls a PythonObject method, then in case python
// calls something back in hoc, the hoc interpreter must be
// at the top level
auto interp = HocTopContext();
auto interp = HocTopContextManager();
nb::tuple tup(args);
nb::object p = nb::steal(PyObject_CallObject(callable.ptr(), tup.ptr()));
#if 0
Expand Down
2 changes: 1 addition & 1 deletion src/nrnpython/nrnpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ static void nrnpython_real() {
int retval = 0;
#if USE_PYTHON
{
auto interp = HocTopContext();
auto interp = HocTopContextManager();
nanobind::gil_scoped_acquire lock{};
retval = (PyRun_SimpleString(hoc_gargstr(1)) == 0);
}
Expand Down

0 comments on commit 318dd73

Please sign in to comment.