Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 91e3b9d

Browse files
committed
Deprecate contextlib.nested(). The with-statement now provides this functionality directly.
1 parent fb12391 commit 91e3b9d

5 files changed

Lines changed: 10 additions & 123 deletions

File tree

Doc/library/contextlib.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ Functions provided:
8080
:meth:`__exit__` methods should avoid raising exceptions, and in particular they
8181
should not re-raise a passed-in exception.
8282

83+
.. deprecated:: 3.1
84+
The with-statement now supports this functionality directly.
8385

8486
.. function:: closing(thing)
8587

Doc/whatsnew/3.1.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ Some smaller changes made to the core Python language are:
164164
... if '<critical>' in line:
165165
... outfile.write(line)
166166

167+
With the new syntax, the :func:`contextlib.nested` function is no longer
168+
needed and is not deprecated.
169+
167170
(Contributed by Georg Brandl and Mattias Brändström;
168171
`appspot issue 53094 <http://codereview.appspot.com/53094>`_.)
169172

Lib/contextlib.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import sys
44
from functools import wraps
5+
from warnings import warn
56

67
__all__ = ["contextmanager", "nested", "closing"]
78

@@ -101,6 +102,8 @@ def nested(*managers):
101102
<body>
102103
103104
"""
105+
warn("With-statements now directly support multiple context managers",
106+
DeprecationWarning, 2)
104107
exits = []
105108
vars = []
106109
exc = (None, None, None)

Lib/test/test_contextlib.py

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -100,128 +100,6 @@ def baz(spam):
100100
self.assertEqual(baz.foo, 'bar')
101101
self.assertEqual(baz.__doc__, "Whee!")
102102

103-
class NestedTestCase(unittest.TestCase):
104-
105-
# XXX This needs more work
106-
107-
def test_nested(self):
108-
@contextmanager
109-
def a():
110-
yield 1
111-
@contextmanager
112-
def b():
113-
yield 2
114-
@contextmanager
115-
def c():
116-
yield 3
117-
with nested(a(), b(), c()) as (x, y, z):
118-
self.assertEqual(x, 1)
119-
self.assertEqual(y, 2)
120-
self.assertEqual(z, 3)
121-
122-
def test_nested_cleanup(self):
123-
state = []
124-
@contextmanager
125-
def a():
126-
state.append(1)
127-
try:
128-
yield 2
129-
finally:
130-
state.append(3)
131-
@contextmanager
132-
def b():
133-
state.append(4)
134-
try:
135-
yield 5
136-
finally:
137-
state.append(6)
138-
try:
139-
with nested(a(), b()) as (x, y):
140-
state.append(x)
141-
state.append(y)
142-
1/0
143-
except ZeroDivisionError:
144-
self.assertEqual(state, [1, 4, 2, 5, 6, 3])
145-
else:
146-
self.fail("Didn't raise ZeroDivisionError")
147-
148-
def test_nested_right_exception(self):
149-
state = []
150-
@contextmanager
151-
def a():
152-
yield 1
153-
class b(object):
154-
def __enter__(self):
155-
return 2
156-
def __exit__(self, *exc_info):
157-
try:
158-
raise Exception()
159-
except:
160-
pass
161-
try:
162-
with nested(a(), b()) as (x, y):
163-
1/0
164-
except ZeroDivisionError:
165-
self.assertEqual((x, y), (1, 2))
166-
except Exception:
167-
self.fail("Reraised wrong exception")
168-
else:
169-
self.fail("Didn't raise ZeroDivisionError")
170-
171-
def test_nested_b_swallows(self):
172-
@contextmanager
173-
def a():
174-
yield
175-
@contextmanager
176-
def b():
177-
try:
178-
yield
179-
except:
180-
# Swallow the exception
181-
pass
182-
try:
183-
with nested(a(), b()):
184-
1/0
185-
except ZeroDivisionError:
186-
self.fail("Didn't swallow ZeroDivisionError")
187-
188-
def test_nested_break(self):
189-
@contextmanager
190-
def a():
191-
yield
192-
state = 0
193-
while True:
194-
state += 1
195-
with nested(a(), a()):
196-
break
197-
state += 10
198-
self.assertEqual(state, 1)
199-
200-
def test_nested_continue(self):
201-
@contextmanager
202-
def a():
203-
yield
204-
state = 0
205-
while state < 3:
206-
state += 1
207-
with nested(a(), a()):
208-
continue
209-
state += 10
210-
self.assertEqual(state, 3)
211-
212-
def test_nested_return(self):
213-
@contextmanager
214-
def a():
215-
try:
216-
yield
217-
except:
218-
pass
219-
def foo():
220-
with nested(a(), a()):
221-
return 1
222-
return 10
223-
self.assertEqual(foo(), 1)
224-
225103
class ClosingTestCase(unittest.TestCase):
226104

227105
# XXX This needs more work

Misc/NEWS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Core and Builtins
1515
- Issue #6089: Fixed str.format with certain invalid field specifiers
1616
that would raise SystemError.
1717

18-
- Added support for multiple context managers in the same with statement.
18+
- Added support for multiple context managers in the same with-statement.
19+
Deprecated contextlib.nested() which is no longer needed.
1920

2021
- Issue #5829: complex("1e500") no longer raises OverflowError. This
2122
makes it consistent with float("1e500") and interpretation of real

0 commit comments

Comments
 (0)