gh-81057: Move OS-Related Globals to _PyRuntimeState (gh-100082)

https://github.com/python/cpython/issues/81057
This commit is contained in:
Eric Snow 2022-12-08 15:38:06 -07:00 committed by GitHub
parent c85be734d1
commit cda9f0236f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 107 additions and 27 deletions

View file

@ -495,9 +495,11 @@ extern char *ctermid_r(char *);
#ifdef MS_WINDOWS
# define INITFUNC PyInit_nt
# define MODNAME "nt"
# define MODNAME_OBJ &_Py_ID(nt)
#else
# define INITFUNC PyInit_posix
# define MODNAME "posix"
# define MODNAME_OBJ &_Py_ID(posix)
#endif
#if defined(__sun)
@ -974,6 +976,7 @@ typedef struct {
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
PyObject *SchedParamType;
#endif
newfunc statresult_new_orig;
PyObject *StatResultType;
PyObject *StatVFSResultType;
PyObject *TerminalSizeType;
@ -2225,7 +2228,6 @@ static PyStructSequence_Desc waitid_result_desc = {
5
};
#endif
static newfunc structseq_new;
static PyObject *
statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
@ -2233,6 +2235,18 @@ statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyStructSequence *result;
int i;
// ht_module doesn't get set in PyStructSequence_NewType(),
// so we can't use PyType_GetModule().
PyObject *mod = PyImport_GetModule(MODNAME_OBJ);
if (mod == NULL) {
return NULL;
}
_posixstate *state = get_posix_state(mod);
if (state == NULL) {
return NULL;
}
#define structseq_new state->statresult_new_orig
result = (PyStructSequence*)structseq_new(type, args, kwds);
if (!result)
return NULL;
@ -9051,10 +9065,23 @@ build_times_result(PyObject *module, double user, double system,
}
#ifndef MS_WINDOWS
#define NEED_TICKS_PER_SECOND
static long ticks_per_second = -1;
#endif /* MS_WINDOWS */
#ifdef _OS_NEED_TICKS_PER_SECOND
#define ticks_per_second _PyRuntime.os.ticks_per_second
static void
ticks_per_second_init(void)
{
if (ticks_per_second != -1) {
return;
}
# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
ticks_per_second = sysconf(_SC_CLK_TCK);
# elif defined(HZ)
ticks_per_second = HZ;
# else
ticks_per_second = 60; /* magic fallback value; may be bogus */
# endif
}
#endif
/*[clinic input]
os.times
@ -9089,10 +9116,10 @@ os_times_impl(PyObject *module)
(double)0,
(double)0);
}
#elif !defined(_OS_NEED_TICKS_PER_SECOND)
# error "missing ticks_per_second"
#else /* MS_WINDOWS */
{
struct tms t;
clock_t c;
errno = 0;
@ -15912,7 +15939,7 @@ posixmodule_exec(PyObject *m)
}
PyModule_AddObject(m, "stat_result", Py_NewRef(StatResultType));
state->StatResultType = StatResultType;
structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
state->statresult_new_orig = ((PyTypeObject *)StatResultType)->tp_new;
((PyTypeObject *)StatResultType)->tp_new = statresult_new;
statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
@ -15922,14 +15949,9 @@ posixmodule_exec(PyObject *m)
}
PyModule_AddObject(m, "statvfs_result", Py_NewRef(StatVFSResultType));
state->StatVFSResultType = StatVFSResultType;
#ifdef NEED_TICKS_PER_SECOND
# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
ticks_per_second = sysconf(_SC_CLK_TCK);
# elif defined(HZ)
ticks_per_second = HZ;
# else
ticks_per_second = 60; /* magic fallback value; may be bogus */
# endif
#ifdef _OS_NEED_TICKS_PER_SECOND
ticks_per_second_init();
#endif
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)