[Patches] [ python-Patches-1035498 ] -m option to run a module as a
script
SourceForge.net
noreply at sourceforge.net
Thu Sep 30 11:39:39 CEST 2004
Patches item #1035498, was opened at 2004-09-28 00:22
Message generated for change (Comment added) made by ncoghlan
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1035498&group_id=5470
Category: Core (C code)
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Nick Coghlan (ncoghlan)
Assigned to: Nobody/Anonymous (nobody)
Summary: -m option to run a module as a script
Initial Comment:
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.py
Note 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'.
----------------------------------------------------------------------
>Comment By: Nick Coghlan (ncoghlan)
Date: 2004-09-30 19:39
Message:
Logged In: YES
user_id=1038590
New 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.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1035498&group_id=5470
More information about the Patches
mailing list