bpo-38698: Prevent UnboundLocalError to pop up in parse_message_id (GH-17277)

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




https://bugs.python.org/issue38698
This commit is contained in:
Claudiu Popa 2019-12-05 04:14:26 +01:00 committed by Miss Islington (bot)
parent 8b787964e0
commit bb815499af
3 changed files with 13 additions and 1 deletions

View File

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

View File

@ -2638,6 +2638,12 @@ class TestParser(TestParserMixin, TestEmailBase):
)
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:17843586-40@example.com"
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")

View File

@ -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.