Skip to content

[3.9] bpo-43295: Fix error handling of datetime.strptime format string '%z' (GH-24627) #25695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2021

Conversation

miss-islington
Copy link
Contributor

@miss-islington miss-islington commented Apr 28, 2021

Previously, datetime.strptime would match 'z' with the format string '%z' (for UTC offsets), throwing an IndexError by erroneously trying to parse 'z' as a timestamp. As a special case, '%z' matches the string 'Z' which is equivalent to the offset '+00:00', however this behavior is not defined for lowercase 'z'.

This change ensures a ValueError is thrown when encountering the original example, as follows:

>>> from datetime import datetime
>>> datetime.strptime('z', '%z')
ValueError: time data 'z' does not match format '%z'

Automerge-Triggered-By: GH:pganssle
(cherry picked from commit 04f6fbb)

Co-authored-by: Noor Michael nsmichael31@gmail.com

https://bugs.python.org/issue43295

…pythonGH-24627)

Previously, `datetime.strptime` would match `'z'` with the format string `'%z'` (for UTC offsets), throwing an `IndexError` by erroneously trying to parse `'z'` as a timestamp. As a special case, `'%z'` matches the string `'Z'` which is equivalent to the offset `'+00:00'`, however this behavior is not defined for lowercase `'z'`.

This change ensures a `ValueError` is thrown when encountering the original example, as follows:

```
>>> from datetime import datetime
>>> datetime.strptime('z', '%z')
ValueError: time data 'z' does not match format '%z'
```

Automerge-Triggered-By: GH:pganssle
(cherry picked from commit 04f6fbb)

Co-authored-by: Noor Michael <nsmichael31@gmail.com>
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

I created a new backport, the previous one was stuck (PR #24728).

@iritkatriel
Copy link
Member

Should this be merged?

Copy link
Member

@pganssle pganssle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iritkatriel I believe so! Thanks!

@pganssle pganssle merged commit c87b81d into python:3.9 May 20, 2021
@bedevere-bot
Copy link

@pganssle: Please replace # with GH- in the commit message next time. Thanks!

@miss-islington miss-islington deleted the backport-04f6fbb-3.9 branch May 20, 2021 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants