From ef75a625cdf8377d687a04948b4db9bc1917bf19 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 12 Nov 2020 15:14:13 +0100 Subject: bpo-42260: Initialize time and warnings earlier at startup (GH-23249) * Call _PyTime_Init() and _PyWarnings_InitState() earlier during the Python initialization. * Inline _PyImportHooks_Init() into _PySys_InitCore(). * The _warnings initialization function no longer call _PyWarnings_InitState() to prevent resetting filters_version to 0. * _PyWarnings_InitState() now returns an int and no longer clear the state in case of error (it's done anyway at Python exit). * Rework init_importlib(), fix refleaks on errors. --- Python/_warnings.c | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index e42b7c3be3..8d33fbe0f8 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -114,37 +114,34 @@ init_filters(void) } /* Initialize the given warnings module state. */ -static int -warnings_init_state(WarningsState *st) +int +_PyWarnings_InitState(PyThreadState *tstate) { + WarningsState *st = &tstate->interp->warnings; + if (st->filters == NULL) { st->filters = init_filters(); if (st->filters == NULL) { - goto error; + return -1; } } if (st->once_registry == NULL) { st->once_registry = PyDict_New(); if (st->once_registry == NULL) { - goto error; + return -1; } } if (st->default_action == NULL) { st->default_action = PyUnicode_FromString("default"); if (st->default_action == NULL) { - goto error; + return -1; } } st->filters_version = 0; - return 0; - -error: - warnings_clear_state(st); - return -1; } @@ -1367,16 +1364,6 @@ static struct PyModuleDef warningsmodule = { }; -PyStatus -_PyWarnings_InitState(PyThreadState *tstate) -{ - if (warnings_init_state(&tstate->interp->warnings) < 0) { - return _PyStatus_ERR("can't initialize warnings"); - } - return _PyStatus_OK(); -} - - PyMODINIT_FUNC _PyWarnings_Init(void) { @@ -1391,9 +1378,6 @@ _PyWarnings_Init(void) if (st == NULL) { goto error; } - if (warnings_init_state(st) < 0) { - goto error; - } if (PyModule_AddObjectRef(m, "filters", st->filters) < 0) { goto error; -- cgit v1.2.1