[Cython] 'with gil:' statement

mark florisson markflorisson88 at gmail.com
Fri Mar 18 00:49:04 CET 2011

On 18 March 2011 00:32, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Dag Sverre Seljebotn wrote:
>> def f():
>>    with nogil:
>>        for ...:
>>            A
>>            if something_exceptional:
>>                with gil:
>>                    raise Exception(...)
>>            B
>>        C
> If that's to be supported, the following really ought to be
> supported as well:
>  def f():
>     with nogil:
>        try:
>           ...
>           with gil:
>              raise Exception()
>        finally:
>           ...do some cleanup...

Why? I assume in his example the for loop was a Cython C for loop, not
one that deals with Python objects. If you want to do cleanup you can
catch the exception in the 'with gil:' block, or use try/finally in
the 'with gil:' block or outside the 'with nogil:' block.

Special-casing try/finally in nogil sections for only 'with gil:'
sounds somewhat weird. On the other hand, it may be somewhat more
convenient, and I think we could support it without having to acquire
the GIL in the finally clause. I wouldn't be particularly opposed to

> Greg
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> http://mail.python.org/mailman/listinfo/cython-devel

More information about the cython-devel mailing list