##// END OF EJS Templates
Backwards compatibility hack to use nested() in Python 3.2
Thomas Kluyver -
Show More
@@ -0,0 +1,50 b''
1 """Backwards compatibility - we use contextlib.nested to support Python 2.6,
2 but it's removed in Python 3.2."""
3
4 # TODO : Remove this once we drop support for Python 2.6, and use
5 # "with a, b:" instead.
6
7 from contextlib import contextmanager
8
9 @contextmanager
10 def nested(*managers):
11 """Combine multiple context managers into a single nested context manager.
12
13 This function has been deprecated in favour of the multiple manager form
14 of the with statement.
15
16 The one advantage of this function over the multiple manager form of the
17 with statement is that argument unpacking allows it to be
18 used with a variable number of context managers as follows:
19
20 with nested(*managers):
21 do_something()
22
23 """
24 warn("With-statements now directly support multiple context managers",
25 DeprecationWarning, 3)
26 exits = []
27 vars = []
28 exc = (None, None, None)
29 try:
30 for mgr in managers:
31 exit = mgr.__exit__
32 enter = mgr.__enter__
33 vars.append(enter())
34 exits.append(exit)
35 yield vars
36 except:
37 exc = sys.exc_info()
38 finally:
39 while exits:
40 exit = exits.pop()
41 try:
42 if exit(*exc):
43 exc = (None, None, None)
44 except:
45 exc = sys.exc_info()
46 if exc != (None, None, None):
47 # Don't rely on sys.exc_info() still containing
48 # the right information. Another exception may
49 # have been raised and caught by an exit method
50 raise exc[0], exc[1], exc[2]
@@ -29,7 +29,10 b' import re'
29 import sys
29 import sys
30 import tempfile
30 import tempfile
31 import types
31 import types
32 from contextlib import nested
32 try:
33 from contextlib import nested
34 except:
35 from IPython.utils.nested_context import nested
33
36
34 from IPython.config.configurable import SingletonConfigurable
37 from IPython.config.configurable import SingletonConfigurable
35 from IPython.core import debugger, oinspect
38 from IPython.core import debugger, oinspect
@@ -26,7 +26,10 b' from __future__ import with_statement'
26 import __main__
26 import __main__
27
27
28 import sys
28 import sys
29 from contextlib import nested
29 try:
30 from contextlib import nested
31 except:
32 from IPython.utils.nested_context import nested
30
33
31 from IPython.core import ultratb
34 from IPython.core import ultratb
32 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
35 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
@@ -16,11 +16,15 b''
16
16
17 import __builtin__
17 import __builtin__
18 import bdb
18 import bdb
19 from contextlib import nested
20 import os
19 import os
21 import re
20 import re
22 import sys
21 import sys
23
22
23 try:
24 from contextlib import nested
25 except:
26 from IPython.utils.nested_context import nested
27
24 from IPython.core.error import TryNext
28 from IPython.core.error import TryNext
25 from IPython.core.usage import interactive_usage, default_banner
29 from IPython.core.usage import interactive_usage, default_banner
26 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
30 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
General Comments 0
You need to be logged in to leave comments. Login now