Skip to content

Commit 6d2cbf5

Browse files
committed
Return back old behavior and update doc
1 parent ade1f41 commit 6d2cbf5

File tree

4 files changed

+10
-50
lines changed

4 files changed

+10
-50
lines changed

Doc/library/email.message.rst

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ message objects.
6262
.. note::
6363

6464
The :class:`EmailMessage` class requires a policy that provides a
65-
``content_manager`` attribute (such as :class:`~email.policy.EmailPolicy`)
66-
for content management methods like :meth:`set_content` and
67-
:meth:`get_content` to work. The legacy :class:`~email.policy.Compat32`
68-
policy does not support these methods. To serialize a message with
69-
compat32 formatting, construct the message with a modern policy and pass
70-
:const:`~email.policy.compat32` to :meth:`as_string` or :meth:`as_bytes`.
65+
``content_manager`` attribute for content management methods like
66+
:meth:`set_content` and :meth:`get_content` to work.
67+
The legacy :class:`~email.policy.Compat32` policy does not support
68+
these methods and should not be used with :class:`EmailMessage`.
7169

7270
.. method:: as_string(unixfrom=False, maxheaderlen=None, policy=None)
7371

@@ -613,12 +611,6 @@ message objects.
613611
*content_manager* is not specified, use the ``content_manager`` specified
614612
by the current :mod:`~email.policy`.
615613

616-
.. note::
617-
618-
This method requires a policy with a ``content_manager`` attribute.
619-
The legacy :const:`~email.policy.compat32` policy does not support
620-
this method.
621-
622614

623615
.. method:: set_content(*args, content_manager=None, **kw)
624616

@@ -628,12 +620,6 @@ message objects.
628620
*content_manager* is not specified, use the ``content_manager`` specified
629621
by the current :mod:`~email.policy`.
630622

631-
.. note::
632-
633-
This method requires a policy with a ``content_manager`` attribute.
634-
The legacy :const:`~email.policy.compat32` policy does not support
635-
this method.
636-
637623

638624
.. method:: make_related(boundary=None)
639625

Doc/library/email.policy.rst

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -664,15 +664,10 @@ The header objects and their attributes are described in
664664

665665
.. note::
666666

667-
The :const:`compat32` policy does not provide a ``content_manager``
668-
attribute and cannot be used with content management methods like
669-
:meth:`~email.message.EmailMessage.set_content` and
670-
:meth:`~email.message.EmailMessage.get_content`. To serialize an
671-
:class:`~email.message.EmailMessage` with compat32 formatting, construct
672-
the message with :data:`~email.policy.default` or another
673-
:class:`EmailPolicy` instance, then pass :const:`compat32` to
674-
:meth:`~email.message.EmailMessage.as_string` or
675-
:meth:`~email.message.EmailMessage.as_bytes`.
667+
The :const:`compat32` policy should not be used as a policy for
668+
:class:`~email.message.EmailMessage` objects, and should only be used
669+
to serialize messages that were created using the :const:`compat32`
670+
policy.
676671

677672

678673
.. rubric:: Footnotes

Lib/email/message.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,18 +1136,12 @@ def iter_parts(self):
11361136

11371137
def get_content(self, *args, content_manager=None, **kw):
11381138
if content_manager is None:
1139-
content_manager = getattr(self.policy, 'content_manager', None)
1140-
if content_manager is None:
1141-
raise TypeError('policy %s lacks content_manager'
1142-
% type(self.policy).__name__)
1139+
content_manager = self.policy.content_manager
11431140
return content_manager.get_content(self, *args, **kw)
11441141

11451142
def set_content(self, *args, content_manager=None, **kw):
11461143
if content_manager is None:
1147-
content_manager = getattr(self.policy, 'content_manager', None)
1148-
if content_manager is None:
1149-
raise TypeError('policy %s lacks content_manager'
1150-
% type(self.policy).__name__)
1144+
content_manager = self.policy.content_manager
11511145
content_manager.set_content(self, *args, **kw)
11521146

11531147
def _make_multipart(self, subtype, disallowed_subtypes, boundary):

Lib/test/test_email/test_message.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,21 +1113,6 @@ def test_string_payload_with_multipart_content_type(self):
11131113
attachments = msg.iter_attachments()
11141114
self.assertEqual(list(attachments), [])
11151115

1116-
def test_set_content_with_compat32_policy_raises(self):
1117-
m = MIMEPart(policy=policy.compat32)
1118-
with self.assertRaises(TypeError) as cm:
1119-
m.set_content(b'\x00'*100, 'image', 'png')
1120-
self.assertIn('content_manager', str(cm.exception))
1121-
1122-
def test_set_content_with_compat32_policy_flattening(self):
1123-
m = MIMEPart()
1124-
m.set_content(b'\x00'*100, 'image', 'png')
1125-
message_as_bytes = m.as_bytes(policy=policy.compat32)
1126-
self.assertEqual(message_as_bytes,
1127-
b'Content-Type: image/png\nContent-Transfer-Encoding: base64\n\n'
1128-
b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n'
1129-
b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==\n')
1130-
11311116

11321117
if __name__ == '__main__':
11331118
unittest.main()

0 commit comments

Comments
 (0)