summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2013-04-14 14:38:35 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2013-04-14 14:38:35 (GMT)
commit43aa0d07e2ab93bde71fb2dd5d300fef524b36cd (patch)
tree2c3248941c8aef267d978eb71954a239a050979c
parent76b3b2726c3c3c58932b460d9a9af1b5021fd8f9 (diff)
parent6d10139d70b80cf1326c2763dd13ecc169ce1931 (diff)
downloadcpython-43aa0d07e2ab93bde71fb2dd5d300fef524b36cd.zip
cpython-43aa0d07e2ab93bde71fb2dd5d300fef524b36cd.tar.gz
cpython-43aa0d07e2ab93bde71fb2dd5d300fef524b36cd.tar.bz2
(Merge 3.3) 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).
-rw-r--r--Lib/os.py16
-rw-r--r--Lib/test/test_os.py18
-rw-r--r--Misc/NEWS4
3 files changed, 34 insertions, 4 deletions
diff --git a/Lib/os.py b/Lib/os.py
index 5f35f4c..12d80ee 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -644,7 +644,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):
@@ -654,9 +658,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 32a67e5..e6aae5c 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()."""
diff --git a/Misc/NEWS b/Misc/NEWS
index df012b2..af2d056 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -46,6 +46,10 @@ Core and Builtins
Library
-------
+- Issue #17702: os.environ now raises KeyError with the original environment
+ variable name (str on UNIX), instead of using the encoded name (bytes on
+ UNIX).
+
- Issue #16163: Make the importlib based version of pkgutil.iter_importers
work for submodules. Initial patch by Berker Peksag.