From 78c5c37249f18d7fefbeb086db848c9bf52fb6cc Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Wed, 22 Jan 2020 13:20:13 -0700 Subject: [PATCH] bpo-24337: Implement email.message.Message.__repr__() --- Lib/email/message.py | 4 ++++ Lib/test/test_email/test_email.py | 7 +++++++ .../next/Library/2020-01-22-13-19-44.bpo-24337.ORTuPh.rst | 1 + 3 files changed, 12 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-01-22-13-19-44.bpo-24337.ORTuPh.rst 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`.