Skip to content

bpo-38698: Prevent UnboundLocalError to pop up in parse_message_id #17277

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,8 @@ def parse_message_id(value):
except errors.HeaderParseError:
message_id.defects.append(errors.InvalidHeaderDefect(
"Expected msg-id but found {!r}".format(value)))
message_id.append(token)
else:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like you to change the code like below.

     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)))
-    else:
-        message_id.append(token)
     return message_id

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is wrong with having the append call inside the else branch? Especially since the body of the try statement contains just the instruction that might fail. This seems to me like an extra nitpicky comment for a personal flavour, not necessarily an issue with the code itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't mean your code was wrong. I apologize if you feel bad.
As I read the code again, the original author's intention seems to be the code you proposed.

message_id.append(token)
return message_id

#
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_email/test__header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2638,6 +2638,12 @@ def test_get_msg_id_no_id_right_part(self):
)
self.assertEqual(msg_id.token_type, 'msg-id')

def test_get_msg_id_invalid_expected_msg_id_not_found(self):
text = "Message-Id: 935-XPB-567:0:86089:180874:0:45327:9:90305:[email protected]"
msg_id = parser.parse_message_id(text)
self.assertDefectsEqual(msg_id.all_defects,
[errors.InvalidHeaderDefect])

def test_get_msg_id_no_angle_start(self):
with self.assertRaises(errors.HeaderParseError):
parser.get_msg_id("msgwithnoankle")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Prevent UnboundLocalError to pop up in parse_message_id

parse_message_id() was improperly using a token defined inside an exception
handler, which was raising `UnboundLocalError` on parsing an invalid value.
Patch by Claudiu Popa.