From c0c332d17426eab2c508e6ac379256e02b7eee53 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith [Google LLC]" Date: Fri, 1 Mar 2024 08:35:39 +0000 Subject: [PATCH] gh-70647: Add detail to error message when parsing a bad day of month. The presence of the values in the error message gives a stronger hint as to what went wrong. ``` >>> datetime.strptime("2.29", "%m.%d") Traceback (most recent call last): File "", line 1, in datetime.strptime("2.29", "%m.%d") ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ File ".../Lib/_strptime.py", line 565, in _strptime_datetime return cls(*args) ~~~^^^^^^^ ValueError: day 29 is out of range for month 2 in year 1900 ``` --- Modules/_datetimemodule.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index a626bda2ea9be9..950a80b0442a81 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -481,8 +481,9 @@ check_date_args(int year, int month, int day) return -1; } if (day < 1 || day > days_in_month(year, month)) { - PyErr_SetString(PyExc_ValueError, - "day is out of range for month"); + PyErr_Format(PyExc_ValueError, + "day %i is out of range for month %i in year %i", + day, month, year); return -1; } return 0;