summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2019-11-22 18:24:49 (GMT)
committerGitHub <noreply@github.com>2019-11-22 18:24:49 (GMT)
commit2582d46fbcf7bdf86b9cf4016850b8d155267ac6 (patch)
tree7ac19636e1def16e85485e159ec98a127abb12e2
parent82c83bd907409c287a5bd0d0f4598f2c0538f34d (diff)
downloadcpython-2582d46fbcf7bdf86b9cf4016850b8d155267ac6.zip
cpython-2582d46fbcf7bdf86b9cf4016850b8d155267ac6.tar.gz
cpython-2582d46fbcf7bdf86b9cf4016850b8d155267ac6.tar.bz2
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.
-rw-r--r--Doc/data/refcounts.dat2
-rw-r--r--Include/internal/pycore_pylifecycle.h1
-rw-r--r--Python/import.c11
-rw-r--r--Python/pylifecycle.c53
4 files changed, 24 insertions, 43 deletions
diff --git a/Doc/data/refcounts.dat b/Doc/data/refcounts.dat
index cfed1bd..b55e972 100644
--- a/Doc/data/refcounts.dat
+++ b/Doc/data/refcounts.dat
@@ -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:
diff --git a/Include/internal/pycore_pylifecycle.h b/Include/internal/pycore_pylifecycle.h
index 73aa5ef..cd3be21 100644
--- a/Include/internal/pycore_pylifecycle.h
+++ b/Include/internal/pycore_pylifecycle.h
@@ -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);
diff --git a/Python/import.c b/Python/import.c
index 923c6d0..045b6d0 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -49,17 +49,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)
{
PyObject *v, *path_hooks = NULL;
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index e692d75..e63fb64 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -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;
+ }
+
+ PyInterpreterState *interp = tstate->interp;
+ if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
+ 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");
+ PyObject *builtins_dict = PyModule_GetDict(bimod);
+ if (builtins_dict == NULL) {
+ goto error;
}
- Py_INCREF(interp->builtins);
+ 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;