This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ethan.furman
Recipients ethan.furman, ncoghlan, rhettinger, serhiy.storchaka
Date 2020-02-22.21:05:03
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1582405503.93.0.737085396902.issue39725@roundup.psfhosted.org>
In-reply-to
Content
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?
History
Date User Action Args
2020-02-22 21:05:03ethan.furmansetrecipients: + ethan.furman, rhettinger, ncoghlan, serhiy.storchaka
2020-02-22 21:05:03ethan.furmansetmessageid: <1582405503.93.0.737085396902.issue39725@roundup.psfhosted.org>
2020-02-22 21:05:03ethan.furmanlinkissue39725 messages
2020-02-22 21:05:03ethan.furmancreate