@@ -978,6 +978,16 @@ _PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char *name)
978978 return 0 ;
979979}
980980
981+ static inline int
982+ match_mod_name (PyObject * actual , const char * expected )
983+ {
984+ if (PyUnicode_CompareWithASCIIString (actual , expected ) == 0 ) {
985+ return 1 ;
986+ }
987+ assert (!PyErr_Occurred ());
988+ return 0 ;
989+ }
990+
981991static int
982992fix_up_extension (PyObject * mod , PyObject * name , PyObject * filename )
983993{
@@ -1001,7 +1011,8 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
10011011 // when the extension module doesn't support sub-interpreters.
10021012 // XXX Why special-case the main interpreter?
10031013 if (_Py_IsMainInterpreter (tstate -> interp ) || def -> m_size == -1 ) {
1004- if (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 ) {
10051016 if (def -> m_base .m_copy ) {
10061017 /* Somebody already imported the module,
10071018 likely under a different name.
@@ -1055,18 +1066,34 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
10551066 PyObject * modules = MODULES (tstate -> interp );
10561067
10571068 if (def -> m_size == -1 ) {
1069+ PyObject * m_copy = def -> m_base .m_copy ;
10581070 /* Module does not support repeated initialization */
1059- if (def -> m_base . m_copy == NULL )
1071+ if (m_copy == NULL ) {
10601072 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" );
1083+ return NULL ;
1084+ }
1085+ }
1086+ /* m_copy of Py_None means it is copied some other way. */
10611087 mod = import_add_module (tstate , name );
1062- if (mod == NULL )
1088+ if (mod == NULL ) {
10631089 return NULL ;
1090+ }
10641091 mdict = PyModule_GetDict (mod );
10651092 if (mdict == NULL ) {
10661093 Py_DECREF (mod );
10671094 return NULL ;
10681095 }
1069- if (PyDict_Update (mdict , def -> m_base . m_copy )) {
1096+ if (PyDict_Update (mdict , m_copy )) {
10701097 Py_DECREF (mod );
10711098 return NULL ;
10721099 }
0 commit comments