On 06/11/2012 15:35, Martin Hellmich wrote:
I've based my code on.
http://stackoverflow.com/questions/9620268/boost-python-custom-exception-class

I have used the same reference and also this suggestion for including the
other attributes shown here:
http://stackoverflow.com/questions/11448735/boostpython-export-custom-exception-and-inherit-from-pythons-exception

However, this doesn't solve my problem.
It works fine when I throw the MyException in C++, but when I raise the
MyCPPException in Python, the translator is not called (which makes sense), so
the attribute 'cause' does not change.

Actually, before I throw the MyException for the first time in C++,
MyCPPException does not have the attribute 'cause' at all.

Of course.

This issue should be the same, if I set the exception with PyErr_SetObject,
since the translator is not called, when I 'raise' it.

Very likely the SetString uses SetObject under the hood, but I never used SetObject.
I'd try to:

*) As you were writing before, declare a C++ class MyException: public std::exception {
 MyException();
 MyException(int code, const std::string &string);

 int code();
 const char* what();

 int errorCode;
 std::string errorMessage;
 };
*) Wrap it as a bpy::class_ so you can use it like a standard exception with all the attributes you need. *) Register an exception translator as defined before. Maybe the to-python conversion performed in the exception translator does work even if you have wrapped MyException as a standard class.

--
            Giuseppe Corbelli
WASP Software Engineer, Copan Italia S.p.A
Phone: +390303666318  Fax: +390302659932
E-mail: giuseppe.corbe...@copanitalia.com
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to