summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2007-11-02 10:09:12 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2007-11-02 10:09:12 (GMT)
commit3814a911aa6953a5aefbe8150d280d3a20747d6d (patch)
treef61fd04a3978608907ac3390a057537ca0da51fd /Lib
parentac3d429edc5d1488c00c443d860f3e00f6bdd49e (diff)
downloadcpython-3814a911aa6953a5aefbe8150d280d3a20747d6d.zip
cpython-3814a911aa6953a5aefbe8150d280d3a20747d6d.tar.gz
cpython-3814a911aa6953a5aefbe8150d280d3a20747d6d.tar.bz2
Fix for bug 1705170 - contextmanager swallowing StopIteration (2.5 backport candidate)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/contextlib.py4
-rw-r--r--Lib/test/test_with.py17
2 files changed, 21 insertions, 0 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index 4f83ef6..dbd1c57 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -25,6 +25,10 @@ class GeneratorContextManager(object):
else:
raise RuntimeError("generator didn't stop")
else:
+ if value is None:
+ # Need to force instantiation so we can reliably
+ # tell if we get the same exception back
+ value = type()
try:
self.gen.throw(type, value, traceback)
raise RuntimeError("generator didn't stop after throw()")
diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py
index 5750508..8242c91 100644
--- a/Lib/test/test_with.py
+++ b/Lib/test/test_with.py
@@ -440,6 +440,7 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
self.assertAfterWithGeneratorInvariantsNoError(self.bar)
def testRaisedStopIteration1(self):
+ # From bug 1462485
@contextmanager
def cm():
yield
@@ -451,6 +452,7 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
self.assertRaises(StopIteration, shouldThrow)
def testRaisedStopIteration2(self):
+ # From bug 1462485
class cm(object):
def __enter__(self):
pass
@@ -463,7 +465,21 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
self.assertRaises(StopIteration, shouldThrow)
+ def testRaisedStopIteration3(self):
+ # Another variant where the exception hasn't been instantiated
+ # From bug 1705170
+ @contextmanager
+ def cm():
+ yield
+
+ def shouldThrow():
+ with cm():
+ raise iter([]).next()
+
+ self.assertRaises(StopIteration, shouldThrow)
+
def testRaisedGeneratorExit1(self):
+ # From bug 1462485
@contextmanager
def cm():
yield
@@ -475,6 +491,7 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
self.assertRaises(GeneratorExit, shouldThrow)
def testRaisedGeneratorExit2(self):
+ # From bug 1462485
class cm (object):
def __enter__(self):
pass