Skip to content

gh-155175: Reject a fraction without a decimal mark in fromisoformat() - #161018

Closed
arxhr007 wants to merge 1 commit into
python:mainfrom
arxhr007:gh-161017-fromisoformat-fraction-separator
Closed

arxhr007 wants to merge 1 commit into
python:mainfrom
arxhr007:gh-161017-fromisoformat-fraction-separator

Conversation

@arxhr007

@arxhr007 arxhr007 commented Oct 31, 2026 •

Copy link
Copy Markdown

In parse_hh_mm_ss_ff() a basic-format time (no : separators) steps back one character after each component so the next one can be parsed. After the seconds the loop then ends and the fractional part is parsed from whatever character comes next, without checking for a . or ,. So time.fromisoformat("12304512") returned 12:30:45.12 in the C implementation, while _pydatetime raises ValueError, as the C parser itself does for "12:30:4512".

The fix returns the existing "malformed microsecond separator" error (-4) when the character after the seconds is not a decimal mark and there is no separator.

test_fromisoformat_fails (time) and test_fromisoformat_fails_datetime have new cases. They run for both implementations, and fail without the change for the C one (9 subtests across the _Fast classes).

Checked on a build of main: test_datetime, test_time, test_strptime, test_calendar, test_zoneinfo, test_email and test_json pass. I did not run a refleak build, since the change only adds an early error return before any allocation.

This is similar to gh-158501, which made the C parser reject stray text before an offset.

This was found by comparing the C datetime with _pydatetime using Hypothesis. The change was prepared with the help of an AI coding assistant (Devin CLI).

…ormat()

time.fromisoformat() and datetime.fromisoformat() read digits after the
seconds of a basic-format time as a fractional second even without a
decimal mark, so "12304512" was accepted by the C implementation but
rejected by the pure-Python one.
@python-cla-bot

python-cla-bot Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@StanFromIreland StanFromIreland changed the title gh-161017: Reject a fraction without a decimal mark in fromisoformat() gh-155175: Reject a fraction without a decimal mark in fromisoformat() Oct 31, 2026
@arxhr007

Copy link
Copy Markdown
Author

Closing as a duplicate of #155177, which makes the same change. Sorry for the noise, I missed it when searching.

@arxhr007 arxhr007 closed this Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant