-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
PEP 499 implementation: "python -m foo" binds the main module as both __main__ and foo in sys.modules #80556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This issue is to track the implementation of PEP-499 which we hope to get into the upcoming 3.8 release. I've made it because cpython PRs want a bpo number in the subject line. I'll link in the proposed PR once I've filed off a few grammar issues I've just noticed (documentation English grammar). |
I've withdrawn the PR; I hadn't run the full test suite and there are things to fix. - Cameron |
New PR 12490 attached with a fix for test_pdb. More extensive comments are in the leading comment on the PR itself. - Cameron |
[Belatedly updating this issue with the current status as of March] Cameron's implementation generally looks good, but there are couple of compatibility/migration questions that we need to consider, as spelled out in the PEP update that added me as BDFL-Delegate: https://github.com/python/peps/pull/946/files
if __name__ == "__main__":
# To prevent inadvertent double imports, the -m
# switch in Python 3.9+ defaults to aliasing __main__
# under the executed module's import name. We actually
# want the double import, so remove the alias if it exists
import sys
_main_module = sys.modules.get(__name__)
_spec_module = sys.modules.get(__spec__.name)
if _main_module is _spec_module:
sys.modules.pop(__spec__.name) We'd test the above snippet by adding it to the
if __name__ == "__main__":
# To prevent inadvertent double imports, the -m
# switch in Python 3.9+ defaults to aliasing __main__
# under the executed module's import name. We need
# pickle to use the real module name for objects from
# __main__ though, so we set the import name here
_running_as_main = True
__name__ = __spec__.name |
I want to start with an apology. I have become a little swamped by work and didn't let anyone know I've made little time for this since March. To my naive eye Nick's snippet looks like it would work for pdb; I became a little stalled there trying to understand how pdb managed the main module. I totally lack the expertise to address pickle; I've never used it and the long history of feature updates sugests to me that I cannot get a deep enough understanding in a meaningful time. |
Just a remark about the preamble comment: it reads to me as though PEP-499 is a misfeature. Possibly change: We actually want the double import, so remove the alias if it exists. to: This module is unusual, and actually wants the double import. Verbose, but at least I know the intent. |
FWIW, I have some feedback on the PEP. (See msg357448.) Can we discuss here or should I open a mailing list thread? |
On 25Nov2019 17:38, Python Bug Reports <report@bugs.python.org> wrote:
Let's discuss it here unless it looks like we need wider input. This is With PEP-499, "python -m foo" should have __name__=='__main__' and |
Exactly. :) I'd expect PEP-499 to specify changing __module__ of classes and functions from __main__ to the module name (spec.name). This aligns closely with the whole point of the PEP. :) As a bonus, it will simplify things for pickling (which doesn't inherently deal well with __main__). |
Leaving the relationship between pickle and __name__ alone wasn't an oversight, as folks already rely on that to gracefully transition from single-file modules to multi-file packages without breaking pickle compatibility in either direction. The trick is to do a "from ._submodule import *" in the package's __init__.py (exposing all the names), and then a "__name__ = __package__" in the submodule itself. Thus the second snippet above, as a way to port code that was specifically relying on the double import to provide pickle compatibility without risking introducing other unintended incompatibility problems. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: