translating C++ exceptions to python

Discussion in 'Python' started by calin.hanchevici@gmail.com, Jun 13, 2005.

  1. Guest

    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
    , Jun 13, 2005
    #1
    1. Advertising

  2. On 13.06.2005, at 13:23, 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.
    ---
    harold fellermann, Jun 13, 2005
    #2
    1. Advertising

  3. On 13 Jun 2005 04:23:03 -0700
    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]
    Denis S. Otkidach, Jun 14, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    255
  2. Davis Marques
    Replies:
    6
    Views:
    310
    Paul Rubin
    Jan 12, 2004
  3. Dave

    translating PHP to Python

    Dave, Feb 5, 2006, in forum: Python
    Replies:
    10
    Views:
    597
    Eric Nieuwland
    Feb 6, 2006
  4. Vyz
    Replies:
    5
    Views:
    2,012
  5. hofer
    Replies:
    12
    Views:
    1,481
Loading...

Share This Page