bpo-39968: Convert extension modules' macros of get_module_state() to inline functions (GH-19017)

This commit is contained in:
Hai Shi 2020-03-16 21:15:01 +08:00 committed by GitHub
parent 4ab362cec6
commit f707d94af6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 279 additions and 164 deletions

View file

@ -842,7 +842,14 @@ typedef struct {
static struct PyModuleDef posixmodule;
#define _posixstate(o) ((_posixstate *)PyModule_GetState(o))
static inline _posixstate*
get_posix_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (_posixstate *)state;
}
#define _posixstate_global ((_posixstate *)PyModule_GetState(PyState_FindModule(&posixmodule)))
/*
@ -2103,48 +2110,48 @@ statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static int
_posix_clear(PyObject *module)
{
Py_CLEAR(_posixstate(module)->billion);
Py_CLEAR(_posixstate(module)->DirEntryType);
Py_CLEAR(_posixstate(module)->ScandirIteratorType);
Py_CLEAR(get_posix_state(module)->billion);
Py_CLEAR(get_posix_state(module)->DirEntryType);
Py_CLEAR(get_posix_state(module)->ScandirIteratorType);
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Py_CLEAR(_posixstate(module)->SchedParamType);
Py_CLEAR(get_posix_state(module)->SchedParamType);
#endif
Py_CLEAR(_posixstate(module)->StatResultType);
Py_CLEAR(_posixstate(module)->StatVFSResultType);
Py_CLEAR(_posixstate(module)->TerminalSizeType);
Py_CLEAR(_posixstate(module)->TimesResultType);
Py_CLEAR(_posixstate(module)->UnameResultType);
Py_CLEAR(get_posix_state(module)->StatResultType);
Py_CLEAR(get_posix_state(module)->StatVFSResultType);
Py_CLEAR(get_posix_state(module)->TerminalSizeType);
Py_CLEAR(get_posix_state(module)->TimesResultType);
Py_CLEAR(get_posix_state(module)->UnameResultType);
#if defined(HAVE_WAITID) && !defined(__APPLE__)
Py_CLEAR(_posixstate(module)->WaitidResultType);
Py_CLEAR(get_posix_state(module)->WaitidResultType);
#endif
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Py_CLEAR(_posixstate(module)->struct_rusage);
Py_CLEAR(get_posix_state(module)->struct_rusage);
#endif
Py_CLEAR(_posixstate(module)->st_mode);
Py_CLEAR(get_posix_state(module)->st_mode);
return 0;
}
static int
_posix_traverse(PyObject *module, visitproc visit, void *arg)
{
Py_VISIT(_posixstate(module)->billion);
Py_VISIT(_posixstate(module)->DirEntryType);
Py_VISIT(_posixstate(module)->ScandirIteratorType);
Py_VISIT(get_posix_state(module)->billion);
Py_VISIT(get_posix_state(module)->DirEntryType);
Py_VISIT(get_posix_state(module)->ScandirIteratorType);
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Py_VISIT(_posixstate(module)->SchedParamType);
Py_VISIT(get_posix_state(module)->SchedParamType);
#endif
Py_VISIT(_posixstate(module)->StatResultType);
Py_VISIT(_posixstate(module)->StatVFSResultType);
Py_VISIT(_posixstate(module)->TerminalSizeType);
Py_VISIT(_posixstate(module)->TimesResultType);
Py_VISIT(_posixstate(module)->UnameResultType);
Py_VISIT(get_posix_state(module)->StatResultType);
Py_VISIT(get_posix_state(module)->StatVFSResultType);
Py_VISIT(get_posix_state(module)->TerminalSizeType);
Py_VISIT(get_posix_state(module)->TimesResultType);
Py_VISIT(get_posix_state(module)->UnameResultType);
#if defined(HAVE_WAITID) && !defined(__APPLE__)
Py_VISIT(_posixstate(module)->WaitidResultType);
Py_VISIT(get_posix_state(module)->WaitidResultType);
#endif
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Py_VISIT(_posixstate(module)->struct_rusage);
Py_VISIT(get_posix_state(module)->struct_rusage);
#endif
Py_VISIT(_posixstate(module)->st_mode);
Py_VISIT(get_posix_state(module)->st_mode);
return 0;
}
@ -4673,7 +4680,7 @@ os_uname_impl(PyObject *module)
if (res < 0)
return posix_error();
PyObject *UnameResultType = _posixstate(module)->UnameResultType;
PyObject *UnameResultType = get_posix_state(module)->UnameResultType;
value = PyStructSequence_New((PyTypeObject *)UnameResultType);
if (value == NULL)
return NULL;
@ -7840,7 +7847,7 @@ os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
if (si.si_pid == 0)
Py_RETURN_NONE;
PyObject *WaitidResultType = _posixstate(module)->WaitidResultType;
PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType;
result = PyStructSequence_New((PyTypeObject *)WaitidResultType);
if (!result)
return NULL;
@ -12407,7 +12414,7 @@ get_terminal_size(PyObject *self, PyObject *args)
}
#endif /* TERMSIZE_USE_CONIO */
PyObject *TerminalSizeType = _posixstate(self)->TerminalSizeType;
PyObject *TerminalSizeType = get_posix_state(self)->TerminalSizeType;
termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
if (termsize == NULL)
return NULL;
@ -13425,7 +13432,7 @@ os_scandir_impl(PyObject *module, path_t *path)
return NULL;
}
PyObject *ScandirIteratorType = _posixstate(module)->ScandirIteratorType;
PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType;
iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
if (!iterator)
return NULL;
@ -14643,7 +14650,7 @@ INITFUNC(void)
}
Py_INCREF(WaitidResultType);
PyModule_AddObject(m, "waitid_result", WaitidResultType);
_posixstate(m)->WaitidResultType = WaitidResultType;
get_posix_state(m)->WaitidResultType = WaitidResultType;
#endif
stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
@ -14656,7 +14663,7 @@ INITFUNC(void)
}
Py_INCREF(StatResultType);
PyModule_AddObject(m, "stat_result", StatResultType);
_posixstate(m)->StatResultType = StatResultType;
get_posix_state(m)->StatResultType = StatResultType;
structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
((PyTypeObject *)StatResultType)->tp_new = statresult_new;
@ -14667,7 +14674,7 @@ INITFUNC(void)
}
Py_INCREF(StatVFSResultType);
PyModule_AddObject(m, "statvfs_result", StatVFSResultType);
_posixstate(m)->StatVFSResultType = StatVFSResultType;
get_posix_state(m)->StatVFSResultType = StatVFSResultType;
#ifdef NEED_TICKS_PER_SECOND
# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
ticks_per_second = sysconf(_SC_CLK_TCK);
@ -14686,7 +14693,7 @@ INITFUNC(void)
}
Py_INCREF(SchedParamType);
PyModule_AddObject(m, "sched_param", SchedParamType);
_posixstate(m)->SchedParamType = SchedParamType;
get_posix_state(m)->SchedParamType = SchedParamType;
((PyTypeObject *)SchedParamType)->tp_new = os_sched_param;
#endif
@ -14697,14 +14704,14 @@ INITFUNC(void)
}
Py_INCREF(TerminalSizeType);
PyModule_AddObject(m, "terminal_size", TerminalSizeType);
_posixstate(m)->TerminalSizeType = TerminalSizeType;
get_posix_state(m)->TerminalSizeType = TerminalSizeType;
/* initialize scandir types */
PyObject *ScandirIteratorType = PyType_FromSpec(&ScandirIteratorType_spec);
if (ScandirIteratorType == NULL) {
return NULL;
}
_posixstate(m)->ScandirIteratorType = ScandirIteratorType;
get_posix_state(m)->ScandirIteratorType = ScandirIteratorType;
PyObject *DirEntryType = PyType_FromSpec(&DirEntryType_spec);
if (DirEntryType == NULL) {
@ -14712,7 +14719,7 @@ INITFUNC(void)
}
Py_INCREF(DirEntryType);
PyModule_AddObject(m, "DirEntry", DirEntryType);
_posixstate(m)->DirEntryType = DirEntryType;
get_posix_state(m)->DirEntryType = DirEntryType;
times_result_desc.name = MODNAME ".times_result";
PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
@ -14721,7 +14728,7 @@ INITFUNC(void)
}
Py_INCREF(TimesResultType);
PyModule_AddObject(m, "times_result", TimesResultType);
_posixstate(m)->TimesResultType = TimesResultType;
get_posix_state(m)->TimesResultType = TimesResultType;
PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc);
if (UnameResultType == NULL) {
@ -14729,7 +14736,7 @@ INITFUNC(void)
}
Py_INCREF(UnameResultType);
PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
_posixstate(m)->UnameResultType = (PyObject *)UnameResultType;
get_posix_state(m)->UnameResultType = (PyObject *)UnameResultType;
#ifdef __APPLE__
/*
@ -14769,15 +14776,15 @@ INITFUNC(void)
#endif /* __APPLE__ */
if ((_posixstate(m)->billion = PyLong_FromLong(1000000000)) == NULL)
if ((get_posix_state(m)->billion = PyLong_FromLong(1000000000)) == NULL)
return NULL;
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
_posixstate(m)->struct_rusage = PyUnicode_InternFromString("struct_rusage");
if (_posixstate(m)->struct_rusage == NULL)
get_posix_state(m)->struct_rusage = PyUnicode_InternFromString("struct_rusage");
if (get_posix_state(m)->struct_rusage == NULL)
return NULL;
#endif
_posixstate(m)->st_mode = PyUnicode_InternFromString("st_mode");
if (_posixstate(m)->st_mode == NULL)
get_posix_state(m)->st_mode = PyUnicode_InternFromString("st_mode");
if (get_posix_state(m)->st_mode == NULL)
return NULL;
/* suppress "function not used" warnings */