bpo-36900: import.c uses PyInterpreterState.core_config (GH-13278)

Move _PyImportZip_Init() to the internal C API and add an 'interp'
parameter.
This commit is contained in:
Victor Stinner 2019-05-13 17:12:45 +02:00 committed by GitHub
parent 2c10538d11
commit 410b85a7f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 29 deletions

View File

@ -8,8 +8,6 @@ extern "C" {
#endif #endif
#ifndef Py_LIMITED_API #ifndef Py_LIMITED_API
PyAPI_FUNC(_PyInitError) _PyImportZip_Init(void);
PyMODINIT_FUNC PyInit__imp(void); PyMODINIT_FUNC PyInit__imp(void);
#endif /* !Py_LIMITED_API */ #endif /* !Py_LIMITED_API */
PyAPI_FUNC(long) PyImport_GetMagicNumber(void); PyAPI_FUNC(long) PyImport_GetMagicNumber(void);

View File

@ -54,6 +54,8 @@ extern int _PyFloat_Init(void);
extern _PyInitError _Py_HashRandomization_Init(const _PyCoreConfig *); extern _PyInitError _Py_HashRandomization_Init(const _PyCoreConfig *);
extern _PyInitError _PyTypes_Init(void); extern _PyInitError _PyTypes_Init(void);
extern _PyInitError _PyImportZip_Init(PyInterpreterState *interp);
/* Various internal finalizers */ /* Various internal finalizers */

View File

@ -91,7 +91,7 @@ _PyImportHooks_Init(void)
} }
_PyInitError _PyInitError
_PyImportZip_Init(void) _PyImportZip_Init(PyInterpreterState *interp)
{ {
PyObject *path_hooks, *zipimport; PyObject *path_hooks, *zipimport;
int err = 0; int err = 0;
@ -102,14 +102,17 @@ _PyImportZip_Init(void)
goto error; goto error;
} }
if (Py_VerboseFlag) int verbose = interp->core_config.verbose;
if (verbose) {
PySys_WriteStderr("# installing zipimport hook\n"); PySys_WriteStderr("# installing zipimport hook\n");
}
zipimport = PyImport_ImportModule("zipimport"); zipimport = PyImport_ImportModule("zipimport");
if (zipimport == NULL) { if (zipimport == NULL) {
PyErr_Clear(); /* No zip import module -- okay */ PyErr_Clear(); /* No zip import module -- okay */
if (Py_VerboseFlag) if (verbose) {
PySys_WriteStderr("# can't import zipimport\n"); PySys_WriteStderr("# can't import zipimport\n");
}
} }
else { else {
_Py_IDENTIFIER(zipimporter); _Py_IDENTIFIER(zipimporter);
@ -118,9 +121,9 @@ _PyImportZip_Init(void)
Py_DECREF(zipimport); Py_DECREF(zipimport);
if (zipimporter == NULL) { if (zipimporter == NULL) {
PyErr_Clear(); /* No zipimporter object -- okay */ PyErr_Clear(); /* No zipimporter object -- okay */
if (Py_VerboseFlag) if (verbose) {
PySys_WriteStderr( PySys_WriteStderr("# can't import zipimport.zipimporter\n");
"# can't import zipimport.zipimporter\n"); }
} }
else { else {
/* sys.path_hooks.insert(0, zipimporter) */ /* sys.path_hooks.insert(0, zipimporter) */
@ -129,9 +132,9 @@ _PyImportZip_Init(void)
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
if (Py_VerboseFlag) if (verbose) {
PySys_WriteStderr( PySys_WriteStderr("# installed zipimport hook\n");
"# installed zipimport hook\n"); }
} }
} }
@ -415,22 +418,26 @@ PyImport_Cleanup(void)
/* XXX Perhaps these precautions are obsolete. Who knows? */ /* XXX Perhaps these precautions are obsolete. Who knows? */
if (Py_VerboseFlag) int verbose = interp->core_config.verbose;
if (verbose) {
PySys_WriteStderr("# clear builtins._\n"); PySys_WriteStderr("# clear builtins._\n");
}
if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) { if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) {
PyErr_WriteUnraisable(NULL); PyErr_WriteUnraisable(NULL);
} }
for (p = sys_deletes; *p != NULL; p++) { for (p = sys_deletes; *p != NULL; p++) {
if (Py_VerboseFlag) if (verbose) {
PySys_WriteStderr("# clear sys.%s\n", *p); PySys_WriteStderr("# clear sys.%s\n", *p);
}
if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) { if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) {
PyErr_WriteUnraisable(NULL); PyErr_WriteUnraisable(NULL);
} }
} }
for (p = sys_files; *p != NULL; p+=2) { for (p = sys_files; *p != NULL; p+=2) {
if (Py_VerboseFlag) if (verbose) {
PySys_WriteStderr("# restore sys.%s\n", *p); PySys_WriteStderr("# restore sys.%s\n", *p);
}
value = _PyDict_GetItemStringWithError(interp->sysdict, *(p+1)); value = _PyDict_GetItemStringWithError(interp->sysdict, *(p+1));
if (value == NULL) { if (value == NULL) {
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
@ -469,8 +476,9 @@ PyImport_Cleanup(void)
} }
#define CLEAR_MODULE(name, mod) \ #define CLEAR_MODULE(name, mod) \
if (PyModule_Check(mod)) { \ if (PyModule_Check(mod)) { \
if (Py_VerboseFlag && PyUnicode_Check(name)) \ if (verbose && PyUnicode_Check(name)) { \
PySys_FormatStderr("# cleanup[2] removing %U\n", name); \ PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
} \
STORE_MODULE_WEAKREF(name, mod); \ STORE_MODULE_WEAKREF(name, mod); \
if (PyObject_SetItem(modules, name, Py_None) < 0) { \ if (PyObject_SetItem(modules, name, Py_None) < 0) { \
PyErr_WriteUnraisable(NULL); \ PyErr_WriteUnraisable(NULL); \
@ -563,8 +571,9 @@ PyImport_Cleanup(void)
if (dict == interp->builtins || dict == interp->sysdict) if (dict == interp->builtins || dict == interp->sysdict)
continue; continue;
Py_INCREF(mod); Py_INCREF(mod);
if (Py_VerboseFlag && PyUnicode_Check(name)) if (verbose && PyUnicode_Check(name)) {
PySys_FormatStderr("# cleanup[3] wiping %U\n", name); PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
}
_PyModule_Clear(mod); _PyModule_Clear(mod);
Py_DECREF(mod); Py_DECREF(mod);
} }
@ -572,11 +581,13 @@ PyImport_Cleanup(void)
} }
/* Next, delete sys and builtins (in that order) */ /* Next, delete sys and builtins (in that order) */
if (Py_VerboseFlag) if (verbose) {
PySys_FormatStderr("# cleanup[3] wiping sys\n"); PySys_FormatStderr("# cleanup[3] wiping sys\n");
}
_PyModule_ClearDict(interp->sysdict); _PyModule_ClearDict(interp->sysdict);
if (Py_VerboseFlag) if (verbose) {
PySys_FormatStderr("# cleanup[3] wiping builtins\n"); PySys_FormatStderr("# cleanup[3] wiping builtins\n");
}
_PyModule_ClearDict(interp->builtins); _PyModule_ClearDict(interp->builtins);
/* Clear and delete the modules directory. Actual modules will /* Clear and delete the modules directory. Actual modules will
@ -755,9 +766,11 @@ _PyImport_FindExtensionObjectEx(PyObject *name, PyObject *filename,
PyMapping_DelItem(modules, name); PyMapping_DelItem(modules, name);
return NULL; return NULL;
} }
if (Py_VerboseFlag) int verbose = _PyInterpreterState_Get()->core_config.verbose;
if (verbose) {
PySys_FormatStderr("import %U # previously loaded (%R)\n", PySys_FormatStderr("import %U # previously loaded (%R)\n",
name, filename); name, filename);
}
return mod; return mod;
} }
@ -1427,7 +1440,7 @@ PyImport_ImportModuleNoBlock(const char *name)
/* Remove importlib frames from the traceback, /* Remove importlib frames from the traceback,
* except in Verbose mode. */ * except in Verbose mode. */
static void static void
remove_importlib_frames(void) remove_importlib_frames(PyInterpreterState *interp)
{ {
const char *importlib_filename = "<frozen importlib._bootstrap>"; const char *importlib_filename = "<frozen importlib._bootstrap>";
const char *external_filename = "<frozen importlib._bootstrap_external>"; const char *external_filename = "<frozen importlib._bootstrap_external>";
@ -1442,8 +1455,10 @@ remove_importlib_frames(void)
which end with a call to "_call_with_frames_removed". */ which end with a call to "_call_with_frames_removed". */
PyErr_Fetch(&exception, &value, &base_tb); PyErr_Fetch(&exception, &value, &base_tb);
if (!exception || Py_VerboseFlag) if (!exception || interp->core_config.verbose) {
goto done; goto done;
}
if (PyType_IsSubtype((PyTypeObject *) exception, if (PyType_IsSubtype((PyTypeObject *) exception,
(PyTypeObject *) PyExc_ImportError)) (PyTypeObject *) PyExc_ImportError))
always_trim = 1; always_trim = 1;
@ -1853,8 +1868,9 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
Py_XDECREF(abs_name); Py_XDECREF(abs_name);
Py_XDECREF(mod); Py_XDECREF(mod);
Py_XDECREF(package); Py_XDECREF(package);
if (final_mod == NULL) if (final_mod == NULL) {
remove_importlib_frames(); remove_importlib_frames(interp);
}
return final_mod; return final_mod;
} }
@ -2303,13 +2319,16 @@ PyInit__imp(void)
PyObject *m, *d; PyObject *m, *d;
m = PyModule_Create(&impmodule); m = PyModule_Create(&impmodule);
if (m == NULL) if (m == NULL) {
goto failure; goto failure;
}
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
if (d == NULL) if (d == NULL) {
goto failure; goto failure;
_PyCoreConfig *config = &_PyInterpreterState_Get()->core_config; }
PyObject *pyc_mode = PyUnicode_FromWideChar(config->check_hash_pycs_mode, -1);
const wchar_t *mode = _PyInterpreterState_Get()->core_config.check_hash_pycs_mode;
PyObject *pyc_mode = PyUnicode_FromWideChar(mode, -1);
if (pyc_mode == NULL) { if (pyc_mode == NULL) {
goto failure; goto failure;
} }

View File

@ -205,7 +205,7 @@ init_importlib_external(PyInterpreterState *interp)
return _Py_INIT_ERR("external importer setup failed"); return _Py_INIT_ERR("external importer setup failed");
} }
Py_DECREF(value); Py_DECREF(value);
return _PyImportZip_Init(); return _PyImportZip_Init(interp);
} }
/* Helper functions to better handle the legacy C locale /* Helper functions to better handle the legacy C locale