From 9dbe91090a92b373e78b30c15f15a5f3f3a0acd8 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Fri, 14 Feb 2025 23:38:16 +0400 Subject: [PATCH 1/4] Make more consistent to raising errors of `fromisoformat` implementations --- Lib/_pydatetime.py | 8 ++++---- Lib/test/datetimetester.py | 4 ++++ .../2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index 600a7e08345a82..80035b2f7be8b9 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -404,7 +404,7 @@ def _parse_isoformat_date(dtstr): def _parse_hh_mm_ss_ff(tstr): - # Parses things of the form HH[:?MM[:?SS[{.,}fff[fff]]]] + # Parses things of the form HH[:?MM[:?SS[{.,:}fff[fff]]]] len_str = len(tstr) time_comps = [0, 0, 0, 0] @@ -430,8 +430,8 @@ def _parse_hh_mm_ss_ff(tstr): pos += has_sep if pos < len_str: - if tstr[pos] not in '.,': - raise ValueError("Invalid microsecond component") + if tstr[pos] not in '.,:': + raise ValueError("Invalid microsecond separator") else: pos += 1 @@ -489,7 +489,7 @@ def _parse_isoformat_time(tstr): # HH:MM:SS len: 8 # HH:MM:SS.f+ len: 10+ - if len(tzstr) in (0, 1, 3): + if len(tzstr) in (0, 1, 3) or tstr[tz_pos-1] == 'Z': raise ValueError("Malformed time zone string") tz_comps = _parse_hh_mm_ss_ff(tzstr) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index d1ef6339789d20..25849028b78574 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -3533,6 +3533,7 @@ def test_fromisoformat_fails_datetime(self): '2009-04-32T24:00:00.000000', # Day is invalid before wrapping due to 24:00 '2009-13-01T24:00:00.000000', # Month is invalid before wrapping due to 24:00 '9999-12-31T24:00:00.000000', # Year is invalid after wrapping due to 24:00 + '2009-04-19T12:30Z12:00', # Extra time zone info after Z ] for bad_str in bad_strs: @@ -4658,6 +4659,8 @@ def test_fromisoformat_time_examples(self): ('00:00:00.000', self.theclass(0, 0)), ('000000.000000', self.theclass(0, 0)), ('00:00:00.000000', self.theclass(0, 0)), + ('00:00:00,100000', self.theclass(0, 0, 0, 100000)), + ('00:00:00:100000', self.theclass(0, 0, 0, 100000)), ('1200', self.theclass(12, 0)), ('12:00', self.theclass(12, 0)), ('120000', self.theclass(12, 0)), @@ -4725,6 +4728,7 @@ def test_fromisoformat_fails(self): '12:30:45.123456+12:00:30a', # Extra at end of full time '12.5', # Decimal mark at end of hour '12:30,5', # Decimal mark at end of minute + '12:30:45.123456Z12:00', # Extra time zone info after Z ] for bad_str in bad_strs: diff --git a/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst b/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst new file mode 100644 index 00000000000000..9b6972ba6cb18d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst @@ -0,0 +1,3 @@ +Make the occurrence of :meth:`datetime.time.fromisoformat` and +:meth:`datetime.datetime.fromisoformat` methods errors more consistent in +both implementations. Patch by Semyon Moroz. From 9e75bfe75133305f9f8d036b8f6462ba3b7bd94c Mon Sep 17 00:00:00 2001 From: donBarbos Date: Tue, 18 Feb 2025 17:57:31 +0400 Subject: [PATCH 2/4] Disallow `:` as microsecond separator for both implementations --- Lib/_pydatetime.py | 4 ++-- Lib/test/datetimetester.py | 3 ++- Modules/_datetimemodule.c | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index 80035b2f7be8b9..ed8a5cdfaf1cde 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -404,7 +404,7 @@ def _parse_isoformat_date(dtstr): def _parse_hh_mm_ss_ff(tstr): - # Parses things of the form HH[:?MM[:?SS[{.,:}fff[fff]]]] + # Parses things of the form HH[:?MM[:?SS[{.,}fff[fff]]]] len_str = len(tstr) time_comps = [0, 0, 0, 0] @@ -430,7 +430,7 @@ def _parse_hh_mm_ss_ff(tstr): pos += has_sep if pos < len_str: - if tstr[pos] not in '.,:': + if tstr[pos] not in '.,': raise ValueError("Invalid microsecond separator") else: pos += 1 diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 25849028b78574..b670973a71c748 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -3534,6 +3534,7 @@ def test_fromisoformat_fails_datetime(self): '2009-13-01T24:00:00.000000', # Month is invalid before wrapping due to 24:00 '9999-12-31T24:00:00.000000', # Year is invalid after wrapping due to 24:00 '2009-04-19T12:30Z12:00', # Extra time zone info after Z + '2009-04-19T12:30:45:334034', # Invalid microsecond separator ] for bad_str in bad_strs: @@ -4660,7 +4661,6 @@ def test_fromisoformat_time_examples(self): ('000000.000000', self.theclass(0, 0)), ('00:00:00.000000', self.theclass(0, 0)), ('00:00:00,100000', self.theclass(0, 0, 0, 100000)), - ('00:00:00:100000', self.theclass(0, 0, 0, 100000)), ('1200', self.theclass(12, 0)), ('12:00', self.theclass(12, 0)), ('120000', self.theclass(12, 0)), @@ -4729,6 +4729,7 @@ def test_fromisoformat_fails(self): '12.5', # Decimal mark at end of hour '12:30,5', # Decimal mark at end of minute '12:30:45.123456Z12:00', # Extra time zone info after Z + '12:30:45:334034', # Invalid microsecond separator ] for bad_str in bad_strs: diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 8b202cc51788a9..b5ccf2f7b65d63 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -1014,6 +1014,9 @@ parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end, int *hour, return c != '\0'; } else if (has_separator && (c == ':')) { + if (i == 2) { + return -4; // Malformed microsecond separator + } continue; } else if (c == '.' || c == ',') { From f75530fa216a1ff9011822a37a139dd57a174611 Mon Sep 17 00:00:00 2001 From: Paul Ganssle <1377457+pganssle@users.noreply.github.com> Date: Tue, 18 Feb 2025 09:50:48 -0500 Subject: [PATCH 3/4] Update 2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst Emphasize the change in strictness in the C implementation. --- .../Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst b/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst index 9b6972ba6cb18d..e4653f249be6c4 100644 --- a/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst +++ b/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst @@ -1,3 +1,4 @@ -Make the occurrence of :meth:`datetime.time.fromisoformat` and -:meth:`datetime.datetime.fromisoformat` methods errors more consistent in -both implementations. Patch by Semyon Moroz. +Forbid the use of colon ("`:`") as a fractional component separator and other +improvements to the consistency of error raising between the C and Python +implementaitons of :meth:`datetime.time.fromisoformat` and +:meth:`datetime.datetime.fromisoformat`. Patch by Semyon Moroz. From 7f4827aaa7082ae76092b0f69ce517340edf40dd Mon Sep 17 00:00:00 2001 From: Paul Ganssle <1377457+pganssle@users.noreply.github.com> Date: Tue, 18 Feb 2025 09:57:02 -0500 Subject: [PATCH 4/4] Update 2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst Fix lint --- .../next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst b/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst index e4653f249be6c4..453aa4a8678fe4 100644 --- a/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst +++ b/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst @@ -1,4 +1,4 @@ -Forbid the use of colon ("`:`") as a fractional component separator and other +Forbid the use of colon (":") as a fractional component separator and other improvements to the consistency of error raising between the C and Python implementaitons of :meth:`datetime.time.fromisoformat` and :meth:`datetime.datetime.fromisoformat`. Patch by Semyon Moroz.