]> granicus.if.org Git - python/commitdiff
Close #17702: On error, os.environb now removes suppress the except context
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 23 Aug 2013 17:19:15 +0000 (19:19 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Fri, 23 Aug 2013 17:19:15 +0000 (19:19 +0200)
when raising a new KeyError with the original key.

Lib/os.py
Lib/test/test_os.py
Misc/NEWS

index 06616c64d8712bc4a0dcbf108aaae34d104e3f1d..87689cc26901eaa383f30b116ade34cb6d66e9a9 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -673,7 +673,7 @@ class _Environ(MutableMapping):
             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):
@@ -689,7 +689,7 @@ class _Environ(MutableMapping):
             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:
index 2db030e508afc197b140ff44ab61cb5d0aa007a4..fbf6c0cf7e5a5c3b1183105e958d5ea961ebd32e 100644 (file)
@@ -644,10 +644,13 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol):
         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()."""
index bfb2fd8d8f02243ac34b1a711a5d4f93a23d2c96..81ad8f5d8fc958af2359fe090ff2df2db00e88d6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -66,6 +66,9 @@ Core and Builtins
 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.