Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
Commita6f948b
committed
Fix exception context in @contextmanagers
Previously, when using a `try...yield...except` construct within a`@contextmanager` function, an exception raised by the `yield` wouldn'tbe properly cleared when handled the `except` block. Instead, calling`sys.exception()` would continue to return the exception, leading toconfusing tracebacks and logs.This was happening due to the way that the `@contextmanager` decoratordrives its decorated generator function. When an exception occurs, ituses `.throw(exc)` to throw the exception into the generator. However,even if the generator handles this exception, because the exception wasthrown into it in the context of the `@contextmanager` decoratorhandling it (and not being finished yet), `sys.exception()` was notbeing reset.In order to fix this, the exception context as the `@contextmanager`decorator is `__enter__`'d is stored and set as the current exceptioncontext just before throwing the new exception into the generator.Doing this means that after the generator handles the thrown exception,`sys.exception()` reverts back to what it was when the `@contextmanager`function was started.1 parentd2f6251 commita6f948b
File tree
3 files changed
+96
-0
lines changed- Lib
- test
- Misc/NEWS.d/next/Library
3 files changed
+96
-0
lines changedLines changed: 12 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
106 | 106 |
| |
107 | 107 |
| |
108 | 108 |
| |
| 109 | + | |
109 | 110 |
| |
110 | 111 |
| |
111 | 112 |
| |
| |||
134 | 135 |
| |
135 | 136 |
| |
136 | 137 |
| |
| 138 | + | |
| 139 | + | |
137 | 140 |
| |
138 | 141 |
| |
139 | 142 |
| |
| |||
143 | 146 |
| |
144 | 147 |
| |
145 | 148 |
| |
| 149 | + | |
| 150 | + | |
| 151 | + | |
146 | 152 |
| |
147 | 153 |
| |
148 | 154 |
| |
| |||
159 | 165 |
| |
160 | 166 |
| |
161 | 167 |
| |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
162 | 174 |
| |
163 | 175 |
| |
164 | 176 |
| |
|
Lines changed: 82 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
306 | 306 |
| |
307 | 307 |
| |
308 | 308 |
| |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
309 | 391 |
| |
310 | 392 |
| |
311 | 393 |
| |
|
Lines changed: 2 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
| 1 | + | |
| 2 | + |
0 commit comments
Comments
(0)