From 0c2dd0c0a9498422645a2805a955b9a898298bc6 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 23 Aug 2013 19:19:15 +0200 Subject: [PATCH] Close #17702: On error, os.environb now removes suppress the except context when raising a new KeyError with the original key. --- Lib/os.py | 4 ++-- Lib/test/test_os.py | 3 +++ Misc/NEWS | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/os.py b/Lib/os.py index 06616c64d87..87689cc2690 100644 --- 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: diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 2db030e508a..fbf6c0cf7e5 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -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().""" diff --git a/Misc/NEWS b/Misc/NEWS index bfb2fd8d8f0..81ad8f5d8fc 100644 --- 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.