[Python-ideas] Letting context managers react to yields inside their scope

Nathaniel Smith njs at pobox.com
Thu Apr 30 06:36:39 CEST 2015


On Wed, Apr 29, 2015 at 1:36 PM, Guido van Rossum <guido at python.org> wrote:
> This seems reasonable, though mostly also non-urgent. Have you thought about
> how it interacts with PEP 492 yet?

The interaction with PEP 492 is pretty trivial AFAICT; they're almost
entirely orthogonal. The only points of interaction are:

1) PEP 492 adds new suspension points beyond "yield" and "yield from"
(i.e., "await" plus the implicit suspension points in "async for" and
"async with"). This proposal tweaks the behavior of suspension points
(so that they call __suspend__/__resume__). So obviously the solution
is that if PEP 492 is accepted then we have to document that yes, the
new suspension points also trigger __suspend__/__resume__ calls in the
obvious way, just like "yield" does.

2) PEP 492 defines a new asynchronous context manager protocol, which
is "the same as the regular context manager protocol, except with the
letter 'a' added and they're coroutines instead of regular methods".
This proposal adds stuff to the regular context manager protocol, so
one would want to make the analogous changes to the asynchronous
context manager protocol too. It's not 100% clear to me whether the
__asuspend__/__aresume__ versions should be coroutines or not, but
this is a pretty simple question to resolve. (I see downthread Yury
that thinks not, so okay, I guess not, done :-).)

So there aren't any real gotchas here AFAICT.

-n

> On Wed, Apr 29, 2015 at 1:22 PM, Nathaniel Smith <njs at pobox.com> wrote:
>>
>> Hi all,
>>
>> Well, after a few days no-one has responded to my post on another
>> thread about this [1], but the more I thought about it the more this
>> seemed like a good idea, so I wrote up a little more-formal proposal
>> (attached) for letting context managers react to 'yield's that occur
>> within their 'with' block.
>>
>> This should in many ways be treated as a straw man proposal -- there's
>> tons I don't know about how async code is written in Python these days
>> -- but it seems like a good idea to me and I'd like to hear what
>> everyone else thinks :-).
>>
>> -n
>>
>> [1] https://mail.python.org/pipermail/python-ideas/2015-April/033176.html
>>
>> --
>> Nathaniel J. Smith -- http://vorpus.org
>>
>> _______________________________________________
>> 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/
>
>
>
>
> --
> --Guido van Rossum (python.org/~guido)



-- 
Nathaniel J. Smith -- http://vorpus.org


More information about the Python-ideas mailing list