[Cython] [cython-users] C++: how to handle failures of 'new'?
Robert Bradshaw
robertwb at gmail.com
Thu Jul 19 21:29:39 CEST 2012
On Thu, Jul 19, 2012 at 12:19 PM, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Stefan Behnel, 15.07.2012 19:41:
>> Stefan Behnel, 29.06.2012 07:45:
>>> [moving this to cython-devel as it's getting technical]
>>>
>>> Robert Bradshaw, 28.06.2012 21:46:
>>>> On Thu, Jun 28, 2012 at 11:38 AM, Stefan Behnel wrote:
>>>>> currently, when I write "new CppClass()" in Cython, it generates a straight
>>>>> call to the "new" operator. It doesn't do any error handling. And the
>>>>> current documentation doesn't even mention this case.
>>>>>
>>>>> Is there a "standard" way to handle this? It seems that C++ has different
>>>>> ways to deal with failures here but raises an exception by default. Would
>>>>> you declare the constructor(s) with an "except +MemoryError"? Is there a
>>>>> reason Cython shouldn't be doing this automatically (if nothing else was
>>>>> declared) ?
>>>>
>>>> I think it certainly makes sense to declare the default constructor as
>>>> "except +" (and std::bad_alloc should become MemoryError),
>>>
>>> Right. The code in the constructor can raise other exceptions that must
>>> also be handled properly. An explicit "except +" will handle that.
>>
>> What about the declarations that we ship in libcpp.*? They currently lack
>> any such exception declarations. Can we safely add them where appropriate?
>>
>> And, would someone care to do it?
>
> Maybe the question at the end just scares off too many potential repliers.
>
> May I assume that the answer to the first paragraph is "yes"? Or does
> anyone see problems with changing that now? My reading is that it would
> (above all) fix user code that's currently broken.
My thoughts are that we can (and should) safely
(1) Add these exception declarations to our shipped pxd files, and
(2) Let implicit constructors be declared as "except +MemoryError."
If no one beats me too it, I suppose I could, but it's an easy bug for
anyone to knock off.
- Robert
More information about the cython-devel
mailing list