Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
StanFromIreland wants to merge23 commits intopython:main
base:main
Choose a base branch
Loading
fromStanFromIreland:ascii-strptime
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
23 commits
Select commitHold shift + click to select a range
c20c6da
Initial
StanFromIrelandMar 9, 2025
decd90f
Fix NEWS
StanFromIrelandMar 9, 2025
81276eb
sggestion
StanFromIrelandMar 9, 2025
0a3e67b
Alphabetical order…
StanFromIrelandMar 9, 2025
5d1d53d
Benedikts suggestions
StanFromIrelandMar 9, 2025
a0b0f07
Add docs and tests for time.strptime
StanFromIrelandMar 9, 2025
d0e6a1f
Cover the majority
StanFromIrelandMar 10, 2025
2f6d8e2
Cover all
StanFromIrelandMar 10, 2025
ccabadf
Clean up
StanFromIrelandMar 10, 2025
35bc090
Benedikt's Doc suggestions
StanFromIrelandMar 12, 2025
0637c29
Apply suggestions from code review
StanFromIrelandMar 15, 2025
71d7c8a
Re-word news
StanFromIrelandMar 15, 2025
212c763
Apply suggestions from code review
StanFromIrelandMar 15, 2025
0201347
PEP 8
StanFromIrelandMar 15, 2025
bebb241
REQUESTED CHANGES
StanFromIrelandMar 23, 2025
40d2368
Refactor documentation changes
pganssleMar 26, 2025
77c936a
Clarify non-ASCII parsing in news
pganssleMar 26, 2025
e652581
fixup! Refactor documentation changes
pganssleMar 26, 2025
0115ee4
Unrequire ascii
StanFromIrelandMar 26, 2025
25a6705
Merge branch 'main' into ascii-strptime
StanFromIrelandApr 24, 2025
3bca8e9
Merge branch 'main' into ascii-strptime
pganssleMay 19, 2025
ddd1d01
Adjust documentation to be specific about where non-ASCII digits are …
pganssleMay 19, 2025
0139e57
Move whatsnew
StanFromIrelandMay 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Add docs and tests for time.strptime
  • Loading branch information
@StanFromIreland
StanFromIreland committedMar 9, 2025
commita0b0f07b2a2e20a1252da4513c52a5792d94e55d
5 changes: 3 additions & 2 deletionsDoc/library/datetime.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2432,7 +2432,8 @@ Class attributes:

:class:`date`, :class:`.datetime`, and :class:`.time` objects all support a
``strftime(format)`` method, to create a string representing the time under the
control of an explicit format string. The methods only accept ASCII digits.
control of an explicit format string. A :exc:`ValueError` will be raised if digits
are not ASCII.

Conversely, the :meth:`date.strptime`, :meth:`datetime.strptime` and
:meth:`time.strptime` class methods create an object from a string
Expand DownExpand Up@@ -2612,7 +2613,7 @@ differences between platforms in handling of unsupported format specifiers.
``%:z`` was added.

.. versionchanged:: next
Digits must be ASCII
Non-ASCII digits are now rejected by ``strptime`` for numerical directives.

Technical Detail
^^^^^^^^^^^^^^^^
Expand Down
10 changes: 7 additions & 3 deletionsDoc/library/time.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -594,9 +594,10 @@ Functions
:func:`strftime`; it defaults to ``"%a %b %d %H:%M:%S %Y"`` which matches the
formatting returned by :func:`ctime`. If *string* cannot be parsed according
to *format*, or if it has excess data after parsing, :exc:`ValueError` is
raised. The default values used to fill in any missing data when more
accurate values cannot be inferred are ``(1900, 1, 1, 0, 0, 0, 0, 1, -1)``.
Both *string* and *format* must be strings.
raised. :exc:`ValueError` is raised if digits are not ASCII. The default
values used to fill in any missing data when more accurate values cannot be
inferred are ``(1900, 1, 1, 0, 0, 0, 0, 1, -1)``. Both *string* and *format*
must be strings.

For example:

