[Python-Dev] Issue 4195: Can't execute packages with -m in Python 2.6/3.0 (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Tue Nov 18 11:55:50 CET 2008


I need a release manager call on whether or not the proposed resolution of this issue goes beyond what is acceptable for a bug fix in 2.6.1.

Short version:

Long version:

There was a bug in python 2.5 that allowed a package name to be passed to the -m switch or runpy.run_module() and it would mostly work.

However, the 'mostly' was due to the fact that doing this put the internal import machinery into a slightly inconsistent state: the interpreter was running code from inside a package, but that package wasn't actually present in sys.modules. This could lead to assorted hard to trace import-related weirdness, similar to what you can get when executing a file from inside a package by name. You would often get away with it, but good luck figuring out what is happening if things go wrong (and you aren't already an expert on Python import mechanics).

Since the ability to execute packages wasn't intentional, I added the missing check to block it explicitly in 2.6 (but left it alone for 2.5). It seemed like a really niche feature, so I didn't figure out a clean replacement for it at the time.

Benjamin noticed this change earlier in the 2.6 release cycle, I pointed out it was a deliberate change, and that's the way it stayed until after 2.6 was released.

After the release, Andi Vajda (from the JCC project [1]) contacted me, and together we worked out a better implementation of package support for the -m switch (and runpy.run_module) [2] by extending the main.py approach used to support direct execution of zipfiles and directories [3].

That's what I would like to add, since it nicely complements the ability to execute zipfiles and directories, while also restoring the ability to pass a package name to the -m switch (but in a way that keeps the import machinery in a consistent state).

Cheers, Nick.

[1] http://pypi.python.org/pypi/JCC

[2] http://bugs.python.org/issue4195 (package execution with -m) [3] http://bugs.python.org/issue1739468 (zipfile execution)

-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia



More information about the Python-Dev mailing list