-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
datetime: pure Python implementation of fromisoformat()
handles times with trailing spaces inconsistently with the C extension
#130959
Comments
fromisoformat()
mishandles times with trailing spacesfromisoformat()
handles times with trailing spaces inconsistently
fromisoformat()
handles times with trailing spaces inconsistentlyfromisoformat()
handles times with trailing spaces inconsistently with the C extension
Hmm, another difference is that when giving 6 digits of microseconds, C implementation accepts them but Python implementation does not: >>> datetime.datetime.fromisoformat("2021-12-20T05:05:05.600000 +02:30")
datetime.datetime(2021, 12, 20, 5, 5, 5, 600000, tzinfo=datetime.timezone(datetime.timedelta(seconds=9000)))
>>> _pydatetime.datetime.fromisoformat("2021-12-20T05:05:05.600000 +02:30")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/mgorny/miniforge3/lib/python3.12/_pydatetime.py", line 1874, in fromisoformat
raise ValueError(
ValueError: Invalid isoformat string: '2021-12-20T05:05:05.600000 +02:30' |
…isoformat()` Fix the pure Python implementation of `fromisoformat()` to reject any non-digit characters, including whitespace, in the fractional part of time specification. This makes the behavior consistent with the C implementation, and prevents incorrect parsing of these fractions (e.g. `.400 ` would be misinterpreted as `.04`).
I think the spec doesn't allow for whitespace, right? If so, we should do whatever the spec says. It's slightly more likely to break things if we make changes to the C implementation than the Python implementation (since the vast majority of people are using the C implementation). I think we can fix the issue with the Python implementation and backport the changes to 3.12+, since right now it just gives the wrong answer, and it's better to start raising exceptions here. If we reject the version with a space from the C implementation, I think we would want that to target only 3.14+, because that will take something that currently parses and gives the "correct" answer and start raising exceptions. We don't have to go as far as deprecating the behavior, since the documented scope of the function says that this should be rejected, but I don't think on net it serves users to make that kind of change in a bugfix version. |
Yes, the standard doesn't permit whitespace, and it's not listed in "exceptions" in the documentation. |
Bug report
Bug description:
Discovered primarily because PyPy uses the Python version of
datetime
from the stdlib, anddjango.utils.dateparse.parse_datetime()
supports wider range of values than the C version ofdatetime.datetime.fromisoformat()
, and falls back to their own parser.For comparison, the C extension rejects all variants containing spaces, causing Django to use its own parser.
PyPy bug report: pypy/pypy#5240
I'm going to try preparing a patch.
CPython versions tested on:
3.11, 3.13, CPython main branch
Operating systems tested on:
Linux
Linked PRs
fromisoformat()
#130962The text was updated successfully, but these errors were encountered: