@@ -135,59 +135,61 @@ Py_IsInitialized(void)
135135 having the lock, but you cannot use multiple threads.)
136136
137137*/
138-
139- static PyStatus
138+ static int
140139init_importlib (PyThreadState * tstate , PyObject * sysmod )
141140{
142- PyObject * importlib ;
143- PyObject * impmod ;
144- PyObject * value ;
141+ assert (!_PyErr_Occurred (tstate ));
142+
145143 PyInterpreterState * interp = tstate -> interp ;
146144 int verbose = _PyInterpreterState_GetConfig (interp )-> verbose ;
147145
148- /* Import _importlib through its frozen version, _frozen_importlib. */
149- if (PyImport_ImportFrozenModule ("_frozen_importlib" ) <= 0 ) {
150- return _PyStatus_ERR ("can't import _frozen_importlib" );
151- }
152- else if (verbose ) {
146+ // Import _importlib through its frozen version, _frozen_importlib.
147+ if (verbose ) {
153148 PySys_FormatStderr ("import _frozen_importlib # frozen\n" );
154149 }
155- importlib = PyImport_AddModule ("_frozen_importlib" );
150+ if (PyImport_ImportFrozenModule ("_frozen_importlib" ) <= 0 ) {
151+ return -1 ;
152+ }
153+ PyObject * importlib = PyImport_AddModule ("_frozen_importlib" ); // borrowed
156154 if (importlib == NULL ) {
157- return _PyStatus_ERR ( "couldn't get _frozen_importlib from sys.modules" ) ;
155+ return -1 ;
158156 }
159- interp -> importlib = importlib ;
160- Py_INCREF (interp -> importlib );
161-
162- interp -> import_func = _PyDict_GetItemStringWithError (interp -> builtins , "__import__" );
163- if (interp -> import_func == NULL )
164- return _PyStatus_ERR ("__import__ not found" );
165- Py_INCREF (interp -> import_func );
157+ interp -> importlib = Py_NewRef (importlib );
166158
167- /* Import the _imp module */
168- impmod = PyInit__imp ( );
169- if (impmod == NULL ) {
170- return _PyStatus_ERR ( "can't import _imp" ) ;
159+ PyObject * import_func = _PyDict_GetItemStringWithError ( interp -> builtins ,
160+ "__import__" );
161+ if (import_func == NULL ) {
162+ return -1 ;
171163 }
172- else if (verbose ) {
164+ interp -> import_func = Py_NewRef (import_func );
165+
166+ // Import the _imp module
167+ if (verbose ) {
173168 PySys_FormatStderr ("import _imp # builtin\n" );
174169 }
175- if (_PyImport_SetModuleString ("_imp" , impmod ) < 0 ) {
176- return _PyStatus_ERR ("can't save _imp to sys.modules" );
170+ PyObject * imp_mod = PyInit__imp ();
171+ if (imp_mod == NULL ) {
172+ return -1 ;
173+ }
174+ if (_PyImport_SetModuleString ("_imp" , imp_mod ) < 0 ) {
175+ Py_DECREF (imp_mod );
176+ return -1 ;
177177 }
178178
179- /* Install importlib as the implementation of import */
180- value = PyObject_CallMethod (importlib , "_install" , "OO" , sysmod , impmod );
179+ // Install importlib as the implementation of import
180+ PyObject * value = PyObject_CallMethod (importlib , "_install" ,
181+ "OO" , sysmod , imp_mod );
182+ Py_DECREF (imp_mod );
181183 if (value == NULL ) {
182- _PyErr_Print (tstate );
183- return _PyStatus_ERR ("importlib install failed" );
184+ return -1 ;
184185 }
185186 Py_DECREF (value );
186- Py_DECREF (impmod );
187187
188- return _PyStatus_OK ();
188+ assert (!_PyErr_Occurred (tstate ));
189+ return 0 ;
189190}
190191
192+
191193static PyStatus
192194init_importlib_external (PyThreadState * tstate )
193195{
@@ -700,6 +702,9 @@ pycore_init_types(PyThreadState *tstate)
700702 }
701703 }
702704
705+ if (_PyWarnings_InitState (tstate ) < 0 ) {
706+ return _PyStatus_ERR ("can't initialize warnings" );
707+ }
703708 return _PyStatus_OK ();
704709}
705710
@@ -747,37 +752,6 @@ pycore_init_builtins(PyThreadState *tstate)
747752}
748753
749754
750- static PyStatus
751- pycore_init_import_warnings (PyThreadState * tstate , PyObject * sysmod )
752- {
753- assert (!_PyErr_Occurred (tstate ));
754-
755- PyStatus status = _PyImportHooks_Init (tstate );
756- if (_PyStatus_EXCEPTION (status )) {
757- return status ;
758- }
759-
760- /* Initialize _warnings. */
761- status = _PyWarnings_InitState (tstate );
762- if (_PyStatus_EXCEPTION (status )) {
763- return status ;
764- }
765-
766- const PyConfig * config = _PyInterpreterState_GetConfig (tstate -> interp );
767- if (config -> _install_importlib ) {
768- /* This call sets up builtin and frozen import support */
769- status = init_importlib (tstate , sysmod );
770- if (_PyStatus_EXCEPTION (status )) {
771- return status ;
772- }
773- }
774-
775- assert (!_PyErr_Occurred (tstate ));
776-
777- return _PyStatus_OK ();
778- }
779-
780-
781755static PyStatus
782756pycore_interp_init (PyThreadState * tstate )
783757{
@@ -789,6 +763,12 @@ pycore_interp_init(PyThreadState *tstate)
789763 goto done ;
790764 }
791765
766+ if (_Py_IsMainInterpreter (tstate )) {
767+ if (_PyTime_Init () < 0 ) {
768+ return _PyStatus_ERR ("can't initialize time" );
769+ }
770+ }
771+
792772 status = _PySys_Create (tstate , & sysmod );
793773 if (_PyStatus_EXCEPTION (status )) {
794774 goto done ;
@@ -799,7 +779,13 @@ pycore_interp_init(PyThreadState *tstate)
799779 goto done ;
800780 }
801781
802- status = pycore_init_import_warnings (tstate , sysmod );
782+ const PyConfig * config = _PyInterpreterState_GetConfig (tstate -> interp );
783+ if (config -> _install_importlib ) {
784+ /* This call sets up builtin and frozen import support */
785+ if (init_importlib (tstate , sysmod ) < 0 ) {
786+ return _PyStatus_ERR ("failed to initialize importlib" );
787+ }
788+ }
803789
804790done :
805791 /* sys.modules['sys'] contains a strong reference to the module */
@@ -1044,12 +1030,6 @@ init_interp_main(PyThreadState *tstate)
10441030 return status ;
10451031 }
10461032
1047- if (is_main_interp ) {
1048- if (_PyTime_Init () < 0 ) {
1049- return _PyStatus_ERR ("can't initialize time" );
1050- }
1051- }
1052-
10531033 if (interpreter_update_config (tstate , 1 ) < 0 ) {
10541034 return _PyStatus_ERR ("failed to update the Python config" );
10551035 }
0 commit comments