0

I have some C++ code that runs Python 3 code using Boost.Python. If any exception in Python occurs, it is caught and a C++ exception thrown. I want to pass some minimal error information to the C++ exception.

What I currently have is this:

try {
    // execute python code via Boost.Python
} catch(boost::python::error_already_set&) {
    PyObject *ptype, *pvalue, *ptraceback;
    PyErr_Fetch(&ptype, &pvalue, &ptraceback);
    if(pvalue) {
        // ???
    }
}

There are many helpful answers here at SO, but they all seem to apply to Python 2, and their solutions do not work anymore. I have tried a few things like PyBytes_AsString, PyUnicode_AsUTF8, but all I get back are null pointers.

How do I go about extracting something meaningful from pvalue? For starters, the type name of the exception raised would already be quite helpful. If there was a string passed to the exception, getting that would be most helpful.

0

1 Answer 1

4

Of course, I found a solution myself immediately after I had formulated and posted this question.

This code finally does the trick and provides nice messages:

try {
    // execute python code via Boost.Python
} catch(boost::python::error_already_set&) {
    PyObject *ptype, *pvalue, *ptraceback;
    PyErr_Fetch(&ptype, &pvalue, &ptraceback);
    if(pvalue) {
        PyObject *pstr = PyObject_Str(pvalue);
        if(pstr) {
            const char* err_msg = PyUnicode_AsUTF8(pstr);
            if(pstr)
                // use err_msg
        }
        PyErr_Restore(ptype, pvalue, ptraceback);
    }
}

I'd be glad for any feedback, though. I know little of Python and less of its C API.

Sign up to request clarification or add additional context in comments.

1 Comment

PyObject_Str() creates a new reference. I am trying to do the same thing, but using PyBytes_AsString()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.