According to the documentation for PyErr_CheckSignals() (at
http://docs.python.org/c-api/exceptions.html#PyErr_CheckSignals ), the
KeyboardInterrupt is already raised. So, you just need to break out of
your loop and tell Cython to raise the exception. I don't know how to do
that, but presumably it would just cause the cython compiler to emit C
code like "goto fail;".

-Andrew

David Cournapeau wrote:
> Hi,
> 
> I am using cython to wrap some relatively low level C api (ALSA). It
> has worked great so far, specially for integrating numpy array and C
> API. But I have a problem with signals. ALSA is the Linux API for
> sound devices, and I have a cython class like the following:
> 
> cdef class AlsaDevice:
>     def __init__(AlsaDevice self):
>         # Set up the device
> 
>     def __dealloca__(AlsaDevice self):
>         # Free-up the device and related resources
> 
>     def play(AlsaDevice self, ndarray data):
>         # Play the data through the device
> 
>         # Number of buffers to play
>         cdef int nbuf
>         cdef int i
> 
>         for i in range(mbuf):
>             # Call the C API
> 
> When a KeyboardInterrupt is raised while executing the loop, it is
> blocked until the end of the loop, which is not ideal, obviously.
> After having tried different things, I came up with the following:
> 
>         cdef int err
>         ....
>         for i in range(mbuf):
>             err = python_exc.PyErr_CheckSignals()
>             if err != 0:
>                  if python_exc.PyErr_ExceptionMatches(KeyboardInterrupt):
>                      raise KeyboardInterrupt()
>             # Call the C API
> 
> Which seems to do what I want. But I don't really understand why it
> works, in particular, is it guaranteed that every ressource is freed
> (assuming everything is correctly handled when no SIGINT is send to
> the python process) ? Is there a better way ?
> 
> David
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to