[Cython] Generators not closed on dealloc (Cython 0.15.1+, current git master)
Vitja Makarov
vitja.makarov at gmail.com
Tue Nov 15 18:18:28 CET 2011
2011/11/14 Vitja Makarov <vitja.makarov at gmail.com>:
> 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
> i
>> 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):
> try:
> yield a
> finally:
> 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.
>
I've fixed it here
https://github.com/vitek/cython/commits/_generators_cleanup
I've taken deallocation code from CPython it performs magic object
resurrection when it''s almost dead.
Now I'm waiting for hudson job then I'll create a pull request.
--
vitja.
More information about the cython-devel
mailing list