def nesting_contexts(): signals = [] @contextmanager def one(): signals.append("inner one") try: yield "one" finally: signals.append("outer one") @contextmanager def two(): signals.append("inner two") try: yield "two" finally: signals.append("outer two") ctx = utils.nested([one, two]) assert not signals with raises(ZeroDivisionError): with ctx as args: assert signals == ["inner one", "inner two"] assert args == ["one", "two"] 1 / 0 assert signals == ["inner one", "inner two", "outer two", "outer one"] args = None signals = [] @contextmanager def one(): signals.append("inner one") try: yield "one" finally: signals.append("outer one") @contextmanager def two(): signals.append("inner two") 1 / 0 try: yield "two" finally: signals.append("outer two") ctx = utils.nested([one, two]) assert not signals with raises(ZeroDivisionError): with ctx as args: pass assert signals == ["inner one", "inner two", "outer one"] assert args is None
def nesting_contexts(): signals = [] @contextmanager def one(): signals.append('inner one') try: yield 'one' finally: signals.append('outer one') @contextmanager def two(): signals.append('inner two') try: yield 'two' finally: signals.append('outer two') ctx = utils.nested([one, two]) assert not signals with raises(ZeroDivisionError): with ctx as args: assert signals == ['inner one', 'inner two'] assert args == ['one', 'two'] 1 / 0 assert signals == ['inner one', 'inner two', 'outer two', 'outer one'] args = None signals = [] @contextmanager def one(): signals.append('inner one') try: yield 'one' finally: signals.append('outer one') @contextmanager def two(): signals.append('inner two') 1 / 0 try: yield 'two' finally: signals.append('outer two') ctx = utils.nested([one, two]) assert not signals with raises(ZeroDivisionError): with ctx as args: pass assert signals == ['inner one', 'inner two', 'outer one'] assert args is None
def nesting_contexts(): signals = [] @contextmanager def one(): signals.append('inner one') try: yield 'one' finally: signals.append('outer one') @contextmanager def two(): signals.append('inner two') try: yield 'two' finally: signals.append('outer two') ctx = utils.nested([one, two]) assert not signals with raises(ZeroDivisionError): with ctx as args: assert signals == ['inner one', 'inner two'] assert args == ['one', 'two'] 1/0 assert signals == ['inner one', 'inner two', 'outer two', 'outer one'] args = None signals = [] @contextmanager def one(): signals.append('inner one') try: yield 'one' finally: signals.append('outer one') @contextmanager def two(): signals.append('inner two') 1/0 try: yield 'two' finally: signals.append('outer two') ctx = utils.nested([one, two]) assert not signals with raises(ZeroDivisionError): with ctx as args: pass assert signals == ['inner one', 'inner two', 'outer one'] assert args is None
def nested(): try: with utils.nested([]): assert 1 == 0, "message" except AssertionError, e: print e.args assert e.args == ("message", )
def wrapper(): with nested(self._contexts) as context: context = [c for c in context if c is not None] argc = len(inspect.getargspec(func)[0]) args = [] for arg in context: if type(arg) is tuple: # type() is intentional args.extend(arg) else: args.append(arg) func(*args[:argc])