New submission from Nick Coghlan:

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?

----------
messages: 280185
nosy: haypo, ncoghlan, njs, yselivanov
priority: normal
severity: normal
status: open
title: Emit ResourceWarning when implicitly terminating a suspended frame?
type: resource usage
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28629>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to