You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
email.parser header-only parsing records MultipartInvariantViolationDefect for valid multipart emails #106186
A valid multipart email message, when parsed with email.parser.HeaderParser(policy=email.policy.default) will record a email.errors.MultipartInvariantViolationDefect.
If the parser isn't going to attempt to parse the message body, it shouldn't report that as a defect.
Simple test script:
importemail.parserimportemail.policyemail_str='''\Date: 01 Jan 2001 00:01+0000From: [email protected]MIME-Version: 1.0Content-Type: multipart/mixed; boundary=autocracy--autocracyContent-Type: text/plainBy hanging on to outdated imperialist dogma which perpetuates the economic andsocial differences in our society.--autocracyContent-Type: text/html<html><body><p>By hanging on to outdated imperialist dogma which perpetuatesthe economic and social differences in our society.</p></body></html>--autocracy--'''full_parser=email.parser.Parser(policy=email.policy.default)
parsed_email_full=full_parser.parsestr(email_str)
print(parsed_email_full.defects) # Prints [] as expectedheader_parser=email.parser.HeaderParser(policy=email.policy.default)
parsed_email_headers_only=header_parser.parsestr(email_str)
print(parsed_email_headers_only.defects) # Prints [MultipartInvariantViolationDefect()]
I believe this is the expected behaviour. Per the documentation, HeaderParser acts like Parser with headersonly=True. Modifying the test script as follows, the printed value is [MultipartInvariantViolationDefect()].
email_str = '''\
Date: 01 Jan 2001 00:01+0000
From: [email protected]
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=autocracy
--autocracy
Content-Type: text/plain
By hanging on to outdated imperialist dogma which perpetuates the economic and
social differences in our society.
--autocracy
Content-Type: text/html
<html><body><p>By hanging on to outdated imperialist dogma which perpetuates
the economic and social differences in our society.</p></body></html>
--autocracy--
'''
full_parser = email.parser.Parser(policy=email.policy.default)
parsed_email_full = full_parser.parsestr(email_str)
print(parsed_email_full.defects) # Prints [] as reported
full_parser = email.parser.Parser(policy=email.policy.default)
parsed_email_full = full_parser.parsestr(email_str, headersonly=True)
print(parsed_email_full.defects) # Prints[MultipartInvariantViolationDefect()]
header_parser = email.parser.HeaderParser(policy=email.policy.default)
parsed_email_headers_only = header_parser.parsestr(email_str)
print(parsed_email_headers_only.defects) # Prints [MultipartInvariantViolationDefect()]
Optional headersonly is a flag specifying whether to stop parsing after reading the headers or not. The default is False, meaning it parses the entire contents of the file.
From this reading, the issue is valid and the fix in the attached PR is the correct bugfix.
Activity
htsedebenham commentedon Jul 22, 2023
I believe this is the expected behaviour. Per the documentation,
HeaderParseracts likeParserwithheadersonly=True. Modifying the test script as follows, the printed value is[MultipartInvariantViolationDefect()].htsedebenham commentedon Jul 22, 2023
I see the issue, looking into it now.
ambv commentedon Jul 23, 2023
Per documentation of
Parser.parse():From this reading, the issue is valid and the fix in the attached PR is the correct bugfix.
gh-106186: Don't report MultipartInvariantViolationDefect for valid m…
pythongh-106186: Don't report MultipartInvariantViolationDefect for v…
pythongh-106186: Don't report MultipartInvariantViolationDefect for v…
[3.12] gh-106186: Don't report MultipartInvariantViolationDefect for …
[3.11] gh-106186: Don't report MultipartInvariantViolationDefect for …
pythongh-106186: Don't report MultipartInvariantViolationDefect for v…
pythongh-106186: Don't report MultipartInvariantViolationDefect for v…