Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a93c6db

Browse files
committed
(Merge 3.3) Close #17702: On error, os.environb now removes suppress the except
context when raising a new KeyError with the original key.
2 parents 330cc52 + 0c2dd0c commit a93c6db

3 files changed

Lines changed: 8 additions & 2 deletions

File tree

Lib/os.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ def __getitem__(self, key):
648648
value = self._data[self.encodekey(key)]
649649
except KeyError:
650650
# raise KeyError with the original key value
651-
raise KeyError(key)
651+
raise KeyError(key) from None
652652
return self.decodevalue(value)
653653

654654
def __setitem__(self, key, value):
@@ -664,7 +664,7 @@ def __delitem__(self, key):
664664
del self._data[encodedkey]
665665
except KeyError:
666666
# raise KeyError with the original key value
667-
raise KeyError(key)
667+
raise KeyError(key) from None
668668

669669
def __iter__(self):
670670
for key in self._data:

Lib/test/test_os.py

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

650651
with self.assertRaises(KeyError) as cm:
651652
del os.environ[missing]
652653
self.assertIs(cm.exception.args[0], missing)
654+
self.assertTrue(cm.exception.__suppress_context__)
655+
653656

654657
class WalkTests(unittest.TestCase):
655658
"""Tests for os.walk()."""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ Core and Builtins
3838
Library
3939
-------
4040

41+
- Issue #17702: On error, os.environb now removes suppress the except context
42+
when raising a new KeyError with the original key.
43+
4144
- Issue #16809: Fixed some tkinter incompabilities with Tcl/Tk 8.6.
4245

4346
- Issue #16809: Tkinter's splitlist() and split() methods now accept Tcl_Obj

0 commit comments

Comments
 (0)