diff --git a/Lib/email/message.py b/Lib/email/message.py index 12626026179..408d1c68cd1 100644 --- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -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. diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index 8ec39190ea8..14f635f4819 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -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') diff --git a/Misc/NEWS.d/next/Library/2020-01-22-13-19-44.bpo-24337.ORTuPh.rst b/Misc/NEWS.d/next/Library/2020-01-22-13-19-44.bpo-24337.ORTuPh.rst new file mode 100644 index 00000000000..847d2a72bfd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-01-22-13-19-44.bpo-24337.ORTuPh.rst @@ -0,0 +1 @@ +Implement ``__repr__()`` for :class:`email.message.Message`.