try:
next(self.gen)
except StopIteration:
- return
+ return False
else:
raise RuntimeError("generator didn't stop")
else:
# Likewise, avoid suppressing if a StopIteration exception
# was passed to throw() and later wrapped into a RuntimeError
# (see PEP 479).
- if exc.__cause__ is value:
+ if type is StopIteration and exc.__cause__ is value:
return False
raise
except:
# fixes the impedance mismatch between the throw() protocol
# and the __exit__() protocol.
#
- if sys.exc_info()[1] is not value:
- raise
- else:
- raise RuntimeError("generator didn't stop after throw()")
+ if sys.exc_info()[1] is value:
+ return False
+ raise
+ raise RuntimeError("generator didn't stop after throw()")
def contextmanager(func):
else:
self.fail('StopIteration was suppressed')
+ def test_contextmanager_do_not_unchain_non_stopiteration_exceptions(self):
+ @contextmanager
+ def test_issue29692():
+ try:
+ yield
+ except Exception as exc:
+ raise RuntimeError('issue29692:Chained') from exc
+ try:
+ with test_issue29692():
+ raise ZeroDivisionError
+ except Exception as ex:
+ self.assertIs(type(ex), RuntimeError)
+ self.assertEqual(ex.args[0], 'issue29692:Chained')
+ self.assertIsInstance(ex.__cause__, ZeroDivisionError)
+
+ try:
+ with test_issue29692():
+ raise StopIteration('issue29692:Unchained')
+ except Exception as ex:
+ self.assertIs(type(ex), StopIteration)
+ self.assertEqual(ex.args[0], 'issue29692:Unchained')
+ self.assertIsNone(ex.__cause__)
+
def _create_contextmanager_attribs(self):
def attribs(**kw):
def decorate(func):
Library
-------
+- bpo-29692: Fixed arbitrary unchaining of RuntimeError exceptions in
+ contextlib.contextmanager.
+ Patch by Siddharth Velankar.
- bpo-29998: Pickling and copying ImportError now preserves name and path
attributes.