- Applied Matteo`s tips to the error handling code. Now all loading errors whould be handled. Don`t know about runtime errors, though

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@832 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
elvez 2006-08-09 13:24:42 +00:00
parent 6911676b53
commit addad6aa26
3 changed files with 42 additions and 31 deletions

View File

@ -35,32 +35,13 @@ using namespace std;
PythonCPUPolicy::PythonCPUPolicy(const char* name) throw(MalformedPolicyException)
: _upolicy_dict(NULL), _adapter(NULL), _name(name)
{
//PyRun_SimpleString("import StringIO\nimport sys\nsys.stderr = cStringIO.StringIO()\n");
//PyObject* pBuiltinsMod = PyImport_ImportModule("__builtin__");
//PyObject* pBuiltinsDict = PyModule_GetDict(pBuiltinsMod);
PyObject* pLoadmeStr = PyString_FromString(name);
PyObject* pUserCPUPolicyModule = PyImport_Import(pLoadmeStr);
Py_DECREF(pLoadmeStr);
if( !pUserCPUPolicyModule )
{
PyErr_Print(); // Error in import
// PyObject* msg = PyRun_String("import sys\nsys.stderr.getvalue()", Py_eval_input, pBuiltinsDict, NULL);
// if(msg == NULL)
// PyErr_Print();
// char* str = PyString_AsString(msg);
// throw MalformedPolicyException(PyString_AS_STRING(msg));
throw MalformedPolicyException("");
}
if(pUserCPUPolicyModule == NULL)
throw MalformedPolicyException(get_exception_information().c_str());
// Dictionary with defined ``symbols'' for .pyc file
_upolicy_dict = PyModule_GetDict(pUserCPUPolicyModule);
assert(_upolicy_dict);
@ -89,12 +70,8 @@ PythonCPUPolicy::PythonCPUPolicy(const char* name) throw(MalformedPolicyExceptio
Py_DECREF(pScriptAdapterModule);
if(_adapter == NULL)
{
PyErr_Print();
throw MalformedPolicyException(_("unable to instantiate ScriptAdapter, "
"check that all methods are implemented"));
}
throw MalformedPolicyException(get_exception_information().c_str());
// And now, who's your daddy, huh?
}
@ -212,8 +189,8 @@ PythonCPUPolicy::wait_unlock() const throw(UserInterruptException)
PyEval_RestoreThread(_save);
throw UserInterruptException("User-defined policy is "
"taking too long to terminate.");
throw UserInterruptException(_("User-defined policy is "
"taking too long to terminate."));
}
}
while(still_locked);
@ -234,3 +211,36 @@ PythonCPUPolicy::wait_unlock() const throw(UserInterruptException)
} */
}
string
PythonCPUPolicy::get_exception_information()
{
if(PyErr_Occurred() == NULL)
return _("no error");
PyObject* pType = NULL;
PyObject* pValue = NULL;
PyObject* pTraceback = NULL;
PyErr_Fetch(&pType, &pValue, &pTraceback);
string msg;
if(pValue != NULL)
{
PyObject* pValueStr = PyObject_Str(pValue);
msg = PyString_AsString(pValueStr);
Py_DECREF(pValueStr);
}
else
msg = string(_("no available information for this error"));
if(pType != NULL) Py_DECREF(pType);
if(pValue != NULL) Py_DECREF(pValue);
if(pTraceback != NULL) Py_DECREF(pTraceback);
return msg;
}

View File

@ -91,6 +91,7 @@ namespace sgpem
PythonCPUPolicy& operator=(const PythonCPUPolicy&);
void wait_unlock() const throw(UserInterruptException);
static std::string get_exception_information();
PyObject* _upolicy_dict;
PyObject* _adapter;

View File

@ -141,7 +141,7 @@ PythonCPUPolicyManager::collect_policies()
}
catch(MalformedPolicyException e)
{
std::cerr << e.what() << endl;
std::cerr << "POLICY LOAD ERROR: " << e.what() << endl;
}
}
else