Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > translating C++ exceptions to python

Reply
Thread Tools

translating C++ exceptions to python

 
 
calin.hanchevici@gmail.com
Guest
Posts: n/a
 
      06-13-2005
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

 
Reply With Quote
 
 
 
 
harold fellermann
Guest
Posts: n/a
 
      06-13-2005

On 13.06.2005, at 13:23, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
---

 
Reply With Quote
 
 
 
 
Denis S. Otkidach
Guest
Posts: n/a
 
      06-14-2005
On 13 Jun 2005 04:23:03 -0700
(E-Mail Removed) 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]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dict slice in python (translating perl to python) hofer Python 12 09-11-2008 11:46 PM
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments