bpo-44631: Make the repr() of the _Environ class more readable. (#27128)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
Leonardo Freua 2021-07-20 14:15:45 -03:00 committed by GitHub
parent 42205ee512
commit 85fa3b6b7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 6 deletions

View File

@ -704,9 +704,11 @@ class _Environ(MutableMapping):
return len(self._data)
def __repr__(self):
return 'environ({{{}}})'.format(', '.join(
('{!r}: {!r}'.format(self.decodekey(key), self.decodevalue(value))
for key, value in self._data.items())))
formatted_items = ", ".join(
f"{self.decodekey(key)!r}: {self.decodevalue(value)!r}"
for key, value in self._data.items()
)
return f"environ({{{formatted_items}}})"
def copy(self):
return dict(self)

View File

@ -1029,9 +1029,11 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol):
def test___repr__(self):
"""Check that the repr() of os.environ looks like environ({...})."""
env = os.environ
self.assertEqual(repr(env), 'environ({{{}}})'.format(', '.join(
'{!r}: {!r}'.format(key, value)
for key, value in env.items())))
formatted_items = ", ".join(
f"{key!r}: {value!r}"
for key, value in env.items()
)
self.assertEqual(repr(env), f"environ({{{formatted_items}}})")
def test_get_exec_path(self):
defpath_list = os.defpath.split(os.pathsep)

View File

@ -0,0 +1 @@
Refactored the ``repr()`` code of the ``_Environ`` (os module).