Title: PyImport_GetModule() can return partially-initialized module
Type: behavior Stage: needs patch
Components: Interpreter Core Versions: Python 3.8, Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, eric.snow, ncoghlan, p-ganssle, pablogsal, pitrou, vstinner
Priority: normal Keywords:

Created on 2019-02-08 17:13 by pitrou, last changed 2019-02-22 17:27 by pitrou.

Messages (2)
msg335097 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2019-02-08 17:13
PyImport_GetModule() returns whatever is in sys.modules, even if the module is still importing and therefore only partially initialized.

One possibility is to reuse the optimization already done in PyImport_ImportModuleLevelObject():

        /* Optimization: only call _bootstrap._lock_unlock_module() if
           __spec__._initializing is true.
           NOTE: because of this, initializing must be set *before*
           stuffing the new module in sys.modules.
        spec = _PyObject_GetAttrId(mod, &PyId___spec__);
        if (_PyModuleSpec_IsInitializing(spec)) {
            PyObject *value = _PyObject_CallMethodIdObjArgs(interp->importlib,
                                            &PyId__lock_unlock_module, abs_name,
            if (value == NULL) {
                goto error;

Issue originally mentioned in issue34572.
msg335100 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2019-02-08 17:18
Yeah, that makes sense.
Date User Action Args
2019-02-22 17:27:35pitrousetnosy: + vstinner
2019-02-08 18:56:13p-gansslesetnosy: + p-ganssle
2019-02-08 17:19:40pitrousetnosy: + pablogsal
2019-02-08 17:18:15eric.snowsetmessages: + msg335100
2019-02-08 17:13:37pitroucreate