[Python-checkins] cpython (3.3): Close #17702: os.environ now raises KeyError with the original environment
victor.stinner
python-checkins at python.org
Sun Apr 14 16:44:01 CEST 2013
http://hg.python.org/cpython/rev/72df981e83d3
changeset: 83369:72df981e83d3
branch: 3.3
parent: 83366:c40b50d65a00
user: Victor Stinner <victor.stinner at gmail.com>
date: Sun Apr 14 16:35:04 2013 +0200
summary:
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).
files:
Lib/os.py | 16 ++++++++++++----
Lib/test/test_os.py | 18 ++++++++++++++++++
Misc/NEWS | 4 ++++
3 files changed, 34 insertions(+), 4 deletions(-)
diff --git a/Lib/os.py b/Lib/os.py
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -669,7 +669,11 @@
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 @@
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
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -632,6 +632,24 @@
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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,10 @@
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.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list