[Python-Dev] BDFL-Delegate appointments for several PEPs (original) (raw)
Terry Reedy tjreedy at udel.edu
Mon Mar 25 03:52:45 EDT 2019
- Previous message (by thread): [Python-Dev] BDFL-Delegate appointments for several PEPs
- Next message (by thread): [Python-Dev] BDFL-Delegate appointments for several PEPs
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 3/25/2019 12:27 AM, Cameron Simpson wrote:
On 24Mar2019 23:22, Terry Reedy <tjreedy at udel.edu> wrote:
On 3/24/2019 10:01 PM, Terry Reedy wrote:
On 3/24/2019 7:00 PM, Cameron Simpson wrote:
Did you have a specific scenario in mind?
I was thinking about IDLE and its tangled web of circular inports, but I am now convinced that this change will not affect it. Indeed, idlelib/pyshell.py already implements idea of the proposal, ending with if name == "main": sys.modules['pyshell'] = sys.modules['main'] main() After more investigation, I realized that to stop having duplicate modulue: 1. The alias should be 'idlelib.pyshell', not 'pyshell', at least when imports are all absolute. The PEP499 patch effectively uses main.spec.name for the name of the alias. Does that simplify your issue? The current PR is here if you want to look at it: https://github.com/python/cpython/pull/12490
The new test passes on Win10.
2. It should be done at the top of the file, before the import of modules that import pyshell. Hmm, if PEP499 comes in you shouldn't need to do this at all. If PEP499 gets delayed or rejected I guess you're supporting this without it. Yes, you'll want to do it before any other imports happen (well, as you say, before any which import pyshell). What about (untested): if name == 'main': if spec.name not in sys.modules:
When I start pyshell in my master repository directory on windows with python -m idlelib.pyshell spec.name is 'idlelib.pyshell, which I currently hard-coded. When I start with what should be equivalent python f:/dev/3x/lib/idlelib/pyshell.py spec is None and spec.name an attribute error.
sys.modules[spec.name] = sys.modules['main']
as a forward compatible setup?
If I run python f:/dev/3x/lib/idlelib/pyshell.py, the PEP patch would have to notice that pyshell is a module within idlelib and alias 'main' to 'idlelib.pyshell', not 'pyshell'. Would the same be true if within-package import were all relative? I think so because we're using .spec.name, which I though was post import name resolution.
You must be doing something different when spec is None ;-). I tested the patch and it does not raise AttributeError with the command above.
Testing in my PEP499 branch:
Test 1: [~/src/cpython-cs at github(git:PEP499-cs)]fleet*> ./python.exe -i Lib/idlelib/pyshell.py Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'run'
This is because of an obsolete 'command = ...' around 420. The if line is correct always and the if/then not needed.
>>> sys.modules['main'] <module '_main_' (<frozenimportlibexternal.SourceFileLoader_ _object at 0x1088e6040>)> >>> sys.modules['pyshell'] <module '_main_' (<frozenimportlibexternal.SourceFileLoader_ _object at 0x1088e6040>)> >>> sys.modules['idlelib.pyshell'] <module 'idlelib.pyshell' from_ _'/Users/cameron/src/cpython-cs at github/Lib/idlelib/pyshell.py'>
So pyshell and idlelib.pyshell are distinct here.
I verified that the module was being executed twice by putting print('running') at the top.
main and pyshell
are the same module, courtesy of your sys.modules assignment at the bottom of pyshell.py.
Obsolete and removed.
Test 3 below will be with that commented out.
Test 2: [~/src/cpython-cs at github(git:PEP499-cs)]fleet*> PYTHONPATH=$PWD/Lib ./python.exe -i -m idlelib.pyshell Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'run' >>> sys.modules['main'] <module 'idlelib.pyshell' from_ _'/Users/cameron/src/cpython-cs at github/Lib/idlelib/pyshell.py'> >>> sys.modules['pyshell'] <module 'idlelib.pyshell' from_ _'/Users/cameron/src/cpython-cs at github/Lib/idlelib/pyshell.py'> >>> sys.modules['idlelib.pyshell'] <module 'idlelib.pyshell' from_ _'/Users/cameron/src/cpython-cs at github/Lib/idlelib/pyshell.py'> >>> id(sys.modules['main']) 4551072712 >>> id(sys.modules['pyshell']) 4551072712 >>> id(sys.modules['idlelib.pyshell']) 4551072712 So this has main and idlelib.pyshell the same module from the PEP499 patch and pyshell also the same from your sys.modules assignment. Test 3, with the pyshell.py sys.modules assignment commented out: [~/src/cpython-cs at github(git:PEP499-cs)]fleet*> PYTHONPATH=$PWD/Lib ./python.exe -i -m idlelib.pyshell Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'run' >>> sys.modules['main'] <module 'idlelib.pyshell' from_ _'/Users/cameron/src/cpython-cs at github/Lib/idlelib/pyshell.py'> >>> sys.modules['pyshell'] Traceback (most recent call last): File "", line 1, in KeyError: 'pyshell' >>> sys.modules['idlelib.pyshell'] <module 'idlelib.pyshell' from_ _'/Users/cameron/src/cpython-cs at github/Lib/idlelib/pyshell.py'> >>> id(sys.modules['main']) 4552379336 >>> id(sys.modules['idlelib.pyshell']) 4552379336 Here we've got main and idlelib.pyshell the same module and no 'pyshell' in sys.modules.
I don't think I understand your "relative import" scenario.
If files other that pyshell used relative 'import ./pyshell' instead of absolute 'import idlelib.pyshell', would the sys.modules key still be 'idlelib.pyshell' or 'pyshell'? Which is to ask, would the alias needed to avoid a second pyshell module still be 'idlelib.pyshell' or 'pyshell'?
- Previous message (by thread): [Python-Dev] BDFL-Delegate appointments for several PEPs
- Next message (by thread): [Python-Dev] BDFL-Delegate appointments for several PEPs
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]