summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2006-03-10 02:28:35 (GMT)
committerGuido van Rossum <guido@python.org>2006-03-10 02:28:35 (GMT)
commitf669436189dd44a841caa9ab1ad97a3f7662bf58 (patch)
tree1a717975d09d4867e8807710a36a6c2999afdb7e /Lib/test
parent692cdbc5d648da5239b5caececc954960aa024e9 (diff)
downloadcpython-f669436189dd44a841caa9ab1ad97a3f7662bf58.zip
cpython-f669436189dd44a841caa9ab1ad97a3f7662bf58.tar.gz
cpython-f669436189dd44a841caa9ab1ad97a3f7662bf58.tar.bz2
Um, I thought I'd already checked this in.
Anyway, this is the changes to the with-statement so that __exit__ must return a true value in order for a pending exception to be ignored. The PEP (343) is already updated.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_with.py32
1 files changed, 22 insertions, 10 deletions
diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py
index 36035e3..4854436 100644
--- a/Lib/test/test_with.py
+++ b/Lib/test/test_with.py
@@ -78,8 +78,8 @@ class Nested(object):
vars.append(mgr.__enter__())
self.entered.appendleft(mgr)
except:
- self.__exit__(*sys.exc_info())
- raise
+ if not self.__exit__(*sys.exc_info()):
+ raise
return vars
def __exit__(self, *exc_info):
@@ -89,7 +89,8 @@ class Nested(object):
ex = exc_info
for mgr in self.entered:
try:
- mgr.__exit__(*ex)
+ if mgr.__exit__(*ex):
+ ex = (None, None, None)
except:
ex = sys.exc_info()
self.entered = None
@@ -574,9 +575,7 @@ class AssignmentTargetTestCase(unittest.TestCase):
class C:
def __context__(self): return self
def __enter__(self): return 1, 2, 3
- def __exit__(self, t, v, tb):
- if t is not None:
- raise t, v, tb
+ def __exit__(self, t, v, tb): pass
targets = {1: [0, 1, 2]}
with C() as (targets[1][0], targets[1][1], targets[1][2]):
self.assertEqual(targets, {1: [1, 2, 3]})
@@ -594,17 +593,30 @@ class AssignmentTargetTestCase(unittest.TestCase):
class ExitSwallowsExceptionTestCase(unittest.TestCase):
- def testExitSwallowsException(self):
- class AfricanOrEuropean:
+ def testExitTrueSwallowsException(self):
+ class AfricanSwallow:
def __context__(self): return self
def __enter__(self): pass
- def __exit__(self, t, v, tb): pass
+ def __exit__(self, t, v, tb): return True
try:
- with AfricanOrEuropean():
+ with AfricanSwallow():
1/0
except ZeroDivisionError:
self.fail("ZeroDivisionError should have been swallowed")
+ def testExitFalseDoesntSwallowException(self):
+ class EuropeanSwallow:
+ def __context__(self): return self
+ def __enter__(self): pass
+ def __exit__(self, t, v, tb): return False
+ try:
+ with EuropeanSwallow():
+ 1/0
+ except ZeroDivisionError:
+ pass
+ else:
+ self.fail("ZeroDivisionError should have been raised")
+
def test_main():
run_unittest(FailureTestCase, NonexceptionalTestCase,