Merge pull request #450 from rbberger/python_destruction_fix

Prevent segfault if Python was never initialized
This commit is contained in:
sjplimp
2017-04-12 13:58:23 -06:00
committed by GitHub

View File

@ -45,23 +45,25 @@ Python::Python(LAMMPS *lmp) : Pointers(lmp)
Python::~Python() Python::~Python()
{ {
// clean up if(pyMain) {
PyGILState_STATE gstate = PyGILState_Ensure(); // clean up
PyGILState_STATE gstate = PyGILState_Ensure();
for (int i = 0; i < nfunc; i++) { for (int i = 0; i < nfunc; i++) {
delete [] pfuncs[i].name; delete [] pfuncs[i].name;
deallocate(i); deallocate(i);
PyObject *pFunc = (PyObject *) pfuncs[i].pFunc; PyObject *pFunc = (PyObject *) pfuncs[i].pFunc;
Py_XDECREF(pFunc); Py_XDECREF(pFunc);
} }
// shutdown Python interpreter // shutdown Python interpreter
if (pyMain && !external_interpreter) { if (!external_interpreter) {
Py_Finalize(); Py_Finalize();
} }
else { else {
PyGILState_Release(gstate); PyGILState_Release(gstate);
}
} }
memory->sfree(pfuncs); memory->sfree(pfuncs);