By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

translating C++ exceptions to python

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 13 Jun 2005 04:23:03 -0700
ca**************@gmail.com wrote:
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;
}

--
Denis S. Otkidach
http://www.python.ru/ [ru]
Jul 19 '05 #2

P: n/a

On 13.06.2005, at 13:23, ca**************@gmail.com wrote:
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 -

---
Everybody lies. but it does not matter, as no one listens.
---

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.