Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/email/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't match the suggestion in the issue. self._default_type is also wrong, since that's just the default, not the actual content-type of the message (which comes from the headers).

What exactly goes in the repr should be discussed further in the issue.


def as_string(self, unixfrom=False, maxheaderlen=0, policy=None):
"""Return the entire formatted message as a string.

Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement ``__repr__()`` for :class:`email.message.Message`.