@@ -1125,10 +1125,10 @@ _PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char *name)
1125
1125
1126
1126
static PyObject *
1127
1127
get_core_module_dict (PyInterpreterState * interp ,
1128
- PyObject * name , PyObject * filename )
1128
+ PyObject * name , PyObject * path )
1129
1129
{
1130
1130
/* Only builtin modules are core. */
1131
- if (filename == name ) {
1131
+ if (path == name ) {
1132
1132
assert (!PyErr_Occurred ());
1133
1133
if (PyUnicode_CompareWithASCIIString (name , "sys" ) == 0 ) {
1134
1134
return interp -> sysdict_copy ;
@@ -1143,11 +1143,11 @@ get_core_module_dict(PyInterpreterState *interp,
1143
1143
}
1144
1144
1145
1145
static inline int
1146
- is_core_module (PyInterpreterState * interp , PyObject * name , PyObject * filename )
1146
+ is_core_module (PyInterpreterState * interp , PyObject * name , PyObject * path )
1147
1147
{
1148
1148
/* This might be called before the core dict copies are in place,
1149
1149
so we can't rely on get_core_module_dict() here. */
1150
- if (filename == name ) {
1150
+ if (path == name ) {
1151
1151
if (PyUnicode_CompareWithASCIIString (name , "sys" ) == 0 ) {
1152
1152
return 1 ;
1153
1153
}
@@ -1159,7 +1159,7 @@ is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *filename)
1159
1159
}
1160
1160
1161
1161
static int
1162
- fix_up_extension (PyObject * mod , PyObject * name , PyObject * filename )
1162
+ fix_up_extension (PyObject * mod , PyObject * name , PyObject * path )
1163
1163
{
1164
1164
if (mod == NULL || !PyModule_Check (mod )) {
1165
1165
PyErr_BadInternalCall ();
@@ -1180,7 +1180,7 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
1180
1180
// bpo-44050: Extensions and def->m_base.m_copy can be updated
1181
1181
// when the extension module doesn't support sub-interpreters.
1182
1182
if (def -> m_size == -1 ) {
1183
- if (!is_core_module (tstate -> interp , name , filename )) {
1183
+ if (!is_core_module (tstate -> interp , name , path )) {
1184
1184
assert (PyUnicode_CompareWithASCIIString (name , "sys" ) != 0 );
1185
1185
assert (PyUnicode_CompareWithASCIIString (name , "builtins" ) != 0 );
1186
1186
if (def -> m_base .m_copy ) {
@@ -1202,7 +1202,7 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
1202
1202
1203
1203
// XXX Why special-case the main interpreter?
1204
1204
if (_Py_IsMainInterpreter (tstate -> interp ) || def -> m_size == -1 ) {
1205
- if (_extensions_cache_set (filename , name , def ) < 0 ) {
1205
+ if (_extensions_cache_set (path , name , def ) < 0 ) {
1206
1206
return -1 ;
1207
1207
}
1208
1208
}
@@ -1227,10 +1227,10 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
1227
1227
1228
1228
static PyObject *
1229
1229
import_find_extension (PyThreadState * tstate , PyObject * name ,
1230
- PyObject * filename )
1230
+ PyObject * path )
1231
1231
{
1232
1232
/* Only single-phase init modules will be in the cache. */
1233
- PyModuleDef * def = _extensions_cache_get (filename , name );
1233
+ PyModuleDef * def = _extensions_cache_get (path , name );
1234
1234
if (def == NULL ) {
1235
1235
return NULL ;
1236
1236
}
@@ -1253,7 +1253,7 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
1253
1253
if (m_copy == NULL ) {
1254
1254
/* It might be a core module (e.g. sys & builtins),
1255
1255
for which we don't set m_copy. */
1256
- m_copy = get_core_module_dict (tstate -> interp , name , filename );
1256
+ m_copy = get_core_module_dict (tstate -> interp , name , path );
1257
1257
if (m_copy == NULL ) {
1258
1258
return NULL ;
1259
1259
}
@@ -1292,16 +1292,16 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
1292
1292
int verbose = _PyInterpreterState_GetConfig (tstate -> interp )-> verbose ;
1293
1293
if (verbose ) {
1294
1294
PySys_FormatStderr ("import %U # previously loaded (%R)\n" ,
1295
- name , filename );
1295
+ name , path );
1296
1296
}
1297
1297
return mod ;
1298
1298
}
1299
1299
1300
1300
static int
1301
1301
clear_singlephase_extension (PyInterpreterState * interp ,
1302
- PyObject * name , PyObject * filename )
1302
+ PyObject * name , PyObject * path )
1303
1303
{
1304
- PyModuleDef * def = _extensions_cache_get (filename , name );
1304
+ PyModuleDef * def = _extensions_cache_get (path , name );
1305
1305
if (def == NULL ) {
1306
1306
if (PyErr_Occurred ()) {
1307
1307
return -1 ;
@@ -1322,7 +1322,7 @@ clear_singlephase_extension(PyInterpreterState *interp,
1322
1322
}
1323
1323
1324
1324
/* Clear the cached module def. */
1325
- _extensions_cache_delete (filename , name );
1325
+ _extensions_cache_delete (path , name );
1326
1326
1327
1327
return 0 ;
1328
1328
}
@@ -1336,18 +1336,28 @@ int
1336
1336
_PyImport_FixupBuiltin (PyObject * mod , const char * name , PyObject * modules )
1337
1337
{
1338
1338
int res = -1 ;
1339
+ assert (mod != NULL && PyModule_Check (mod ));
1340
+
1339
1341
PyObject * nameobj ;
1340
1342
nameobj = PyUnicode_InternFromString (name );
1341
1343
if (nameobj == NULL ) {
1342
1344
return -1 ;
1343
1345
}
1346
+
1347
+ PyModuleDef * def = PyModule_GetDef (mod );
1348
+ if (def == NULL ) {
1349
+ PyErr_BadInternalCall ();
1350
+ goto finally ;
1351
+ }
1352
+
1344
1353
if (PyObject_SetItem (modules , nameobj , mod ) < 0 ) {
1345
1354
goto finally ;
1346
1355
}
1347
1356
if (fix_up_extension (mod , nameobj , nameobj ) < 0 ) {
1348
1357
PyMapping_DelItem (modules , nameobj );
1349
1358
goto finally ;
1350
1359
}
1360
+
1351
1361
res = 0 ;
1352
1362
1353
1363
finally :
@@ -1382,39 +1392,45 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
1382
1392
}
1383
1393
1384
1394
PyObject * modules = MODULES (tstate -> interp );
1395
+ struct _inittab * found = NULL ;
1385
1396
for (struct _inittab * p = INITTAB ; p -> name != NULL ; p ++ ) {
1386
1397
if (_PyUnicode_EqualToASCIIString (name , p -> name )) {
1387
- if (p -> initfunc == NULL ) {
1388
- /* Cannot re-init internal module ("sys" or "builtins") */
1389
- return import_add_module (tstate , name );
1390
- }
1391
- mod = (* p -> initfunc )();
1392
- if (mod == NULL ) {
1393
- return NULL ;
1394
- }
1398
+ found = p ;
1399
+ }
1400
+ }
1401
+ if (found == NULL ) {
1402
+ // not found
1403
+ Py_RETURN_NONE ;
1404
+ }
1395
1405
1396
- if (PyObject_TypeCheck (mod , & PyModuleDef_Type )) {
1397
- return PyModule_FromDefAndSpec ((PyModuleDef * )mod , spec );
1398
- }
1399
- else {
1400
- /* Remember pointer to module init function. */
1401
- PyModuleDef * def = PyModule_GetDef (mod );
1402
- if (def == NULL ) {
1403
- return NULL ;
1404
- }
1406
+ PyModInitFunction p0 = (PyModInitFunction )found -> initfunc ;
1407
+ if (p0 == NULL ) {
1408
+ /* Cannot re-init internal module ("sys" or "builtins") */
1409
+ assert (is_core_module (tstate -> interp , name , name ));
1410
+ return import_add_module (tstate , name );
1411
+ }
1405
1412
1406
- def -> m_base .m_init = p -> initfunc ;
1407
- if (_PyImport_FixupExtensionObject (mod , name , name ,
1408
- modules ) < 0 ) {
1409
- return NULL ;
1410
- }
1411
- return mod ;
1412
- }
1413
- }
1413
+ mod = p0 ();
1414
+ if (mod == NULL ) {
1415
+ return NULL ;
1414
1416
}
1415
1417
1416
- // not found
1417
- Py_RETURN_NONE ;
1418
+ if (PyObject_TypeCheck (mod , & PyModuleDef_Type )) {
1419
+ return PyModule_FromDefAndSpec ((PyModuleDef * )mod , spec );
1420
+ }
1421
+ else {
1422
+ /* Remember pointer to module init function. */
1423
+ PyModuleDef * def = PyModule_GetDef (mod );
1424
+ if (def == NULL ) {
1425
+ return NULL ;
1426
+ }
1427
+
1428
+ def -> m_base .m_init = p0 ;
1429
+ if (_PyImport_FixupExtensionObject (mod , name , name , modules ) < 0 ) {
1430
+ return NULL ;
1431
+ }
1432
+ return mod ;
1433
+ }
1418
1434
}
1419
1435
1420
1436
@@ -3724,44 +3740,64 @@ static PyObject *
3724
3740
_imp_create_dynamic_impl (PyObject * module , PyObject * spec , PyObject * file )
3725
3741
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
3726
3742
{
3727
- PyObject * mod , * name , * path ;
3743
+ PyObject * mod , * name , * filename ;
3728
3744
FILE * fp ;
3729
3745
3730
3746
name = PyObject_GetAttrString (spec , "name" );
3731
3747
if (name == NULL ) {
3732
3748
return NULL ;
3733
3749
}
3734
3750
3735
- path = PyObject_GetAttrString (spec , "origin" );
3736
- if (path == NULL ) {
3751
+ filename = PyObject_GetAttrString (spec , "origin" );
3752
+ if (filename == NULL ) {
3737
3753
Py_DECREF (name );
3738
3754
return NULL ;
3739
3755
}
3740
3756
3741
3757
PyThreadState * tstate = _PyThreadState_GET ();
3742
- mod = import_find_extension (tstate , name , path );
3758
+ mod = import_find_extension (tstate , name , filename );
3743
3759
if (mod != NULL || _PyErr_Occurred (tstate )) {
3744
3760
assert (mod == NULL || !_PyErr_Occurred (tstate ));
3745
3761
goto finally ;
3746
3762
}
3747
3763
3764
+ if (PySys_Audit ("import" , "OOOOO" , name , filename ,
3765
+ Py_None , Py_None , Py_None ) < 0 )
3766
+ {
3767
+ goto finally ;
3768
+ }
3769
+
3770
+ /* Is multi-phase init or this is the first time being loaded. */
3771
+
3772
+ /* We would move this (and the fclose() below) into
3773
+ * _PyImport_GetModInitFunc(), but it isn't clear if the intervening
3774
+ * code relies on fp still being open. */
3748
3775
if (file != NULL ) {
3749
- fp = _Py_fopen_obj (path , "r" );
3776
+ fp = _Py_fopen_obj (filename , "r" );
3750
3777
if (fp == NULL ) {
3751
3778
goto finally ;
3752
3779
}
3753
3780
}
3754
- else
3781
+ else {
3755
3782
fp = NULL ;
3783
+ }
3756
3784
3757
3785
mod = _PyImport_LoadDynamicModuleWithSpec (spec , fp );
3786
+ if (mod != NULL ) {
3787
+ /* Remember the filename as the __file__ attribute */
3788
+ if (PyModule_AddObjectRef (mod , "__file__" , filename ) < 0 ) {
3789
+ PyErr_Clear (); /* Not important enough to report */
3790
+ }
3791
+ }
3758
3792
3759
- if (fp )
3793
+ // XXX Shouldn't this happen in the error cases too.
3794
+ if (fp ) {
3760
3795
fclose (fp );
3796
+ }
3761
3797
3762
3798
finally :
3763
3799
Py_DECREF (name );
3764
- Py_DECREF (path );
3800
+ Py_DECREF (filename );
3765
3801
return mod ;
3766
3802
}
3767
3803
0 commit comments