gh-127260: Improve error consistency in both `fromisoformat` implemen… · python/cpython@427dd10 · GitHub
Skip to content

Commit 427dd10

Browse files
authored
gh-127260: Improve error consistency in both fromisoformat implementations (#130134)
In the Python implementation, "Z" was allowed where only "+" or "-" should be allowed in time zone specifiers. In the C implementation, ":" was allowed as a separator between the whole and fractional portion of times (seconds). These have both been forbidden and the error messages harmonized.
1 parent 46ac85e commit 427dd10

4 files changed

Lines changed: 14 additions & 2 deletions

File tree

Lib/_pydatetime.py

Lines changed: 2 additions & 2 deletions

Lib/test/datetimetester.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3533,6 +3533,8 @@ def test_fromisoformat_fails_datetime(self):
35333533
'2009-04-32T24:00:00.000000', # Day is invalid before wrapping due to 24:00
35343534
'2009-13-01T24:00:00.000000', # Month is invalid before wrapping due to 24:00
35353535
'9999-12-31T24:00:00.000000', # Year is invalid after wrapping due to 24:00
3536+
'2009-04-19T12:30Z12:00', # Extra time zone info after Z
3537+
'2009-04-19T12:30:45:334034', # Invalid microsecond separator
35363538
]
35373539

35383540
for bad_str in bad_strs:
@@ -4658,6 +4660,7 @@ def test_fromisoformat_time_examples(self):
46584660
('00:00:00.000', self.theclass(0, 0)),
46594661
('000000.000000', self.theclass(0, 0)),
46604662
('00:00:00.000000', self.theclass(0, 0)),
4663+
('00:00:00,100000', self.theclass(0, 0, 0, 100000)),
46614664
('1200', self.theclass(12, 0)),
46624665
('12:00', self.theclass(12, 0)),
46634666
('120000', self.theclass(12, 0)),
@@ -4725,6 +4728,8 @@ def test_fromisoformat_fails(self):
47254728
'12:30:45.123456+12:00:30a', # Extra at end of full time
47264729
'12.5', # Decimal mark at end of hour
47274730
'12:30,5', # Decimal mark at end of minute
4731+
'12:30:45.123456Z12:00', # Extra time zone info after Z
4732+
'12:30:45:334034', # Invalid microsecond separator
47284733
]
47294734

47304735
for bad_str in bad_strs:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Forbid the use of colon (":") as a fractional component separator and other
2+
improvements to the consistency of error raising between the C and Python
3+
implementaitons of :meth:`datetime.time.fromisoformat` and
4+
:meth:`datetime.datetime.fromisoformat`. Patch by Semyon Moroz.

Modules/_datetimemodule.c

Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)