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

Skip to content

Commit cfa0f7c

Browse files
[3.11] gh-105375: Harden _datetime initialisation (GH-105604) (#105646)
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 3c08e54 commit cfa0f7c

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
@@ -6878,24 +6878,49 @@ _datetime_exec(PyObject *module)
68786878
assert(DI100Y == days_before_year(100+1));
68796879

68806880
us_per_ms = PyLong_FromLong(1000);
6881+
if (us_per_ms == NULL) {
6882+
goto error;
6883+
}
68816884
us_per_second = PyLong_FromLong(1000000);
6885+
if (us_per_second == NULL) {
6886+
goto error;
6887+
}
68826888
us_per_minute = PyLong_FromLong(60000000);
6889+
if (us_per_minute == NULL) {
6890+
goto error;
6891+
}
68836892
seconds_per_day = PyLong_FromLong(24 * 3600);
6884-
if (us_per_ms == NULL || us_per_second == NULL ||
6885-
us_per_minute == NULL || seconds_per_day == NULL) {
6886-
return -1;
6893+
if (seconds_per_day == NULL) {
6894+
goto error;
68876895
}
68886896

68896897
/* The rest are too big for 32-bit ints, but even
68906898
* us_per_week fits in 40 bits, so doubles should be exact.
68916899
*/
68926900
us_per_hour = PyLong_FromDouble(3600000000.0);
6901+
if (us_per_hour == NULL) {
6902+
goto error;
6903+
}
68936904
us_per_day = PyLong_FromDouble(86400000000.0);
6905+
if (us_per_day == NULL) {
6906+
goto error;
6907+
}
68946908
us_per_week = PyLong_FromDouble(604800000000.0);
6895-
if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) {
6896-
return -1;
6909+
if (us_per_week == NULL) {
6910+
goto error;
68976911
}
6912+
68986913
return 0;
6914+
6915+
error:
6916+
Py_XDECREF(us_per_ms);
6917+
Py_XDECREF(us_per_second);
6918+
Py_XDECREF(us_per_minute);
6919+
Py_XDECREF(us_per_hour);
6920+
Py_XDECREF(us_per_day);
6921+
Py_XDECREF(us_per_week);
6922+
Py_XDECREF(seconds_per_day);
6923+
return -1;
68996924
}
69006925

69016926
static struct PyModuleDef datetimemodule = {

0 commit comments

Comments
 (0)