translating C++ exceptions to python

C

calin.hanchevici

Hi all,

I have a C++ library I call from python. The problem is I have c++
exceptions that i want to be translated to python. I want to be able to
do stuff like:
try:
my_cpp_function()
except cpp_exception_1:
do_stuff
except cpp_exception_2:
do_other_stuff

any ideas how can i do the translation?
Thanks, calin
 
H

harold fellermann

Hi all,

I have a C++ library I call from python. The problem is I have c++
exceptions that i want to be translated to python. I want to be able to
do stuff like:
try:
my_cpp_function()
except cpp_exception_1:
do_stuff
except cpp_exception_2:
do_other_stuff

any ideas how can i do the translation?

If you do not already use it, have a look at
http://www.boost.org/libs/python/ a C++ -- library to wrap the
Python C API, i.e. it helps you to extend Python in C++.
AFAIK it has fascilities to transform exceptions from one type
into the other.

- harold -
 
D

Denis S. Otkidach

On 13 Jun 2005 04:23:03 -0700
Hi all,

I have a C++ library I call from python. The problem is I have c++
exceptions that i want to be translated to python. I want to be able to
do stuff like:
try:
my_cpp_function()
except cpp_exception_1:
do_stuff
except cpp_exception_2:
do_other_stuff

any ideas how can i do the translation?

1. Create Python class for your exception. For simple case the code
will be:

static PyObject *YouExceptionClass;

# and in module initialization function:
YouExceptionClass = PyErr_NewException("YourModule.YourException", 0, 0);

2. Add it to module dictionary.

3. In wrapper for my_cpp_function use something like the following
code:

try {
my_cpp_function_impl();
} catch (YouException &exc) {
PyErr_SetString(YouExceptionClass, exc.what());
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top