oN Sat, 2 May 2009 at 22:12, Georg Brandl wrote:
I see; you want to construct your context manager programmatically and pass
it to "with" without knowing what is in there.
While this would be possible, we have to be aware that with this we would
effectively change the context manager protocol, rather like the iterator
protocol's __getitem__ alternate realization. This muddies the definition
of a context manager.
(The interesting thing is that you could already implement *that* version
without any new syntactic support, by giving tuples an __enter__/__exit__
method pair.)
With the syntax in the patch, I will still have to implement a custom
nesting context manager to do this, which sort of defeats the purpose.
Not really. Having an unknown number of stacked context managers is not
the purpose -- for that, I'd still say a custom nesting context manager
is better, because it is also more explicit when created not at the "with"
site. (You could even write it as a tuple subclass, if you like the tuple
interface.)
As I understand it, the primary problem the patch Georg is talking
about solves is the fact that currently if you pass multiple contexts
to contextlib.nested, and one of the later items in the argument list
throws an error, the context(s) from the earlier context manager(s) does
not get cleaned up properly. This patch solves that problem very neatly.
I'm +1 on the patch, including preferring the syntax over the alternative.
Georg, maybe you should post the link to the python-ideas discussion?
--David
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com