@@ -1328,19 +1328,19 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
1328
1328
1329
1329
1330
1330
static PyObject *
1331
- import_find_extension (PyThreadState * tstate , PyObject * name ,
1332
- PyObject * path )
1331
+ import_find_extension (PyThreadState * tstate ,
1332
+ struct _Py_ext_module_loader_info * info )
1333
1333
{
1334
1334
/* Only single-phase init modules will be in the cache. */
1335
- PyModuleDef * def = _extensions_cache_get (path , name );
1335
+ PyModuleDef * def = _extensions_cache_get (info -> path , info -> name );
1336
1336
if (def == NULL ) {
1337
1337
return NULL ;
1338
1338
}
1339
1339
1340
1340
/* It may have been successfully imported previously
1341
1341
in an interpreter that allows legacy modules
1342
1342
but is not allowed in the current interpreter. */
1343
- const char * name_buf = PyUnicode_AsUTF8 (name );
1343
+ const char * name_buf = PyUnicode_AsUTF8 (info -> name );
1344
1344
assert (name_buf != NULL );
1345
1345
if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed (name_buf ) < 0 ) {
1346
1346
return NULL ;
@@ -1355,12 +1355,13 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
1355
1355
if (m_copy == NULL ) {
1356
1356
/* It might be a core module (e.g. sys & builtins),
1357
1357
for which we don't set m_copy. */
1358
- m_copy = get_core_module_dict (tstate -> interp , name , path );
1358
+ m_copy = get_core_module_dict (
1359
+ tstate -> interp , info -> name , info -> path );
1359
1360
if (m_copy == NULL ) {
1360
1361
return NULL ;
1361
1362
}
1362
1363
}
1363
- mod = import_add_module (tstate , name );
1364
+ mod = import_add_module (tstate , info -> name );
1364
1365
if (mod == NULL ) {
1365
1366
return NULL ;
1366
1367
}
@@ -1378,23 +1379,24 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
1378
1379
if (def -> m_base .m_init == NULL )
1379
1380
return NULL ;
1380
1381
mod = def -> m_base .m_init ();
1381
- if (mod == NULL )
1382
+ if (mod == NULL ) {
1382
1383
return NULL ;
1383
- if (PyObject_SetItem (modules , name , mod ) == -1 ) {
1384
+ }
1385
+ if (PyObject_SetItem (modules , info -> name , mod ) == -1 ) {
1384
1386
Py_DECREF (mod );
1385
1387
return NULL ;
1386
1388
}
1387
1389
}
1388
1390
if (_modules_by_index_set (tstate -> interp , def , mod ) < 0 ) {
1389
- PyMapping_DelItem (modules , name );
1391
+ PyMapping_DelItem (modules , info -> name );
1390
1392
Py_DECREF (mod );
1391
1393
return NULL ;
1392
1394
}
1393
1395
1394
1396
int verbose = _PyInterpreterState_GetConfig (tstate -> interp )-> verbose ;
1395
1397
if (verbose ) {
1396
1398
PySys_FormatStderr ("import %U # previously loaded (%R)\n" ,
1397
- name , path );
1399
+ info -> name , info -> path );
1398
1400
}
1399
1401
return mod ;
1400
1402
}
@@ -1505,44 +1507,56 @@ static PyObject*
1505
1507
create_builtin (PyThreadState * tstate , PyObject * name , PyObject * spec )
1506
1508
{
1507
1509
PyModuleDef * def = NULL ;
1508
- PyObject * mod = import_find_extension (tstate , name , name );
1510
+
1511
+ struct _Py_ext_module_loader_info info ;
1512
+ if (_Py_ext_module_loader_info_init (& info , name , NULL ) < 0 ) {
1513
+ return NULL ;
1514
+ }
1515
+
1516
+ PyObject * mod = import_find_extension (tstate , & info );
1509
1517
if (mod || _PyErr_Occurred (tstate )) {
1510
- return mod ;
1518
+ goto finally ;
1511
1519
}
1512
1520
1513
1521
struct _inittab * found = NULL ;
1514
1522
for (struct _inittab * p = INITTAB ; p -> name != NULL ; p ++ ) {
1515
- if (_PyUnicode_EqualToASCIIString (name , p -> name )) {
1523
+ if (_PyUnicode_EqualToASCIIString (info . name , p -> name )) {
1516
1524
found = p ;
1517
1525
}
1518
1526
}
1519
1527
if (found == NULL ) {
1520
1528
// not found
1521
- Py_RETURN_NONE ;
1529
+ mod = Py_NewRef (Py_None );
1530
+ goto finally ;
1522
1531
}
1523
1532
1524
1533
PyModInitFunction p0 = (PyModInitFunction )found -> initfunc ;
1525
1534
if (p0 == NULL ) {
1526
1535
/* Cannot re-init internal module ("sys" or "builtins") */
1527
- assert (is_core_module (tstate -> interp , name , name ));
1528
- return import_add_module (tstate , name );
1536
+ assert (is_core_module (tstate -> interp , info .name , info .path ));
1537
+ mod = import_add_module (tstate , info .name );
1538
+ goto finally ;
1529
1539
}
1530
1540
1531
1541
mod = p0 ();
1532
1542
if (mod == NULL ) {
1533
- return NULL ;
1543
+ goto finally ;
1534
1544
}
1535
1545
1536
1546
if (PyObject_TypeCheck (mod , & PyModuleDef_Type )) {
1537
1547
def = (PyModuleDef * )mod ;
1538
1548
assert (!is_singlephase (def ));
1539
- return PyModule_FromDefAndSpec (def , spec );
1549
+ mod = PyModule_FromDefAndSpec (def , spec );
1550
+ if (mod == NULL ) {
1551
+ goto finally ;
1552
+ }
1540
1553
}
1541
1554
else {
1542
1555
assert (PyModule_Check (mod ));
1543
1556
def = PyModule_GetDef (mod );
1544
1557
if (def == NULL ) {
1545
- return NULL ;
1558
+ Py_CLEAR (mod );
1559
+ goto finally ;
1546
1560
}
1547
1561
assert (is_singlephase (def ));
1548
1562
@@ -1553,22 +1567,29 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
1553
1567
// gh-88216: Extensions and def->m_base.m_copy can be updated
1554
1568
// when the extension module doesn't support sub-interpreters.
1555
1569
if (def -> m_size == -1
1556
- && !is_core_module (tstate -> interp , name , name ))
1570
+ && !is_core_module (tstate -> interp , info . name , info . path ))
1557
1571
{
1558
1572
singlephase .m_dict = PyModule_GetDict (mod );
1559
1573
assert (singlephase .m_dict != NULL );
1560
1574
}
1561
1575
if (update_global_state_for_extension (
1562
- tstate , name , name , def , & singlephase ) < 0 )
1576
+ tstate , info . name , info . path , def , & singlephase ) < 0 )
1563
1577
{
1564
- return NULL ;
1578
+ Py_CLEAR (mod );
1579
+ goto finally ;
1565
1580
}
1566
1581
PyObject * modules = get_modules_dict (tstate , true);
1567
- if (finish_singlephase_extension (tstate , mod , def , name , modules ) < 0 ) {
1568
- return NULL ;
1582
+ if (finish_singlephase_extension (
1583
+ tstate , mod , def , info .name , modules ) < 0 )
1584
+ {
1585
+ Py_CLEAR (mod );
1586
+ goto finally ;
1569
1587
}
1570
- return mod ;
1571
1588
}
1589
+
1590
+ finally :
1591
+ _Py_ext_module_loader_info_clear (& info );
1592
+ return mod ;
1572
1593
}
1573
1594
1574
1595
@@ -3878,28 +3899,22 @@ static PyObject *
3878
3899
_imp_create_dynamic_impl (PyObject * module , PyObject * spec , PyObject * file )
3879
3900
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
3880
3901
{
3881
- PyObject * mod , * name , * filename ;
3902
+ PyObject * mod = NULL ;
3882
3903
FILE * fp ;
3883
3904
3884
- name = PyObject_GetAttrString (spec , "name" );
3885
- if (name == NULL ) {
3886
- return NULL ;
3887
- }
3888
-
3889
- filename = PyObject_GetAttrString (spec , "origin" );
3890
- if (filename == NULL ) {
3891
- Py_DECREF (name );
3905
+ struct _Py_ext_module_loader_info info ;
3906
+ if (_Py_ext_module_loader_info_init_from_spec (& info , spec ) < 0 ) {
3892
3907
return NULL ;
3893
3908
}
3894
3909
3895
3910
PyThreadState * tstate = _PyThreadState_GET ();
3896
- mod = import_find_extension (tstate , name , filename );
3911
+ mod = import_find_extension (tstate , & info );
3897
3912
if (mod != NULL || _PyErr_Occurred (tstate )) {
3898
3913
assert (mod == NULL || !_PyErr_Occurred (tstate ));
3899
3914
goto finally ;
3900
3915
}
3901
3916
3902
- if (PySys_Audit ("import" , "OOOOO" , name , filename ,
3917
+ if (PySys_Audit ("import" , "OOOOO" , info . name , info . filename ,
3903
3918
Py_None , Py_None , Py_None ) < 0 )
3904
3919
{
3905
3920
goto finally ;
@@ -3911,7 +3926,7 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
3911
3926
* _PyImport_GetModInitFunc(), but it isn't clear if the intervening
3912
3927
* code relies on fp still being open. */
3913
3928
if (file != NULL ) {
3914
- fp = _Py_fopen_obj (filename , "r" );
3929
+ fp = _Py_fopen_obj (info . filename , "r" );
3915
3930
if (fp == NULL ) {
3916
3931
goto finally ;
3917
3932
}
@@ -3920,16 +3935,15 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
3920
3935
fp = NULL ;
3921
3936
}
3922
3937
3923
- mod = _PyImport_LoadDynamicModuleWithSpec (spec , fp );
3938
+ mod = _PyImport_LoadDynamicModuleWithSpec (& info , spec , fp );
3924
3939
3925
3940
// XXX Shouldn't this happen in the error cases too.
3926
3941
if (fp ) {
3927
3942
fclose (fp );
3928
3943
}
3929
3944
3930
3945
finally :
3931
- Py_DECREF (name );
3932
- Py_DECREF (filename );
3946
+ _Py_ext_module_loader_info_clear (& info );
3933
3947
return mod ;
3934
3948
}
3935
3949
0 commit comments