bpo-38858: Fix reference leak in pycore_init_types() (GH-17286)

Only call _PyGC_Init(), _PyExc_Init() and _PyErr_Init() in
new_interpreter().
This commit is contained in:
Victor Stinner 2019-11-20 12:08:13 +01:00 committed by GitHub
parent 67e0de6f0b
commit e7e699e4df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 20 deletions

View File

@ -558,7 +558,7 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
static PyStatus static PyStatus
pycore_init_types(PyThreadState *tstate) pycore_init_types(PyThreadState *tstate, int is_main_interp)
{ {
PyStatus status; PyStatus status;
@ -567,18 +567,20 @@ pycore_init_types(PyThreadState *tstate)
return status; return status;
} }
status = _PyTypes_Init(); if (is_main_interp) {
if (_PyStatus_EXCEPTION(status)) { status = _PyTypes_Init();
return status; if (_PyStatus_EXCEPTION(status)) {
} return status;
}
if (!_PyLong_Init()) { if (!_PyLong_Init()) {
return _PyStatus_ERR("can't init longs"); return _PyStatus_ERR("can't init longs");
} }
status = _PyUnicode_Init(); status = _PyUnicode_Init();
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
}
} }
status = _PyExc_Init(); status = _PyExc_Init();
@ -586,12 +588,14 @@ pycore_init_types(PyThreadState *tstate)
return status; return status;
} }
if (!_PyFloat_Init()) { if (is_main_interp) {
return _PyStatus_ERR("can't init float"); if (!_PyFloat_Init()) {
} return _PyStatus_ERR("can't init float");
}
if (_PyStructSequence_Init() < 0) { if (_PyStructSequence_Init() < 0) {
return _PyStatus_ERR("can't initialize structseq"); return _PyStatus_ERR("can't initialize structseq");
}
} }
status = _PyErr_Init(); status = _PyErr_Init();
@ -599,8 +603,10 @@ pycore_init_types(PyThreadState *tstate)
return status; return status;
} }
if (!_PyContext_Init()) { if (is_main_interp) {
return _PyStatus_ERR("can't init context"); if (!_PyContext_Init()) {
return _PyStatus_ERR("can't init context");
}
} }
return _PyStatus_OK(); return _PyStatus_OK();
@ -690,7 +696,7 @@ pyinit_config(_PyRuntimeState *runtime,
config = &tstate->interp->config; config = &tstate->interp->config;
*tstate_p = tstate; *tstate_p = tstate;
status = pycore_init_types(tstate); status = pycore_init_types(tstate, 1);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -1477,7 +1483,7 @@ new_interpreter(PyThreadState **tstate_p)
} }
config = &interp->config; config = &interp->config;
status = pycore_init_types(tstate); status = pycore_init_types(tstate, 0);
/* XXX The following is lax in error checking */ /* XXX The following is lax in error checking */
PyObject *modules = PyDict_New(); PyObject *modules = PyDict_New();