Thread: Sdk
View Single Post
  #23  
Old 05-23-2011, 10:00 PM
dcnieho dcnieho is offline
Member
 
Join Date: Feb 2011
Posts: 59
Ok, I have a (temporary) solution:

In my C++ code I have defined a helper function modeled after the ones in your viz/python include:
Code:
// helper for making the list to communicate an exception
// Returns new reference to list of a python object and a string
inline PyObject* PYTHON_EXCEPTION_STRING_LIST(PyObject* exc, const char * msg)
{
    PyObject *list = PyList_New(2);
    PyList_SET_ITEM(list,0,exc);
    PyList_SET_ITEM(list,1,PyString_FromString(msg));
    return list;
}
I then "raise" my exception by
Code:
data.set( PYTHON_RETURN_OBJECT, PYTHON_EXCEPTION_STRING_LIST(PyExc_RuntimeError,"testing");
at the python end, I wrap all my command() calls in a function errorHandler defined as follows:
Code:
# function that should be wrapped around any interaction with the stimulus class through command()s
# detect if an error case is communicated, if not, simply pass on the input
def errorHandler(self,input):
    if isinstance(input,list) and issubclass(input[0],BaseException):
        # we have an exception to handle, raise it
        raise input[0](input[1])    # raise exception communicated by C++ code
    else:
        return input
This works also if the C++ code doesn't set any return value as in this case command simply returns None (at least that's the input to errorHandler).

I suppose this is dirty and not how its supposed to be done, but tis a workaround for now. Hope you can show me how to get PyErr_SetString to work!
Reply With Quote