[Python-ideas] Deterministic iterator cleanup

Gustavo Carneiro gjcarneiro at gmail.com
Fri Oct 21 11:19:23 EDT 2016


Personally, I hadn't realised we had this problem in asyncio until now.

Does this problem happen in asyncio at all?  Or does asyncio somehow work
around it by making sure to always explicitly destroy the frames of all
coroutine objects, as long as someone waits on each task?

On 21 October 2016 at 16:08, Yury Selivanov <yselivanov.ml at gmail.com> wrote:

>
>
> On 2016-10-21 6:29 AM, Steven D'Aprano wrote:
>
>> On Wed, Oct 19, 2016 at 05:52:34PM -0400, Yury Selivanov wrote:
>>
> [..]
>
>> With you proposal, to achieve the same (and make the code compatible
>>> with new for-loop semantics), users will have to implement both
>>> __iterclose__ and __del__.
>>>
>> As I ask above, couldn't we just inherit a default __(a)iterclose__ from
>> object that looks like this?
>>
>>      def __iterclose__(self):
>>          finalizer = getattr(type(self), '__del__', None)
>>          if finalizer:
>>              finalizer(self)
>>
>>
>> I know it looks a bit funny for non-iterables to have an iterclose
>> method, but they'll never actually be called.
>>
>
> No, we can't call __del__ from __iterclose__.  Otherwise we'd
> break even more code that this proposal already breaks:
>
>
>   for i in iter:
>      ...
>   iter.something()  # <- this would be call after iter.__del__()
>
> [..]
>
>> As for the amount of good, this proposal originally came from PyPy. I
>> expect that CPython users won't appreciate it as much as PyPy users, and
>> Jython/IronPython users when they eventually support Python 3.x.
>>
>
> AFAIK the proposal came "for" PyPy, not "from".  And the
> issues Nathaniel tries to solve do also exist in CPython.  It's
> only a question if changing 'for' statement and iteration protocol
> is worth the trouble.
>
> Yury
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Gustavo J. A. M. Carneiro
Gambit Research
"The universe is always one step beyond logic." -- Frank Herbert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161021/668141b8/attachment.html>


More information about the Python-ideas mailing list