New submission from James William Pye <pyt...@jwp.name>: 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 ---------- components: None files: nested_issue.py messages: 81957 nosy: jwp severity: normal status: open title: contextlib.nested inconsistent with, well, nested with statements due exceptions raised in __enter__ type: behavior versions: Python 2.5, Python 2.6, Python 2.7, Python 3.0, Python 3.1 Added file: http://bugs.python.org/file13075/nested_issue.py _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue5251> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com