Commits

Victor Stinner committed 26c049d

Close #17702: On error, os.environb now removes suppress the except context
when raising a new KeyError with the original key.

Comments (0)

Files changed (3)

             value = self._data[self.encodekey(key)]
         except KeyError:
             # raise KeyError with the original key value
-            raise KeyError(key)
+            raise KeyError(key) from None
         return self.decodevalue(value)
 
     def __setitem__(self, key, value):
             del self._data[encodedkey]
         except KeyError:
             # raise KeyError with the original key value
-            raise KeyError(key)
+            raise KeyError(key) from None
 
     def __iter__(self):
         for key in self._data:

Lib/test/test_os.py

         with self.assertRaises(KeyError) as cm:
             os.environ[missing]
         self.assertIs(cm.exception.args[0], missing)
+        self.assertTrue(cm.exception.__suppress_context__)
 
         with self.assertRaises(KeyError) as cm:
             del os.environ[missing]
         self.assertIs(cm.exception.args[0], missing)
+        self.assertTrue(cm.exception.__suppress_context__)
+
 
 class WalkTests(unittest.TestCase):
     """Tests for os.walk()."""
 Library
 -------
 
+- Issue #17702: On error, os.environb now removes suppress the except context
+  when raising a new KeyError with the original key.
+
 - Issue #18755: Fixed the loader used in imp to allow get_data() to be called
   multiple times.