Skip to content

gh-83461: Don't allow datetime parsing to accept non-ASCII digits - #131008

Open
StanFromIreland wants to merge 30 commits into
python:mainfrom
StanFromIreland:ascii-strptime
Open

gh-83461: Don't allow datetime parsing to accept non-ASCII digits#131008
StanFromIreland wants to merge 30 commits into
python:mainfrom
StanFromIreland:ascii-strptime

Conversation

@StanFromIreland

@StanFromIreland StanFromIreland commented Mar 9, 2025

Copy link
Copy Markdown
Member

@picnixz picnixz added the type-security A security issue label Mar 9, 2025
@picnixz

picnixz commented Mar 9, 2025

Copy link
Copy Markdown
Member

The original issue was marked with a security bug but I haven't looked at the entire thread so we might consider it a simple bug fix

@StanFromIreland

Copy link
Copy Markdown
Member Author

@picnixz

No it needs to be relabeled:

-versions -type-security +type-bug

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need to update the C implementation?

Comment thread Lib/_strptime.py Outdated
@picnixz picnixz removed the type-security A security issue label Mar 9, 2025
@StanFromIreland

Copy link
Copy Markdown
Member Author

C calls the Python implementation.

Comment thread Lib/_strptime.py Outdated
Comment thread Lib/_strptime.py Outdated
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@StanFromIreland
StanFromIreland requested a review from picnixz March 9, 2025 12:27

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually wonder but what about languages for which their input has non-ASCII strings such as Japanese?

Comment thread Misc/NEWS.d/next/Library/2025-03-09-11-01-00.gh-issue-83461.auwd13.rst Outdated
Comment thread Lib/test/datetimetester.py Outdated

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the ASCII flag restriction is too broad here as it applies to all formats, not just the digit part.

Comment thread Lib/_strptime.py Outdated
@StanFromIreland
StanFromIreland requested a review from picnixz March 9, 2025 20:19
Comment thread Lib/_strptime.py Outdated
picnixz
picnixz previously requested changes Mar 9, 2025

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please carefully address the following suggestions and please make sure that typos lines are correctly formatted.

Comment thread Lib/_strptime.py
Comment thread Lib/_strptime.py Outdated
Comment thread Lib/test/datetimetester.py Outdated
Comment thread Doc/whatsnew/3.14.rst Outdated
Comment thread Doc/library/datetime.rst Outdated
Comment thread Doc/library/datetime.rst Outdated
@bedevere-app

bedevere-app Bot commented Mar 9, 2025

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@picnixz

picnixz commented Mar 9, 2025

Copy link
Copy Markdown
Member

Note: I'll review more this PR once I'm back because it will be easier when I'm on a laptop and not on mobile (so on Wednesday/Thursday)

@picnixz
picnixz self-requested a review March 9, 2025 20:57
Comment thread Lib/_strptime.py Outdated
@picnixz

picnixz commented Apr 1, 2025

Copy link
Copy Markdown
Member

Also, I realized that the summary of "reject non-ASCII digits" isn't quite right, because there are actually locales where codes like %c and %X will generate strings with non-ASCII digits, e.g.:

Oh I wasn't aware of this so thank you for the corrections.

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe in Lib/_striptime.py, you can add a small comment saying that the O* formats are locale-specific? (just above `for d in 'dmyHIMS'?

Comment thread Doc/library/time.rst Outdated
calculations when the day of the week and the year are specified.

(5)
The :func:`strptime` function does not accept non-ASCII digits for numeric values.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we mention the "non-locale-specific numeric format codes" here or, since it's not officially supported, we can be a bit lazy?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pganssle Do you want to do this too? Or should I?

@StanFromIreland

Copy link
Copy Markdown
Member Author

@pganssle friendly ping :-)

@pganssle pganssle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this looks good to me now. Since @serhiy-storchaka asked us to wait on it, though, I'll leave it up to him as to when we merge it or if other changes are necessary.

@StanFromIreland

Copy link
Copy Markdown
Member Author

Friendly ping @serhiy-storchaka

@StanFromIreland

Copy link
Copy Markdown
Member Author

Any update on this @serhiy-storchaka ?

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 22, 2026
@serhiy-storchaka

Copy link
Copy Markdown
Member

@StanFromIreland, could you please update this PR?

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jul 14, 2026
@read-the-docs-community

read-the-docs-community Bot commented Jul 20, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34154350 | 📁 Comparing 2f2bc87 against main (e0293b0)

  🔍 Preview build  

90 files changed · ± 90 modified

± Modified

@serhiy-storchaka

Copy link
Copy Markdown
Member

The new tests pass without the change: four of them raise ValueError for a different reason, even with ASCII digits.

>>> strptime('2025-02-29', '%Y-%m-%d')          # day 29 must be in range 1..28 for month 2 in year 2025
>>> strptime('2555', '%G')                      # ISO year directive '%G' must be used with the ISO week directive '%V' ...
>>> strptime('2/02 02a2', '%j/%y %I%p:%M:%S')   # time data does not match format
>>> strptime('02/2/2002', '%U/%V')              # unconverted data remains: /2002

Only '1٢:02:٢7' with %H:%M:%S and '٢5' with %y test what they are meant to test.

These parse with ASCII digits, so the non-ASCII variants of them fail for the right reason:

>>> strptime('2025-02-28', '%Y-%m-%d')
>>> strptime('2555 01 1', '%G %V %u')
>>> strptime('2/02 02AM', '%j/%y %I%p')
>>> strptime('02', '%U')

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.

4 participants