New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
While parsing email message id: UnboundLocalError #82879
Comments
Parsing an invalid email message id can throw a header parser error. A bug in parse_message_ still tries to append the unset token to a variable. File "/opt/python-3.8.0/lib/python3.8/email/header_value_parser.py", line 2116, in parse_message Version 3.7 is not affected. |
Thanks for the report. Can you please attach a sample script to reproduce this error? |
This was also reported in bpo-38708 with the original code added in bpo-35805. Commenting out the except clause also doesn't raise any error in test suite so I assume the code path was not tested. Maybe the script could be added as part of the test suite. $ git diff
diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py
index 1668b4a14e..9b6ca3a268 100644
--- a/Lib/email/_header_value_parser.py
+++ b/Lib/email/_header_value_parser.py
@@ -2110,10 +2110,10 @@ def parse_message_id(value):
message_id = MessageID()
try:
token, value = get_msg_id(value)
+ message_id.append(token)
except errors.HeaderParseError:
message_id.defects.append(errors.InvalidHeaderDefect(
"Expected msg-id but found {!r}".format(value)))
- message_id.append(token)
return message_id |
More tests are always good :) The "correct" solution here (as far as I remember, its has been a while since I've had time to even looked at the _header_value_parser code) would be to add a new 'invalid-msg-id' token, and do this: message_id = MessageID()
try:
token, value = get_msg_id(value)
message_id.append(token)
except HeaderParseError as ex:
message_id = InvalidMessageID(value)
message_id.defects.append(InvalidHeaderDefect(
f"Invalid msg_id: {ex}"))
return message_id |
Actually, the success path there should also check that value is empty, and if it is not register a defect for that as well. |
Closing this as fixed. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: