#7310: fix the repr() of os.environ
This commit is contained in:
parent
5961b0e35f
commit
19e4acfa15
10
Lib/os.py
10
Lib/os.py
|
@ -387,22 +387,32 @@ class _Environ(MutableMapping):
|
|||
self.data = data = {}
|
||||
for key, value in environ.items():
|
||||
data[keymap(key)] = str(value)
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.data[self.keymap(key)]
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
value = str(value)
|
||||
self.putenv(key, value)
|
||||
self.data[self.keymap(key)] = value
|
||||
|
||||
def __delitem__(self, key):
|
||||
self.unsetenv(key)
|
||||
del self.data[self.keymap(key)]
|
||||
|
||||
def __iter__(self):
|
||||
for key in self.data:
|
||||
yield key
|
||||
|
||||
def __len__(self):
|
||||
return len(self.data)
|
||||
|
||||
def __repr__(self):
|
||||
return 'environ({!r})'.format(self.data)
|
||||
|
||||
def copy(self):
|
||||
return dict(self)
|
||||
|
||||
def setdefault(self, key, value):
|
||||
if key not in self:
|
||||
self[key] = value
|
||||
|
|
|
@ -400,6 +400,14 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol):
|
|||
for key, value in self._reference().items():
|
||||
self.assertEqual(os.environ.get(key), value)
|
||||
|
||||
# Issue 7310
|
||||
def test___repr__(self):
|
||||
"""Check that the repr() of os.environ looks like environ({...})."""
|
||||
env = os.environ
|
||||
self.assertTrue(isinstance(env.data, dict))
|
||||
self.assertEqual(repr(env), 'environ({!r})'.format(env.data))
|
||||
|
||||
|
||||
class WalkTests(unittest.TestCase):
|
||||
"""Tests for os.walk()."""
|
||||
|
||||
|
|
Loading…
Reference in New Issue