@@ -1377,14 +1377,21 @@ import_find_extension(PyThreadState *tstate,
1377
1377
}
1378
1378
struct _Py_ext_module_loader_result res ;
1379
1379
if (_PyImport_RunModInitFunc (def -> m_base .m_init , info , & res ) < 0 ) {
1380
- _Py_ext_module_loader_result_apply_error (& res );
1380
+ _Py_ext_module_loader_result_apply_error (& res , name_buf );
1381
+ _Py_ext_module_loader_result_clear (& res );
1381
1382
return NULL ;
1382
1383
}
1383
- assert (!PyErr_Occurred ());
1384
+ assert (!PyErr_Occurred () && res . err == NULL );
1384
1385
assert (res .kind == _Py_ext_module_loader_result_SINGLEPHASE );
1385
1386
mod = res .module ;
1387
+ /* Tchnically, the init function could return a different module def.
1388
+ * Then we would probably need to update the global cache.
1389
+ * However, we don't expect anyone to change the def. */
1390
+ assert (res .def == def );
1391
+ _Py_ext_module_loader_result_clear (& res );
1386
1392
1387
1393
// XXX __file__ doesn't get set!
1394
+
1388
1395
if (PyObject_SetItem (modules , info -> name , mod ) == -1 ) {
1389
1396
Py_DECREF (mod );
1390
1397
return NULL ;
@@ -1411,15 +1418,16 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
1411
1418
{
1412
1419
PyObject * mod = NULL ;
1413
1420
PyModuleDef * def = NULL ;
1421
+ const char * name_buf = PyBytes_AS_STRING (info -> name_encoded );
1414
1422
1415
1423
struct _Py_ext_module_loader_result res ;
1416
1424
if (_PyImport_RunModInitFunc (p0 , info , & res ) < 0 ) {
1417
1425
/* We discard res.def. */
1418
1426
assert (res .module == NULL );
1419
- _Py_ext_module_loader_result_apply_error (& res );
1427
+ _Py_ext_module_loader_result_apply_error (& res , name_buf );
1420
1428
goto finally ;
1421
1429
}
1422
- assert (!PyErr_Occurred ());
1430
+ assert (!PyErr_Occurred () && res . err == NULL );
1423
1431
1424
1432
mod = res .module ;
1425
1433
res .module = NULL ;
@@ -1439,7 +1447,6 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
1439
1447
assert (is_singlephase (def ));
1440
1448
assert (PyModule_Check (mod ));
1441
1449
1442
- const char * name_buf = PyBytes_AS_STRING (info -> name_encoded );
1443
1450
if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed (name_buf ) < 0 ) {
1444
1451
Py_CLEAR (mod );
1445
1452
goto finally ;
@@ -1448,13 +1455,14 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
1448
1455
/* Remember pointer to module init function. */
1449
1456
def -> m_base .m_init = p0 ;
1450
1457
1458
+ /* Remember the filename as the __file__ attribute */
1451
1459
if (info -> filename != NULL ) {
1452
- /* Remember the filename as the __file__ attribute */
1453
1460
if (PyModule_AddObjectRef (mod , "__file__" , info -> filename ) < 0 ) {
1454
1461
PyErr_Clear (); /* Not important enough to report */
1455
1462
}
1456
1463
}
1457
1464
1465
+ /* Update global import state. */
1458
1466
struct singlephase_global_update singlephase = {0 };
1459
1467
// gh-88216: Extensions and def->m_base.m_copy can be updated
1460
1468
// when the extension module doesn't support sub-interpreters.
@@ -1471,6 +1479,7 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
1471
1479
goto finally ;
1472
1480
}
1473
1481
1482
+ /* Update per-interpreter import state. */
1474
1483
PyObject * modules = get_modules_dict (tstate , true);
1475
1484
if (finish_singlephase_extension (
1476
1485
tstate , mod , def , info -> name , modules ) < 0 )
@@ -1481,6 +1490,7 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
1481
1490
}
1482
1491
1483
1492
finally :
1493
+ _Py_ext_module_loader_result_clear (& res );
1484
1494
return mod ;
1485
1495
}
1486
1496
0 commit comments