[Python-Dev] PEP 567 v2

Nathaniel Smith njs at pobox.com
Wed Jan 3 20:35:56 EST 2018


On Wed, Jan 3, 2018 at 3: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()).

To me this sounds like a oversight (= bug), not intended behavior. At
the conceptual level, I think what we want is:

- Context is a mutable object representing a mapping
- BUT it doesn't allow mutation through the MutableMapping interface;
instead, the only way to mutate it is by calling Context.run and then
ContextVar.set(). Funneling all 'set' operations through a single
place makes it easier to do clever caching tricks, and it lets us
avoid dealing with operations that we don't want here (like 'del')
just because they happen to be in the MutableMapping interface.
- OTOH we do implement the (read-only) Mapping interface because
there's no harm in it and it's probably useful for debuggers.

(Note that I didn't say anything about HAMTs here, because that's
orthogonal implementation detail. It would make perfect sense to have
Context be an opaque wrapper around a regular dict; it would just give
different performance trade-offs.)

-n

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


More information about the Python-Dev mailing list