Author jwp
Recipients jwp
Date 2009-02-13.18:09:27
SpamBayes Score 8.05639e-12
Marked as misclassified No
Message-id <>
Basically, nested() doesn't seem to be consistent with explicitly nested
with-statements when an exception is thrown in a CM's __enter__.

Consider a pair of nested CMs, the inner __enter__ raises an exception
trapped by the outer. In the situation of explicitly nested
with-statements, the inner's block will not be ran as an exception was
raised prior to the block. The outer traps the exception and code
continues to flow after the outer's block because the exception was
*not* raised.
Currently, if contextlib.nested() is used in such a situation, it blows
up with a RuntimeError("generator didn't yield").

See the attached file for a set of naive variations and their resulting
exceptions or lack thereof in the situation of explicitly nested CMs.

Despite the RuntimeError raised by nested(), I'm not sure it's
*currently* possible for an implementation of nested() to be *totally*
consistent with explicitly nested with-statements. It would seem that an
additional facility(AbortBlock exception?) would be needed to
communicate that the block should not actually be ran, and that no
exception should be raised as it was consumed by an "outer" CM.

If this is considered to be the intended behavior, I would think the
doc-string on contextlib.nested should be updated to document the
inconsistency as it currently states that nested() and nested with
statements are equivalent. Based on the results of the attached file in
Python 3.0, they are clearly not.

Cheers folks; lovin' CMs. And, of course, apologies if this has already
been discussed. :P
Date User Action Args
2009-02-13 18:09:31jwpsetrecipients: + jwp
2009-02-13 18:09:30jwpsetmessageid: <>
2009-02-13 18:09:29jwplinkissue5251 messages
2009-02-13 18:09:28jwpcreate