On Tue, Oct 10, 2017 at 5:34 AM, Nick Coghlan email@example.com wrote:
On 10 October 2017 at 01:24, Guido van Rossum firstname.lastname@example.org wrote:
On Sun, Oct 8, 2017 at 11:46 PM, Nick Coghlan email@example.com wrote:
On 8 October 2017 at 08:40, Koos Zevenhoven firstname.lastname@example.org wrote:
I do remember Yury mentioning that the first draft of PEP 550 captured something when the generator function was called. I think I started reading the discussions after that had already been removed, so I don't know exactly what it was. But I doubt that it was exactly the above, because PEP 550 uses set and get operations instead of "assignment contexts" like PEP 555 (this one) does.
We didn't forget it, we just don't think it's very useful.
I'm not sure I agree on the usefulness. Certainly a lot of the complexity of PEP 550 exists just to cater to Nathaniel's desire to influence what a generator sees via the context of the send()/next() call. I'm still not sure that's worth it. In 550 v1 there's no need for chained lookups.
The compatibility concern is that we want developers of existing libraries to be able to transparently switch from using thread local storage to context local storage, and the way thread locals interact with generators means that decimal (et al) currently use the thread local state at the time when next() is called, not when the generator is created.
Apart from the example in PEP 550, is that really a known idiom?
I like Yury's example for this, which is that the following two examples are currently semantically equivalent, and we want to preserve that equivalence:
with decimal.localcontext() as ctx: ctc.prex = 30 for i in gen(): pass g = gen() with decimal.localcontext() as ctx: ctc.prex = 30 for i in g: pass
Do we really want that equivalence? It goes against the equivalence from Koos' example.
The easiest way to maintain that equivalence is to say that even though preventing state changes leaking out of generators is considered a desirable change, we see preventing them leaking in as a gratuitous backwards compatibility break.
I dunno, I think them leaking in in the first place is a dubious feature, and I'm not too excited that the design of the way forward should bend over backwards to be compatible here.
The only real use case I've seen so far (not counting examples that just show how it works) is Nathaniel's timeout example (see point 9 in Nathaniel’s message https://mail.python.org/pipermail/python-ideas/2017-August/046736.html), and I'm still not convinced that that example is important enough to support either.
It would all be easier to decide if there were use cases that were less far-fetched, or if the far-fetched use cases would be supportable with a small tweak. As it is, it seems that we could live in a simpler, happier world if we gave up on context values leaking in via next() etc. (I still claim that in that case we wouldn't need chained lookup in the exposed semantics, just fast copying of contexts.)
This does mean that neither form is semantically equivalent to eager extraction of the generator values before the decimal context is changed, but that's the status quo, and we don't have a compelling justification for changing it.
I think the justification is that we could have a significantly simpler semantics and implementation.
If folks subsequently decide that they do want "capture on creation" or "capture on first iteration" semantics for their generators, those are easy enough to add as wrappers on top of the initial thread-local-compatible base by using the same building blocks as are being added to help event loops manage context snapshots for coroutine execution.
(BTW Capture on first iteration sounds just awful.)
I think we really need to do more soul-searching before we decide that a much more complex semantics and implementation is worth it to maintain backwards compatibility for leaking in via next().
-- --Guido van Rossum (python.org/~guido)