summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel M. Pelt <D.M.Pelt@cwi.nl>2015-07-20 10:34:55 +0200
committerWillem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl>2015-07-23 11:57:17 +0200
commitef9eb1dc7eb494e87f728af7caff8e5291cf320c (patch)
tree7a31b1ef8e77ff713e7e1fd8002401917e542d73
parent3d136b7c819b0b142ad056bf01c8c1191eea9ba0 (diff)
downloadastra-ef9eb1dc7eb494e87f728af7caff8e5291cf320c.tar.gz
astra-ef9eb1dc7eb494e87f728af7caff8e5291cf320c.tar.bz2
astra-ef9eb1dc7eb494e87f728af7caff8e5291cf320c.tar.xz
astra-ef9eb1dc7eb494e87f728af7caff8e5291cf320c.zip
Also log Python errors when importing and creating Python plugins
-rw-r--r--src/PluginAlgorithm.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/PluginAlgorithm.cpp b/src/PluginAlgorithm.cpp
index c26ee3f..a118f54 100644
--- a/src/PluginAlgorithm.cpp
+++ b/src/PluginAlgorithm.cpp
@@ -29,6 +29,7 @@ $Id$
#ifdef ASTRA_PYTHON
#include "astra/PluginAlgorithm.h"
+#include "astra/Logging.h"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/lexical_cast.hpp>
@@ -38,8 +39,53 @@ $Id$
namespace astra {
+
+void logPythonError(){
+ if(PyErr_Occurred()){
+ PyObject *ptype, *pvalue, *ptraceback;
+ PyErr_Fetch(&ptype, &pvalue, &ptraceback);
+ PyObject *traceback = PyImport_ImportModule("traceback");
+ if(traceback!=NULL){
+ PyObject *exc;
+ if(ptraceback==NULL){
+ exc = PyObject_CallMethod(traceback,"format_exception_only","OO",ptype, pvalue);
+ }else{
+ exc = PyObject_CallMethod(traceback,"format_exception","OOO",ptype, pvalue, ptraceback);
+ }
+ if(exc!=NULL){
+ PyObject *six = PyImport_ImportModule("six");
+ if(six!=NULL){
+ PyObject *iter = PyObject_GetIter(exc);
+ if(iter!=NULL){
+ PyObject *line;
+ std::string errStr = "";
+ while(line = PyIter_Next(iter)){
+ PyObject *retb = PyObject_CallMethod(six,"b","O",line);
+ if(retb!=NULL){
+ errStr += std::string(PyBytes_AsString(retb));
+ Py_DECREF(retb);
+ }
+ Py_DECREF(line);
+ }
+ ASTRA_ERROR("%s",errStr.c_str());
+ Py_DECREF(iter);
+ }
+ Py_DECREF(six);
+ }
+ Py_DECREF(exc);
+ }
+ Py_DECREF(traceback);
+ }
+ if(ptype!=NULL) Py_DECREF(ptype);
+ if(pvalue!=NULL) Py_DECREF(pvalue);
+ if(ptraceback!=NULL) Py_DECREF(ptraceback);
+ }
+}
+
+
CPluginAlgorithm::CPluginAlgorithm(PyObject* pyclass){
instance = PyObject_CallObject(pyclass, NULL);
+ if(instance==NULL) logPythonError();
}
CPluginAlgorithm::~CPluginAlgorithm(){
@@ -148,6 +194,8 @@ CPluginAlgorithm * CPluginAlgorithmFactory::getPlugin(std::string name){
if(pyclass!=NULL){
alg = new CPluginAlgorithm(pyclass);
Py_DECREF(pyclass);
+ }else{
+ logPythonError();
}
}else{
alg = new CPluginAlgorithm(className);