diff --git a/Lib/email/message.py b/Lib/email/message.py index fe769580fed5d0..0715925073617b 100644 --- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -164,6 +164,10 @@ def __str__(self): """ 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 39d4ace8d4a1d8..7df74d42f2bce6 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -306,6 +306,13 @@ def test_as_string(self): 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 00000000000000..847d2a72bfd93c --- /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`.