diff options
author | Paulo Henrique Silva <ph.silva@carta.com> | 2020-03-23 18:58:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-23 18:58:23 (GMT) |
commit | bd409bb5b78e7ccac5fcda9ab4cec770552f3090 (patch) | |
tree | afc02a2e7f1a89360a6dc4b143d53b4bbd577ff6 /Modules/timemodule.c | |
parent | 9a81ab107a54b8ca320fb703f7c68e14ccd9d016 (diff) | |
download | cpython-bd409bb5b78e7ccac5fcda9ab4cec770552f3090.zip cpython-bd409bb5b78e7ccac5fcda9ab4cec770552f3090.tar.gz cpython-bd409bb5b78e7ccac5fcda9ab4cec770552f3090.tar.bz2 |
bpo-1635741: Port time module to multiphase initialization (PEP 489) (GH-19107)
Diffstat (limited to 'Modules/timemodule.c')
-rw-r--r-- | Modules/timemodule.c | 103 |
1 files changed, 52 insertions, 51 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c index f4b3f69..f458966 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -1736,82 +1736,64 @@ if it is 1, the time is given in the DST time zone;\n\ if it is -1, mktime() should guess based on the date and time.\n"); - -static struct PyModuleDef timemodule = { - PyModuleDef_HEAD_INIT, - "time", - module_doc, - -1, - time_methods, - NULL, - NULL, - NULL, - NULL -}; - -PyMODINIT_FUNC -PyInit_time(void) +static int +time_exec(PyObject *module) { - PyObject *m; - m = PyModule_Create(&timemodule); - if (m == NULL) - return NULL; - /* Set, or reset, module variables like time.timezone */ - if (init_timezone(m) < 0) { - goto error; + if (init_timezone(module) < 0) { + return -1; } #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_SETTIME) || defined(HAVE_CLOCK_GETRES) #ifdef CLOCK_REALTIME - if (PyModule_AddIntMacro(m, CLOCK_REALTIME) < 0) { - goto error; + if (PyModule_AddIntMacro(module, CLOCK_REALTIME) < 0) { + return -1; } #endif #ifdef CLOCK_MONOTONIC - if (PyModule_AddIntMacro(m, CLOCK_MONOTONIC) < 0) { - goto error; + if (PyModule_AddIntMacro(module, CLOCK_MONOTONIC) < 0) { + return -1; } #endif #ifdef CLOCK_MONOTONIC_RAW - if (PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW) < 0) { - goto error; + if (PyModule_AddIntMacro(module, CLOCK_MONOTONIC_RAW) < 0) { + return -1; } #endif #ifdef CLOCK_HIGHRES - if (PyModule_AddIntMacro(m, CLOCK_HIGHRES) < 0) { - goto error; + if (PyModule_AddIntMacro(module, CLOCK_HIGHRES) < 0) { + return -1; } #endif #ifdef CLOCK_PROCESS_CPUTIME_ID - if (PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID) < 0) { - goto error; + if (PyModule_AddIntMacro(module, CLOCK_PROCESS_CPUTIME_ID) < 0) { + return -1; } #endif #ifdef CLOCK_THREAD_CPUTIME_ID - if (PyModule_AddIntMacro(m, CLOCK_THREAD_CPUTIME_ID) < 0) { - goto error; + if (PyModule_AddIntMacro(module, CLOCK_THREAD_CPUTIME_ID) < 0) { + return -1; } #endif #ifdef CLOCK_PROF - if (PyModule_AddIntMacro(m, CLOCK_PROF) < 0) { - goto error; + if (PyModule_AddIntMacro(module, CLOCK_PROF) < 0) { + return -1; } #endif #ifdef CLOCK_BOOTTIME - if (PyModule_AddIntMacro(m, CLOCK_BOOTTIME) < 0) { - goto error; + if (PyModule_AddIntMacro(module, CLOCK_BOOTTIME) < 0) { + return -1; } #endif #ifdef CLOCK_UPTIME - if (PyModule_AddIntMacro(m, CLOCK_UPTIME) < 0) { - goto error; + if (PyModule_AddIntMacro(module, CLOCK_UPTIME) < 0) { + return -1; } #endif #ifdef CLOCK_UPTIME_RAW - if (PyModule_AddIntMacro(m, CLOCK_UPTIME_RAW) < 0) { - goto error; + if (PyModule_AddIntMacro(module, CLOCK_UPTIME_RAW) < 0) { + return -1; } #endif @@ -1820,16 +1802,16 @@ PyInit_time(void) if (!initialized) { if (PyStructSequence_InitType2(&StructTimeType, &struct_time_type_desc) < 0) { - goto error; + return -1; } } - if (PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 11)) { - goto error; + if (PyModule_AddIntConstant(module, "_STRUCT_TM_ITEMS", 11)) { + return -1; } Py_INCREF(&StructTimeType); - if (PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType)) { + if (PyModule_AddObject(module, "struct_time", (PyObject*) &StructTimeType)) { Py_DECREF(&StructTimeType); - goto error; + return -1; } initialized = 1; @@ -1840,11 +1822,30 @@ PyInit_time(void) utc_string = tm.tm_zone; #endif - return m; + return 0; +} -error: - Py_DECREF(m); - return NULL; +static struct PyModuleDef_Slot time_slots[] = { + {Py_mod_exec, time_exec}, + {0, NULL} +}; + +static struct PyModuleDef timemodule = { + PyModuleDef_HEAD_INIT, + "time", + module_doc, + 0, + time_methods, + time_slots, + NULL, + NULL, + NULL +}; + +PyMODINIT_FUNC +PyInit_time(void) +{ + return PyModuleDef_Init(&timemodule); } /* Implement pysleep() for various platforms. |