| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6fab78e commit 00c75e9
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -88,7 +88,7 @@ def __exit__(self, type, value, traceback): | |||
| 88 | 88 | try: | |
| 89 | 89 | next(self.gen) | |
| 90 | 90 | except StopIteration: | |
| 91 | - return | ||
| 91 | + return False | ||
| 92 | 92 | else: | |
| 93 | 93 | raise RuntimeError("generator didn't stop") | |
| 94 | 94 | else: | |
@@ -110,7 +110,7 @@ def __exit__(self, type, value, traceback): | |||
| 110 | 110 | # Likewise, avoid suppressing if a StopIteration exception | |
| 111 | 111 | # was passed to throw() and later wrapped into a RuntimeError | |
| 112 | 112 | # (see PEP 479). | |
| 113 | - if exc.__cause__ is value: | ||
| 113 | + if type is StopIteration and exc.__cause__ is value: | ||
| 114 | 114 | return False | |
| 115 | 115 | raise | |
| 116 | 116 | except: | |
@@ -121,10 +121,10 @@ def __exit__(self, type, value, traceback): | |||
| 121 | 121 | # fixes the impedance mismatch between the throw() protocol | |
| 122 | 122 | # and the __exit__() protocol. | |
| 123 | 123 | # | |
| 124 | - if sys.exc_info()[1] is not value: | ||
| 125 | - raise | ||
| 126 | - else: | ||
| 127 | - raise RuntimeError("generator didn't stop after throw()") | ||
| 124 | + if sys.exc_info()[1] is value: | ||
| 125 | + return False | ||
| 126 | + raise | ||
| 127 | + raise RuntimeError("generator didn't stop after throw()") | ||
| 128 | 128 | ||
| 129 | 129 | ||
| 130 | 130 | def contextmanager(func): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -152,6 +152,29 @@ def woohoo(): | |||
| 152 | 152 | else: | |
| 153 | 153 | self.fail('StopIteration was suppressed') | |
| 154 | 154 | ||
| 155 | + def test_contextmanager_do_not_unchain_non_stopiteration_exceptions(self): | ||
| 156 | + @contextmanager | ||
| 157 | + def test_issue29692(): | ||
| 158 | + try: | ||
| 159 | + yield | ||
| 160 | + except Exception as exc: | ||
| 161 | + raise RuntimeError('issue29692:Chained') from exc | ||
| 162 | + try: | ||
| 163 | + with test_issue29692(): | ||
| 164 | + raise ZeroDivisionError | ||
| 165 | + except Exception as ex: | ||
| 166 | + self.assertIs(type(ex), RuntimeError) | ||
| 167 | + self.assertEqual(ex.args[0], 'issue29692:Chained') | ||
| 168 | + self.assertIsInstance(ex.__cause__, ZeroDivisionError) | ||
| 169 | + | ||
| 170 | + try: | ||
| 171 | + with test_issue29692(): | ||
| 172 | + raise StopIteration('issue29692:Unchained') | ||
| 173 | + except Exception as ex: | ||
| 174 | + self.assertIs(type(ex), StopIteration) | ||
| 175 | + self.assertEqual(ex.args[0], 'issue29692:Unchained') | ||
| 176 | + self.assertIsNone(ex.__cause__) | ||
| 177 | + | ||
| 155 | 178 | def _create_contextmanager_attribs(self): | |
| 156 | 179 | def attribs(**kw): | |
| 157 | 180 | def decorate(func): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -306,6 +306,9 @@ Extension Modules | |||
| 306 | 306 | ||
| 307 | 307 | Library | |
| 308 | 308 | ------- | |
| 309 | + - bpo-29692: Fixed arbitrary unchaining of RuntimeError exceptions in | ||
| 310 | + contextlib.contextmanager. | ||
| 311 | + Patch by Siddharth Velankar. | ||
| 309 | 312 | ||
| 310 | 313 | - bpo-26187: Test that sqlite3 trace callback is not called multiple | |
| 311 | 314 | times when schema is changing. Indirectly fixed by switching to | |
| Back | FazBrowse Home | New Git URL |
0 commit comments