[Python-3000-checkins] r55964 - in python/branches/py3k-struni/Lib: os.py test/test_os.py

guido.van.rossum python-3000-checkins at python.org
Wed Jun 13 23:51:32 CEST 2007


Author: guido.van.rossum
Date: Wed Jun 13 23:51:27 2007
New Revision: 55964

Modified:
   python/branches/py3k-struni/Lib/os.py
   python/branches/py3k-struni/Lib/test/test_os.py
Log:
Following an idea by Ron Adam, make sure keys and values in the
environ dict are strings (in particular, not 8-bit strings).


Modified: python/branches/py3k-struni/Lib/os.py
==============================================================================
--- python/branches/py3k-struni/Lib/os.py	(original)
+++ python/branches/py3k-struni/Lib/os.py	Wed Jun 13 23:51:27 2007
@@ -420,12 +420,12 @@
             self.unsetenv = unsetenv
             self.data = data = {}
             for key, value in environ.items():
-                data[keymap(key)] = value
+                data[keymap(key)] = str(value)
         def __getitem__(self, key):
             return self.data[self.keymap(key)]
-        def __setitem__(self, key, item):
-            self.putenv(key, item)
-            self.data[self.keymap(key)] = item
+        def __setitem__(self, key, value):
+            self.putenv(key, str(value))
+            self.data[self.keymap(key)] = str(value)
         def __delitem__(self, key):
             self.unsetenv(key)
             del self.data[self.keymap(key)]
@@ -438,7 +438,7 @@
             return dict(self)
         def setdefault(self, key, value):
             if key not in self:
-                self[key] = value
+                self[key] = str(value)
             return self[key]
 
     try:
@@ -456,9 +456,9 @@
         __all__.append("unsetenv")
 
     if name in ('os2', 'nt'): # Where Env Var Names Must Be UPPERCASE
-        _keymap = lambda key: key.upper()
+        _keymap = lambda key: str(key.upper())
     else:  # Where Env Var Names Can Be Mixed Case
-        _keymap = lambda key: key
+        _keymap = lambda key: str(key)
 
     environ = _Environ(environ, _keymap, _putenv, _unsetenv)
 

Modified: python/branches/py3k-struni/Lib/test/test_os.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_os.py	(original)
+++ python/branches/py3k-struni/Lib/test/test_os.py	Wed Jun 13 23:51:27 2007
@@ -273,6 +273,13 @@
             value = os.popen("/bin/sh -c 'echo $HELLO'").read().strip()
             self.assertEquals(value, "World")
 
+    # Verify environ keys and values from the OS are of the
+    # correct str type.
+    def test_keyvalue_types(self):
+        for key, val in os.environ.items():
+            self.assertEquals(type(key), str)
+            self.assertEquals(type(val), str)
+
 class WalkTests(unittest.TestCase):
     """Tests for os.walk()."""
 


More information about the Python-3000-checkins mailing list