@@ -978,14 +978,29 @@ _PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char *name)
978
978
return 0 ;
979
979
}
980
980
981
- static inline int
982
- match_mod_name (PyObject * actual , const char * expected )
981
+ static PyObject *
982
+ get_core_module_dict (PyInterpreterState * interp ,
983
+ PyObject * name , PyObject * filename )
983
984
{
984
- if (PyUnicode_CompareWithASCIIString (actual , expected ) == 0 ) {
985
- return 1 ;
985
+ /* Only builtin modules are core. */
986
+ if (filename == name ) {
987
+ assert (!PyErr_Occurred ());
988
+ if (PyUnicode_CompareWithASCIIString (name , "sys" ) == 0 ) {
989
+ return interp -> sysdict_copy ;
990
+ }
991
+ assert (!PyErr_Occurred ());
992
+ if (PyUnicode_CompareWithASCIIString (name , "builtins" ) == 0 ) {
993
+ return interp -> builtins_copy ;
994
+ }
995
+ assert (!PyErr_Occurred ());
986
996
}
987
- assert (!PyErr_Occurred ());
988
- return 0 ;
997
+ return NULL ;
998
+ }
999
+
1000
+ static inline int
1001
+ is_core_module (PyInterpreterState * interp , PyObject * name , PyObject * filename )
1002
+ {
1003
+ return get_core_module_dict (interp , name , filename ) != NULL ;
989
1004
}
990
1005
991
1006
static int
@@ -1009,10 +1024,8 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
1009
1024
1010
1025
// bpo-44050: Extensions and def->m_base.m_copy can be updated
1011
1026
// when the extension module doesn't support sub-interpreters.
1012
- // XXX Why special-case the main interpreter?
1013
- if (_Py_IsMainInterpreter (tstate -> interp ) || def -> m_size == -1 ) {
1014
- /* m_copy of Py_None means it is copied some other way. */
1015
- if (def -> m_size == -1 && def -> m_base .m_copy != Py_None ) {
1027
+ if (def -> m_size == -1 ) {
1028
+ if (!is_core_module (tstate -> interp , name , filename )) {
1016
1029
if (def -> m_base .m_copy ) {
1017
1030
/* Somebody already imported the module,
1018
1031
likely under a different name.
@@ -1028,7 +1041,10 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
1028
1041
return -1 ;
1029
1042
}
1030
1043
}
1044
+ }
1031
1045
1046
+ // XXX Why special-case the main interpreter?
1047
+ if (_Py_IsMainInterpreter (tstate -> interp ) || def -> m_size == -1 ) {
1032
1048
if (_extensions_cache_set (filename , name , def ) < 0 ) {
1033
1049
return -1 ;
1034
1050
}
@@ -1069,21 +1085,11 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
1069
1085
PyObject * m_copy = def -> m_base .m_copy ;
1070
1086
/* Module does not support repeated initialization */
1071
1087
if (m_copy == NULL ) {
1072
- return NULL ;
1073
- }
1074
- else if (m_copy == Py_None ) {
1075
- if (match_mod_name (name , "sys" )) {
1076
- m_copy = tstate -> interp -> sysdict_copy ;
1077
- }
1078
- else if (match_mod_name (name , "builtins" )) {
1079
- m_copy = tstate -> interp -> builtins_copy ;
1080
- }
1081
- else {
1082
- _PyErr_SetString (tstate , PyExc_ImportError , "missing m_copy" );
1088
+ m_copy = get_core_module_dict (tstate -> interp , name , filename );
1089
+ if (m_copy == NULL ) {
1083
1090
return NULL ;
1084
1091
}
1085
1092
}
1086
- /* m_copy of Py_None means it is copied some other way. */
1087
1093
mod = import_add_module (tstate , name );
1088
1094
if (mod == NULL ) {
1089
1095
return NULL ;
0 commit comments