Skip to content

Commit bd409bb

Browse files
author
Paulo Henrique Silva
authored
bpo-1635741: Port time module to multiphase initialization (PEP 489) (GH-19107)
1 parent 9a81ab1 commit bd409bb

File tree

2 files changed

+54
-51
lines changed

2 files changed

+54
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Port time module to multiphase initialization (:pep:`489`).
2+
Patch by Paulo Henrique Silva.

‎Modules/timemodule.c

+52-51
Original file line numberDiff line numberDiff line change
@@ -1736,82 +1736,64 @@ if it is 1, the time is given in the DST time zone;\n\
17361736
if it is -1, mktime() should guess based on the date and time.\n");
17371737

17381738

1739-
1740-
static struct PyModuleDef timemodule = {
1741-
PyModuleDef_HEAD_INIT,
1742-
"time",
1743-
module_doc,
1744-
-1,
1745-
time_methods,
1746-
NULL,
1747-
NULL,
1748-
NULL,
1749-
NULL
1750-
};
1751-
1752-
PyMODINIT_FUNC
1753-
PyInit_time(void)
1739+
static int
1740+
time_exec(PyObject *module)
17541741
{
1755-
PyObject *m;
1756-
m = PyModule_Create(&timemodule);
1757-
if (m == NULL)
1758-
return NULL;
1759-
17601742
/* Set, or reset, module variables like time.timezone */
1761-
if (init_timezone(m) < 0) {
1762-
goto error;
1743+
if (init_timezone(module) < 0) {
1744+
return -1;
17631745
}
17641746

17651747
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_SETTIME) || defined(HAVE_CLOCK_GETRES)
17661748

17671749
#ifdef CLOCK_REALTIME
1768-
if (PyModule_AddIntMacro(m, CLOCK_REALTIME) < 0) {
1769-
goto error;
1750+
if (PyModule_AddIntMacro(module, CLOCK_REALTIME) < 0) {
1751+
return -1;
17701752
}
17711753
#endif
17721754
#ifdef CLOCK_MONOTONIC
1773-
if (PyModule_AddIntMacro(m, CLOCK_MONOTONIC) < 0) {
1774-
goto error;
1755+
if (PyModule_AddIntMacro(module, CLOCK_MONOTONIC) < 0) {
1756+
return -1;
17751757
}
17761758
#endif
17771759
#ifdef CLOCK_MONOTONIC_RAW
1778-
if (PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW) < 0) {
1779-
goto error;
1760+
if (PyModule_AddIntMacro(module, CLOCK_MONOTONIC_RAW) < 0) {
1761+
return -1;
17801762
}
17811763
#endif
17821764
#ifdef CLOCK_HIGHRES
1783-
if (PyModule_AddIntMacro(m, CLOCK_HIGHRES) < 0) {
1784-
goto error;
1765+
if (PyModule_AddIntMacro(module, CLOCK_HIGHRES) < 0) {
1766+
return -1;
17851767
}
17861768
#endif
17871769
#ifdef CLOCK_PROCESS_CPUTIME_ID
1788-
if (PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID) < 0) {
1789-
goto error;
1770+
if (PyModule_AddIntMacro(module, CLOCK_PROCESS_CPUTIME_ID) < 0) {
1771+
return -1;
17901772
}
17911773
#endif
17921774
#ifdef CLOCK_THREAD_CPUTIME_ID
1793-
if (PyModule_AddIntMacro(m, CLOCK_THREAD_CPUTIME_ID) < 0) {
1794-
goto error;
1775+
if (PyModule_AddIntMacro(module, CLOCK_THREAD_CPUTIME_ID) < 0) {
1776+
return -1;
17951777
}
17961778
#endif
17971779
#ifdef CLOCK_PROF
1798-
if (PyModule_AddIntMacro(m, CLOCK_PROF) < 0) {
1799-
goto error;
1780+
if (PyModule_AddIntMacro(module, CLOCK_PROF) < 0) {
1781+
return -1;
18001782
}
18011783
#endif
18021784
#ifdef CLOCK_BOOTTIME
1803-
if (PyModule_AddIntMacro(m, CLOCK_BOOTTIME) < 0) {
1804-
goto error;
1785+
if (PyModule_AddIntMacro(module, CLOCK_BOOTTIME) < 0) {
1786+
return -1;
18051787
}
18061788
#endif
18071789
#ifdef CLOCK_UPTIME
1808-
if (PyModule_AddIntMacro(m, CLOCK_UPTIME) < 0) {
1809-
goto error;
1790+
if (PyModule_AddIntMacro(module, CLOCK_UPTIME) < 0) {
1791+
return -1;
18101792
}
18111793
#endif
18121794
#ifdef CLOCK_UPTIME_RAW
1813-
if (PyModule_AddIntMacro(m, CLOCK_UPTIME_RAW) < 0) {
1814-
goto error;
1795+
if (PyModule_AddIntMacro(module, CLOCK_UPTIME_RAW) < 0) {
1796+
return -1;
18151797
}
18161798
#endif
18171799

@@ -1820,16 +1802,16 @@ PyInit_time(void)
18201802
if (!initialized) {
18211803
if (PyStructSequence_InitType2(&StructTimeType,
18221804
&struct_time_type_desc) < 0) {
1823-
goto error;
1805+
return -1;
18241806
}
18251807
}
1826-
if (PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 11)) {
1827-
goto error;
1808+
if (PyModule_AddIntConstant(module, "_STRUCT_TM_ITEMS", 11)) {
1809+
return -1;
18281810
}
18291811
Py_INCREF(&StructTimeType);
1830-
if (PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType)) {
1812+
if (PyModule_AddObject(module, "struct_time", (PyObject*) &StructTimeType)) {
18311813
Py_DECREF(&StructTimeType);
1832-
goto error;
1814+
return -1;
18331815
}
18341816
initialized = 1;
18351817

@@ -1840,11 +1822,30 @@ PyInit_time(void)
18401822
utc_string = tm.tm_zone;
18411823
#endif
18421824

1843-
return m;
1825+
return 0;
1826+
}
18441827

1845-
error:
1846-
Py_DECREF(m);
1847-
return NULL;
1828+
static struct PyModuleDef_Slot time_slots[] = {
1829+
{Py_mod_exec, time_exec},
1830+
{0, NULL}
1831+
};
1832+
1833+
static struct PyModuleDef timemodule = {
1834+
PyModuleDef_HEAD_INIT,
1835+
"time",
1836+
module_doc,
1837+
0,
1838+
time_methods,
1839+
time_slots,
1840+
NULL,
1841+
NULL,
1842+
NULL
1843+
};
1844+
1845+
PyMODINIT_FUNC
1846+
PyInit_time(void)
1847+
{
1848+
return PyModuleDef_Init(&timemodule);
18481849
}
18491850

18501851
/* Implement pysleep() for various platforms.

0 commit comments

Comments
 (0)