Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   translating C++ exceptions to python (http://www.velocityreviews.com/forums/t346033-translating-c-exceptions-to-python.html)

calin.hanchevici@gmail.com 06-13-2005 11:23 AM

translating C++ exceptions to python
 
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


harold fellermann 06-13-2005 10:30 PM

Re: translating C++ exceptions to python
 

On 13.06.2005, at 13:23, calin.hanchevici@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.
---


Denis S. Otkidach 06-14-2005 09:20 AM

Re: translating C++ exceptions to python
 
On 13 Jun 2005 04:23:03 -0700
calin.hanchevici@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]


All times are GMT. The time now is 03:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.