classification
Title: strptime exception context
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: Claudiu.Popa, ezio.melotti, python-dev
Priority: normal Keywords: patch

Created on 2013-03-29 10:48 by Claudiu.Popa, last changed 2013-04-03 23:17 by ezio.melotti. This issue is now closed.

Files
File name Uploaded Description Edit
strptime.patch Claudiu.Popa, 2013-03-29 10:48 review
issue17572.diff ezio.melotti, 2013-03-29 21:24 review
Messages (4)
msg185495 - (view) Author: Claudiu Popa (Claudiu.Popa) * Date: 2013-03-29 10:48
When using a directive that is not recognised by time.strptime, the following occurs in Python 3:

>>> time.strptime("", "%D")
Traceback (most recent call last):
  File "C:\Python33\lib\_strptime.py", line 320, in _strptime
    format_regex = _TimeRE_cache.compile(format)
  File "C:\Python33\lib\_strptime.py", line 268, in compile
    return re_compile(self.pattern(format), IGNORECASE)
  File "C:\Python33\lib\_strptime.py", line 262, in pattern
    self[format[directive_index]])
KeyError: 'D'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python33\lib\_strptime.py", line 494, in _strptime_time
    tt = _strptime(data_string, format)[0]
  File "C:\Python33\lib\_strptime.py", line 329, in _strptime
    (bad_directive, format))
ValueError: 'D' is a bad directive in format '%D'
>>>

Probably the KeyError should be supressed when raising the ValueError. The attached patch tries to fix this issue, by suppressing the context.
msg185520 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-03-29 21:24
The fix LGTM.  The attached patch tweaks a bit your test and adds another one to check _strptime directly.
msg185976 - (view) Author: Roundup Robot (python-dev) Date: 2013-04-03 23:16
New changeset 3203e083aa7b by Ezio Melotti in branch '3.3':
#17572: Avoid chained exceptions while passing bad directives to time.strptime().  Initial patch by Claudiu Popa.
http://hg.python.org/cpython/rev/3203e083aa7b

New changeset d408c9bd4bf7 by Ezio Melotti in branch 'default':
#17572: merge with 3.3.
http://hg.python.org/cpython/rev/d408c9bd4bf7
msg185977 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-04-03 23:17
Fixed, thanks for the report and the patch!
History
Date User Action Args
2013-04-03 23:17:26ezio.melottisetstatus: open -> closed
messages: + msg185977

assignee: ezio.melotti
resolution: fixed
stage: commit review -> resolved
2013-04-03 23:16:40python-devsetnosy: + python-dev
messages: + msg185976
2013-03-29 21:24:51ezio.melottisetfiles: + issue17572.diff

nosy: + ezio.melotti
messages: + msg185520

stage: commit review
2013-03-29 10:48:03Claudiu.Popacreate