Skip to content

bpo-43295: Fix error handling of datetime.strptime format string '%z'#24627

Merged
miss-islington merged 6 commits into
python:masterfrom
noormichael:fix-issue-43295
Mar 3, 2021
Merged

bpo-43295: Fix error handling of datetime.strptime format string '%z'#24627
miss-islington merged 6 commits into
python:masterfrom
noormichael:fix-issue-43295

Conversation

@noormichael

@noormichael noormichael commented Feb 23, 2021

Copy link
Copy Markdown
Contributor

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'

https://bugs.python.org/issue43295

Automerge-Triggered-By: GH:pganssle

@the-knights-who-say-ni

Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@noormichael

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@pganssle pganssle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the quick fix on this! Just a few minor changes.

Comment thread Lib/test/test_strptime.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two things here:

  1. I much prefer the context manager version of assertRaises, since it's much clearer what calls are being tested.
  2. It would be preferable to test this using only the public interface. I realize that there are other uses of _strptime._strptime_time in this file, but going forward I'd like to keep the tests focused on the public interface, so can you try and exercise this using time.strptime and/or datetime.strptime?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a very good summary of the issue, but it is long for a news stub. They're usually 1 or two lines like this: https://docs.python.org/3/whatsnew/changelog.html

How about:

Previously, :meth:`datetime.datetime.strptime` would fail with `IndexError` when the string `'z'`
was found for the `%z` format specifier. This has been fixed to properly raise `ValueError` instead.

(You'll want to check to make sure that the :meth: directive works correctly, I may have made a mistake with that.
A BPO link is automatically generated, and users who want more information can click on it for an MWE and such.

@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@noormichael

Copy link
Copy Markdown
Contributor Author

Is there a way to re-run the tests? test_ssl failed on Ubuntu which has passed on previous runs. In any case, I've made the requested changes. Could you please review this again?

@pganssle pganssle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@noormichael No need to worry about that. I believe this is something to do with github upgrading the default Ubuntu container from 18.04 to 20.04 and some patch in Ubuntu that breaks OpenSSL. See this bug for more details.

I believe no further action on your part is necessary, this looks good and we can probably wait until that bug is resolved to merge so that backports will go more easily. Thanks for your contribution to CPython!

@miss-islington

Copy link
Copy Markdown
Contributor

@noormichael: Status check is done, and it's a success ✅ .

@miss-islington miss-islington merged commit 04f6fbb into python:master Mar 3, 2021
@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @noormichael for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8, 3.9.
🐍🍒⛏🤖

@bedevere-bot

Copy link
Copy Markdown

GH-24728 is a backport of this pull request to the 3.9 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 3, 2021
…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>
@bedevere-bot

Copy link
Copy Markdown

GH-24729 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 3, 2021
…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>
@Immorningstar

Immorningstar commented Mar 3, 2021 via email

Copy link
Copy Markdown

@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @noormichael for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@bedevere-bot

Copy link
Copy Markdown

GH-25695 is a backport of this pull request to the 3.9 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 28, 2021
…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>
pganssle pushed a commit that referenced this pull request May 20, 2021
…GH-24627) (#25695)

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>

Co-authored-by: Noor Michael <nsmichael31@gmail.com>
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.

9 participants