Skip to content

Commit 0c2dd0c

Browse files
committed
Close #17702: On error, os.environb now removes suppress the except context
when raising a new KeyError with the original key.
1 parent f1e0273 commit 0c2dd0c

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

Lib/os.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ def __getitem__(self, key):
673673
value = self._data[self.encodekey(key)]
674674
except KeyError:
675675
# raise KeyError with the original key value
676-
raise KeyError(key)
676+
raise KeyError(key) from None
677677
return self.decodevalue(value)
678678

679679
def __setitem__(self, key, value):
@@ -689,7 +689,7 @@ def __delitem__(self, key):
689689
del self._data[encodedkey]
690690
except KeyError:
691691
# raise KeyError with the original key value
692-
raise KeyError(key)
692+
raise KeyError(key) from None
693693

694694
def __iter__(self):
695695
for key in self._data:

Lib/test/test_os.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,13 @@ def test_key_type(self):
644644
with self.assertRaises(KeyError) as cm:
645645
os.environ[missing]
646646
self.assertIs(cm.exception.args[0], missing)
647+
self.assertTrue(cm.exception.__suppress_context__)
647648

648649
with self.assertRaises(KeyError) as cm:
649650
del os.environ[missing]
650651
self.assertIs(cm.exception.args[0], missing)
652+
self.assertTrue(cm.exception.__suppress_context__)
653+
651654

652655
class WalkTests(unittest.TestCase):
653656
"""Tests for os.walk()."""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Core and Builtins
6666
Library
6767
-------
6868

69+
- Issue #17702: On error, os.environb now removes suppress the except context
70+
when raising a new KeyError with the original key.
71+
6972
- Issue #18755: Fixed the loader used in imp to allow get_data() to be called
7073
multiple times.
7174

0 commit comments

Comments
 (0)