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.14:29:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1478528964.54.0.995188265471.issue28629@psf.upfronthosting.co.za>
In-reply-to
Content
As Victor suggests, the reason I ended the issue title with a question mark is because I'm worried that doing this by default is likely to be too noisy to be useful. It's worth trying out though, even if it ends up being something we have to hide behind a -X flag.

If that concern does prove well-founded, another possible way to go would be to change the way generators interact with the context management protocol. In order to ensure a clear exception when you accidentally leave out `contextlib.contextmanager`, they currently simply don't implement it, so you have to use `contextlib.closing` instead. However, you can't wrap an existing generator in that unconditionally, as you'll break direct calls (`closing` relies on `__enter__` to give callers transparent access to the original object).

One way to thread that needle would be to offer a `contextlib.managed_generator` alternative decorator that tweaked the behaviour of that particular generator-iterator instance to:

- emit a ResourceWarning in __del__
- support the context management protocol

Unfortunately, I haven't been able to think of a particularly clean implementation strategy that wouldn't introduce undesirable runtime performance overheads.
History
Date User Action Args
2016-11-07 14:29:24ncoghlansetrecipients: + ncoghlan, vstinner, njs, yselivanov
2016-11-07 14:29:24ncoghlansetmessageid: <1478528964.54.0.995188265471.issue28629@psf.upfronthosting.co.za>
2016-11-07 14:29:24ncoghlanlinkissue28629 messages
2016-11-07 14:29:23ncoghlancreate