Title: Pure-Python `date.fromisoformat` silently mis-parses malformed basic-format dates · Issue #152204 · python/cpython · GitHub
Open Graph Title: Pure-Python `date.fromisoformat` silently mis-parses malformed basic-format dates · Issue #152204 · python/cpython
X Title: Pure-Python `date.fromisoformat` silently mis-parses malformed basic-format dates · Issue #152204 · python/cpython
Description: Bug description _pydatetime.date.fromisoformat (the pure-Python reference used when the C accelerator is unavailable, and via _pydatetime directly) returns a wrong-but-plausible date for several strings that are not valid ISO-8601 dates....
Open Graph Description: Bug description _pydatetime.date.fromisoformat (the pure-Python reference used when the C accelerator is unavailable, and via _pydatetime directly) returns a wrong-but-plausible date for several st...
X Description: Bug description _pydatetime.date.fromisoformat (the pure-Python reference used when the C accelerator is unavailable, and via _pydatetime directly) returns a wrong-but-plausible date for several st...
Opengraph URL: https://github.com/python/cpython/issues/152204
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Pure-Python `date.fromisoformat` silently mis-parses malformed basic-format dates","articleBody":"### Bug description\n\n`_pydatetime.date.fromisoformat` (the pure-Python reference used when the C\naccelerator is unavailable, and via `_pydatetime` directly) returns a\n**wrong-but-plausible `date`** for several strings that are not valid ISO-8601\ndates. The C accelerator raises `ValueError` for every one of them. Because the\nresult is a silently incorrect date rather than an error, malformed input\nbecomes valid-looking data with no signal that anything went wrong.\n\nThere are two surface forms of the same underlying defect in\n`_parse_isoformat_date`, which slices fixed-width substrings and calls `int()`\non them without checking that each slice is exactly *N* ASCII digits:\n\n**(1) `int()` tolerates a leading `+` / `-` / space in a basic-format field.**\n\n```pycon\n>>> import _datetime, _pydatetime\n>>> _pydatetime.date.fromisoformat('2020+12')\ndatetime.date(2020, 1, 2)\n>>> _datetime.date.fromisoformat('2020+12')\nTraceback (most recent call last):\n ...\nValueError: Invalid isoformat string: '2020+12'\n>>> _pydatetime.date.fromisoformat('+020-06-15')\ndatetime.date(20, 6, 15)\n>>> _pydatetime.date.fromisoformat('2020-W 5')\ndatetime.date(2020, 1, 27)\n>>> _pydatetime.date.fromisoformat('202012+9')\ndatetime.date(2020, 12, 9)\n>>> _pydatetime.date.fromisoformat('2020 12')\ndatetime.date(2020, 1, 2)\n```\n\nHere `int('+1') == 1`, `int(' 1') == 1` and `int('+9') == 9`, so the month /\nday / week fields parse a sign or space that is not part of any ISO-8601 date.\n\n**(2) The length gate admits 7-character strings, and a fixed-width slice then\nreads a 1-character tail.**\n\n```pycon\n>>> _pydatetime.date.fromisoformat('2020061')\ndatetime.date(2020, 6, 1)\n>>> _datetime.date.fromisoformat('2020061')\nTraceback (most recent call last):\n ...\nValueError: Invalid isoformat string: '2020061'\n>>> _pydatetime.date.fromisoformat('2020123')\ndatetime.date(2020, 12, 3)\n>>> _pydatetime.date.fromisoformat('2020-W2')\ndatetime.date(2020, 1, 6)\n>>> _pydatetime.date.fromisoformat('9999121')\ndatetime.date(9999, 12, 1)\n```\n\n`'2020061'` is 7 chars; the gate `len(date_string) in (7, 8, 10)` lets it\nthrough, the month slice reads `'06'` and the day slice `dtstr[6:8]` reads the\n1-character tail `'1'`, giving `date(2020, 6, 1)`. `'2020-W2'` reads a 1-digit\nweek `int('2')`. The C `parse_digits(p, ..., 2)` requires exactly two digits, so\nC rejects all of these.\n\n`datetime.fromisoformat` inherits the same defect via the date branch, e.g.\n`_pydatetime.datetime.fromisoformat('2020061')` returns\n`datetime.datetime(2020, 6, 1, 0, 0)` while the C path raises.\n\n### C vs pure-Python\n\n| input | C `_datetime` | pure-Python `_pydatetime` |\n|---|---|---|\n| `date.fromisoformat('2020+12')` | `ValueError` | `date(2020, 1, 2)` |\n| `date.fromisoformat('+020-06-15')` | `ValueError` | `date(20, 6, 15)` |\n| `date.fromisoformat('2020-W 5')` | `ValueError` | `date(2020, 1, 27)` |\n| `date.fromisoformat('202012+9')` | `ValueError` | `date(2020, 12, 9)` |\n| `date.fromisoformat('2020061')` | `ValueError` | `date(2020, 6, 1)` |\n| `date.fromisoformat('2020123')` | `ValueError` | `date(2020, 12, 3)` |\n| `date.fromisoformat('2020-W2')` | `ValueError` | `date(2020, 1, 6)` |\n| `date.fromisoformat('9999121')` | `ValueError` | `date(9999, 12, 1)` |\n\n### Root cause\n\n`Lib/_pydatetime.py`, `_parse_isoformat_date` (the function's own comment notes\nit \"assumes an ASCII-only string of lengths 7, 8 or 10\"). On current `main` the\nfunction body is:\n\n```python\ndef _parse_isoformat_date(dtstr):\n # It is assumed that this is an ASCII-only string of lengths 7, 8 or 10,\n # see the comment on Modules/_datetimemodule.c:_find_isoformat_datetime_separator\n if len(dtstr) not in (7, 8, 10): # line 361\n raise ValueError(\"Invalid isoformat string\")\n year = int(dtstr[0:4]) # line 363\n ...\n weekno = int(dtstr[pos:pos + 2]) # line 370 (week field)\n ...\n dayno = int(dtstr[pos:pos + 1]) # line 380 (week day field)\n ...\n month = int(dtstr[pos:pos + 2]) # line 384 (month field)\n ...\n day = int(dtstr[pos:pos + 2]) # line 390 (day field)\n```\n\nThe `if len(dtstr) not in (7, 8, 10)` gate at line 361 only bounds the *total*\nlength; `date.fromisoformat` (the caller, lines 1059-1060) applies the same\nlength gate before calling in. Neither gate checks the *content* of the\nfixed-width fields. Each field is read with `int(dtstr[pos:pos+N])`. `int()`\naccepts a leading `+`/`-`/whitespace and a short string, so:\n\n* a `+`/`-`/space that lands in a month/day/week field is silently consumed\n (form 1), and\n* on a 7-char string the day/week slice runs off the end and `int()` happily\n parses the 1-character remainder (form 2).\n\nWrong side: **pure-Python**, which over-accepts. ISO-8601 calendar dates contain\nno sign or space inside the date, and have no 1-digit month-day or 1-digit\nweek; `date.fromisoformat`'s docstring promises a string \"in the format emitted\nby `date.isoformat()`\". The C accelerator's `parse_digits` rejects any non-digit\nbyte and requires the exact field width, then verifies the whole string was\nconsumed, so C is correct here.\n\n### Suggested fix\n\nValidate each slice in `_parse_isoformat_date` before converting: require that\nthe year/month/day/week/weekday slice is exactly *N* ASCII digits (mirroring the\nC `parse_digits`). The module already has an `_is_ascii_digit` helper used by\nthe fraction path (`_parse_hh_mm_ss_ff` does\n`all(map(_is_ascii_digit, tstr[pos:]))`), so reusing it keeps the check\nconsistent, e.g. raise `ValueError` unless\n`len(s) == N and all(map(_is_ascii_digit, s))` before calling `int(s)`. That\nmakes the malformed basic-format strings above raise `ValueError` on the\npure-Python path exactly as the C accelerator does, and closes the 7-char\nshort-slice hole at the same time. (The length gate `(7, 8, 10)` can stay; the\nper-field width check is what rejects `'2020061'`, since its day slice is then a\n1-char string.)\n\n### Environment\n\n* Reproduced on current `main`, `3.16.0a0`.\n* On current `main`, the length gate in `_parse_isoformat_date` is already an\n `if len(dtstr) not in (7, 8, 10): raise ValueError` (gh-152060 / PR #152061,\n merged, replaced the earlier `assert len(dtstr) in (7, 8, 10)` so that a bad\n length raises `ValueError` instead of `AssertionError`). That change only\n touched the *length-gate exception type*; it did not touch the\n `int(dtstr[...])` slices, so both mis-parse forms above still reproduce on\n `main`. (A pre-PR-#152061 checkout still carries the `assert`; the slice\n defect is the same either way.)\n* The same slice-based `_parse_isoformat_date` exists in the 3.14 / 3.15\n branches (the pure-Python module is `_pydatetime` in all of them), so those\n branches are affected wherever the pure-Python path is exercised (the C\n accelerator masks it when present).\n\n### Relation to existing issues\n\nThis is distinct from the known nearby issues:\n\n* **gh-107779** (\"incorrectly accepts and parses strings without date-time\n separator\", open; PR #107791) is about `_find_isoformat_datetime_separator`\n returning an index that is not a real separator in `datetime.fromisoformat`.\n That is a different function and mechanism; it does not address the\n `int()`-slice leniency in `_parse_isoformat_date`. Verified on this build that\n `'2024-01-17T15:21:00-0800'` (the basic/extended-mixing class) is accepted by\n *both* implementations, i.e. not a C-vs-pure-Python divergence.\n* **gh-152060** / PR #152061 (merged) was the *exception-type* fix: a\n wrong-*length* `dtstr` used to raise `AssertionError` (from the old\n `assert len(...)`); PR #152061 turned that into a `ValueError`. That is a\n disjoint defect: it is about the length gate's exception type, whereas this\n issue is about strings of a *valid* length (7/8/10) whose fixed-width fields\n are mis-sliced into a silently *wrong value*. The per-field slices PR #152061\n left untouched are exactly the ones at fault here.\n\nFound with a differential C-vs-pure-Python `fromisoformat` testing harness\n(AI-assisted, each case hand-verified).\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-152205\n* gh-156359\n* gh-156367\n* gh-156368\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/tonghuaroot","@type":"Person","name":"tonghuaroot"},"datePublished":"2026-06-25T13:49:14Z","url":"https://github.com/python/cpython/issues/152204"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:751193fd-26ce-d60c-56df-6260fc46ba4d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CED2:22DB35:3C60B61:4CFD9E5:6B01FA41 |
| html-safe-nonce | 6c9c1026b03fe5955a691b65093f645809dada511c9e8795249eba5026262eeb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRUQyOjIyREIzNTozQzYwQjYxOjRDRkQ5RTU6NkIwMUZBNDEiLCJ2aXNpdG9yX2lkIjoiNDg4NjM0MjQ1Mjg4MzA5NDA4MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bc27633d900f1b18c796bd115fa8295dfa06fca8edf40f125e2a18b6d9f5cadd |
| hovercard-subject-tag | issue:4744401322 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/152204/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c012cd5afebc12f531cf022b0933b1de75efdd0b6a1a3608b1b5e7dbec3f4692/python/cpython/issues/152204 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c012cd5afebc12f531cf022b0933b1de75efdd0b6a1a3608b1b5e7dbec3f4692/python/cpython/issues/152204 |
| og:image:alt | Bug description _pydatetime.date.fromisoformat (the pure-Python reference used when the C accelerator is unavailable, and via _pydatetime directly) returns a wrong-but-plausible date for several st... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tonghuaroot |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width