Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f1e400

Browse files
committed
Merge: #18854: make it explicit that is_multipart does not mean 'multipart/xxx'.
2 parents 10177be + 9cc5fd7 commit 6f1e400

1 file changed

Lines changed: 54 additions & 17 deletions

File tree

Doc/library/email.message.rst

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ Here are the methods of the :class:`Message` class:
131131

132132
Return ``True`` if the message's payload is a list of sub-\
133133
:class:`Message` objects, otherwise return ``False``. When
134-
:meth:`is_multipart` returns ``False``, the payload should be a string object.
134+
:meth:`is_multipart` returns ``False``, the payload should be a string
135+
object. (Note that :meth:`is_multipart` returning ``True`` does not
136+
necessarily mean that "msg.get_content_maintype() == 'multipart'" will
137+
return the ``True``. For example, ``is_multipart`` will return ``True``
138+
when the :class:`Message` is of type ``message/rfc822``.)
135139

136140

137141
.. method:: set_unixfrom(unixfrom)
@@ -584,23 +588,56 @@ Here are the methods of the :class:`Message` class:
584588
Here's an example that prints the MIME type of every part of a multipart
585589
message structure:
586590

587-
.. testsetup::
591+
.. testsetup::
592+
593+
>>> from email import message_from_binary_file
594+
>>> with open('Lib/test/test_email/data/msg_16.txt', 'rb') as f:
595+
... msg = message_from_binary_file(f)
596+
>>> from email.iterators import _structure
597+
598+
.. doctest::
599+
600+
>>> for part in msg.walk():
601+
... print(part.get_content_type())
602+
multipart/report
603+
text/plain
604+
message/delivery-status
605+
text/plain
606+
text/plain
607+
message/rfc822
608+
text/plain
609+
610+
``walk`` iterates over the subparts of any part where
611+
:meth:`is_multipart` returns ``True``, even though
612+
``msg.get_content_maintype() == 'multipart'`` may return ``False``. We
613+
can see this in our example by making use of the ``_structure`` debug
614+
helper function:
615+
616+
.. doctest::
617+
618+
>>> for part in msg.walk():
619+
... print(part.get_content_maintype() == 'multipart'),
620+
... part.is_multipart())
621+
True True
622+
False False
623+
False True
624+
False False
625+
False False
626+
False True
627+
False False
628+
>>> _structure(msg)
629+
multipart/report
630+
text/plain
631+
message/delivery-status
632+
text/plain
633+
text/plain
634+
message/rfc822
635+
text/plain
636+
637+
Here the ``message`` parts are not ``multiparts``, but they do contain
638+
subparts. ``is_multipart()`` returns ``True`` and ``walk`` descends
639+
into the subparts.
588640

589-
>>> from email import message_from_binary_file
590-
>>> with open('Lib/test/test_email/data/msg_16.txt', 'rb') as f:
591-
... msg = message_from_binary_file(f)
592-
593-
.. doctest::
594-
595-
>>> for part in msg.walk():
596-
... print(part.get_content_type())
597-
multipart/report
598-
text/plain
599-
message/delivery-status
600-
text/plain
601-
text/plain
602-
message/rfc822
603-
text/plain
604641

605642
:class:`Message` objects can also optionally contain two instance attributes,
606643
which can be used when generating the plain text of a MIME message.

0 commit comments

Comments
 (0)