Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36ecbc3

Browse files
[3.12] gh-105375: Harden _datetime initialisation (GH-105604) (#105645)
Improve error handling so init bails on the first exception. (cherry picked from commit 16d4968) Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 85a1a09 commit 36ecbc3

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bugs in :mod:`!_datetime` where exceptions could be overwritten in case
2+
of module initialisation failure.

Modules/_datetimemodule.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6871,24 +6871,49 @@ _datetime_exec(PyObject *module)
68716871
assert(DI100Y == days_before_year(100+1));
68726872

68736873
us_per_ms = PyLong_FromLong(1000);
6874+
if (us_per_ms == NULL) {
6875+
goto error;
6876+
}
68746877
us_per_second = PyLong_FromLong(1000000);
6878+
if (us_per_second == NULL) {
6879+
goto error;
6880+
}
68756881
us_per_minute = PyLong_FromLong(60000000);
6882+
if (us_per_minute == NULL) {
6883+
goto error;
6884+
}
68766885
seconds_per_day = PyLong_FromLong(24 * 3600);
6877-
if (us_per_ms == NULL || us_per_second == NULL ||
6878-
us_per_minute == NULL || seconds_per_day == NULL) {
6879-
return -1;
6886+
if (seconds_per_day == NULL) {
6887+
goto error;
68806888
}
68816889

68826890
/* The rest are too big for 32-bit ints, but even
68836891
* us_per_week fits in 40 bits, so doubles should be exact.
68846892
*/
68856893
us_per_hour = PyLong_FromDouble(3600000000.0);
6894+
if (us_per_hour == NULL) {
6895+
goto error;
6896+
}
68866897
us_per_day = PyLong_FromDouble(86400000000.0);
6898+
if (us_per_day == NULL) {
6899+
goto error;
6900+
}
68876901
us_per_week = PyLong_FromDouble(604800000000.0);
6888-
if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) {
6889-
return -1;
6902+
if (us_per_week == NULL) {
6903+
goto error;
68906904
}
6905+
68916906
return 0;
6907+
6908+
error:
6909+
Py_XDECREF(us_per_ms);
6910+
Py_XDECREF(us_per_second);
6911+
Py_XDECREF(us_per_minute);
6912+
Py_XDECREF(us_per_hour);
6913+
Py_XDECREF(us_per_day);
6914+
Py_XDECREF(us_per_week);
6915+
Py_XDECREF(seconds_per_day);
6916+
return -1;
68926917
}
68936918

68946919
static struct PyModuleDef datetimemodule = {

0 commit comments

Comments
 (0)