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.
This commit is contained in:
Victor Stinner 2020-11-12 15:14:13 +01:00 committed by GitHub
parent d19fa7a337
commit ef75a625cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 68 additions and 133 deletions

View file

@ -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;