Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can we in python code, catch exception generated by C++ pythonized function?

484 views
Skip to first unread message

Gnana Kanagaratnam

unread,
Jan 8, 2001, 5:49:07 PM1/8/01
to
I tried and it won't. Anybody have any workaround for that? Any pointer
is appreciated. Thanks.

Regards,

Tong

Siu-Tong Hui

unread,
Jan 8, 2001, 5:55:01 PM1/8/01
to

Alex Martelli

unread,
Jan 9, 2001, 8:15:52 AM1/9/01
to
"Siu-Tong Hui" <to...@aristotech.com> wrote in message
news:3A5A4545...@aristotech.com...

> I tried and it won't. Anybody have any workaround for that? Any pointer
> is appreciated. Thanks.

What you see on the Python level as a Python exception
is denoted by a C-level function returning NULL (a zero
pointer) rather than a PyObject* (a pointer to a PyObject),
as well as having made the appropriate PyErr_... calls
to define the details of the exception-object being
'raised'. This is part of Python's C API.

"Exceptions" raised/handled by other protocols (such as,
C++ Exceptions, COM Exceptions, Win/NT SEH Exceptions,
and so on) have no a-priori connection with this specific
exception protocol. You (or whatever framework you're
using to map your code to Python's C API) must bridge
the gap, if you want it to be bridged.

You're not telling us which (if any) framework (such as
Boost Python, CXX, etc) you may be using to map C++ code
to Python's C API, so, as a guess, you may be using the
raw C API from C++. That's OK, but then you also need
to do the needed bridging at the raw C API level.


E.g., where you now have something like:

PyObject* myFunction(PyObject* self, PyObject* args)
{
// lots and lots of code snipped
return Py_BuildValue(""); // or whatever
}

you will instead have something like:

PyObject* myFunction(PyObject* self, PyObject* args)
{
try {
// lots and lots of code snipped
return Py_BuildValue(""); // or whatever
} catch(...) { // more specific catch clauses work better
// whatever PyErr... calls are appropriate
return 0;
}
}


Alex

Mark Hammond

unread,
Jan 9, 2001, 8:18:09 PM1/9/01
to
In article <3A5A4545...@aristotech.com>,

Siu-Tong Hui <to...@aristotech.com> wrote:
> I tried and it won't. Anybody have any workaround for that? Any
pointer
> is appreciated. Thanks.

I tried and it will. Anybody have any further details for that? Any
pointer to exactly what you mean is appreciated. Thanks.


Sent via Deja.com
http://www.deja.com/

Siu-Tong Hui

unread,
Jan 12, 2001, 8:07:58 PM1/12/01
to
Thanks for all the responses.

We use SWIG to link from python to C++. Our highest level code is in
python.

What I tried was simply using:

try:
# call to C++ pythonized function
except:
print "Error: Abnormal Program Termination"

In the C++ code, I throw an exception. I couldn't catch it in the above
python code.

If I cannot catch exception in python, since we are using python as high
level command shell, we would have to put in exception control codes in
all pythonized C++ functions in order to have bullet-proof exception
protection. If I can catch exception in python, then I only need to do it
once in my highest level command shell in python.

Tong

Mark Hammond

unread,
Jan 12, 2001, 8:44:29 PM1/12/01
to
In article <3A5FAA6E...@aristotech.com>,

Siu-Tong Hui <to...@aristotech.com> wrote:
> Thanks for all the responses.
>
> We use SWIG to link from python to C++. Our highest level code is in
> python.
...

> level command shell, we would have to put in exception control
> codes in all pythonized C++ functions in order to have
> bullet-proof exception protection. If I can catch exception in
> python, then I only need to do it once in my highest level command
> shell in python.

You can't catch C++ exceptions directly in Python. But check out the
SWIG %except typedef. You should be able to say something like:

%typemap(python,except) MYBOOL {
try {
$function
} catch (e) {
PyErr_SetString(MyError, e->description_or_whatever);
return NULL;
}
}

This is the technique I use to ensure that most SWIG wrapped functions
in the Win32 extensions correctly convert Win32 error codes to Python
exceptions - see pywintypes.i in the Python for win32 sources.

Mark.

0 new messages