Author eric.snow
Recipients barry, brett.cannon, eric.snow, ncoghlan
Date 2022-01-12.22:04:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1642025090.39.0.843504931319.issue46360@roundup.psfhosted.org>
In-reply-to
Content
Let's look at a hypothetical module "spam" and its submodule "spam.eggs":

```
# spam.py
import sys
sys.modules['spam.eggs'] = None
```

Then:

>>> import spam.eggs
>>> import sys
>>> sys.modules['spam.eggs'] is None
True
>>> spam.eggs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'spam' has no attribute 'eggs'

The key inconsistent behaviors:

* `import spam.eggs` succeeded even though the sys.modules entry is None
* `import spam.eggs` succeeded even though "spam" isn't a package (e.g. no `__path__`, `spec.submodule_search_locations`, etc.)
* the "eggs" attr wasn't bound on "spam"

The relevant code is _find_and_load_unlocked() and _find_and_load() in Lib/importlib/_bootstrap.py.

In _find_and_load_unlocked() we first import the parent module.  Then we have a special case, where we see if "spam.eggs" was added to sys.modules as a side effect.  If it was then we short-circuit the rest of import and return the submodule as-is.  This leads to some of the inconsistent behavior described above, since the subsequent code (e.g. checks, binding to the parent) get skipped.

In _find_and_load() we have code which raises ModuleNotFoundError if the resulting module is None, which acts as a marker that importing the module is disabled.  This check is always skipped when importing the module for the first time, leading to the other inconsistent behavior from above.

The is definitely a corner case, but os.path demonstrates it's a real scenario.  In fact, os.path is what drew my attention to this code.

Is it worth fixing?  The change shouldn't be invasive so I'm leaning toward yes.  It isn't a high priority though.
History
Date User Action Args
2022-01-12 22:04:50eric.snowsetrecipients: + eric.snow, barry, brett.cannon, ncoghlan
2022-01-12 22:04:50eric.snowsetmessageid: <1642025090.39.0.843504931319.issue46360@roundup.psfhosted.org>
2022-01-12 22:04:50eric.snowlinkissue46360 messages
2022-01-12 22:04:50eric.snowcreate