This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients ncoghlan, njs, vstinner, yselivanov
Date 2016-11-07.02:50:30
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1478487031.39.0.63356302946.issue28629@psf.upfronthosting.co.za>
In-reply-to
Content
There have been a few discussions recently regarding the fact that generator and coroutine cleanup can suppress ResourceWarnings from other components. For example:

    def mygen(fname):
        with open(fname) as f:
            yield from f

    print(next(mygen("README.md")))

Here, the opened file is actually being cleaned up by __del__ on the generator-iterator instance (when it throws GeneratorExit into the suspended frame), but there's no ResourceWarning as the *file itself* is being cleaned up by the file's __exit__ method.

I've been thinking about how we might be able to help developers better manage this, and one conclusion I've come to is that it means we need to start thinking about suspended frames that don't terminate naturally during the course of program execution as resources to be managed - their locals can and do reference scarce system resources, and folks are expecting "with" and "try-finally" statements to provide reliable management of those resources, even when there's a "yield", "yield from" or "await" in the nested suite.

So what do folks think of the idea of making __del__ on generator-iterator objects emit ResourceWarning in cases where:

- gi_frame is not None (i.e. the frame hasn't completed execution)
- gi_frame.f_lasti isn't -1 (i.e. the frame has started execution)

and similarly for coroutines and cr_frame?
History
Date User Action Args
2016-11-07 02:50:31ncoghlansetrecipients: + ncoghlan, vstinner, njs, yselivanov
2016-11-07 02:50:31ncoghlansetmessageid: <1478487031.39.0.63356302946.issue28629@psf.upfronthosting.co.za>
2016-11-07 02:50:31ncoghlanlinkissue28629 messages
2016-11-07 02:50:30ncoghlancreate