diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-04-14 14:35:04 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-04-14 14:35:04 (GMT) |
commit | 6d10139d70b80cf1326c2763dd13ecc169ce1931 (patch) | |
tree | 89f487d9481b0ddfde8efbeea9872daab60f1333 /Lib | |
parent | c4e0d982f3b967973553c99722b1e4002c8b35f6 (diff) | |
download | cpython-6d10139d70b80cf1326c2763dd13ecc169ce1931.zip cpython-6d10139d70b80cf1326c2763dd13ecc169ce1931.tar.gz cpython-6d10139d70b80cf1326c2763dd13ecc169ce1931.tar.bz2 |
Close #17702: os.environ now raises KeyError with the original environment
variable name (str on UNIX), instead of using the encoded name (bytes on UNIX).
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/os.py | 16 | ||||
-rw-r--r-- | Lib/test/test_os.py | 18 |
2 files changed, 30 insertions, 4 deletions
@@ -669,7 +669,11 @@ class _Environ(MutableMapping): self._data = data def __getitem__(self, key): - value = self._data[self.encodekey(key)] + try: + value = self._data[self.encodekey(key)] + except KeyError: + # raise KeyError with the original key value + raise KeyError(key) return self.decodevalue(value) def __setitem__(self, key, value): @@ -679,9 +683,13 @@ class _Environ(MutableMapping): self._data[key] = value def __delitem__(self, key): - key = self.encodekey(key) - self.unsetenv(key) - del self._data[key] + encodedkey = self.encodekey(key) + self.unsetenv(encodedkey) + try: + del self._data[encodedkey] + except KeyError: + # raise KeyError with the original key value + raise KeyError(key) def __iter__(self): for key in self._data: diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 184c9ae..83373be 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -632,6 +632,24 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol): key = 'key=' self.assertRaises(OSError, os.environ.__delitem__, key) + def test_key_type(self): + missing = 'missingkey' + self.assertNotIn(missing, os.environ) + + try: + os.environ[missing] + except KeyError as err: + self.assertIs(err.args[0], missing) + else: + self.fail("KeyError not raised") + + try: + del os.environ[missing] + except KeyError as err: + self.assertIs(err.args[0], missing) + else: + self.fail("KeyError not raised") + class WalkTests(unittest.TestCase): """Tests for os.walk().""" |