C++ throw causes abort in extension?

Chris Tavares christophertavares at earthlink.net
Mon Dec 24 14:02:22 EST 2001


"Robert Nikander" <nikander at mindspring.nospamcom> wrote in message
news:20011224.123518.378096161.2956 at localhost.localdomain...
> Hi everyone,
> I am trying to use C++ in my Python extension, but any throwing of
> exceptions cause the program to 'abort.'
>
> I put this code in my init module function; and below you see what happens
> when I try to import...
>
>   cout << "about to throw in initrobs" << endl;
>   try {
>     throw 100;
>   }
>   catch (...) {
>     cout << "Caught ..." << endl;
>   }
>
> Python 2.2c1 (#1, Dec 18 2001, 11:04:07) [GCC 3.0.2] on linux2 Type
> "help", "copyright", "credits" or "license" for more information.
> >>> import robs
> about to throw in initrobs
> Aborted
> [rob at ibooker py_ext2]$
>
> I tested exceptions apart from Python and they are working fine on my
system
> normally.  So is there something wrong with throwning an exception when
> your C++ code was called by C code?  I also tried downloading CXX and it
> aborts foir the same reasons, so maybe this is something with GCC 3.02?
>
> Thanks for any help,
> Rob

Typically, this sort of problem arises when mixing C++ and C. I'm not a GCC
expert (I do most of my programming on Windows, actually) so take what I'm
saying with a grain of salt, but...

A C++ application needs to do more setup at start time than a C app,
including initializing exception handling. When you use a dynamically loaded
extension module written in C++, the caller (the Python interpreter) is a C
application, so the setup of exception handling wasn't done. Therefore, when
you call throw, some data structure it was expecting wasn't there, and
things go boom.

In the Windows world, the solution is to compile main with a C++ compiler,
not a C compiler. No clue what to do under GCC/Linux, though.

-Chris






More information about the Python-list mailing list