https://github.com/python/cpython/commit/0baca623e642fad8d02c2a67c11d05e8ef7e9ccc commit: 0baca623e642fad8d02c2a67c11d05e8ef7e9ccc branch: 3.14 author: Saugat Pandey <[email protected]> committer: StanFromIreland <[email protected]> date: 2026-11-11T15:50:41Z summary:
[3.14] gh-158500: Reject trailing characters in `datetime.date.fromisoformat()` (GH-160737) (#161802) (cherry picked from commit 17a64027984f1aa417e451f6d56afa88d070906d) Co-authored-by: Stan Ulbrych <[email protected]> files: A Misc/NEWS.d/next/Library/2026-10-26-21-37-42.gh-issue-158500.zxyuq5.rst M Lib/_pydatetime.py M Lib/test/datetimetester.py M Modules/_datetimemodule.c diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index 884d2af6955fc7..87028458066e6c 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -389,6 +389,10 @@ def _parse_isoformat_date(dtstr): pos += has_sep dayno = _read_isoformat_component(dtstr[pos:pos + 1], 1) + pos += 1 + + if pos != len(dtstr): + raise ValueError("Invalid isoformat string") return list(_isoweek_to_gregorian(year, weekno, dayno)) else: @@ -399,6 +403,10 @@ def _parse_isoformat_date(dtstr): pos += has_sep day = _read_isoformat_component(dtstr[pos:pos + 2], 2) + pos += 2 + + if pos != len(dtstr): + raise ValueError("Invalid isoformat string") return [year, month, day] @@ -1892,7 +1900,7 @@ def fromisoformat(cls, date_string): raise ValueError( f'Invalid isoformat string: {date_string!r}') from None - if tstr: + if separator_location < len(date_string): try: time_components, became_next_day, error_from_components = _parse_isoformat_time(tstr) except ValueError: diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 2440c9294d4674..f7a1e15a087800 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -2115,6 +2115,10 @@ def test_fromisoformat_fails(self): '2020-W 5', # space in the week number '2020061', # 7 chars: day slice reads a 1-character tail '2020-W2', # 1-digit week number + # gh-158500: trailing characters must not be ignored + '2020010112', # YYYYMMDD followed by two characters + '2020W011xx', # YYYYWwwD followed by two characters + '20200101\u00e9', # 9 characters, but 10 bytes in UTF-8 '٢025-03-09', # Unicode characters '2009\ud80002\ud80028', # Separators are surrogate codepoints ] @@ -3610,6 +3614,11 @@ def test_fromisoformat_fails_datetime(self): '2009-04-19T12:30:45.-05:00', # Empty fraction before offset '2009-04-19T12:30:45.Z', # Empty fraction before Z '2009-04-19T12:30:45,+05:00', # Empty fraction (comma) before offset + # gh-158500: a separator must be followed by a time + '2009-04-19T', # Separator without a time + '20090419T', # Separator without a time + '2009-W16-7T', # Separator without a time + '2009W167T', # Separator without a time ] for bad_str in bad_strs: diff --git a/Misc/NEWS.d/next/Library/2026-10-26-21-37-42.gh-issue-158500.zxyuq5.rst b/Misc/NEWS.d/next/Library/2026-10-26-21-37-42.gh-issue-158500.zxyuq5.rst new file mode 100644 index 00000000000000..98588df2f8ce83 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-26-21-37-42.gh-issue-158500.zxyuq5.rst @@ -0,0 +1,4 @@ +:meth:`datetime.date.fromisoformat` now rejects strings with trailing +characters after a basic-format date, such as ``'2020010112'``. The pure-Python +:meth:`datetime.datetime.fromisoformat` now rejects a date followed by a bare +separator, such as ``'2020-01-01T'``, matching the C implementation. diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 92c0d1c9b0f114..55f58e441abf96 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -985,6 +985,10 @@ parse_isoformat_date(const char *dtstr, const size_t len, int *year, int *month, iso_day = 1; } + if ((size_t)(p - dtstr) != len) { + return -1; + } + int rv = iso_to_ymd(*year, iso_week, iso_day, year, month, day); if (rv) { return -3 + rv; @@ -1005,6 +1009,9 @@ parse_isoformat_date(const char *dtstr, const size_t len, int *year, int *month, if (p == NULL) { return -1; } + if ((size_t)(p - dtstr) != len) { + return -1; + } return 0; } _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
