[New-bugs-announce] [issue29692] contextlib.contextmanager may incorrectly unchain RuntimeError

Nick Coghlan report at bugs.python.org
Thu Mar 2 05:11:35 EST 2017


New submission from Nick Coghlan:

As part of PEP 479, an extra check was added to contextlib._GeneratorContextManager to avoid getting confused when a StopIteration exception was raised in the body of the with statement, and hence thrown into the generator body implementing the context manager.

This extra check should only be used when the passed in exception is `StopIteration`, but that guard is currently missing, so it may unchain arbitrary RuntimeError exceptions if they set their `__cause__` to the originally passed in value.

Compare the current contextmanager behaviour:

```
>>> from contextlib import contextmanager
>>> @contextmanager
... def chain_thrown_exc():
...     try:
...         yield
...     except Exception as exc:
...         raise RuntimeError("Chained!") from exc
... 
>>> with chain_thrown_exc():
...     1/0
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
```

To the expected inline behaviour:

```
>>> try:
...     1/0
... except Exception as exc:
...     raise RuntimeError("Chained!") from exc
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
RuntimeError: Chained!

```

----------
keywords: 3.5regression
messages: 288793
nosy: ncoghlan
priority: normal
severity: normal
stage: test needed
status: open
title: contextlib.contextmanager may incorrectly unchain RuntimeError
type: behavior
versions: Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29692>
_______________________________________


More information about the New-bugs-announce mailing list