]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-151770: Fix `datetime.fromisoformat()` on an out-of-range month w/ a 24...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 20 Jun 2026 16:59:20 +0000 (18:59 +0200)
committerGitHub <noreply@github.com>
Sat, 20 Jun 2026 16:59:20 +0000 (16:59 +0000)
(cherry picked from commit 1fb874cc076e771c39a7bbc650dce386e3c5b7a0)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
Lib/_pydatetime.py
Lib/test/datetimetester.py
Misc/NEWS.d/next/Library/2026-06-20-15-00-00.gh-issue-151770.dtiso0.rst [new file with mode: 0644]
Modules/_datetimemodule.c

index 70251dbb6535d26c64496e82e19a3eb40d0ff22b..a353fa6dc17a535f29c457c04824b6968f7a262a 100644 (file)
@@ -55,7 +55,7 @@ def _days_before_year(year):
 
 def _days_in_month(year, month):
     "year, month -> number of days in that month in that year."
-    assert 1 <= month <= 12, month
+    assert 1 <= month <= 12, f"month must be in 1..12, not {month}"
     if month == 2 and _is_leap(year):
         return 29
     return _DAYS_IN_MONTH[month]
@@ -1947,7 +1947,7 @@ class datetime(date):
                 if became_next_day:
                     year, month, day = date_components
                     # Only wrap day/month when it was previously valid
-                    if month <= 12 and day <= (days_in_month := _days_in_month(year, month)):
+                    if 1 <= month <= 12 and day <= (days_in_month := _days_in_month(year, month)):
                         # Calculate midnight of the next day
                         day += 1
                         if day > days_in_month:
index c1bb6138a1beb7b221f166a0fb42d6adcecd0d36..80891903a5e53beb2673b9d73bbe45c708d15a0a 100644 (file)
@@ -3597,6 +3597,7 @@ class TestDateTime(TestDate):
             "2009-04-01T12:30:90",          # Second out of range
             "2009-04-01T12:90:45",          # Minute out of range
             "2009-04-01T25:30:45",          # Hour out of range
+            "2009-00-01T24:00:00",          # Month below range
             "2009-13-01T24:00:00",          # Month out of range
             "9999-12-31T24:00:00",          # Year out of range
         ]
diff --git a/Misc/NEWS.d/next/Library/2026-06-20-15-00-00.gh-issue-151770.dtiso0.rst b/Misc/NEWS.d/next/Library/2026-06-20-15-00-00.gh-issue-151770.dtiso0.rst
new file mode 100644 (file)
index 0000000..10b3db8
--- /dev/null
@@ -0,0 +1,3 @@
+Fix :meth:`datetime.datetime.fromisoformat` raising :exc:`AssertionError`
+instead of :exc:`ValueError` for an out-of-range month combined with a
+``24:00`` time.
index b3b76acc97ee0a9c59b6824dbdb7f653dc249335..b4a93819b242804bbcb3d12027c3a5b8d8720129 100644 (file)
@@ -5981,7 +5981,7 @@ datetime_fromisoformat(PyObject *cls, PyObject *dtstr)
         goto error;
     }
 
-    if ((hour == 24) && (month <= 12))  {
+    if ((hour == 24) && (month >= 1 && month <= 12))  {
         int d_in_month = days_in_month(year, month);
         if (day <= d_in_month) {
             if (minute == 0 && second == 0 && microsecond == 0) {