From 961096f9df1e3fdb29322d316a688f9d13947052 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Wed, 12 Apr 2017 11:17:15 -0400 Subject: [PATCH] Prevent segfault if Python was never initialized --- src/PYTHON/python.cpp | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/PYTHON/python.cpp b/src/PYTHON/python.cpp index 74591c1c16..c2ff7f03c7 100644 --- a/src/PYTHON/python.cpp +++ b/src/PYTHON/python.cpp @@ -45,23 +45,25 @@ Python::Python(LAMMPS *lmp) : Pointers(lmp) Python::~Python() { - // clean up - PyGILState_STATE gstate = PyGILState_Ensure(); + if(pyMain) { + // clean up + PyGILState_STATE gstate = PyGILState_Ensure(); - for (int i = 0; i < nfunc; i++) { - delete [] pfuncs[i].name; - deallocate(i); - PyObject *pFunc = (PyObject *) pfuncs[i].pFunc; - Py_XDECREF(pFunc); - } + for (int i = 0; i < nfunc; i++) { + delete [] pfuncs[i].name; + deallocate(i); + PyObject *pFunc = (PyObject *) pfuncs[i].pFunc; + Py_XDECREF(pFunc); + } - // shutdown Python interpreter + // shutdown Python interpreter - if (pyMain && !external_interpreter) { - Py_Finalize(); - } - else { - PyGILState_Release(gstate); + if (!external_interpreter) { + Py_Finalize(); + } + else { + PyGILState_Release(gstate); + } } memory->sfree(pfuncs);