bpo-38858: new_interpreter() reuses pycore_init_builtins() (GH-17351)

new_interpreter() now calls _PyBuiltin_Init() to create the builtins
module and calls _PyImport_FixupBuiltin(), rather than using
_PyImport_FindBuiltin(tstate, "builtins").

pycore_init_builtins() is now responsible to initialize
intepr->builtins_copy: inline _PyImport_Init() and remove this
function.
This commit is contained in:
Victor Stinner 2019-11-22 19:24:49 +01:00 committed by GitHub
parent 82c83bd907
commit 2582d46fbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 43 deletions

View File

@ -3045,8 +3045,6 @@ Py_XINCREF:PyObject*:o:+1:if o is not NULL
_PyImport_Fini:void:::
_PyImport_Init:void:::
_PyObject_New:PyObject*::+1:
_PyObject_New:PyTypeObject*:type:0:

View File

@ -44,7 +44,6 @@ extern PyStatus _PySys_SetPreliminaryStderr(PyObject *sysdict);
extern PyStatus _PySys_ReadPreinitWarnOptions(PyWideStringList *options);
extern PyStatus _PySys_ReadPreinitXOptions(PyConfig *config);
extern int _PySys_InitMain(PyThreadState *tstate);
extern PyStatus _PyImport_Init(PyThreadState *tstate);
extern PyStatus _PyExc_Init(void);
extern PyStatus _PyErr_Init(void);
extern PyStatus _PyBuiltins_AddExceptions(PyObject * bltinmod);

View File

@ -48,17 +48,6 @@ module _imp
/* Initialize things */
PyStatus
_PyImport_Init(PyThreadState *tstate)
{
PyInterpreterState *interp = tstate->interp;
interp->builtins_copy = PyDict_Copy(interp->builtins);
if (interp->builtins_copy == NULL) {
return _PyStatus_ERR("Can't backup builtins dict");
}
return _PyStatus_OK();
}
PyStatus
_PyImportHooks_Init(PyThreadState *tstate)
{

View File

@ -622,25 +622,36 @@ pycore_init_types(PyThreadState *tstate)
static PyStatus
pycore_init_builtins(PyThreadState *tstate)
{
PyInterpreterState *interp = tstate->interp;
PyObject *bimod = _PyBuiltin_Init(tstate);
if (bimod == NULL) {
return _PyStatus_ERR("can't initialize builtins modules");
goto error;
}
_PyImport_FixupBuiltin(bimod, "builtins", interp->modules);
interp->builtins = PyModule_GetDict(bimod);
if (interp->builtins == NULL) {
return _PyStatus_ERR("can't initialize builtins dict");
PyInterpreterState *interp = tstate->interp;
if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
goto error;
}
Py_INCREF(interp->builtins);
PyObject *builtins_dict = PyModule_GetDict(bimod);
if (builtins_dict == NULL) {
goto error;
}
Py_INCREF(builtins_dict);
interp->builtins = builtins_dict;
PyStatus status = _PyBuiltins_AddExceptions(bimod);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
interp->builtins_copy = PyDict_Copy(interp->builtins);
if (interp->builtins_copy == NULL) {
goto error;
}
return _PyStatus_OK();
error:
return _PyStatus_ERR("can't initialize builtins module");
}
@ -649,12 +660,7 @@ pycore_init_import_warnings(PyThreadState *tstate, PyObject *sysmod)
{
const PyConfig *config = &tstate->interp->config;
PyStatus status = _PyImport_Init(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
status = _PyImportHooks_Init(tstate);
PyStatus status = _PyImportHooks_Init(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
@ -1558,23 +1564,12 @@ new_interpreter(PyThreadState **tstate_p)
goto handle_exc;
}
PyObject *bimod = _PyImport_FindBuiltin(tstate, "builtins");
if (bimod != NULL) {
interp->builtins = PyModule_GetDict(bimod);
if (interp->builtins == NULL)
goto handle_exc;
Py_INCREF(interp->builtins);
}
else if (_PyErr_Occurred(tstate)) {
goto handle_exc;
status = pycore_init_builtins(tstate);
if (_PyStatus_EXCEPTION(status)) {
goto done;
}
if (bimod != NULL && sysmod != NULL) {
status = _PyBuiltins_AddExceptions(bimod);
if (_PyStatus_EXCEPTION(status)) {
goto done;
}
if (sysmod != NULL) {
status = _PySys_SetPreliminaryStderr(interp->sysdict);
if (_PyStatus_EXCEPTION(status)) {
goto done;