##// END OF EJS Templates
Merge pull request #4016 from takluyver/IPython-start-funcs...
Merge pull request #4016 from takluyver/IPython-start-funcs Fix IPython.start_* functions start_kernel() didn't work at all (#4005), and I found while investigating that passing user_ns to either start_ipython() or start_kernel() had no effect, because the Application wasn't passing it on to the Shell. This is a simple fix for the most pressing issues. We should think about any other parameters that we want to be passed through from our top level API, e.g. possibly user_module. closes #4005

File last commit:

r9244:666fdfa8
r12189:d3e2f732 merge
Show More
nested_context.py
50 lines | 1.5 KiB | text/x-python | PythonLexer
"""Backwards compatibility - we use contextlib.nested to support Python 2.6,
but it's removed in Python 3.2."""
# TODO : Remove this once we drop support for Python 2.6, and use
# "with a, b:" instead.
import sys
from contextlib import contextmanager
@contextmanager
def nested(*managers):
"""Combine multiple context managers into a single nested context manager.
This function has been deprecated in favour of the multiple manager form
of the with statement.
The one advantage of this function over the multiple manager form of the
with statement is that argument unpacking allows it to be
used with a variable number of context managers as follows::
with nested(*managers):
do_something()
"""
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]