Author ncoghlan
Recipients brett.cannon, eric.snow, martin.panter, ncoghlan, wolma
Date 2016-07-12.04:06:33
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1468296394.77.0.983351456836.issue27487@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2016-07-12 04:06:34ncoghlansetrecipients: + ncoghlan, brett.cannon, eric.snow, martin.panter, wolma
2016-07-12 04:06:34ncoghlansetmessageid: <1468296394.77.0.983351456836.issue27487@psf.upfronthosting.co.za>
2016-07-12 04:06:34ncoghlanlinkissue27487 messages
2016-07-12 04:06:33ncoghlancreate