Hi Giuseppe,

thank you for the detailed response!

On 11/07/2012 08:56 AM, Giuseppe Corbelli wrote:
On 06/11/2012 15:35, Martin Hellmich wrote:
I've based my code on.
http://stackoverflow.c  om/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.

This is the part that I miss, I guess. How can I wrap a class so that it works like an exception in python? In the solutions on stackoverflow the translation to an exception only happens in the translator, meaning that I cannot call 'raise MyException' in python, as it doesn't inherit from (the python) Exception.

PyErr_NewException has to be based on a type of python Exception, so I cannot base it on my c++ class, right?
Have you ever used the 3rd parameter to specify a dict?

Cheers
Martin

*) 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.


--
Martin Hellmich                    Information Technology Department
mhell...@cern.ch                                                CERN
+41 22 76 765 26                                   CH-1211 Geneva 23
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to