From: Ben Darnell Date: Sat, 14 May 2011 23:04:54 +0000 (-0700) Subject: Merge branch 'master' into python3 X-Git-Tag: v2.0.0~85^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=70f08bf302ea6b4b328bb1f6c61b123cd92d8997;p=thirdparty%2Ftornado.git Merge branch 'master' into python3 --- 70f08bf302ea6b4b328bb1f6c61b123cd92d8997 diff --cc tornado/stack_context.py index b2e418158,d12c6bc44..fd3ac50e1 --- a/tornado/stack_context.py +++ b/tornado/stack_context.py @@@ -172,45 -179,7 +180,42 @@@ def wrap(fn) callback(*args, **kwargs) else: callback(*args, **kwargs) - if getattr(fn, 'stack_context_wrapped', False): + if isinstance(fn, (_StackContextWrapper, NoneType)): return fn - contexts = _state.contexts - result = functools.partial(wrapped, fn, contexts) - result.stack_context_wrapped = True - return result + return _StackContextWrapper(wrapped, fn, _state.contexts) +@contextlib.contextmanager +def _nested(*managers): + """Support multiple context managers in a single with-statement. + + Copied from the python 2.6 standard library. It's no longer present + in python 3 because the with statement natively supports multiple + context managers, but that doesn't help if the list of context + managers is not known until runtime. + """ + exits = [] + vars = [] + exc = (None, None, None) + try: + for mgr in managers: + exit = mgr.__exit__ + enter = mgr.__enter__ + vars.append(enter()) + exits.append(exit) + yield vars + except: + exc = sys.exc_info() + finally: + while exits: + exit = exits.pop() + try: + if exit(*exc): + exc = (None, None, None) + except: + exc = sys.exc_info() + if exc != (None, None, None): + # Don't rely on sys.exc_info() still containing + # the right information. Another exception may + # have been raised and caught by an exit method + raise exc[0], exc[1], exc[2] +