Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

datetime: pure Python implementation offromisoformat() handles times with trailing spaces inconsistently with the C extension #130959

Closed
Labels
extension-modulesC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@mgorny

Description

@mgorny

Bug report

Bug description:

Discovered primarily because PyPy uses the Python version ofdatetime 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.

>>>datetime.datetime.fromisoformat("2012-04-23T10:20:30.400")datetime.datetime(2012,4,23,10,20,30,400000)>>>datetime.datetime.fromisoformat("2012-04-23T10:20:30.400 ")datetime.datetime(2012,4,23,10,20,30,40000)>>>datetime.datetime.fromisoformat("2012-04-23T10:20:30.400  ")datetime.datetime(2012,4,23,10,20,30,4000)>>>datetime.datetime.fromisoformat("2012-04-23T10:20:30.400   ")datetime.datetime(2012,4,23,10,20,30,400)>>>datetime.datetime.fromisoformat("2012-04-23T10:20:30.400    ")Traceback (mostrecentcalllast):File"<python-input-9>",line1,in<module>datetime.datetime.fromisoformat("2012-04-23T10:20:30.400    ")~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/home/mgorny/git/cpython/Lib/_pydatetime.py",line1949,infromisoformatraiseValueError(f'Invalid isoformat string:{date_string!r}')fromNoneValueError:Invalidisoformatstring:'2012-04-23T10:20:30.400    '>>>datetime.datetime.fromisoformat("2012-04-23T10:20:30.400 +02:30")datetime.datetime(2012,4,23,10,20,30,40000,tzinfo=datetime.timezone(datetime.timedelta(seconds=9000)))

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp