[Python-checkins] cpython: #15925: fix regression: return None for null and non-date strings.

r.david.murray python-checkins at python.org
Sat Sep 22 16:00:50 CEST 2012


http://hg.python.org/cpython/rev/3b03d31f6a79
changeset:   79096:3b03d31f6a79
user:        R David Murray <rdmurray at bitdance.com>
date:        Sat Sep 22 09:59:51 2012 -0400
summary:
  #15925: fix regression: return None for null and non-date strings.

Since the logic for null detection had to move into the _parseaddr
functions, I removed the wrappers from email.utils and just import the
_parseaddr functions directly.

files:
  Lib/email/_parseaddr.py           |   4 +++
  Lib/email/utils.py                |  24 +-----------------
  Lib/test/test_email/test_email.py |  13 ++++++++-
  Misc/NEWS                         |   3 ++
  4 files changed, 20 insertions(+), 24 deletions(-)


diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py
--- a/Lib/email/_parseaddr.py
+++ b/Lib/email/_parseaddr.py
@@ -48,6 +48,8 @@
     Accounts for military timezones.
     """
     res = _parsedate_tz(data)
+    if not res:
+        return
     if res[9] is None:
         res[9] = 0
     return tuple(res)
@@ -62,6 +64,8 @@
     source timezone really was UTC.
 
     """
+    if not data:
+        return
     data = data.split()
     # The FWS after the comma after the day-of-week is optional, so search and
     # adjust for this.
diff --git a/Lib/email/utils.py b/Lib/email/utils.py
--- a/Lib/email/utils.py
+++ b/Lib/email/utils.py
@@ -37,10 +37,7 @@
 from email._parseaddr import AddressList as _AddressList
 from email._parseaddr import mktime_tz
 
-# We need wormarounds for bugs in these methods in older Pythons (see below)
-from email._parseaddr import parsedate as _parsedate
-from email._parseaddr import parsedate_tz as _parsedate_tz
-from email._parseaddr import _parsedate_tz as __parsedate_tz
+from email._parseaddr import parsedate, parsedate_tz, _parsedate_tz
 
 from quopri import decodestring as _qdecode
 
@@ -222,25 +219,8 @@
     return msgid
 
 
-
-# These functions are in the standalone mimelib version only because they've
-# subsequently been fixed in the latest Python versions.  We use this to worm
-# around broken older Pythons.
-def parsedate(data):
-    if not data:
-        return None
-    return _parsedate(data)
-
-
-def parsedate_tz(data):
-    if not data:
-        return None
-    return _parsedate_tz(data)
-
 def parsedate_to_datetime(data):
-    if not data:
-        return None
-    *dtuple, tz = __parsedate_tz(data)
+    *dtuple, tz = _parsedate_tz(data)
     if tz is None:
         return datetime.datetime(*dtuple[:6])
     return datetime.datetime(*dtuple[:6],
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -2718,8 +2718,17 @@
             utils.formatdate(now, localtime=False, usegmt=True),
             time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now)))
 
-    def test_parsedate_none(self):
-        self.assertEqual(utils.parsedate(''), None)
+    # parsedate and parsedate_tz will become deprecated interfaces someday
+    def test_parsedate_returns_None_for_invalid_strings(self):
+        self.assertIsNone(utils.parsedate(''))
+        self.assertIsNone(utils.parsedate_tz(''))
+        self.assertIsNone(utils.parsedate('0'))
+        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'))
+        # Not a part of the spec but, but this has historically worked:
+        self.assertIsNone(utils.parsedate(None))
+        self.assertIsNone(utils.parsedate_tz(None))
 
     def test_parsedate_compact(self):
         # The FWS after the comma is optional
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@
 Library
 -------
 
+- Issue #15925: fixed regression in email.utils.parsedate and parsedate_tz
+  handling of empty and non-date strings.
+
 - Issue #15421: fix an OverflowError in Calendar.itermonthdates() after
   datetime.MAXYEAR.  Patch by Cédric Krier.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list