Patch to implement PEP 366. Note that it doesn't implement precisely the semantics described in the version of the PEP posted in July, as some of those ideas didn't prove feasible due to the fact that imp.new_module can't tell the difference between normal modules and packages. An updated version of the PEP will be posted shortly to correct those problems.
I think the implementation is fine too (others will have to check it more carefully) but I noticed that the promised functionality of -m doesn't work yet: I created a file Lib/test/foo.py whose sole contents was "from . import test_support". Then I tried to import this using -m: $ ./python.exe -m test.foo Traceback (most recent call last): File "/Users/guido/p/Lib/runpy.py", line 104, in _run_module_as_main "__main__", fname, loader) File "/Users/guido/p/Lib/runpy.py", line 34, in _run_code exec code in run_globals File "/Users/guido/p/Lib/test/foo.py", line 1, in from . import test_support ValueError: Attempted relative import in non-package $ What's missing here?
I forgot to update the special _run_module_as_main variant when I modified run_module. I'll modify the patch to fix that and to test '-m' explicitly in test_cmd_line_script.
Posted v2 of the patch, which fixes the problem Guido noted, and improved the tests to make sure __package__ is being set correctly. There's also now an explicit test for using -m on a module inside a package (which proved harder to write than I expected - it isn't easy to fiddle with sys.path when the subprocess is running python with the -E flag!).
Great, now check it in! On Dec 2, 2007 6:01 AM, Nick Coghlan <report@bugs.python.org> wrote: > > Nick Coghlan added the comment: > > Posted v2 of the patch, which fixes the problem Guido noted, and > improved the tests to make sure __package__ is being set correctly. > There's also now an explicit test for using -m on a module inside a > package (which proved harder to write than I expected - it isn't easy to > fiddle with sys.path when the subprocess is running python with the -E > flag!). > > Added file: http://bugs.python.org/file8852/pep_366_v2.diff > > > __________________________________ > Tracker <report@bugs.python.org> > <http://bugs.python.org/issue1487> > __________________________________