[Cython] Generators not closed on dealloc (Cython 0.15.1+, current git master)

Vitja Makarov vitja.makarov at gmail.com
Sun Nov 13 22:09:05 CET 2011

2011/11/13 Matt Hagy <hagy at gatech.edu>:
> Hi,
> Cython generators aren’t closed on dealloc and this can lead to cleanup code not being ran (see example below). I couldn’t find any reference to this issue on the trac or the wiki and therefore wanted to report it. If this is a know issue please disregard this post.

Good catch!

> I believe this can easily be corrected by adding the following line to the top of each generator dealloc function:
>    if (p->resume_label != -1) __Pyx_Generator_Close(o);

I think that correct fix should be > 0 as we don't wanna close just
started generator
> This explicitly closes the generator unless it’s already finished. For testing I manually added this code to the generated C file and it worked as expected.
> -Matt

It's not that easy to fix. For convenience Closure sublasses generator type:

def foo(a):
        yield a
        print 'cleanup'
        print '"a" might be dead at this point', a

The problem is closure's __dealloc__ is called first then generator's.
So we can't call Geneator_Close() with deallocated closure.

I see few options here:
 - do cleanup in closure type itsef
 - make closure generator's attribute

I like the second case.


More information about the cython-devel mailing list