diff --git a/src/nrnpython/nrnpy_hoc.cpp b/src/nrnpython/nrnpy_hoc.cpp index b6eb51a9b9..311139567c 100644 --- a/src/nrnpython/nrnpy_hoc.cpp +++ b/src/nrnpython/nrnpy_hoc.cpp @@ -2903,7 +2903,7 @@ static Object* rvp_rxd_to_callable_(Object* obj) { extern "C" NRN_EXPORT PyObject* get_plotshape_data(PyObject* sp) { - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; PyHocObject* pho = (PyHocObject*) sp; ShapePlotInterface* spi; if (!is_obj_type(pho->ho_, "PlotShape")) { @@ -3404,7 +3404,7 @@ extern "C" NRN_EXPORT PyObject* nrnpy_hoc() { nrnpy_nrncore_enable_value_p_ = nrncore_enable_value; nrnpy_nrncore_file_mode_value_p_ = nrncore_file_mode_value; nrnpy_rvp_rxd_to_callable = rvp_rxd_to_callable_; - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; char endian_character = 0; diff --git a/src/nrnpython/nrnpy_p2h.cpp b/src/nrnpython/nrnpy_p2h.cpp index 07ac58daba..f84cb29bcf 100644 --- a/src/nrnpython/nrnpy_p2h.cpp +++ b/src/nrnpython/nrnpy_p2h.cpp @@ -23,7 +23,7 @@ static PyObject* main_namespace; struct Py2Nrn final { ~Py2Nrn() { - PyLockGIL lock{}; + nanobind::gil_scoped_acquire lock{}; Py_XDECREF(po_); } int type_{}; // 0 toplevel @@ -43,7 +43,7 @@ Member_func p_members[] = {{nullptr, nullptr}}; static void call_python_with_section(Object* pyact, Section* sec) { PyObject* po = ((Py2Nrn*) pyact->u.this_pointer)->po_; PyObject* r; - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; PyObject* args = PyTuple_Pack(1, (PyObject*) newpysechelp(sec)); r = nrnpy_pyCallObject(po, args); @@ -156,7 +156,7 @@ static void py2n_component(Object* ob, Symbol* sym, int nindex, int isfunc) { Py2Nrn* pn = (Py2Nrn*) ob->u.this_pointer; PyObject* head = pn->po_; PyObject* tail; - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; if (pn->type_ == 0) { // top level if (!main_module) { main_module = PyImport_AddModule("__main__"); @@ -362,7 +362,7 @@ static PyObject* hoccommand_exec_help(Object* ho) { } static double praxis_efun(Object* ho, Object* v) { - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; PyObject* pc = nrnpy_ho2po(ho); PyObject* pv = nrnpy_ho2po(v); @@ -391,7 +391,7 @@ static double praxis_efun(Object* ho, Object* v) { } static int hoccommand_exec(Object* ho) { - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; PyObject* r = hoccommand_exec_help(ho); if (r == NULL) { @@ -411,7 +411,7 @@ static int hoccommand_exec(Object* ho) { } static int hoccommand_exec_strret(Object* ho, char* buf, int size) { - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; PyObject* r = hoccommand_exec_help(ho); if (r) { @@ -438,7 +438,7 @@ static int hoccommand_exec_strret(Object* ho, char* buf, int size) { static void grphcmdtool(Object* ho, int type, double x, double y, int key) { PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_; PyObject* r; - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; PyObject* args = PyTuple_Pack( 4, PyInt_FromLong(type), PyFloat_FromDouble(x), PyFloat_FromDouble(y), PyInt_FromLong(key)); @@ -460,7 +460,7 @@ static void grphcmdtool(Object* ho, int type, double x, double y, int key) { static Object* callable_with_args(Object* ho, int narg) { PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_; - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; PyObject* args = PyTuple_New((Py_ssize_t) narg); if (args == NULL) { @@ -493,7 +493,7 @@ static Object* callable_with_args(Object* ho, int narg) { static double func_call(Object* ho, int narg, int* err) { PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_; PyObject* r; - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; PyObject* args = PyTuple_New((Py_ssize_t) narg); if (args == NULL) { @@ -549,7 +549,7 @@ static double func_call(Object* ho, int narg, int* err) { static double guigetval(Object* ho) { PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_; - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; PyObject* r = NULL; PyObject* p = PyTuple_GetItem(po, 0); if (PySequence_Check(p) || PyMapping_Check(p)) { @@ -565,7 +565,7 @@ static double guigetval(Object* ho) { static void guisetval(Object* ho, double x) { PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_; - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; PyObject* pn = PyFloat_FromDouble(x); PyObject* p = PyTuple_GetItem(po, 0); if (PySequence_Check(p) || PyMapping_Check(p)) { @@ -578,7 +578,7 @@ static void guisetval(Object* ho, double x) { static int guigetstr(Object* ho, char** cpp) { PyObject* po = ((Py2Nrn*) ho->u.this_pointer)->po_; - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; PyObject* r = PyObject_GetAttr(PyTuple_GetItem(po, 0), PyTuple_GetItem(po, 1)); PyObject* pn = PyObject_Str(r); diff --git a/src/nrnpython/nrnpy_utils.h b/src/nrnpython/nrnpy_utils.h index 5decb2f31b..e6e3264bf5 100644 --- a/src/nrnpython/nrnpy_utils.h +++ b/src/nrnpython/nrnpy_utils.h @@ -160,22 +160,6 @@ class PyErr2NRNString { char* str_; }; - -struct PyLockGIL { - PyLockGIL() - : state_{PyGILState_Ensure()} {} - PyLockGIL(PyLockGIL&&) = delete; - PyLockGIL(PyLockGIL const&) = delete; - PyLockGIL& operator=(PyLockGIL&&) = delete; - PyLockGIL& operator=(PyLockGIL const&) = delete; - ~PyLockGIL() { - PyGILState_Release(state_); - } - - private: - PyGILState_STATE state_; -}; - extern void nrnpy_sec_referr(); #define CHECK_SEC_INVALID(sec) \ { \ diff --git a/src/nrnpython/nrnpython.cpp b/src/nrnpython/nrnpython.cpp index 41fb8c4c30..0a39a4eb46 100644 --- a/src/nrnpython/nrnpython.cpp +++ b/src/nrnpython/nrnpython.cpp @@ -17,6 +17,9 @@ #include #include #include + +#include + extern HocStr* hoc_cbufstr; extern int nrnpy_nositeflag; extern std::string nrnpy_pyexe; @@ -79,7 +82,7 @@ PyObject* basic_sys_path{}; * @param new_first Path to decode and prepend to sys.path. */ void reset_sys_path(std::string_view new_first) { - PyLockGIL _{}; + nanobind::gil_scoped_acquire _{}; auto* const path = PySys_GetObject("path"); nrn_assert(path); // Clear sys.path @@ -261,7 +264,7 @@ static int nrnpython_start(int b) { check("Could not initialise Python", Py_InitializeFromConfig(config)); // Manipulate sys.path, starting from the default values { - PyLockGIL _{}; + nanobind::gil_scoped_acquire _{}; auto* const sys_path = PySys_GetObject("path"); if (!sys_path) { throw std::runtime_error("Could not get sys.path from C++"); @@ -376,7 +379,7 @@ static void nrnpython_real() { #if USE_PYTHON HocTopContextSet { - PyLockGIL lock; + nanobind::gil_scoped_acquire lock{}; retval = (PyRun_SimpleString(hoc_gargstr(1)) == 0); } HocContextRestore