Title: datetime/time.fromisoformat: C accelerator accepts an empty fraction before a timezone designator · Issue #152157 · python/cpython · GitHub
Open Graph Title: datetime/time.fromisoformat: C accelerator accepts an empty fraction before a timezone designator · Issue #152157 · python/cpython
X Title: datetime/time.fromisoformat: C accelerator accepts an empty fraction before a timezone designator · Issue #152157 · python/cpython
Description: Bug report The C accelerator for datetime.fromisoformat() and time.fromisoformat() accepts a decimal separator (. or ,) that is followed by zero fractional digits when a timezone designator (+, -, or Z) comes next. The pure-Python implem...
Open Graph Description: Bug report The C accelerator for datetime.fromisoformat() and time.fromisoformat() accepts a decimal separator (. or ,) that is followed by zero fractional digits when a timezone designator (+, -, ...
X Description: Bug report The C accelerator for datetime.fromisoformat() and time.fromisoformat() accepts a decimal separator (. or ,) that is followed by zero fractional digits when a timezone designator (+, -, ...
Opengraph URL: https://github.com/python/cpython/issues/152157
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"datetime/time.fromisoformat: C accelerator accepts an empty fraction before a timezone designator","articleBody":"## Bug report\n\nThe C accelerator for `datetime.fromisoformat()` and `time.fromisoformat()` accepts a\ndecimal separator (`.` or `,`) that is followed by **zero** fractional digits when a\ntimezone designator (`+`, `-`, or `Z`) comes next. The pure-Python implementation\n(`_pydatetime`) correctly rejects these strings with\n`ValueError: Invalid isoformat string`.\n\n## Reproduction\n\n```python\n>>> import datetime\n>>> datetime.datetime.fromisoformat('2020-01-01T12:34:56.+05:00')\ndatetime.datetime(2020, 1, 1, 12, 34, 56, tzinfo=datetime.timezone(datetime.timedelta(seconds=18000)))\n>>> datetime.time.fromisoformat('12:34:56.Z')\ndatetime.time(12, 34, 56, tzinfo=datetime.timezone.utc)\n```\n\nThe C implementation parses these and silently sets `microsecond=0`. The pure-Python\nimplementation raises:\n\n```python\n>>> import _pydatetime\n>>> _pydatetime.datetime.fromisoformat('2020-01-01T12:34:56.+05:00')\nTraceback (most recent call last):\n ...\nValueError: Invalid isoformat string: '2020-01-01T12:34:56.+05:00'\n```\n\n## C-vs-pure-Python divergence\n\n| Input | C accelerator | Pure-Python (`_pydatetime`) |\n|--------------------------------|-----------------------|-----------------------------|\n| `'2020-01-01T12:34:56.+05:00'` | parses, microsecond=0 | `ValueError` |\n| `'2020-01-01T12:34:56.Z'` | parses, microsecond=0 | `ValueError` |\n| `'2020-01-01T12:34:56,+05:00'` | parses, microsecond=0 | `ValueError` |\n| `'12:34:56.+05:00'` | parses, microsecond=0 | `ValueError` |\n| `'12:34:56,+05:00'` | parses, microsecond=0 | `ValueError` |\n| `'12:34:56.Z'` | parses, microsecond=0 | `ValueError` |\n\nControl cases that already agree (both reject / both accept), unchanged:\n\n| Input | C / pure-Python |\n|-------------------|---------------------------------|\n| `'12:34:56.'` | both `ValueError` |\n| `'12:34:56.5'` | both `time(12, 34, 56, 500000)` |\n| `'12:34:56+05:00'`| both accept (no fraction) |\n\n## Which side is wrong\n\nThe C accelerator is the lenient (wrong) side; pure-Python is correct.\n\n- ISO 8601 §4.2.2.4 and RFC 3339 §5.6: a decimal sign in a time fraction must be\n followed by at least one fractional digit. An empty fraction is not valid.\n- The C implementation already rejects the standalone trailing decimal separator\n (`'12:34:56.'`), so rejecting `'12:34:56.+05:00'` is consistent with its own\n end-of-string behavior; the lenient outcome here is incidental rather than intentional.\n\n## Root cause\n\nIn `Modules/_datetimemodule.c`, `parse_hh_mm_ss_ff()` consumes the `.`/`,` separator and\nthen computes `to_parse` as the number of fractional-digit characters. When a timezone\ndesignator follows immediately, the time substring handed to the function ends right after\nthe separator, so the generic end-of-substring check\n(`if (p >= p_end) return c != '\\0';`) fires *before* the decimal-separator branch and\nreturns \"trailing content present\" (1) rather than flagging an empty fraction. The caller\nthen accepts the value because a timezone is present. The standalone-`.` case is rejected\nonly because that same end-of-string check happens to map to a `ValueError` in the\nno-timezone path.\n\n## Suggested fix\n\nReject `parse_hh_mm_ss_ff()` when a `.`/`,` separator is seen but no fractional digit\nfollows, by handling the decimal-separator case before the generic end-of-substring check.\nThis matches the pure-Python behavior and the spec. A patch and regression tests are ready.\n\nAlternative the maintainers may prefer: make pure-Python lenient instead. The spec\n(≥1 digit required) and the C implementation's own rejection of the standalone trailing\nseparator both argue for fixing C to reject, so that is what the attached patch does.\n\n## Environment\n\nReproduced on current `main` (3.16.0a0). Affects all maintained branches that ship the C\naccelerator.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-152161\n* gh-152761\n* gh-152765\n* gh-152766\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/tonghuaroot","@type":"Person","name":"tonghuaroot"},"datePublished":"2026-06-25T07:03:57Z","url":"https://github.com/python/cpython/issues/152157"}
| 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:02bd75af-e3f1-252d-6209-841dd7f61a60 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8EE2:3E418A:15AE24D:1C856C4:6B01FA89 |
| html-safe-nonce | 998d09cd1b5bee8dc76c84c38da258df6a531ddd1c7e8471cc1c97323191042b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RUUyOjNFNDE4QToxNUFFMjREOjFDODU2QzQ6NkIwMUZBODkiLCJ2aXNpdG9yX2lkIjoiNDE3MDY1MDE4NjY3MjExMDIxNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6eb4ccd1048c4519465cab2016cf5a3558310f2b2e72a90edf7677e9386d1aac |
| hovercard-subject-tag | issue:4741520544 |
| 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/152157/issue_layout |
| twitter:image | https://opengraph.githubassets.com/75055f575418e81893576f0873a41a1d552eea138abc6092d021fbbfba6b4714/python/cpython/issues/152157 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/75055f575418e81893576f0873a41a1d552eea138abc6092d021fbbfba6b4714/python/cpython/issues/152157 |
| og:image:alt | Bug report The C accelerator for datetime.fromisoformat() and time.fromisoformat() accepts a decimal separator (. or ,) that is followed by zero fractional digits when a timezone designator (+, -, ... |
| 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