@@ -1390,99 +1390,6 @@ clear_singlephase_extension(PyInterpreterState *interp,
13901390 return 0 ;
13911391}
13921392
1393- static PyObject *
1394- create_dynamic (PyThreadState * tstate , struct _Py_ext_module_loader_info * info ,
1395- PyObject * file , PyObject * spec )
1396- {
1397- PyObject * mod = NULL ;
1398- PyModuleDef * def = NULL ;
1399-
1400- /* We would move this (and the fclose() below) into
1401- * _PyImport_GetModInitFunc(), but it isn't clear if the intervening
1402- * code relies on fp still being open. */
1403- FILE * fp ;
1404- if (file != NULL ) {
1405- fp = _Py_fopen_obj (info -> filename , "r" );
1406- if (fp == NULL ) {
1407- goto finally ;
1408- }
1409- }
1410- else {
1411- fp = NULL ;
1412- }
1413-
1414- PyModInitFunction p0 = _PyImport_GetModInitFunc (info , fp );
1415- if (p0 == NULL ) {
1416- goto finally ;
1417- }
1418-
1419- struct _Py_ext_module_loader_result res ;
1420- if (_PyImport_RunModInitFunc (p0 , info , & res ) < 0 ) {
1421- assert (PyErr_Occurred ());
1422- goto finally ;
1423- }
1424-
1425- mod = res .module ;
1426- res .module = NULL ;
1427- def = res .def ;
1428- assert (def != NULL );
1429-
1430- if (mod == NULL ) {
1431- //assert(!is_singlephase(def));
1432- mod = PyModule_FromDefAndSpec (def , spec );
1433- if (mod == NULL ) {
1434- goto finally ;
1435- }
1436- }
1437- else {
1438- assert (is_singlephase (def ));
1439- assert (!is_core_module (tstate -> interp , info -> name , info -> filename ));
1440- assert (!is_core_module (tstate -> interp , info -> name , info -> name ));
1441- mod = Py_NewRef (mod );
1442-
1443- const char * name_buf = PyBytes_AS_STRING (info -> name_encoded );
1444- if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed (name_buf ) < 0 ) {
1445- Py_CLEAR (mod );
1446- goto finally ;
1447- }
1448-
1449- /* Remember the filename as the __file__ attribute */
1450- if (PyModule_AddObjectRef (mod , "__file__" , info -> filename ) < 0 ) {
1451- PyErr_Clear (); /* Not important enough to report */
1452- }
1453-
1454- struct singlephase_global_update singlephase = {0 };
1455- // gh-88216: Extensions and def->m_base.m_copy can be updated
1456- // when the extension module doesn't support sub-interpreters.
1457- if (def -> m_size == -1 ) {
1458- singlephase .m_dict = PyModule_GetDict (mod );
1459- assert (singlephase .m_dict != NULL );
1460- }
1461- if (update_global_state_for_extension (
1462- tstate , info -> filename , info -> name , def , & singlephase ) < 0 )
1463- {
1464- Py_CLEAR (mod );
1465- goto finally ;
1466- }
1467-
1468- PyObject * modules = get_modules_dict (tstate , true);
1469- if (finish_singlephase_extension (
1470- tstate , mod , def , info -> name , modules ) < 0 )
1471- {
1472- Py_CLEAR (mod );
1473- goto finally ;
1474- }
1475- }
1476-
1477- // XXX Shouldn't this happen in the error cases too.
1478- if (fp ) {
1479- fclose (fp );
1480- }
1481-
1482- finally :
1483- return mod ;
1484- }
1485-
14861393
14871394/*******************/
14881395/* builtin modules */
@@ -3957,6 +3864,7 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
39573864/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
39583865{
39593866 PyObject * mod = NULL ;
3867+ PyModuleDef * def = NULL ;
39603868 PyThreadState * tstate = _PyThreadState_GET ();
39613869
39623870 struct _Py_ext_module_loader_info info ;
@@ -3981,12 +3889,88 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
39813889 goto finally ;
39823890 }
39833891
3984- /* Is multi-phase init or this is the first time being loaded. */
3985- mod = create_dynamic (tstate , & info , file , spec );
3986- if (mod == NULL ) {
3892+ /* We would move this (and the fclose() below) into
3893+ * _PyImport_GetModInitFunc(), but it isn't clear if the intervening
3894+ * code relies on fp still being open. */
3895+ FILE * fp ;
3896+ if (file != NULL ) {
3897+ fp = _Py_fopen_obj (info .filename , "r" );
3898+ if (fp == NULL ) {
3899+ goto finally ;
3900+ }
3901+ }
3902+ else {
3903+ fp = NULL ;
3904+ }
3905+
3906+ PyModInitFunction p0 = _PyImport_GetModInitFunc (& info , fp );
3907+ if (p0 == NULL ) {
39873908 goto finally ;
39883909 }
39893910
3911+ struct _Py_ext_module_loader_result res ;
3912+ if (_PyImport_RunModInitFunc (p0 , & info , & res ) < 0 ) {
3913+ assert (PyErr_Occurred ());
3914+ goto finally ;
3915+ }
3916+
3917+ mod = res .module ;
3918+ res .module = NULL ;
3919+ def = res .def ;
3920+ assert (def != NULL );
3921+
3922+ if (mod == NULL ) {
3923+ //assert(!is_singlephase(def));
3924+ mod = PyModule_FromDefAndSpec (def , spec );
3925+ if (mod == NULL ) {
3926+ goto finally ;
3927+ }
3928+ }
3929+ else {
3930+ assert (is_singlephase (def ));
3931+ assert (!is_core_module (tstate -> interp , info .name , info .filename ));
3932+ assert (!is_core_module (tstate -> interp , info .name , info .name ));
3933+ mod = Py_NewRef (mod );
3934+
3935+ const char * name_buf = PyBytes_AS_STRING (info .name_encoded );
3936+ if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed (name_buf ) < 0 ) {
3937+ Py_CLEAR (mod );
3938+ goto finally ;
3939+ }
3940+
3941+ /* Remember the filename as the __file__ attribute */
3942+ if (PyModule_AddObjectRef (mod , "__file__" , info .filename ) < 0 ) {
3943+ PyErr_Clear (); /* Not important enough to report */
3944+ }
3945+
3946+ struct singlephase_global_update singlephase = {0 };
3947+ // gh-88216: Extensions and def->m_base.m_copy can be updated
3948+ // when the extension module doesn't support sub-interpreters.
3949+ if (def -> m_size == -1 ) {
3950+ singlephase .m_dict = PyModule_GetDict (mod );
3951+ assert (singlephase .m_dict != NULL );
3952+ }
3953+ if (update_global_state_for_extension (
3954+ tstate , info .filename , info .name , def , & singlephase ) < 0 )
3955+ {
3956+ Py_CLEAR (mod );
3957+ goto finally ;
3958+ }
3959+
3960+ PyObject * modules = get_modules_dict (tstate , true);
3961+ if (finish_singlephase_extension (
3962+ tstate , mod , def , info .name , modules ) < 0 )
3963+ {
3964+ Py_CLEAR (mod );
3965+ goto finally ;
3966+ }
3967+ }
3968+
3969+ // XXX Shouldn't this happen in the error cases too.
3970+ if (fp ) {
3971+ fclose (fp );
3972+ }
3973+
39903974finally :
39913975 _Py_ext_module_loader_info_clear (& info );
39923976 return mod ;
0 commit comments