Skip to content

Commit a94bdc2

Browse files
[3.12] pythongh-111295: Fix error checking in time extension module init (pythonGH-111296) (python#111300)
pythongh-111295: Fix error checking in time extension module init (pythonGH-111296) Introduce ADD_INT macro wrapper for PyModule_AddIntConstant() (cherry picked from commit 81b03e7) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent f108785 commit a94bdc2

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`time` not checking for errors when initializing.

Modules/timemodule.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,12 @@ get_gmtoff(time_t t, struct tm *p)
17371737
static int
17381738
init_timezone(PyObject *m)
17391739
{
1740+
#define ADD_INT(NAME, VALUE) do { \
1741+
if (PyModule_AddIntConstant(m, NAME, VALUE) < 0) { \
1742+
return -1; \
1743+
} \
1744+
} while (0)
1745+
17401746
assert(!PyErr_Occurred());
17411747

17421748
/* This code moved from PyInit_time wholesale to allow calling it from
@@ -1760,13 +1766,13 @@ init_timezone(PyObject *m)
17601766
#if !defined(MS_WINDOWS) || defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
17611767
tzset();
17621768
#endif
1763-
PyModule_AddIntConstant(m, "timezone", _Py_timezone);
1769+
ADD_INT("timezone", _Py_timezone);
17641770
#ifdef HAVE_ALTZONE
1765-
PyModule_AddIntConstant(m, "altzone", altzone);
1771+
ADD_INT("altzone", altzone);
17661772
#else
1767-
PyModule_AddIntConstant(m, "altzone", _Py_timezone-3600);
1773+
ADD_INT("altzone", _Py_timezone-3600);
17681774
#endif
1769-
PyModule_AddIntConstant(m, "daylight", _Py_daylight);
1775+
ADD_INT("daylight", _Py_daylight);
17701776
#ifdef MS_WINDOWS
17711777
TIME_ZONE_INFORMATION tzinfo = {0};
17721778
GetTimeZoneInformation(&tzinfo);
@@ -1825,20 +1831,21 @@ init_timezone(PyObject *m)
18251831
PyObject *tzname_obj;
18261832
if (janzone < julyzone) {
18271833
/* DST is reversed in the southern hemisphere */
1828-
PyModule_AddIntConstant(m, "timezone", julyzone);
1829-
PyModule_AddIntConstant(m, "altzone", janzone);
1830-
PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
1834+
ADD_INT("timezone", julyzone);
1835+
ADD_INT("altzone", janzone);
1836+
ADD_INT("daylight", janzone != julyzone);
18311837
tzname_obj = Py_BuildValue("(zz)", julyname, janname);
18321838
} else {
1833-
PyModule_AddIntConstant(m, "timezone", janzone);
1834-
PyModule_AddIntConstant(m, "altzone", julyzone);
1835-
PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
1839+
ADD_INT("timezone", janzone);
1840+
ADD_INT("altzone", julyzone);
1841+
ADD_INT("daylight", janzone != julyzone);
18361842
tzname_obj = Py_BuildValue("(zz)", janname, julyname);
18371843
}
18381844
if (_PyModule_Add(m, "tzname", tzname_obj) < 0) {
18391845
return -1;
18401846
}
18411847
#endif // !HAVE_DECL_TZNAME
1848+
#undef ADD_INT
18421849

18431850
if (PyErr_Occurred()) {
18441851
return -1;

0 commit comments

Comments
 (0)