From 09d0a8f619f99d009ffd7974e1599d2c57592691 Mon Sep 17 00:00:00 2001 From: Ben Hoyt Date: Sun, 19 Sep 2021 10:49:48 +1200 Subject: [PATCH 1/2] bpo-45239: Fix parsedate_tz when time has more than 2 dots in it Fixes issue 45239: https://bugs.python.org/issue45239 --- Lib/email/_parseaddr.py | 2 ++ Lib/test/test_email/test_email.py | 1 + 2 files changed, 3 insertions(+) diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py index 4d27f87974b20d..ee14ad5c4a8aff 100644 --- a/Lib/email/_parseaddr.py +++ b/Lib/email/_parseaddr.py @@ -126,6 +126,8 @@ def _parsedate_tz(data): tss = 0 elif len(tm) == 3: [thh, tmm, tss] = tm + else: + return None else: return None try: diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index 044b93862a14eb..c4036fd114f63c 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -3007,6 +3007,7 @@ def test_parsedate_returns_None_for_invalid_strings(self): self.assertIsNone(utils.parsedate_tz('0')) self.assertIsNone(utils.parsedate('A Complete Waste of Time')) self.assertIsNone(utils.parsedate_tz('A Complete Waste of Time')) + self.assertIsNone(utils.parsedate_tz('Wed, 3 Apr 2002 12.34.56.78+0800')) # Not a part of the spec but, but this has historically worked: self.assertIsNone(utils.parsedate(None)) self.assertIsNone(utils.parsedate_tz(None)) From fe7e4e3b0e4bb9366edf2ce237237774dd1c3e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Wed, 13 Oct 2021 17:52:54 +0200 Subject: [PATCH 2/2] Add Blurb --- .../next/Library/2021-10-13-17-52-48.bpo-45239.7li1_0.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-10-13-17-52-48.bpo-45239.7li1_0.rst diff --git a/Misc/NEWS.d/next/Library/2021-10-13-17-52-48.bpo-45239.7li1_0.rst b/Misc/NEWS.d/next/Library/2021-10-13-17-52-48.bpo-45239.7li1_0.rst new file mode 100644 index 00000000000000..9e5ec561c362af --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-10-13-17-52-48.bpo-45239.7li1_0.rst @@ -0,0 +1,3 @@ +Fixed :func:`email.utils.parsedate_tz` crashing with +:exc:`UnboundLocalError` on certain invalid input instead of returning +``None``. Patch by Ben Hoyt.