Context manager, atexit processing, and PEP 3143 DaemonContext.close

Gunter Henriksen gunterhenriksen at gmail.com
Tue May 19 18:22:25 EDT 2009


> > If there is a function which triggers a one-shot switch, I like
> > to have a way to find out if it has already been triggered, I
> > prefer to have the function tell me if it triggered the switch
> > or not, but I would not want that to be by raising an exception.
>
> In this case, though, we're talking about a class intended to
> implement the context manager API (in addition to other API).
>
> So, following your argument above, we might expect a generic
> context manager object to be able to tell us whether we're
> currently inside that context or not.  Does such a thing exist?

By a "one-shot switch" I meant as something which
changes once atomically in isolation, not as
something whose state comprises a transaction during
which the state of multiple externally visible
objects might change.

I imagine if there is an object used as a context
manager whose context state ("currently inside
that context or not") is not strictly under the
control of the single "with" statement where it is
fulfilling the "context manager" contract, would not
be a generic context manager.

In other words, to me it seems like the concept of
"currently inside a context or not" is not something
visible external to the object except in a case
where there is another thread of execution, which is
not a case I would expect to be part of the generic
context manager protocol, but can be added by an
object which implements the context manager protocol
and also keeps track of its state by setting flags
in the __enter__ and __exit__ functions.

Does this make sense?



More information about the Python-list mailing list