This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: email.utils.parsedate() and email.utils.parsedate_tz() should return None when date cannot be parsed
Type: behavior Stage: resolved
Components: email Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: Arfrever, barry, georg.brandl, python-dev, r.david.murray
Priority: release blocker Keywords: 3.3regression, patch

Created on 2012-09-12 00:37 by Arfrever, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
parsedate_invalid.patch r.david.murray, 2012-09-16 14:16 review
Messages (9)
msg170347 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2012-09-12 00:37
Documentation (in Python 3.3) says that None is returned when date cannot be parsed.

In Python 3.2:

>>> email.utils.parsedate("0")
>>> email.utils.parsedate_tz("0")
>>>

In Python 3.3:

>>> email.utils.parsedate("0")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.3/email/utils.py", line 232, in parsedate
    return _parsedate(data)
  File "/usr/lib64/python3.3/email/_parseaddr.py", line 172, in parsedate
    t = parsedate_tz(data)
  File "/usr/lib64/python3.3/email/_parseaddr.py", line 51, in parsedate_tz
    if res[9] is None:
TypeError: 'NoneType' object is not subscriptable
>>> email.utils.parsedate_tz("0")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.3/email/utils.py", line 238, in parsedate_tz
    return _parsedate_tz(data)
  File "/usr/lib64/python3.3/email/_parseaddr.py", line 51, in parsedate_tz
    if res[9] is None:
TypeError: 'NoneType' object is not subscriptable
>>> email.utils.parsedate_to_datetime("0")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.3/email/utils.py", line 243, in parsedate_to_datetime
    *dtuple, tz = __parsedate_tz(data)
TypeError: 'NoneType' object is not iterable
>>>
msg170556 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-09-16 14:16
Here is a patch.

Georg, this unfortunately represents a regression from 3.2 and should be fixed.
msg170557 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-09-16 14:18
Hmm.  Not sure why Georg didn't get auto-added.
msg170558 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-09-16 14:20
Oh, and I'm purposely allowing parsedate_to_datetime throw exceptions.  I suppose that should be documented, but that's a separate issue.
msg170626 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2012-09-17 20:47
I think that email.utils.parsedate_to_datetime("0") should raise ValueError instead of TypeError.
msg170996 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-09-22 14:02
New changeset 3b03d31f6a79 by R David Murray in branch 'default':
#15925: fix regression: return None for null and non-date strings.
http://hg.python.org/cpython/rev/3b03d31f6a79
msg170997 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-09-22 14:04
I don't think it is worth the extra code to convert the TypeError into a ValueError.  We aren't very strict about the distinction between those two.

Leaving this open for Georg to transplant it to 3.3.0.
msg171002 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-09-22 15:35
This was committed to the release clone as c1e9a1fc6931.
msg171096 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-09-24 05:46
New changeset c1e9a1fc6931 by Georg Brandl in branch 'default':
Closes #15925: fix regression in parsedate() and parsedate_tz() that should return None if unable to parse the argument.
http://hg.python.org/cpython/rev/c1e9a1fc6931
History
Date User Action Args
2022-04-11 14:57:35adminsetgithub: 60129
2012-09-24 05:46:47python-devsetmessages: + msg171096
stage: commit review -> resolved
2012-09-22 15:35:54georg.brandlsetstatus: open -> closed
2012-09-22 15:35:38georg.brandlsetmessages: + msg171002
2012-09-22 14:04:58r.david.murraysetassignee: georg.brandl
messages: + msg170997
resolution: fixed
type: behavior
2012-09-22 14:02:27python-devsetnosy: + python-dev
messages: + msg170996
2012-09-17 20:47:15Arfreversetmessages: + msg170626
2012-09-16 14:20:59r.david.murraysetmessages: + msg170558
title: email.utils.parsedate(), email.utils.parsedate_tz() and email.utils.parsedate_to_datetime() should return None when date cannot be parsed -> email.utils.parsedate() and email.utils.parsedate_tz() should return None when date cannot be parsed
2012-09-16 14:18:08r.david.murraysetnosy: + georg.brandl
messages: + msg170557
2012-09-16 14:16:09r.david.murraysetfiles: + parsedate_invalid.patch
keywords: + patch
messages: + msg170556

stage: commit review
2012-09-12 03:12:34Arfreversetkeywords: + 3.3regression
2012-09-12 00:38:51Arfreversetnosy: + barry, r.david.murray
components: + email
2012-09-12 00:37:45Arfrevercreate