[Python-checkins] bpo-38698: Prevent UnboundLocalError to pop up in parse_message_id (GH-17277)

Miss Islington (bot) webhook-mailer at python.org
Wed Dec 4 22:14:33 EST 2019


https://github.com/python/cpython/commit/bb815499af855b1759c02535f8d7a9d0358e74e8
commit: bb815499af855b1759c02535f8d7a9d0358e74e8
branch: master
author: Claudiu Popa <pcmanticore at gmail.com>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019-12-04T19:14:26-08:00
summary:

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

files:
A Misc/NEWS.d/next/Library/2019-12-02-10-35-19.bpo-38698.WZnAPQ.rst
M Lib/email/_header_value_parser.py
M Lib/test/test_email/test__header_value_parser.py

diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py
index 1668b4a14e9b9..abdef8189ca6f 100644
--- a/Lib/email/_header_value_parser.py
+++ b/Lib/email/_header_value_parser.py
@@ -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
 
 #
diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py
index 46d90b38e91e1..71168f3183d23 100644
--- a/Lib/test/test_email/test__header_value_parser.py
+++ b/Lib/test/test_email/test__header_value_parser.py
@@ -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:17843586-40 at 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")
diff --git a/Misc/NEWS.d/next/Library/2019-12-02-10-35-19.bpo-38698.WZnAPQ.rst b/Misc/NEWS.d/next/Library/2019-12-02-10-35-19.bpo-38698.WZnAPQ.rst
new file mode 100644
index 0000000000000..e606acb5dcf57
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-12-02-10-35-19.bpo-38698.WZnAPQ.rst
@@ -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.



More information about the Python-checkins mailing list