Repository navigation
-m option to run a module as a script #40956
Description
Activity
Implements the '-m' option recently discussed on
python-dev.Runs a module 'as if' it had been invoked from the
command line.E.g., for a build directory, the following two commands
are equivalent:
./python -m pdb
./python Lib/pdb.pyNote that neither of these does the same thing as
"./python -c "import pdb". (In this latter case, any
"if __name__ == "__main__" code would not be executed,
whereas it will be invoked in the first two cases).Given the vagaries of sys.path, this is quite handy -
if a module can be imported, it can be easily used as a
script too. Not that all modules will necessarily do
anything _interesting_ when used as a script. . .The current implementation makes changes to main.c
(handle the new argument), pythonrun.[ch] (new flavour
of PyRun_) and import.[ch] (expose a
PyImport_FindModule C API)Current limitations / to-do if this is pursued:
- print sensible errors for non-Python files (e.g.
hotshot) - allow 'dotted' names
- decide what to do with packages (e.g. run __init__.py)
- check usage messages in main.c are all still under
512 characters (I assume that limit was set for a reason) - the 1K limit on absolute file paths seems rather
arbitrary. - this is code written in the wee hours of the
morning, so it probably has a few other lurking 'features'.
- print sensible errors for non-Python files (e.g.
- addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on Sep 27, 2004 - addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on Sep 27, 2004 Logged In: YES
user_id=1038590New version of patch attached (run_module_2.diff).
This version allows modules within packages to be executed.
It does this *without* loading the packages first (just as
if the script were specified directly on the command line).It's also a little more responsive when it comes to error
message, and I've confirmed that the usage message
components are now under 512 bytes each.The FindModule method is now marked as private (i.e. with
leading underscore).The limit on file paths in pythonrun.c is now taken from
osdefs.h (the same limit that import.c already uses)The only bug I know of is that it misbehaves if you supply a
name like "a.b.module.d". You will get an ImportError saying
that d was not found, and the file pointer for 'module'
won't get closed properly.Logged In: YES
user_id=1038590Barry, kicking this in your direction solely because you
were one of the people to comment on it on py-dev. Feel free
to say 'too busy' :)Logged In: YES
user_id=1038590New version (run_module_3.diff)
Takes more account of the file description reported by
PyImport. Explictly restricts itself to PY_SOURCE and
PY_COMPILED scripts.Behaves correctly when a non-package is encountered while
parsing a dotted name (closes the file descriptor and
reports an error).I also realised that the '-m' option doesn't currently
comprehend "__path__" variables in packages. So maybe we
*should* be loading the packages as we find them, and then
querying them for their __path__ variable.Logged In: YES
user_id=1038590New version (#4)
Trivial fix - tell PyRun_SimpleFile to close the file when
it's done with it.Logged In: YES
user_id=1038590Relevant documentation locations that I can find:
Tutorial section 2.1 (Invoking the Interpreter)
Add a very brief description of the '-m' optionPython/C API section 2 (The Very High Level Layer)
Document PyRun_SimpleModuleWhat's New sections 12 & 13
I'll do this as a separate patch from the previous two,
as I can't recall if Andrew prefers to write these himself.Logged In: YES
user_id=10385905th version of patch attached
-
restricts option to top-level modules only. The correct
semantics for a module inside a package just aren't clear
enough at this stage. Support for dotted names can be added
later if this patch gets accepted, and it still seems like a
good idea. -
adds a missing check from main.c (if we got the module
option, don't try to open the file).
-
Logged In: YES
user_id=1038590Doc patch attached. Take it with a grain of salt, since this
is the first real doc patch I've made.This doesn't cover anything for 'What's New' - that can wait
until the patch gets accepted (if it does).Logged In: YES
user_id=1038590Raymond,
Passing to you to see what you think of the code, rather
than just the feature in the abstract.Guido settled for not vetoing it, but the discussion with
him allowed me to articulate why I think this is a
worthwhile feature.
(Details at
http://mail.python.org/pipermail/python-dev/2004-October/049236.html)Extension to allow packages and zip imports can wait until
there is any demand for either feature. I don't really
expect to see any, since I think the main uses will be
"python -m pdb", "python -m profile" and "python -m timeit".Logged In: YES
user_id=80475Armin, do you have time for this one. I'm buried for a few
days.Logged In: YES
user_id=4771Sorry, I didn't follow any of this discussion, neither here
nor in python-dev, so I cannot really comment.Logged In: YES
user_id=80475The patch does not compile on windows. AFAICT, importdl.h
is the culprit because it loads windows.h for a second time
resulting a double redinition of BYTE deep in the bowls of
include files.Wish I had time to help you on this one, but I don't.
Logged In: YES
user_id=1038590New version attached which should fix the Windows compile
bug (pythonrun.c does the right thing by undefining BYTE
before including windows.h - the inclusion of 'importdl.h'
has been moved to be after that 'fixed' include).Unfortunately, I won't get access to a Windows build machine
until after the beta 1 deadline, or I'd check it myself :(Logged In: YES
user_id=80475Put the test directory in PYTHONPATH and run the following
to see if you get the same results:python -tt -m regrtest -u network python -tt lib\test\regrtest.py -u networkLogged In: YES
user_id=1038590Nice catch. . .
I figured out what the problem is - regrtest.py uses
sys.argv[0] to delete it's own directory from sys.path. And
-m currently sets sys.argv[0] to just 'regrtest', instead of
the full path to the module.Obviously, that is going to have to change. Either
PyRun_SImpleModule will have to do it, or I'll get rid of
that function all together, and put the 'module-finding'
logic directly in main.c.Logged In: YES
user_id=1038590New version (7) attached which does the right thing for
"python -tt -m regrtest -u network". (I really like that
test case btw - very concise)This one sets argv[0] correctly, but was initially a little
ungraceful in some failure modes (such as "python -m
parser"). I had to add a new private API function to
import.h (_PyImport_IsScript) to clean that up.Examples of the error handling in this version:
[...@localhost src]$ ./python -m foo
./python: module foo not found
[...@localhost src]$ ./python -m sys
./python: module sys has no associated file
[...@localhost src]$ ./python -m parser
./python: module parser not usable as script(/home/.../dev/python/dist/src/build/lib.linux-i686-2.4/parser.so)
Logged In: YES
user_id=1038590New version of doc patch which changes the tutorial only
(since PyRun_SimpleModule is no longer part of the patch).Logged In: YES
user_id=80475Please put in a single, combined documentation patch.
Be sure to spell-check and run: python -m texcheck tut.tex
Logged In: YES
user_id=1038590texcheck complains about the Unicode in the tutorial even on
a clean checkout. The line numbers (> 900) are well past the
point where I'm editing. (It turns out editing the the tex
files works a lot better with xemacs than with the KDE
editors - no spurious edits this time)I'm not sure what, if anything, I need to do for "what's
new", but I've uploaded a combined code & documentation
patch that implements version 7 of the -m behaviour, and
makes relevant changes to section 2 of the tutorial.Logged In: YES
user_id=80475Accepted and applied.
Checking in Doc/tut/tut.tex;
/cvsroot/python/python/dist/src/Doc/tut/tut.tex,v <-- tut.tex
new revision: 1.254; previous revision: 1.253
done
Checking in Include/import.h;
/cvsroot/python/python/dist/src/Include/import.h,v <--
import.h
new revision: 2.30; previous revision: 2.29
done
Checking in Misc/NEWS;
/cvsroot/python/python/dist/src/Misc/NEWS,v <-- NEWS
new revision: 1.1154; previous revision: 1.1153
done
Checking in Modules/main.c;
/cvsroot/python/python/dist/src/Modules/main.c,v <-- main.c
new revision: 1.84; previous revision: 1.83
done
Checking in Python/import.c;
/cvsroot/python/python/dist/src/Python/import.c,v <-- import.c
new revision: 2.240; previous revision: 2.239
cvs diff: [06:46:25] waiting for rhettinger's lock in
/cvsroot/python/python/dis
t/src/Doc/tut
doneLogged In: YES
user_id=11105Regarding support for packages, here's a use case:
python -m pychecker.checker mymodule.py
(plus my own tools which live inside packages)
Logged In: YES
user_id=1038590I've opened a new tracker item to cover enhancement to
support packages.
http://sourceforge.net/support/tracker.php?aid=1043356
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: