[New-bugs-announce] [issue43295] datetime.strptime emits IndexError on parsing 'z' as %z

itchyny report at bugs.python.org
Mon Feb 22 08:58:19 EST 2021


New submission from itchyny <itchyny.itchyny at gmail.com>:

In Python 3.9.2, parsing 'z' (small letter) as '%z' (time zone offset) using datetime.strptime emits an IndexError.

>>> from datetime import datetime
>>> datetime.strptime('z', '%z')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/usr/local/lib/python3.9/_strptime.py", line 453, in _strptime
    if z[3] == ':':
IndexError: string index out of range

I expect ValueError (or some another useful error) as follows.
ValueError: time data 'z' does not match format '%z'

This is caused by compiling '%z' to a pattern containing 'Z' (for UTC) with the IGNORECASE flag and accessing z[3] without noticing 'z' is accepted by the regexp.

----------
components: Library (Lib)
messages: 387514
nosy: itchyny
priority: normal
severity: normal
status: open
title: datetime.strptime emits IndexError on parsing 'z' as %z
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43295>
_______________________________________


More information about the New-bugs-announce mailing list