[Cython] Error handling during buffer reassignment

Robert Bradshaw robertwb at gmail.com
Fri Sep 8 00:36:46 EDT 2017


On Wed, Sep 6, 2017 at 3:53 AM, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Hi,
>
> consider this code:
>
>     cdef np.ndarray[int32, ndim=1] a
>     a = new_int32_buffer()
>     a = new_float32_buffer()
>
> This fails during the second assignment, but only during validation, after
> unpacking the buffer. The code that handles this case is generated here:
>
> https://github.com/cython/cython/blob/c95ca9f21a3524718a83c3415bb7102a508154be/Cython/Compiler/Buffer.py#L376
>
> Note the twist where it says:
>
>         # If acquisition failed, attempt to reacquire the old buffer
>         # before raising the exception.
>
> I faintly remember a discussion about that case (apparently back in 2008),
> but can't find it in my mail archive. Might have been off-list at the time.
>
> Question: Instead of re-acquiring the buffer (and thus faking the
> non-assignment), wouldn't it be better to acquire the new buffer in a temp,
> and only overwrite the old "Py_buffer" if all is fine with it?

That makes a lot more sense to me, assuming overwriting is cheap
enough. (Can an error happen in releasing the old value?)


More information about the cython-devel mailing list