mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	gh-102660: Handle m_copy Specially for the sys and builtins Modules (gh-102661)
It doesn't make sense to use multi-phase init for these modules. Using a per-interpreter "m_copy" (instead of PyModuleDef.m_base.m_copy) makes this work okay. (This came up while working on gh-101660.) Note that we might instead end up disallowing re-load for sys/builtins since they are so special. https://github.com/python/cpython/issues/102660
This commit is contained in:
		
							parent
							
								
									80abd62647
								
							
						
					
					
						commit
						cdb21ba74d
					
				
					 5 changed files with 44 additions and 4 deletions
				
			
		| 
						 | 
					@ -111,6 +111,7 @@ struct _is {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    PyObject *dict;  /* Stores per-interpreter state */
 | 
					    PyObject *dict;  /* Stores per-interpreter state */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    PyObject *sysdict_copy;
 | 
				
			||||||
    PyObject *builtins_copy;
 | 
					    PyObject *builtins_copy;
 | 
				
			||||||
    // Initialized to _PyEval_EvalFrameDefault().
 | 
					    // Initialized to _PyEval_EvalFrameDefault().
 | 
				
			||||||
    _PyFrameEvalFunction eval_frame;
 | 
					    _PyFrameEvalFunction eval_frame;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3098,6 +3098,9 @@ _PyBuiltin_Init(PyInterpreterState *interp)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    Py_DECREF(debug);
 | 
					    Py_DECREF(debug);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* m_copy of Py_None means it is copied some other way. */
 | 
				
			||||||
 | 
					    builtinsmodule.m_base.m_copy = Py_NewRef(Py_None);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return mod;
 | 
					    return mod;
 | 
				
			||||||
#undef ADD_TO_ALL
 | 
					#undef ADD_TO_ALL
 | 
				
			||||||
#undef SETBUILTIN
 | 
					#undef SETBUILTIN
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -978,6 +978,16 @@ _PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char *name)
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static inline int
 | 
				
			||||||
 | 
					match_mod_name(PyObject *actual, const char *expected)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if (PyUnicode_CompareWithASCIIString(actual, expected) == 0) {
 | 
				
			||||||
 | 
					        return 1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    assert(!PyErr_Occurred());
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int
 | 
					static int
 | 
				
			||||||
fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
 | 
					fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -1001,7 +1011,8 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
 | 
				
			||||||
    // when the extension module doesn't support sub-interpreters.
 | 
					    // when the extension module doesn't support sub-interpreters.
 | 
				
			||||||
    // XXX Why special-case the main interpreter?
 | 
					    // XXX Why special-case the main interpreter?
 | 
				
			||||||
    if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
 | 
					    if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
 | 
				
			||||||
        if (def->m_size == -1) {
 | 
					        /* m_copy of Py_None means it is copied some other way. */
 | 
				
			||||||
 | 
					        if (def->m_size == -1 && def->m_base.m_copy != Py_None) {
 | 
				
			||||||
            if (def->m_base.m_copy) {
 | 
					            if (def->m_base.m_copy) {
 | 
				
			||||||
                /* Somebody already imported the module,
 | 
					                /* Somebody already imported the module,
 | 
				
			||||||
                   likely under a different name.
 | 
					                   likely under a different name.
 | 
				
			||||||
| 
						 | 
					@ -1055,18 +1066,34 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
 | 
				
			||||||
    PyObject *modules = MODULES(tstate->interp);
 | 
					    PyObject *modules = MODULES(tstate->interp);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (def->m_size == -1) {
 | 
					    if (def->m_size == -1) {
 | 
				
			||||||
 | 
					        PyObject *m_copy = def->m_base.m_copy;
 | 
				
			||||||
        /* Module does not support repeated initialization */
 | 
					        /* Module does not support repeated initialization */
 | 
				
			||||||
        if (def->m_base.m_copy == NULL)
 | 
					        if (m_copy == NULL) {
 | 
				
			||||||
            return NULL;
 | 
					            return NULL;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else if (m_copy == Py_None) {
 | 
				
			||||||
 | 
					            if (match_mod_name(name, "sys")) {
 | 
				
			||||||
 | 
					                m_copy = tstate->interp->sysdict_copy;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else if (match_mod_name(name, "builtins")) {
 | 
				
			||||||
 | 
					                m_copy = tstate->interp->builtins_copy;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else {
 | 
				
			||||||
 | 
					                _PyErr_SetString(tstate, PyExc_ImportError, "missing m_copy");
 | 
				
			||||||
 | 
					                return NULL;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        /* m_copy of Py_None means it is copied some other way. */
 | 
				
			||||||
        mod = import_add_module(tstate, name);
 | 
					        mod = import_add_module(tstate, name);
 | 
				
			||||||
        if (mod == NULL)
 | 
					        if (mod == NULL) {
 | 
				
			||||||
            return NULL;
 | 
					            return NULL;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        mdict = PyModule_GetDict(mod);
 | 
					        mdict = PyModule_GetDict(mod);
 | 
				
			||||||
        if (mdict == NULL) {
 | 
					        if (mdict == NULL) {
 | 
				
			||||||
            Py_DECREF(mod);
 | 
					            Py_DECREF(mod);
 | 
				
			||||||
            return NULL;
 | 
					            return NULL;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (PyDict_Update(mdict, def->m_base.m_copy)) {
 | 
					        if (PyDict_Update(mdict, m_copy)) {
 | 
				
			||||||
            Py_DECREF(mod);
 | 
					            Py_DECREF(mod);
 | 
				
			||||||
            return NULL;
 | 
					            return NULL;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -805,6 +805,7 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
 | 
				
			||||||
    assert(interp->imports.importlib == NULL);
 | 
					    assert(interp->imports.importlib == NULL);
 | 
				
			||||||
    assert(interp->imports.import_func == NULL);
 | 
					    assert(interp->imports.import_func == NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Py_CLEAR(interp->sysdict_copy);
 | 
				
			||||||
    Py_CLEAR(interp->builtins_copy);
 | 
					    Py_CLEAR(interp->builtins_copy);
 | 
				
			||||||
    Py_CLEAR(interp->dict);
 | 
					    Py_CLEAR(interp->dict);
 | 
				
			||||||
#ifdef HAVE_FORK
 | 
					#ifdef HAVE_FORK
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3425,12 +3425,20 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
 | 
				
			||||||
        return _PyStatus_ERR("failed to create a module object");
 | 
					        return _PyStatus_ERR("failed to create a module object");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* m_copy of Py_None means it is copied some other way. */
 | 
				
			||||||
 | 
					    sysmodule.m_base.m_copy = Py_NewRef(Py_None);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    PyObject *sysdict = PyModule_GetDict(sysmod);
 | 
					    PyObject *sysdict = PyModule_GetDict(sysmod);
 | 
				
			||||||
    if (sysdict == NULL) {
 | 
					    if (sysdict == NULL) {
 | 
				
			||||||
        goto error;
 | 
					        goto error;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    interp->sysdict = Py_NewRef(sysdict);
 | 
					    interp->sysdict = Py_NewRef(sysdict);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    interp->sysdict_copy = PyDict_Copy(sysdict);
 | 
				
			||||||
 | 
					    if (interp->sysdict_copy == NULL) {
 | 
				
			||||||
 | 
					        goto error;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (PyDict_SetItemString(sysdict, "modules", modules) < 0) {
 | 
					    if (PyDict_SetItemString(sysdict, "modules", modules) < 0) {
 | 
				
			||||||
        goto error;
 | 
					        goto error;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue