Unverified Commit 26532ffa authored by peastman's avatar peastman Committed by GitHub
Browse files

C++ exceptions get translated to Python OpenMMException class (#2672)

parent fdfdb0f0
...@@ -30,3 +30,7 @@ if sys.platform == 'win32': ...@@ -30,3 +30,7 @@ if sys.platform == 'win32':
os.environ['PATH'] = _path os.environ['PATH'] = _path
del _path del _path
__version__ = Platform.getOpenMMVersion() __version__ = Platform.getOpenMMVersion()
class OpenMMException(Exception):
"""This is the class used for all exceptions thrown by the C++ library."""
pass
...@@ -18,6 +18,7 @@ SKIP_METHODS = [('State', 'getPositions'), ...@@ -18,6 +18,7 @@ SKIP_METHODS = [('State', 'getPositions'),
('State', 'getForces'), ('State', 'getForces'),
('StateBuilder',), ('StateBuilder',),
('Vec3',), ('Vec3',),
('OpenMMException',),
('AngleInfo',), ('AngleInfo',),
('ApplyAndersenThermostatKernel',), ('ApplyAndersenThermostatKernel',),
('ApplyConstraintsKernel',), ('ApplyConstraintsKernel',),
......
...@@ -3,7 +3,9 @@ ...@@ -3,7 +3,9 @@
try { try {
$action $action
} catch (std::exception &e) { } catch (std::exception &e) {
PyErr_SetString(PyExc_Exception, const_cast<char*>(e.what())); PyObject* mm = PyImport_AddModule("simtk.openmm");
PyObject* openmm_exception = PyObject_GetAttrString(mm, "OpenMMException");
PyErr_SetString(openmm_exception, const_cast<char*>(e.what()));
return NULL; return NULL;
} }
} }
...@@ -14,7 +16,9 @@ ...@@ -14,7 +16,9 @@
$action $action
} catch (std::exception &e) { } catch (std::exception &e) {
PyEval_RestoreThread(_savePythonThreadState); PyEval_RestoreThread(_savePythonThreadState);
PyErr_SetString(PyExc_Exception, const_cast<char*>(e.what())); PyObject* mm = PyImport_AddModule("simtk.openmm");
PyObject* openmm_exception = PyObject_GetAttrString(mm, "OpenMMException");
PyErr_SetString(openmm_exception, const_cast<char*>(e.what()));
return NULL; return NULL;
} }
PyEval_RestoreThread(_savePythonThreadState); PyEval_RestoreThread(_savePythonThreadState);
...@@ -26,7 +30,9 @@ ...@@ -26,7 +30,9 @@
$action $action
} catch (std::exception &e) { } catch (std::exception &e) {
PyEval_RestoreThread(_savePythonThreadState); PyEval_RestoreThread(_savePythonThreadState);
PyErr_SetString(PyExc_Exception, const_cast<char*>(e.what())); PyObject* mm = PyImport_AddModule("simtk.openmm");
PyObject* openmm_exception = PyObject_GetAttrString(mm, "OpenMMException");
PyErr_SetString(openmm_exception, const_cast<char*>(e.what()));
return NULL; return NULL;
} }
PyEval_RestoreThread(_savePythonThreadState); PyEval_RestoreThread(_savePythonThreadState);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment