Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c65592c

Browse files
authoredJul 23, 2023
pythongh-106186: Don't report MultipartInvariantViolationDefect for valid multipart emails when parsing header only (python#107016)
1 parent 5463252 commit c65592c

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed
 

‎Lib/email/feedparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def close(self):
189189
assert not self._msgstack
190190
# Look for final set of defects
191191
if root.get_content_maintype() == 'multipart' \
192-
and not root.is_multipart():
192+
and not root.is_multipart() and not self._headersonly:
193193
defect = errors.MultipartInvariantViolationDefect()
194194
self.policy.handle_defect(root, defect)
195195
return root

‎Lib/test/test_email/data/msg_47.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Date: 01 Jan 2001 00:01+0000
2+
From: arthur@example.example
3+
MIME-Version: 1.0
4+
Content-Type: multipart/mixed; boundary=foo
5+
6+
--foo
7+
Content-Type: text/plain
8+
bar
9+
10+
--foo
11+
Content-Type: text/html
12+
<html><body><p>baz</p></body></html>
13+
14+
--foo--

‎Lib/test/test_email/test_email.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3712,6 +3712,16 @@ def test_bytes_header_parser(self):
37123712
self.assertIsInstance(msg.get_payload(), str)
37133713
self.assertIsInstance(msg.get_payload(decode=True), bytes)
37143714

3715+
def test_header_parser_multipart_is_valid(self):
3716+
# Don't flag valid multipart emails as having defects
3717+
with openfile('msg_47.txt', encoding="utf-8") as fp:
3718+
msgdata = fp.read()
3719+
3720+
parser = email.parser.Parser(policy=email.policy.default)
3721+
parsed_msg = parser.parsestr(msgdata, headersonly=True)
3722+
3723+
self.assertEqual(parsed_msg.defects, [])
3724+
37153725
def test_bytes_parser_does_not_close_file(self):
37163726
with openfile('msg_02.txt', 'rb') as fp:
37173727
email.parser.BytesParser().parse(fp)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Do not report ``MultipartInvariantViolationDefect`` defect
2+
when the :class:`email.parser.Parser` class is used
3+
to parse emails with ``headersonly=True``.

0 commit comments

Comments
 (0)
Please sign in to comment.