Nick Coghlan added the comment:

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.

----------

_______________________________________
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