[Cython] [cython-users] C++: how to handle failures of 'new'?

Stefan Behnel stefan_ml at behnel.de
Tue Jul 3 18:38:16 CEST 2012


Dag Sverre Seljebotn, 03.07.2012 18:11:
> On 07/03/2012 09:14 AM, Stefan Behnel wrote:
>> I don't know what happens if a C++ exception is not being caught, but I
>> guess it would simply crash the application. That's a bit more visible than
> 
> Yep.
> 
>> just printing a warning when a Python exception is being ignored due to a
>> missing declaration. It's really unfortunate that our documentation didn't
>> even mention the need for this, because it's not immediately obvious that
>> Cython won't handle errors in "new", and testing for memory errors isn't
>> quite what people commonly do in their test suites.
>>
>> Apart from that, I agree, users have to take care to properly declare the
>> API they are using.
> 
> Is there any time you do NOT want a "catch (...) {}" block? I can't see a
> C++ exception propagating to Python-land doing anything useful ever.

That would have been my intuition, too.


> So shouldn't we just make --cplus turn *all* external functions and methods
> (whether C-like or C++-like) into "except +"? (Or keep except+ for manual
> translation, but always have a catch(...)".
>
> Performance overhead is the only reason I can think of to not do this,
> although IIRC C++ catch blocks are only dealt with during stack unwinds and
> doesn't cost anything/much (?) when they're not triggered.
> 
> "except -1" should then actually mean both; "except + except -1". So it's
> more a question of just adding catch(...) *everywhere*, than making "except
> +" the default.

I have no idea if there is a performance impact, but if there isn't, always
catching all exceptions sounds like a reasonable thing to do. After all, we
have no support for catching C++ exceptions on user side.

Stefan


More information about the cython-devel mailing list