diff --git a/Include/sysmodule.h b/Include/sysmodule.h --- a/Include/sysmodule.h +++ b/Include/sysmodule.h @@ -32,6 +32,8 @@ PyAPI_FUNC(void) PySys_AddXOption(const wchar_t *); PyAPI_FUNC(PyObject *) PySys_GetXOptions(void); +PyAPI_FUNC(void) _SysImportState_Init(void); + #ifdef __cplusplus } #endif diff --git a/Python/import.c b/Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -204,43 +204,22 @@ void _PyImportHooks_Init(void) { - PyObject *v, *path_hooks = NULL, *zimpimport; + PyObject *path_hooks, *zimpimport; int err = 0; - /* adding sys.path_hooks and sys.path_importer_cache, setting up - zipimport */ - if (PyType_Ready(&PyNullImporter_Type) < 0) - goto error; + /* setting up NullImporter, zipimport */ + + if (PyType_Ready(&PyNullImporter_Type) < 0) { + PyErr_Print(); + Py_FatalError("initializing NullImporter failed"); + } + + _SysImportState_Init(); + path_hooks = PySys_GetObject("path_hooks"); if (Py_VerboseFlag) PySys_WriteStderr("# installing zipimport hook\n"); - v = PyList_New(0); - if (v == NULL) - goto error; - err = PySys_SetObject("meta_path", v); - Py_DECREF(v); - if (err) - goto error; - v = PyDict_New(); - if (v == NULL) - goto error; - err = PySys_SetObject("path_importer_cache", v); - Py_DECREF(v); - if (err) - goto error; - path_hooks = PyList_New(0); - if (path_hooks == NULL) - goto error; - err = PySys_SetObject("path_hooks", path_hooks); - if (err) { - error: - PyErr_Print(); - Py_FatalError("initializing sys.meta_path, sys.path_hooks, " - "path_importer_cache, or NullImporter failed" - ); - } - zimpimport = PyImport_ImportModule("zipimport"); if (zimpimport == NULL) { PyErr_Clear(); /* No zip import module -- okay */ @@ -261,8 +240,10 @@ /* sys.path_hooks.append(zipimporter) */ err = PyList_Append(path_hooks, zipimporter); Py_DECREF(zipimporter); - if (err) - goto error; + if (err) { + PyErr_Print(); + Py_FatalError("initializing zipimporter failed"); + } if (Py_VerboseFlag) PySys_WriteStderr( "# installed zipimport hook\n"); diff --git a/Python/sysmodule.c b/Python/sysmodule.c --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1999,3 +1999,42 @@ sys_format("stderr", stderr, format, va); va_end(va); } + +void +_SysImportState_Init(void) +{ + PyObject *v; + int err = 0; + + /* adding sys.path_hooks, sys.path_importer_cache, and + sys.meta_path */ + + v = PyList_New(0); + if (v == NULL) + goto error; + err = PySys_SetObject("meta_path", v); + Py_DECREF(v); + if (err) + goto error; + + v = PyDict_New(); + if (v == NULL) + goto error; + err = PySys_SetObject("path_importer_cache", v); + Py_DECREF(v); + if (err) + goto error; + + v = PyList_New(0); + if (v == NULL) + goto error; + err = PySys_SetObject("path_hooks", v); + if (err) { + error: + PyErr_Print(); + Py_FatalError("initializing sys.meta_path, sys.path_hooks, " + "or path_importer_cache failed" + ); + } +} +