Nick Coghlan added the comment:

Since this worked in 3.5.1, and fails in 3.5.2, I think it's reasonable to 
consider if it makes sense to find a way to make it work again in 3.5.3 (and 
then decide separately whether or not we want to preserve the capability in 
3.6.0).

Specifically, restoring the old behaviour would mean that:

1. *If* the -m target module already exists in sys.modules after importing the 
parent module; and
2. The already imported target module doesn't have a __spec__ attribute; then
3. Ignore that already imported artifact when figuring out what to run as 
__main__

However, the problem with doing that is that this *is* an instance of code that 
falls into the double-import trap: 
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#executing-the-main-module-twice

Even in 3.5.1, the affected module is getting executed twice - once from the 
package's __init__.py, and once via the "-m" switch. For cases that *don't* 
overwrite the module in sys.modules, a solution that resolves the issue in a 
backwards and forwards compatible way is to split the module being executed via 
-m as follows:

* Move the pieces needed by mypkg.__init__ into a "mypkg._mymain_support" module
* In that module, set "__name__ = 'mypkg.mymain'" to preserve pickle 
compatibility and docs introspection
* Change both "mypkg.__init__" and "mypkg.mymain" to import the shared 
components from the "mypkg._mymain_support" module

However, you still run into a problem with figuring out when the replacement 
module should be written into sys.modules - if that stays in "mypkg.mymain" it 
will no longer get done as a side effect of importing "mypkg", while if it 
moves into "mypkg._mymain_support", you'll still run into the problem reported 
here in 3.5.2

----------
nosy: +brett.cannon, eric.snow

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27487>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to