summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-05-28 22:20:03 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-05-28 22:20:03 (GMT)
commit91e3b9d89d6a63149dcd9ab12223f552d1473aab (patch)
tree2d3035aaa19b15a2d5cd437a52905f66698a8c8f /Lib
parentfb12391c44d9491e2d1635eb5f82af3b0da0ec29 (diff)
downloadcpython-91e3b9d89d6a63149dcd9ab12223f552d1473aab.zip
cpython-91e3b9d89d6a63149dcd9ab12223f552d1473aab.tar.gz
cpython-91e3b9d89d6a63149dcd9ab12223f552d1473aab.tar.bz2
Deprecate contextlib.nested(). The with-statement now provides this functionality directly.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/contextlib.py3
-rw-r--r--Lib/test/test_contextlib.py122
2 files changed, 3 insertions, 122 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index 647efdd..0841ac3 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -2,6 +2,7 @@
import sys
from functools import wraps
+from warnings import warn
__all__ = ["contextmanager", "nested", "closing"]
@@ -101,6 +102,8 @@ def nested(*managers):
<body>
"""
+ warn("With-statements now directly support multiple context managers",
+ DeprecationWarning, 2)
exits = []
vars = []
exc = (None, None, None)
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index c05f37b..b878dc1 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -100,128 +100,6 @@ class ContextManagerTestCase(unittest.TestCase):
self.assertEqual(baz.foo, 'bar')
self.assertEqual(baz.__doc__, "Whee!")
-class NestedTestCase(unittest.TestCase):
-
- # XXX This needs more work
-
- def test_nested(self):
- @contextmanager
- def a():
- yield 1
- @contextmanager
- def b():
- yield 2
- @contextmanager
- def c():
- yield 3
- with nested(a(), b(), c()) as (x, y, z):
- self.assertEqual(x, 1)
- self.assertEqual(y, 2)
- self.assertEqual(z, 3)
-
- def test_nested_cleanup(self):
- state = []
- @contextmanager
- def a():
- state.append(1)
- try:
- yield 2
- finally:
- state.append(3)
- @contextmanager
- def b():
- state.append(4)
- try:
- yield 5
- finally:
- state.append(6)
- try:
- with nested(a(), b()) as (x, y):
- state.append(x)
- state.append(y)
- 1/0
- except ZeroDivisionError:
- self.assertEqual(state, [1, 4, 2, 5, 6, 3])
- else:
- self.fail("Didn't raise ZeroDivisionError")
-
- def test_nested_right_exception(self):
- state = []
- @contextmanager
- def a():
- yield 1
- class b(object):
- def __enter__(self):
- return 2
- def __exit__(self, *exc_info):
- try:
- raise Exception()
- except:
- pass
- try:
- with nested(a(), b()) as (x, y):
- 1/0
- except ZeroDivisionError:
- self.assertEqual((x, y), (1, 2))
- except Exception:
- self.fail("Reraised wrong exception")
- else:
- self.fail("Didn't raise ZeroDivisionError")
-
- def test_nested_b_swallows(self):
- @contextmanager
- def a():
- yield
- @contextmanager
- def b():
- try:
- yield
- except:
- # Swallow the exception
- pass
- try:
- with nested(a(), b()):
- 1/0
- except ZeroDivisionError:
- self.fail("Didn't swallow ZeroDivisionError")
-
- def test_nested_break(self):
- @contextmanager
- def a():
- yield
- state = 0
- while True:
- state += 1
- with nested(a(), a()):
- break
- state += 10
- self.assertEqual(state, 1)
-
- def test_nested_continue(self):
- @contextmanager
- def a():
- yield
- state = 0
- while state < 3:
- state += 1
- with nested(a(), a()):
- continue
- state += 10
- self.assertEqual(state, 3)
-
- def test_nested_return(self):
- @contextmanager
- def a():
- try:
- yield
- except:
- pass
- def foo():
- with nested(a(), a()):
- return 1
- return 10
- self.assertEqual(foo(), 1)
-
class ClosingTestCase(unittest.TestCase):
# XXX This needs more work