bpo-24337: Implement email.message.Message.__repr__()

This commit is contained in:
Zackery Spytz 2020-01-22 13:20:13 -07:00
parent 14d80d0b60
commit 78c5c37249
3 changed files with 12 additions and 0 deletions

View File

@ -134,6 +134,10 @@ class Message:
"""
return self.as_string()
def __repr__(self):
return f"{self.__class__.__name__} with {len(self._headers)} " \
f"headers and Content-Type {self._default_type}"
def as_string(self, unixfrom=False, maxheaderlen=0, policy=None):
"""Return the entire formatted message as a string.

View File

@ -302,6 +302,13 @@ class TestMessageAPI(TestEmailBase):
self.assertTrue(lines[0].startswith('From '))
self.assertEqual(text, NL.join(lines[1:]))
def test_repr(self):
msg = self._msgobj('msg_01.txt')
self.assertIn('Content-Type text/plain', repr(msg))
self.assertIn('Message', repr(msg))
self.assertEqual(repr(Message()),
'Message with 0 headers and Content-Type text/plain')
def test_as_string_policy(self):
msg = self._msgobj('msg_01.txt')
newpolicy = msg.policy.clone(linesep='\r\n')

View File

@ -0,0 +1 @@
Implement ``__repr__()`` for :class:`email.message.Message`.