classification
Title: _strptime.TimeRE should not enforce range in regex
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Steve Yeung, r.david.murray
Priority: low Keywords:

Created on 2015-08-24 21:39 by Steve Yeung, last changed 2020-06-05 18:38 by brett.cannon.

Files
File name Uploaded Description Edit
file Steve Yeung, 2015-09-01 22:19
Messages (5)
msg249074 - (view) Author: Steve Yeung (Steve Yeung) Date: 2015-08-24 21:39
Currently, the regex in TimeRE enforces the numeric ranges. For example:
    'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])",

As a result, an invalid month will cause an generic regex error:
    ValueError: time data '2015/16/5' does not match format '%Y/%m/%d'

However, if we relax the regex to not check the range and allow datetime to handle it:
    'm': r"(?P<m>\d{1,2})"

The error will be handle in datetime instead and the error will be much more helpful:
    ValueError: month must be in 1..12

Please consider relaxing the regex for numeric ranges in _strptime.TimeRE.
msg249133 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2015-08-25 16:22
Do realize that the strptime code is shared with time.strptime() and so this change would have to make sense in both contexts.

This change can also only happen in Python 3.6 because it is backwards-incompatible due to people potentially already catching the previous exception.
msg249141 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-08-25 17:27
And it does not make sense for time, since time.strptime does no additional validation, it just returns the result returned by _strptime.
msg249518 - (view) Author: Steve Yeung (Steve Yeung) Date: 2015-09-01 22:19
I'm not sure what format I'm supposed to provide the test in. I attached a file that has the diff of the changes I made, and how the error message is changed (and improved!) in both datetime and time.
msg249541 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-09-02 13:23
A unit test in test_strptime.
History
Date User Action Args
2020-06-05 18:38:36brett.cannonsetnosy: - brett.cannon
2015-09-02 13:23:46r.david.murraysetmessages: + msg249541
2015-09-01 22:19:47Steve Yeungsetfiles: + file

messages: + msg249518
2015-08-25 17:27:15r.david.murraysetnosy: + r.david.murray
messages: + msg249141
2015-08-25 16:22:38brett.cannonsetpriority: normal -> low
versions: + Python 3.6, - Python 2.7
nosy: + brett.cannon

messages: + msg249133

stage: test needed
2015-08-24 21:39:09Steve Yeungcreate