mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
Removed contextlib.nested()
This commit is contained in:
parent
6e5b0a1140
commit
9c4d0edd64
3 changed files with 0 additions and 216 deletions
|
@ -85,51 +85,6 @@ def contextmanager(func):
|
|||
return helper
|
||||
|
||||
|
||||
@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()
|
||||
|
||||
"""
|
||||
warn("With-statements now directly support multiple context managers",
|
||||
DeprecationWarning, 3)
|
||||
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
|
||||
# exc[1] already has the __traceback__ attribute populated
|
||||
raise exc[1]
|
||||
|
||||
|
||||
class closing(object):
|
||||
"""Context to automatically close something at the end of a block.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue