Skip to content

Commit 4924bcf

Browse files
gh-89157: Make C and Python implementation of datetime.date.fromisoformat consistent (#131007)
1 parent 92985e3 commit 4924bcf

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

Lib/_pydatetime.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,12 @@ def fromordinal(cls, n):
10501050
@classmethod
10511051
def fromisoformat(cls, date_string):
10521052
"""Construct a date from a string in ISO 8601 format."""
1053+
10531054
if not isinstance(date_string, str):
1054-
raise TypeError('fromisoformat: argument must be str')
1055+
raise TypeError('Argument must be a str')
1056+
1057+
if not date_string.isascii():
1058+
raise ValueError('Argument must be an ASCII str')
10551059

10561060
if len(date_string) not in (7, 8, 10):
10571061
raise ValueError(f'Invalid isoformat string: {date_string!r}')

Lib/test/datetimetester.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,7 @@ def test_fromisoformat_fails(self):
20872087
'10000-W25-1', # Invalid year
20882088
'2020-W25-0', # Invalid day-of-week
20892089
'2020-W25-8', # Invalid day-of-week
2090+
'٢025-03-09' # Unicode characters
20902091
'2009\ud80002\ud80028', # Separators are surrogate codepoints
20912092
]
20922093

@@ -3542,7 +3543,7 @@ def test_fromisoformat_fails_datetime(self):
35423543
'2009-04-19T03:15:4500:00', # Bad time zone separator
35433544
'2009-04-19T03:15:45.123456+24:30', # Invalid time zone offset
35443545
'2009-04-19T03:15:45.123456-24:30', # Invalid negative offset
3545-
'2009-04-10ᛇᛇᛇᛇᛇ12:15', # Too many unicode separators
3546+
'2009-04-10ᛇᛇᛇᛇᛇ12:15', # Unicode chars
35463547
'2009-04\ud80010T12:15', # Surrogate char in date
35473548
'2009-04-10T12\ud80015', # Surrogate char in time
35483549
'2009-04-19T1', # Incomplete hours
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make the pure Python implementation of :func:`datetime.date.fromisoformat`,
2+
only accept ASCII strings for consistency with the C implementation.

0 commit comments

Comments
 (0)