[New-bugs-announce] [issue39725] unrelated `from None` exceptions lose prior exception information
Ethan Furman
report at bugs.python.org
Sat Feb 22 16:05:03 EST 2020
New submission from Ethan Furman <ethan at stoneleaf.us>:
Using the example from https://bugs.python.org/msg293185:
-----------------------------------------------------------------------
--> import os
--> try:
... os.environ["NEW_VARIABLE"] = bug # bug is not a str
... finally:
... del os.environ["NEW_VARIABLE"] # KeyError
...
Traceback (most recent call last):
...
KeyError: 'NEW_VARIABLE'
-----------------------------------------------------------------------
We lost the original exception, `TypeError: str expected, not object`, because in os.py we have:
def __delitem__(self, key):
encodedkey = self.encodekey(key)
unsetenv(encodedkey)
try:
del self._data[encodedkey]
except KeyError:
# raise KeyError with the original key value
raise KeyError(key) from None
If we remove the `from None` the result of the above code is:
-----------------------------------------------------------------------
Traceback (most recent call last):
TypeError: str expected, not type
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
KeyError: b'NEW_VARIABLE'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
KeyError: 'NEW_VARIABLE'
-----------------------------------------------------------------------
There are various tricks we can do to fix this isolated issue (and others like it), but the real problem is that one exception handler's work was destroyed by an unrelated exception handler.
The intent of `from None` is to get rid of any exception details in the try/except block it is contained within, not to lose details from exceptions that were already in play when its try/except block was entered.
Any ideas on how to correct this?
----------
messages: 362478
nosy: ethan.furman, ncoghlan, rhettinger, serhiy.storchaka
priority: normal
severity: normal
status: open
title: unrelated `from None` exceptions lose prior exception information
type: behavior
versions: Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39725>
_______________________________________
More information about the New-bugs-announce
mailing list