bpo-43295: Fix error handling of datetime.strptime format string '%z' #24627
Conversation
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 MissingOur records indicate the following people have not signed the CLA: 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 You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
Thanks for the quick fix on this! Just a few minor changes.
Lib/test/test_strptime.py
Outdated
def test_timezone_offset_cannot_parse_z(self): | ||
# Check ValueError is raised when matching "z" (ordinary letter) | ||
# with "%z" (timezone offset) | ||
self.assertRaises(ValueError, _strptime._strptime_time, "z", "%z") |
Two things here:
- I much prefer the context manager version of
assertRaises
, since it's much clearer what calls are being tested. - 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 usingtime.strptime
and/ordatetime.strptime
?
@@ -0,0 +1,10 @@ | |||
Previously, datetime.strptime would match 'z' with the format string '%z' |
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.
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 |
Is there a way to re-run the tests? |
@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!
@noormichael: Status check is done, and it's a success |
Thanks @noormichael for the PR |
GH-24728 is a backport of this pull request to the 3.9 branch. |
…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>
GH-24729 is a backport of this pull request to the 3.8 branch. |
…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>
Plz don’t mail me I’m not interested in anything
…Sent from my iPhone
On 03-Mar-2021, at 10:30 PM, Bedevere (bot) ***@***.***> wrote:
GH-24729 is a backport of this pull request to the 3.8 branch.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
…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
Thanks @noormichael for the PR |
GH-25695 is a backport of this pull request to the 3.9 branch. |
…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>
…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
…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>
Previously,
datetime.strptime
would match'z'
with the format string'%z'
(for UTC offsets), throwing anIndexError
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:https://bugs.python.org/issue43295
Automerge-Triggered-By: GH:pganssle
The text was updated successfully, but these errors were encountered: