<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Jan 3, 2018 at 12:32 PM, Glenn Linderman <span dir="ltr"><<a href="mailto:v+python@g.nevcal.com" target="_blank">v+python@g.nevcal.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div bgcolor="#FFFFFF"><span class="gmail-">
<div class="gmail-m_7379124303544455203moz-cite-prefix">On 1/3/2018 11:16 AM, Guido van Rossum
wrote:<br>
</div>
<blockquote type="cite">
<div>Maybe I should clarify again what run() does. Here's how I
think of it in pseudo code:</div>
<div><br>
</div>
<div>def run(self, func, *args, **kwds):</div>
<div> old = _get_current_context()</div>
<div> new = old.copy()</div>
<div> _set_current_context(new)</div>
<div> try:</div>
<div> return func(*args, **kwds)</div>
<div> finally:</div>
<div> _set_current_context(old)</div>
<div><br>
</div>
</blockquote></span>
</lurking><br>
<br>
I find it interesting that self isn't used in the above pseudo-code.
I thought that Context.run() would run the function in the "context"
of self, not in the context of a copy of "current context".<br>
<br>
</lurking><br>
</div></blockquote></div></div><div class="gmail_extra"><br></div><div class="gmail_extra">Heh, you're right, I forgot about that. It should be more like this:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div>def run(self, func, *args, **kwds):</div><div> old = _get_current_context()</div><div> _set_current_context(self) # <--- changed line<br></div><div> try:</div><div> return func(*args, **kwds)</div><div> finally:</div><div> _set_current_context(old)</div></div><div class="gmail_extra"><br></div><div class="gmail_extra">This version, like the PEP, assumes that the Context object is truly immutable (not just in name) and that you should call it like this:</div><div class="gmail_extra"><br></div><div class="gmail_extra">contextvars.copy_context().run(func, <args>)</div><div class="gmail_extra"><br></div><div class="gmail_extra">The PEP definitely needs to be clearer about this -- right now one has to skip back and forth between the specification and the implementation (and sometimes the introduction) to figure out how things really work. Help is wanted!<br></div><div class="gmail_extra"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div></div>