[issue46360] Inconsistent import behavior for (unusual) submodules

2022-01-13 Thread Eric Snow


Eric Snow  added the comment:

> I'm going to assume the "even though sys.modules has `None`" case,
> which I think is an oversight and should probably get fixed

Yep, I agree.  That's the case I was looking at in the first place.  I noticed 
the other two as I was hacking together code to verify the None behavior. :)  
Bothering to change those would be more trouble than its worth.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46360] Inconsistent import behavior for (unusual) submodules

2022-01-13 Thread Brett Cannon

Brett Cannon  added the comment:

So which inconsistency do you want to change because you listed three and this 
is only one issue. 

I'm going to assume the "even though sys.modules has `None`" case, which I 
think is an oversight and should probably get fixed, but I also don't know what 
promises the language spec makes around this.

As for the other two, you can open separate issues if you want to discuss them, 
but I double-check what the language spec says as I am tempted to say both are 
fine (and specifically in the latter case that's on you to have not messed up 
and left the attribute off).

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46360] Inconsistent import behavior for (unusual) submodules

2022-01-12 Thread Eric Snow


New submission from Eric Snow :

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 "", line 1, in 
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.

--
assignee: eric.snow
components: Interpreter Core
messages: 410433
nosy: barry, brett.cannon, eric.snow, ncoghlan
priority: normal
severity: normal
stage: needs patch
status: open
title: Inconsistent import behavior for (unusual) submodules
type: behavior
versions: Python 3.10, Python 3.11, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com