[Python-Dev] PEP 567 v2 (original) (raw)

Guido van Rossum guido at python.org
Wed Jan 3 18:49:31 EST 2018


That was actually Yury's original design, but I insisted on a Mapping so you can introspect it. (There needs to be some way to introspect a Context, for debugging.) Again, more later. :-(

On Wed, Jan 3, 2018 at 4:44 PM, Victor Stinner <victor.stinner at gmail.com> wrote:

Ok, I finally got access to a computer and I was able to test the PEP 567 implementation: see my code snippet below.

The behaviour is more tricky than what I expected. While running context.run(), the context object is out of sync of the "current context". It's only synchronized again at run() exit. So ContextVar.set() doesn't immediately modifies the "current context" object (set by Context.run()). Ok, and now something completely different! What if Context looses its whole mapping API and becomes a "blackbox" object only with a run() method and no attribute? It can be documented as an object mapping context variables to their values, but don't give access to these values directly. It would avoid to have explain the weird run() behaviour (context out of sync). It would avoid to have to decide if it's a mutable or immutable mapping. It would avoid to have to explain the internal O(1) copy using HAMT. Code testing Context.run(): --- import contextvars name = contextvars.ContextVar('name', default='name') def assertNotIn(var, context): try: context[var] except LookupError: pass else: raise AssertionError("name is set is context")

def func1(): name.set('yury') assert name.get() == 'yury' assertNotIn(name, context) def func2(): assert name.get() == 'yury' assert context[name] == 'yury' context = contextvars.copycontext() assert name.get() == 'name' assertNotIn(name, context) context.run(func1) assert name.get() == 'name' assert context[name] == 'yury' context.run(func2) assert name.get() == 'name' assert context[name] == 'yury' --- Victor

-- --Guido van Rossum (python.org/~guido) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20180103/67cc7847/attachment.html>



More information about the Python-Dev mailing list