@@ -1386,6 +1386,84 @@ import_find_extension(PyThreadState *tstate,
1386
1386
return mod ;
1387
1387
}
1388
1388
1389
+ static PyObject *
1390
+ import_run_extension (PyThreadState * tstate , PyModInitFunction p0 ,
1391
+ struct _Py_ext_module_loader_info * info ,
1392
+ PyObject * spec , PyObject * modules )
1393
+ {
1394
+ PyObject * mod = NULL ;
1395
+ PyModuleDef * def = NULL ;
1396
+
1397
+ struct _Py_ext_module_loader_result res ;
1398
+ if (_PyImport_RunModInitFunc (p0 , info , & res ) < 0 ) {
1399
+ /* We discard res.def. */
1400
+ assert (res .module == NULL );
1401
+ assert (PyErr_Occurred ());
1402
+ goto finally ;
1403
+ }
1404
+ assert (!PyErr_Occurred ());
1405
+
1406
+ mod = res .module ;
1407
+ res .module = NULL ;
1408
+ def = res .def ;
1409
+ assert (def != NULL );
1410
+
1411
+ if (mod == NULL ) {
1412
+ //assert(!is_singlephase(def));
1413
+ assert (mod == NULL );
1414
+ mod = PyModule_FromDefAndSpec (def , spec );
1415
+ if (mod == NULL ) {
1416
+ goto finally ;
1417
+ }
1418
+ }
1419
+ else {
1420
+ assert (is_singlephase (def ));
1421
+ assert (PyModule_Check (mod ));
1422
+ mod = Py_NewRef (mod );
1423
+
1424
+ const char * name_buf = PyBytes_AS_STRING (info -> name_encoded );
1425
+ if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed (name_buf ) < 0 ) {
1426
+ Py_CLEAR (mod );
1427
+ goto finally ;
1428
+ }
1429
+
1430
+ if (info -> filename != NULL ) {
1431
+ /* Remember the filename as the __file__ attribute */
1432
+ if (PyModule_AddObjectRef (mod , "__file__" , info -> filename ) < 0 ) {
1433
+ PyErr_Clear (); /* Not important enough to report */
1434
+ }
1435
+ }
1436
+
1437
+ struct singlephase_global_update singlephase = {0 };
1438
+ // gh-88216: Extensions and def->m_base.m_copy can be updated
1439
+ // when the extension module doesn't support sub-interpreters.
1440
+ if (def -> m_size == -1
1441
+ && !is_core_module (tstate -> interp , info -> name , info -> path ))
1442
+ {
1443
+ singlephase .m_dict = PyModule_GetDict (mod );
1444
+ assert (singlephase .m_dict != NULL );
1445
+ }
1446
+ if (update_global_state_for_extension (
1447
+ tstate , info -> path , info -> name , def , & singlephase ) < 0 )
1448
+ {
1449
+ Py_CLEAR (mod );
1450
+ goto finally ;
1451
+ }
1452
+
1453
+ PyObject * modules = get_modules_dict (tstate , true);
1454
+ if (finish_singlephase_extension (
1455
+ tstate , mod , def , info -> name , modules ) < 0 )
1456
+ {
1457
+ Py_CLEAR (mod );
1458
+ goto finally ;
1459
+ }
1460
+ }
1461
+
1462
+ finally :
1463
+ return mod ;
1464
+ }
1465
+
1466
+
1389
1467
static int
1390
1468
clear_singlephase_extension (PyInterpreterState * interp ,
1391
1469
PyObject * name , PyObject * path )
@@ -1491,8 +1569,6 @@ is_builtin(PyObject *name)
1491
1569
static PyObject *
1492
1570
create_builtin (PyThreadState * tstate , PyObject * name , PyObject * spec )
1493
1571
{
1494
- PyModuleDef * def = NULL ;
1495
-
1496
1572
struct _Py_ext_module_loader_info info ;
1497
1573
if (_Py_ext_module_loader_info_init_for_builtin (& info , name ) < 0 ) {
1498
1574
return NULL ;
@@ -1528,49 +1604,9 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
1528
1604
goto finally ;
1529
1605
}
1530
1606
1531
- struct _Py_ext_module_loader_result res ;
1532
- if (_PyImport_RunModInitFunc (p0 , & info , & res ) < 0 ) {
1533
- goto finally ;
1534
- }
1535
-
1536
- mod = res .module ;
1537
- res .module = NULL ;
1538
- def = res .def ;
1539
- assert (def != NULL );
1540
-
1541
- if (mod == NULL ) {
1542
- assert (!is_singlephase (def ));
1543
- mod = PyModule_FromDefAndSpec (def , spec );
1544
- if (mod == NULL ) {
1545
- goto finally ;
1546
- }
1547
- }
1548
- else {
1549
- assert (is_singlephase (def ));
1550
-
1551
- struct singlephase_global_update singlephase = {0 };
1552
- // gh-88216: Extensions and def->m_base.m_copy can be updated
1553
- // when the extension module doesn't support sub-interpreters.
1554
- if (def -> m_size == -1
1555
- && !is_core_module (tstate -> interp , info .name , info .path ))
1556
- {
1557
- singlephase .m_dict = PyModule_GetDict (mod );
1558
- assert (singlephase .m_dict != NULL );
1559
- }
1560
- if (update_global_state_for_extension (
1561
- tstate , info .name , info .path , def , & singlephase ) < 0 )
1562
- {
1563
- Py_CLEAR (mod );
1564
- goto finally ;
1565
- }
1566
- PyObject * modules = get_modules_dict (tstate , true);
1567
- if (finish_singlephase_extension (
1568
- tstate , mod , def , info .name , modules ) < 0 )
1569
- {
1570
- Py_CLEAR (mod );
1571
- goto finally ;
1572
- }
1573
- }
1607
+ /* Now load it. */
1608
+ mod = import_run_extension (
1609
+ tstate , p0 , & info , spec , get_modules_dict (tstate , true));
1574
1610
1575
1611
finally :
1576
1612
_Py_ext_module_loader_info_clear (& info );
@@ -3885,7 +3921,6 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
3885
3921
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
3886
3922
{
3887
3923
PyObject * mod = NULL ;
3888
- PyModuleDef * def = NULL ;
3889
3924
PyThreadState * tstate = _PyThreadState_GET ();
3890
3925
3891
3926
struct _Py_ext_module_loader_info info ;
@@ -3929,65 +3964,13 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
3929
3964
goto finally ;
3930
3965
}
3931
3966
3932
- struct _Py_ext_module_loader_result res ;
3933
- if (_PyImport_RunModInitFunc (p0 , & info , & res ) < 0 ) {
3934
- assert (PyErr_Occurred ());
3935
- goto finally ;
3936
- }
3937
-
3938
- mod = res .module ;
3939
- res .module = NULL ;
3940
- def = res .def ;
3941
- assert (def != NULL );
3942
-
3967
+ mod = import_run_extension (
3968
+ tstate , p0 , & info , spec , get_modules_dict (tstate , true));
3943
3969
if (mod == NULL ) {
3944
- //assert(!is_singlephase(def));
3945
- mod = PyModule_FromDefAndSpec (def , spec );
3946
- if (mod == NULL ) {
3947
- goto finally ;
3948
- }
3949
- }
3950
- else {
3951
- assert (is_singlephase (def ));
3952
- assert (!is_core_module (tstate -> interp , info .name , info .filename ));
3953
- assert (!is_core_module (tstate -> interp , info .name , info .name ));
3954
- mod = Py_NewRef (mod );
3955
-
3956
- const char * name_buf = PyBytes_AS_STRING (info .name_encoded );
3957
- if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed (name_buf ) < 0 ) {
3958
- Py_CLEAR (mod );
3959
- goto finally ;
3960
- }
3961
-
3962
- /* Remember the filename as the __file__ attribute */
3963
- if (PyModule_AddObjectRef (mod , "__file__" , info .filename ) < 0 ) {
3964
- PyErr_Clear (); /* Not important enough to report */
3965
- }
3966
-
3967
- struct singlephase_global_update singlephase = {0 };
3968
- // gh-88216: Extensions and def->m_base.m_copy can be updated
3969
- // when the extension module doesn't support sub-interpreters.
3970
- if (def -> m_size == -1 ) {
3971
- singlephase .m_dict = PyModule_GetDict (mod );
3972
- assert (singlephase .m_dict != NULL );
3973
- }
3974
- if (update_global_state_for_extension (
3975
- tstate , info .filename , info .name , def , & singlephase ) < 0 )
3976
- {
3977
- Py_CLEAR (mod );
3978
- goto finally ;
3979
- }
3980
-
3981
- PyObject * modules = get_modules_dict (tstate , true);
3982
- if (finish_singlephase_extension (
3983
- tstate , mod , def , info .name , modules ) < 0 )
3984
- {
3985
- Py_CLEAR (mod );
3986
- goto finally ;
3987
- }
3970
+ goto finally ;
3988
3971
}
3989
3972
3990
- // XXX Shouldn't this happen in the error cases too.
3973
+ // XXX Shouldn't this happen in the error cases too (i.e. in "finally")?
3991
3974
if (fp ) {
3992
3975
fclose (fp );
3993
3976
}
0 commit comments