Expand All@@ -616,6 +617,9 @@ Functions
and thus does not necessarily support all directives available that are not
documented as supported.

.. versionchanged:: next
Non-ASCII digits are now rejected by ``strptime``.


.. class:: struct_time

Expand Down
17 changes: 13 additions & 4 deletionsDoc/whatsnew/3.14.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -504,10 +504,10 @@ datetime
* Add :meth:`datetime.time.strptime` and :meth:`datetime.date.strptime`.
(Contributed by Wannes Boeykens in :gh:`41431`.)

* The``strptime(format)`` methods: :meth:`datetime.date.strptime`,
:meth:`datetime.datetime.strptime`and :meth:`datetime.time.strptime`
only acceptASCII digits.
(Contributed by Stan Ulbrych in :gh:`131008`
* The:meth:`datetime.date.strptime`, :meth:`datetime.datetime.strptime` and
:meth:`datetime.time.strptime`methods now only accept ASCII digits, will and
raise a :exc:`ValueError` if non-ASCII digits are specified.
(Contributed by Stan Ulbrych in :gh:`131008`.)

decimal
-------
Expand DownExpand Up@@ -883,6 +883,15 @@ sys.monitoring
* Two new events are added: :monitoring-event:`BRANCH_LEFT` and
:monitoring-event:`BRANCH_RIGHT`. The ``BRANCH`` event is deprecated.


time
----

* The :meth:`time.strptime`, now only accept ASCII digits, and will raise a
:exc:`ValueError` if non-ASCII digits are specified.
(Contributed by Stan Ulbrych in :gh:`131008`.)


threading
---------

Expand Down
8 changes: 4 additions & 4 deletionsLib/_strptime.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@
import calendar
from re import compile as re_compile
from re import sub as re_sub
from re importASCII,IGNORECASE
from re import IGNORECASE
from re import escape as re_escape
from datetime import (date as datetime_date,
timedelta as datetime_timedelta,
Expand DownExpand Up@@ -290,7 +290,7 @@ def __init__(self, locale_time=None):
'f': r"(?P<f>[0-9]{1,6})",
'H': r"(?P<H>2[0-3]|[0-1][0-9]|[0-9])",
'I': r"(?P<I>1[0-2]|0[1-9]|[1-9]| [1-9])",
'G': r"(?P<G>[0-9][0-9][0-9][0-9])",
'G': r"(?P<G>[0-9]{4})",
'j': r"(?P<j>36[0-6]|3[0-5][0-9]|[1-2][0-9][0-9]|0[1-9][0-9]|00[1-9]|[1-9][0-9]|0[1-9]|[1-9])",
'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])",
'M': r"(?P<M>[0-5][0-9]|[0-9])",
Expand All@@ -300,8 +300,8 @@ def __init__(self, locale_time=None):
'u': r"(?P<u>[1-7])",
'V': r"(?P<V>5[0-3]|0[1-9]|[1-4][0-9]|[0-9])",
# W is set below by using 'U'
'y': r"(?P<y>[0-9][0-9])",
'Y': r"(?P<Y>[0-9][0-9][0-9][0-9])",
'y': r"(?P<y>[0-9]{2})",
'Y': r"(?P<Y>[0-9]{4})",
'z': r"(?P<z>[+-][0-9][0-9]:?[0-5][0-9](:?[0-5][0-9](\.[0-9]{1,6})?)?|(?-i:Z))",
'A': self.__seqToRE(self.locale_time.f_weekday, 'A'),
'a': self.__seqToRE(self.locale_time.a_weekday, 'a'),
Expand Down
4 changes: 4 additions & 0 deletionsLib/test/test_time.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -352,6 +352,10 @@ def test_strptime_leap_year(self):
r'.*day of month without a year.*'):
time.strptime('02-07 18:28', '%m-%d %H:%M')

def test_strptime_non_ascii(self):
with self.assertRaises(ValueError):
time.strptime('٢025', '%Y')

def test_asctime(self):
time.asctime(time.gmtime(self.t))

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp